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