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.
 
 
 
 
 
 

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