A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

523 line
14 KiB

  1. /* Copyright 2011-2013 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "sxiv.h"
  19. #define _WINDOW_CONFIG
  20. #include "config.h"
  21. #include "icon/data.h"
  22. #include "utf8.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <locale.h>
  26. #include <X11/cursorfont.h>
  27. #include <X11/Xatom.h>
  28. #include <X11/Xresource.h>
  29. #define RES_CLASS "Sxiv"
  30. enum {
  31. H_TEXT_PAD = 5,
  32. V_TEXT_PAD = 1
  33. };
  34. static struct {
  35. int name;
  36. Cursor icon;
  37. } cursors[CURSOR_COUNT] = {
  38. { XC_left_ptr }, { XC_dotbox }, { XC_watch },
  39. { XC_sb_left_arrow }, { XC_sb_right_arrow }
  40. };
  41. static GC gc;
  42. static XftFont *font;
  43. static int fontheight;
  44. static int barheight;
  45. Atom atoms[ATOM_COUNT];
  46. static Bool fs_support;
  47. static Bool fs_warned;
  48. void win_init_font(const win_env_t *e, const char *fontstr)
  49. {
  50. if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
  51. error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
  52. fontheight = font->ascent + font->descent;
  53. barheight = fontheight + 2 * V_TEXT_PAD;
  54. }
  55. void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
  56. {
  57. if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
  58. DefaultColormap(e->dpy, e->scr), name, col))
  59. {
  60. error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
  61. }
  62. }
  63. void win_check_wm_support(Display *dpy, Window root)
  64. {
  65. int format;
  66. long offset = 0, length = 16;
  67. Atom *data, type;
  68. unsigned long i, nitems, bytes_left;
  69. Bool found = False;
  70. while (!found && length > 0) {
  71. if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
  72. offset, length, False, XA_ATOM, &type, &format,
  73. &nitems, &bytes_left, (unsigned char**) &data))
  74. {
  75. break;
  76. }
  77. if (type == XA_ATOM && format == 32) {
  78. for (i = 0; i < nitems; i++) {
  79. if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
  80. found = True;
  81. fs_support = True;
  82. break;
  83. }
  84. }
  85. }
  86. XFree(data);
  87. offset += nitems;
  88. length = MIN(length, bytes_left / 4);
  89. }
  90. }
  91. void win_res(Display *dpy, const char *name, const char **dst)
  92. {
  93. char *type;
  94. XrmValue ret;
  95. XrmDatabase db;
  96. char *res_man;
  97. XrmInitialize();
  98. if ((res_man = XResourceManagerString(dpy)) == NULL)
  99. return;
  100. if ((db = XrmGetStringDatabase(res_man)) == NULL)
  101. return;
  102. if (XrmGetResource(db, name, name, &type, &ret) && STREQ(type, "String"))
  103. *dst = ret.addr;
  104. }
  105. #define INIT_ATOM_(atom) \
  106. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  107. void win_init(win_t *win)
  108. {
  109. win_env_t *e;
  110. memset(win, 0, sizeof(win_t));
  111. e = &win->env;
  112. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  113. error(EXIT_FAILURE, 0, "Error opening X display");
  114. e->scr = DefaultScreen(e->dpy);
  115. e->scrw = DisplayWidth(e->dpy, e->scr);
  116. e->scrh = DisplayHeight(e->dpy, e->scr);
  117. e->vis = DefaultVisual(e->dpy, e->scr);
  118. e->cmap = DefaultColormap(e->dpy, e->scr);
  119. e->depth = DefaultDepth(e->dpy, e->scr);
  120. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  121. error(0, 0, "No locale support");
  122. win_res(e->dpy, RES_CLASS ".background", &WIN_BG_COLOR);
  123. win_res(e->dpy, RES_CLASS ".background", &BAR_FG_COLOR);
  124. win_res(e->dpy, RES_CLASS ".foreground", &BAR_BG_COLOR);
  125. win_res(e->dpy, RES_CLASS ".foreground", &SEL_COLOR);
  126. win_init_font(e, BAR_FONT);
  127. win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
  128. win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
  129. win_alloc_color(e, SEL_COLOR, &win->selcol);
  130. win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
  131. win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
  132. win->bar.l.size = BAR_L_LEN;
  133. win->bar.r.size = BAR_R_LEN;
  134. /* 3 padding bytes needed by utf8_decode */
  135. win->bar.l.buf = emalloc(win->bar.l.size + 3);
  136. win->bar.l.buf[0] = '\0';
  137. win->bar.r.buf = emalloc(win->bar.r.size + 3);
  138. win->bar.r.buf[0] = '\0';
  139. win->bar.h = options->hide_bar ? 0 : barheight;
  140. INIT_ATOM_(WM_DELETE_WINDOW);
  141. INIT_ATOM_(_NET_WM_NAME);
  142. INIT_ATOM_(_NET_WM_ICON_NAME);
  143. INIT_ATOM_(_NET_WM_ICON);
  144. INIT_ATOM_(_NET_WM_STATE);
  145. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  146. INIT_ATOM_(_NET_SUPPORTED);
  147. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  148. }
  149. void win_open(win_t *win)
  150. {
  151. int c, i, j, n;
  152. long parent;
  153. win_env_t *e;
  154. XClassHint classhint;
  155. unsigned long *icon_data;
  156. XColor col;
  157. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  158. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  159. Pixmap none;
  160. int gmask;
  161. XSizeHints sizehints;
  162. Bool fullscreen = options->fullscreen && fs_support;
  163. e = &win->env;
  164. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  165. sizehints.flags = PWinGravity;
  166. sizehints.win_gravity = NorthWestGravity;
  167. /* determine window offsets, width & height */
  168. if (options->geometry == NULL)
  169. gmask = 0;
  170. else
  171. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  172. &win->w, &win->h);
  173. if ((gmask & WidthValue) != 0)
  174. sizehints.flags |= USSize;
  175. else
  176. win->w = WIN_WIDTH;
  177. if ((gmask & HeightValue) != 0)
  178. sizehints.flags |= USSize;
  179. else
  180. win->h = WIN_HEIGHT;
  181. if ((gmask & XValue) != 0) {
  182. if ((gmask & XNegative) != 0) {
  183. win->x += e->scrw - win->w;
  184. sizehints.win_gravity = NorthEastGravity;
  185. }
  186. sizehints.flags |= USPosition;
  187. } else {
  188. win->x = 0;
  189. }
  190. if ((gmask & YValue) != 0) {
  191. if ((gmask & YNegative) != 0) {
  192. win->y += e->scrh - win->h;
  193. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  194. ? SouthEastGravity : SouthWestGravity;
  195. }
  196. sizehints.flags |= USPosition;
  197. } else {
  198. win->y = 0;
  199. }
  200. win->xwin = XCreateWindow(e->dpy, parent,
  201. win->x, win->y, win->w, win->h, 0,
  202. e->depth, InputOutput, e->vis, 0, NULL);
  203. if (win->xwin == None)
  204. error(EXIT_FAILURE, 0, "Error creating X window");
  205. XSelectInput(e->dpy, win->xwin,
  206. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  207. PointerMotionMask | StructureNotifyMask);
  208. for (i = 0; i < ARRLEN(cursors); i++) {
  209. if (i != CURSOR_NONE)
  210. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  211. }
  212. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  213. &col, &col) == 0)
  214. {
  215. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  216. }
  217. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  218. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  219. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  220. n = icons[ARRLEN(icons)-1].size;
  221. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  222. for (i = 0; i < ARRLEN(icons); i++) {
  223. n = 0;
  224. icon_data[n++] = icons[i].size;
  225. icon_data[n++] = icons[i].size;
  226. for (j = 0; j < icons[i].cnt; j++) {
  227. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  228. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  229. }
  230. XChangeProperty(e->dpy, win->xwin,
  231. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  232. i == 0 ? PropModeReplace : PropModeAppend,
  233. (unsigned char *) icon_data, n);
  234. }
  235. free(icon_data);
  236. win_set_title(win, "sxiv");
  237. classhint.res_class = RES_CLASS;
  238. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  239. XSetClassHint(e->dpy, win->xwin, &classhint);
  240. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  241. sizehints.width = win->w;
  242. sizehints.height = win->h;
  243. sizehints.x = win->x;
  244. sizehints.y = win->y;
  245. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  246. win->h -= win->bar.h;
  247. win->buf.w = e->scrw;
  248. win->buf.h = e->scrh;
  249. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  250. win->buf.w, win->buf.h, e->depth);
  251. XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  252. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  253. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  254. XMapWindow(e->dpy, win->xwin);
  255. XFlush(e->dpy);
  256. if (fullscreen)
  257. win_toggle_fullscreen(win);
  258. }
  259. CLEANUP void win_close(win_t *win)
  260. {
  261. int i;
  262. for (i = 0; i < ARRLEN(cursors); i++)
  263. XFreeCursor(win->env.dpy, cursors[i].icon);
  264. XFreeGC(win->env.dpy, gc);
  265. XDestroyWindow(win->env.dpy, win->xwin);
  266. XCloseDisplay(win->env.dpy);
  267. }
  268. bool win_configure(win_t *win, XConfigureEvent *c)
  269. {
  270. bool changed;
  271. changed = win->w != c->width || win->h + win->bar.h != c->height;
  272. win->x = c->x;
  273. win->y = c->y;
  274. win->w = c->width;
  275. win->h = c->height - win->bar.h;
  276. win->bw = c->border_width;
  277. return changed;
  278. }
  279. void win_toggle_fullscreen(win_t *win)
  280. {
  281. XEvent ev;
  282. XClientMessageEvent *cm;
  283. if (!fs_support) {
  284. if (!fs_warned) {
  285. error(0, 0, "No fullscreen support");
  286. fs_warned = True;
  287. }
  288. return;
  289. }
  290. win->fullscreen = !win->fullscreen;
  291. memset(&ev, 0, sizeof(ev));
  292. ev.type = ClientMessage;
  293. cm = &ev.xclient;
  294. cm->window = win->xwin;
  295. cm->message_type = atoms[ATOM__NET_WM_STATE];
  296. cm->format = 32;
  297. cm->data.l[0] = win->fullscreen;
  298. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  299. cm->data.l[2] = cm->data.l[3] = 0;
  300. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  301. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  302. }
  303. void win_toggle_bar(win_t *win)
  304. {
  305. if (win->bar.h != 0) {
  306. win->h += win->bar.h;
  307. win->bar.h = 0;
  308. } else {
  309. win->bar.h = barheight;
  310. win->h -= win->bar.h;
  311. }
  312. }
  313. void win_clear(win_t *win)
  314. {
  315. win_env_t *e;
  316. e = &win->env;
  317. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  318. XFreePixmap(e->dpy, win->buf.pm);
  319. win->buf.w = MAX(win->buf.w, win->w);
  320. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  321. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  322. win->buf.w, win->buf.h, e->depth);
  323. }
  324. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  325. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  326. }
  327. #define TEXTWIDTH(win, text, len) \
  328. win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
  329. int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
  330. char *text, int len, int w)
  331. {
  332. int err, tw = 0;
  333. char *t, *next;
  334. uint32_t rune;
  335. XftFont *f;
  336. FcCharSet *fccharset;
  337. XGlyphInfo ext;
  338. for (t = text; t - text < len; t = next) {
  339. next = utf8_decode(t, &rune, &err);
  340. if (XftCharExists(win->env.dpy, font, rune)) {
  341. f = font;
  342. } else { /* fallback font */
  343. fccharset = FcCharSetCreate();
  344. FcCharSetAddChar(fccharset, rune);
  345. f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
  346. fccharset, FC_SCALABLE, FcTypeBool, FcTrue, NULL);
  347. FcCharSetDestroy(fccharset);
  348. }
  349. XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
  350. tw += ext.xOff;
  351. if (tw <= w) {
  352. XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
  353. x += ext.xOff;
  354. }
  355. if (f != font)
  356. XftFontClose(win->env.dpy, f);
  357. }
  358. return tw;
  359. }
  360. void win_draw_bar(win_t *win)
  361. {
  362. int len, x, y, w, tw;
  363. win_env_t *e;
  364. win_bar_t *l, *r;
  365. XftDraw *d;
  366. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  367. return;
  368. e = &win->env;
  369. y = win->h + font->ascent + V_TEXT_PAD;
  370. w = win->w - 2*H_TEXT_PAD;
  371. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  372. DefaultColormap(e->dpy, e->scr));
  373. XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
  374. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  375. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  376. XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
  377. if ((len = strlen(r->buf)) > 0) {
  378. if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
  379. return;
  380. x = win->w - tw - H_TEXT_PAD;
  381. w -= tw;
  382. win_draw_text(win, d, &win->bar.fgcol, x, y, r->buf, len, tw);
  383. }
  384. if ((len = strlen(l->buf)) > 0) {
  385. x = H_TEXT_PAD;
  386. w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
  387. win_draw_text(win, d, &win->bar.fgcol, x, y, l->buf, len, w);
  388. }
  389. XftDrawDestroy(d);
  390. }
  391. void win_draw(win_t *win)
  392. {
  393. if (win->bar.h > 0)
  394. win_draw_bar(win);
  395. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  396. XClearWindow(win->env.dpy, win->xwin);
  397. XFlush(win->env.dpy);
  398. }
  399. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  400. unsigned long col)
  401. {
  402. XGCValues gcval;
  403. gcval.line_width = lw;
  404. gcval.foreground = col;
  405. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  406. if (fill)
  407. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  408. else
  409. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  410. }
  411. void win_set_title(win_t *win, const char *title)
  412. {
  413. XStoreName(win->env.dpy, win->xwin, title);
  414. XSetIconName(win->env.dpy, win->xwin, title);
  415. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  416. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  417. PropModeReplace, (unsigned char *) title, strlen(title));
  418. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  419. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  420. PropModeReplace, (unsigned char *) title, strlen(title));
  421. }
  422. void win_set_cursor(win_t *win, cursor_t cursor)
  423. {
  424. if (cursor >= 0 && cursor < ARRLEN(cursors)) {
  425. XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
  426. XFlush(win->env.dpy);
  427. }
  428. }
  429. void win_cursor_pos(win_t *win, int *x, int *y)
  430. {
  431. int i;
  432. unsigned int ui;
  433. Window w;
  434. if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
  435. *x = *y = 0;
  436. }