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.
 
 
 
 
 
 

560 lines
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. #define _POSIX_C_SOURCE 200112L
  19. #define _WINDOW_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <locale.h>
  23. #include <X11/cursorfont.h>
  24. #include <X11/Xatom.h>
  25. #include "options.h"
  26. #include "util.h"
  27. #include "window.h"
  28. #include "config.h"
  29. #include "icon/data.h"
  30. enum {
  31. H_TEXT_PAD = 5,
  32. V_TEXT_PAD = 1
  33. };
  34. static Cursor carrow;
  35. static Cursor cnone;
  36. static Cursor chand;
  37. static Cursor cwatch;
  38. static GC gc;
  39. static struct {
  40. int ascent;
  41. int descent;
  42. XFontStruct *xfont;
  43. XFontSet set;
  44. } font;
  45. static int fontheight;
  46. static int barheight;
  47. Atom atoms[ATOM_COUNT];
  48. static Bool fs_support;
  49. static Bool fs_warned;
  50. void win_init_font(Display *dpy, const char *fontstr)
  51. {
  52. int n;
  53. char *def, **missing;
  54. font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  55. if (missing)
  56. XFreeStringList(missing);
  57. if (font.set) {
  58. XFontStruct **xfonts;
  59. char **font_names;
  60. font.ascent = font.descent = 0;
  61. XExtentsOfFontSet(font.set);
  62. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  63. while (n--) {
  64. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  65. font.descent = MAX(font.descent,(*xfonts)->descent);
  66. xfonts++;
  67. }
  68. } else {
  69. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  70. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  71. {
  72. die("could not load font: %s", fontstr);
  73. }
  74. font.ascent = font.xfont->ascent;
  75. font.descent = font.xfont->descent;
  76. }
  77. fontheight = font.ascent + font.descent;
  78. barheight = fontheight + 2 * V_TEXT_PAD;
  79. }
  80. unsigned long win_alloc_color(win_t *win, const char *name)
  81. {
  82. XColor col;
  83. if (win == NULL)
  84. return 0UL;
  85. if (XAllocNamedColor(win->env.dpy,
  86. DefaultColormap(win->env.dpy, win->env.scr),
  87. name, &col, &col) == 0)
  88. {
  89. die("could not allocate color: %s", name);
  90. }
  91. return col.pixel;
  92. }
  93. void win_check_wm_support(Display *dpy, Window root)
  94. {
  95. int format;
  96. long offset = 0, length = 16;
  97. Atom *data, type;
  98. unsigned long i, nitems, bytes_left;
  99. Bool found = False;
  100. while (!found && length > 0) {
  101. if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
  102. offset, length, False, XA_ATOM, &type, &format,
  103. &nitems, &bytes_left, (unsigned char**) &data))
  104. {
  105. break;
  106. }
  107. if (type == XA_ATOM && format == 32) {
  108. for (i = 0; i < nitems; i++) {
  109. if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
  110. found = True;
  111. fs_support = True;
  112. break;
  113. }
  114. }
  115. }
  116. XFree(data);
  117. offset += nitems;
  118. length = MIN(length, bytes_left / 4);
  119. }
  120. }
  121. #define INIT_ATOM_(atom) \
  122. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  123. void win_init(win_t *win)
  124. {
  125. win_env_t *e;
  126. if (win == NULL)
  127. return;
  128. memset(win, 0, sizeof(win_t));
  129. e = &win->env;
  130. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  131. die("could not open display");
  132. e->scr = DefaultScreen(e->dpy);
  133. e->scrw = DisplayWidth(e->dpy, e->scr);
  134. e->scrh = DisplayHeight(e->dpy, e->scr);
  135. e->vis = DefaultVisual(e->dpy, e->scr);
  136. e->cmap = DefaultColormap(e->dpy, e->scr);
  137. e->depth = DefaultDepth(e->dpy, e->scr);
  138. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  139. warn("no locale support");
  140. win_init_font(e->dpy, BAR_FONT);
  141. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  142. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  143. win->selcol = win_alloc_color(win, SEL_COLOR);
  144. win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
  145. win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
  146. win->bar.l.size = BAR_L_LEN;
  147. win->bar.r.size = BAR_R_LEN;
  148. win->bar.l.buf = s_malloc(win->bar.l.size);
  149. win->bar.r.buf = s_malloc(win->bar.r.size);
  150. win->bar.h = options->hide_bar ? 0 : barheight;
  151. INIT_ATOM_(WM_DELETE_WINDOW);
  152. INIT_ATOM_(_NET_WM_NAME);
  153. INIT_ATOM_(_NET_WM_ICON_NAME);
  154. INIT_ATOM_(_NET_WM_ICON);
  155. INIT_ATOM_(_NET_WM_STATE);
  156. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  157. INIT_ATOM_(_NET_SUPPORTED);
  158. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  159. }
  160. void win_open(win_t *win)
  161. {
  162. int c, i, j, n;
  163. win_env_t *e;
  164. XClassHint classhint;
  165. unsigned long *icon_data;
  166. XColor col;
  167. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  168. Pixmap none;
  169. int gmask;
  170. XSizeHints sizehints;
  171. Bool fullscreen = options->fullscreen && fs_support;
  172. if (win == NULL)
  173. return;
  174. e = &win->env;
  175. sizehints.flags = PWinGravity;
  176. sizehints.win_gravity = NorthWestGravity;
  177. /* determine window offsets, width & height */
  178. if (options->geometry == NULL)
  179. gmask = 0;
  180. else
  181. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  182. &win->w, &win->h);
  183. if ((gmask & WidthValue) != 0)
  184. sizehints.flags |= USSize;
  185. else
  186. win->w = WIN_WIDTH;
  187. if ((gmask & HeightValue) != 0)
  188. sizehints.flags |= USSize;
  189. else
  190. win->h = WIN_HEIGHT;
  191. if ((gmask & XValue) != 0) {
  192. if ((gmask & XNegative) != 0) {
  193. win->x += e->scrw - win->w;
  194. sizehints.win_gravity = NorthEastGravity;
  195. }
  196. sizehints.flags |= USPosition;
  197. } else {
  198. win->x = 0;
  199. }
  200. if ((gmask & YValue) != 0) {
  201. if ((gmask & YNegative) != 0) {
  202. win->y += e->scrh - win->h;
  203. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  204. ? SouthEastGravity : SouthWestGravity;
  205. }
  206. sizehints.flags |= USPosition;
  207. } else {
  208. win->y = 0;
  209. }
  210. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  211. win->x, win->y, win->w, win->h, 0,
  212. e->depth, InputOutput, e->vis, 0, NULL);
  213. if (win->xwin == None)
  214. die("could not create window");
  215. XSelectInput(e->dpy, win->xwin,
  216. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  217. PointerMotionMask | StructureNotifyMask);
  218. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  219. chand = XCreateFontCursor(e->dpy, XC_fleur);
  220. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  221. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  222. &col, &col) == 0)
  223. {
  224. die("could not allocate color: black");
  225. }
  226. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  227. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  228. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  229. n = icons[ARRLEN(icons)-1].size;
  230. icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
  231. for (i = 0; i < ARRLEN(icons); i++) {
  232. n = 0;
  233. icon_data[n++] = icons[i].size;
  234. icon_data[n++] = icons[i].size;
  235. for (j = 0; j < icons[i].cnt; j++) {
  236. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  237. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  238. }
  239. XChangeProperty(e->dpy, win->xwin,
  240. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  241. i == 0 ? PropModeReplace : PropModeAppend,
  242. (unsigned char *) icon_data, n);
  243. }
  244. free(icon_data);
  245. win_set_title(win, "sxiv");
  246. classhint.res_class = "Sxiv";
  247. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  248. XSetClassHint(e->dpy, win->xwin, &classhint);
  249. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  250. sizehints.width = win->w;
  251. sizehints.height = win->h;
  252. sizehints.x = win->x;
  253. sizehints.y = win->y;
  254. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  255. win->h -= win->bar.h;
  256. win->buf.w = e->scrw;
  257. win->buf.h = e->scrh;
  258. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  259. win->buf.w, win->buf.h, e->depth);
  260. XSetForeground(e->dpy, gc, fullscreen ? win->fscol : win->bgcol);
  261. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  262. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  263. XMapWindow(e->dpy, win->xwin);
  264. XFlush(e->dpy);
  265. if (fullscreen)
  266. win_toggle_fullscreen(win);
  267. }
  268. void win_close(win_t *win)
  269. {
  270. if (win == NULL || win->xwin == None)
  271. return;
  272. XFreeCursor(win->env.dpy, carrow);
  273. XFreeCursor(win->env.dpy, cnone);
  274. XFreeCursor(win->env.dpy, chand);
  275. XFreeCursor(win->env.dpy, cwatch);
  276. XFreeGC(win->env.dpy, gc);
  277. XDestroyWindow(win->env.dpy, win->xwin);
  278. XCloseDisplay(win->env.dpy);
  279. }
  280. bool win_configure(win_t *win, XConfigureEvent *c)
  281. {
  282. bool changed;
  283. if (win == NULL || c == NULL)
  284. return false;
  285. changed = win->w != c->width || win->h + win->bar.h != c->height;
  286. win->x = c->x;
  287. win->y = c->y;
  288. win->w = c->width;
  289. win->h = c->height - win->bar.h;
  290. win->bw = c->border_width;
  291. return changed;
  292. }
  293. void win_toggle_fullscreen(win_t *win)
  294. {
  295. XEvent ev;
  296. XClientMessageEvent *cm;
  297. if (win == NULL || win->xwin == None)
  298. return;
  299. if (!fs_support) {
  300. if (!fs_warned) {
  301. warn("window manager does not support fullscreen");
  302. fs_warned = True;
  303. }
  304. return;
  305. }
  306. win->fullscreen = !win->fullscreen;
  307. memset(&ev, 0, sizeof(ev));
  308. ev.type = ClientMessage;
  309. cm = &ev.xclient;
  310. cm->window = win->xwin;
  311. cm->message_type = atoms[ATOM__NET_WM_STATE];
  312. cm->format = 32;
  313. cm->data.l[0] = win->fullscreen;
  314. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  315. cm->data.l[2] = cm->data.l[3] = 0;
  316. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  317. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  318. }
  319. void win_toggle_bar(win_t *win)
  320. {
  321. if (win == NULL || win->xwin == None)
  322. return;
  323. if (win->bar.h != 0) {
  324. win->h += win->bar.h;
  325. win->bar.h = 0;
  326. } else {
  327. win->bar.h = barheight;
  328. win->h -= win->bar.h;
  329. }
  330. }
  331. void win_clear(win_t *win)
  332. {
  333. win_env_t *e;
  334. if (win == NULL || win->xwin == None)
  335. return;
  336. e = &win->env;
  337. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  338. XFreePixmap(e->dpy, win->buf.pm);
  339. win->buf.w = MAX(win->buf.w, win->w);
  340. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  341. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  342. win->buf.w, win->buf.h, e->depth);
  343. }
  344. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  345. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  346. }
  347. void win_draw_bar(win_t *win)
  348. {
  349. int len, olen, x, y, w, tw;
  350. char rest[3];
  351. const char *dots = "...";
  352. win_env_t *e;
  353. win_bar_t *l, *r;
  354. if (win == NULL || win->xwin == None)
  355. return;
  356. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  357. return;
  358. e = &win->env;
  359. y = win->h + font.ascent + V_TEXT_PAD;
  360. w = win->w;
  361. XSetForeground(e->dpy, gc, win->bar.bgcol);
  362. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  363. XSetForeground(e->dpy, gc, win->bar.fgcol);
  364. XSetBackground(e->dpy, gc, win->bar.bgcol);
  365. if ((len = strlen(r->buf)) > 0) {
  366. if ((tw = win_textwidth(r->buf, len, true)) > w)
  367. return;
  368. x = win->w - tw + H_TEXT_PAD;
  369. w -= tw;
  370. if (font.set)
  371. XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, r->buf, len);
  372. else
  373. XDrawString(e->dpy, win->buf.pm, gc, x, y, r->buf, len);
  374. }
  375. if ((len = strlen(l->buf)) > 0) {
  376. olen = len;
  377. while (len > 0 && (tw = win_textwidth(l->buf, len, true)) > w)
  378. len--;
  379. if (len > 0) {
  380. if (len != olen) {
  381. w = strlen(dots);
  382. if (len <= w)
  383. return;
  384. memcpy(rest, l->buf + len - w, w);
  385. memcpy(l->buf + len - w, dots, w);
  386. }
  387. x = H_TEXT_PAD;
  388. if (font.set)
  389. XmbDrawString(e->dpy, win->buf.pm, font.set, gc, x, y, l->buf, len);
  390. else
  391. XDrawString(e->dpy, win->buf.pm, gc, x, y, l->buf, len);
  392. if (len != olen)
  393. memcpy(l->buf + len - w, rest, w);
  394. }
  395. }
  396. }
  397. void win_draw(win_t *win)
  398. {
  399. if (win == NULL || win->xwin == None)
  400. return;
  401. if (win->bar.h > 0)
  402. win_draw_bar(win);
  403. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  404. XClearWindow(win->env.dpy, win->xwin);
  405. XFlush(win->env.dpy);
  406. }
  407. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  408. unsigned long col)
  409. {
  410. XGCValues gcval;
  411. if (win == NULL || win->buf.pm == None)
  412. return;
  413. gcval.line_width = lw;
  414. gcval.foreground = col;
  415. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  416. if (fill)
  417. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  418. else
  419. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  420. }
  421. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  422. {
  423. XRectangle r;
  424. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  425. if (font.set) {
  426. XmbTextExtents(font.set, text, len, NULL, &r);
  427. return r.width + padding;
  428. } else {
  429. return XTextWidth(font.xfont, text, len) + padding;
  430. }
  431. }
  432. void win_set_title(win_t *win, const char *title)
  433. {
  434. if (win == NULL || win->xwin == None)
  435. return;
  436. if (title == NULL)
  437. title = "sxiv";
  438. XStoreName(win->env.dpy, win->xwin, title);
  439. XSetIconName(win->env.dpy, win->xwin, title);
  440. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  441. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  442. PropModeReplace, (unsigned char *) title, strlen(title));
  443. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  444. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  445. PropModeReplace, (unsigned char *) title, strlen(title));
  446. }
  447. void win_set_cursor(win_t *win, cursor_t cursor)
  448. {
  449. if (win == NULL || win->xwin == None)
  450. return;
  451. switch (cursor) {
  452. case CURSOR_NONE:
  453. XDefineCursor(win->env.dpy, win->xwin, cnone);
  454. break;
  455. case CURSOR_HAND:
  456. XDefineCursor(win->env.dpy, win->xwin, chand);
  457. break;
  458. case CURSOR_WATCH:
  459. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  460. break;
  461. case CURSOR_ARROW:
  462. default:
  463. XDefineCursor(win->env.dpy, win->xwin, carrow);
  464. break;
  465. }
  466. XFlush(win->env.dpy);
  467. }