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.
 
 
 
 
 
 

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