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.
 
 
 
 
 
 

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