A Simple X Image Viewer
 
 
 
 
 
 

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