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