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.
 
 
 
 
 
 

517 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. #include "sxiv.h"
  19. #define _WINDOW_CONFIG
  20. #include "config.h"
  21. #include "icon/data.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <locale.h>
  25. #include <X11/cursorfont.h>
  26. #include <X11/Xatom.h>
  27. enum {
  28. H_TEXT_PAD = 5,
  29. V_TEXT_PAD = 1
  30. };
  31. static struct {
  32. int name;
  33. Cursor icon;
  34. } cursors[CURSOR_COUNT] = {
  35. { XC_left_ptr }, { XC_dotbox }, { XC_watch },
  36. { XC_sb_left_arrow }, { XC_sb_right_arrow }
  37. };
  38. static GC gc;
  39. static XftFont *font;
  40. static int fontheight;
  41. static int barheight;
  42. Atom atoms[ATOM_COUNT];
  43. static Bool fs_support;
  44. static Bool fs_warned;
  45. void win_init_font(const win_env_t *e, const char *fontstr)
  46. {
  47. if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
  48. error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
  49. fontheight = font->ascent + font->descent;
  50. barheight = fontheight + 2 * V_TEXT_PAD;
  51. }
  52. void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
  53. {
  54. if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
  55. DefaultColormap(e->dpy, e->scr), name, col))
  56. {
  57. error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
  58. }
  59. }
  60. void win_check_wm_support(Display *dpy, Window root)
  61. {
  62. int format;
  63. long offset = 0, length = 16;
  64. Atom *data, type;
  65. unsigned long i, nitems, bytes_left;
  66. Bool found = False;
  67. while (!found && length > 0) {
  68. if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
  69. offset, length, False, XA_ATOM, &type, &format,
  70. &nitems, &bytes_left, (unsigned char**) &data))
  71. {
  72. break;
  73. }
  74. if (type == XA_ATOM && format == 32) {
  75. for (i = 0; i < nitems; i++) {
  76. if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
  77. found = True;
  78. fs_support = True;
  79. break;
  80. }
  81. }
  82. }
  83. XFree(data);
  84. offset += nitems;
  85. length = MIN(length, bytes_left / 4);
  86. }
  87. }
  88. #define INIT_ATOM_(atom) \
  89. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  90. void win_init(win_t *win)
  91. {
  92. win_env_t *e;
  93. memset(win, 0, sizeof(win_t));
  94. e = &win->env;
  95. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  96. error(EXIT_FAILURE, 0, "Error opening X 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. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  104. error(0, 0, "No locale support");
  105. win_init_font(e, BAR_FONT);
  106. win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
  107. win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
  108. win_alloc_color(e, SEL_COLOR, &win->selcol);
  109. win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
  110. win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
  111. win->bar.l.size = BAR_L_LEN;
  112. win->bar.r.size = BAR_R_LEN;
  113. win->bar.l.buf = emalloc(win->bar.l.size);
  114. win->bar.r.buf = emalloc(win->bar.r.size);
  115. win->bar.h = options->hide_bar ? 0 : barheight;
  116. INIT_ATOM_(WM_DELETE_WINDOW);
  117. INIT_ATOM_(_NET_WM_NAME);
  118. INIT_ATOM_(_NET_WM_ICON_NAME);
  119. INIT_ATOM_(_NET_WM_ICON);
  120. INIT_ATOM_(_NET_WM_STATE);
  121. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  122. INIT_ATOM_(_NET_SUPPORTED);
  123. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  124. }
  125. void win_open(win_t *win)
  126. {
  127. int c, i, j, n;
  128. long parent;
  129. win_env_t *e;
  130. XClassHint classhint;
  131. unsigned long *icon_data;
  132. XColor col;
  133. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  134. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  135. Pixmap none;
  136. int gmask;
  137. XSizeHints sizehints;
  138. Bool fullscreen = options->fullscreen && fs_support;
  139. e = &win->env;
  140. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  141. sizehints.flags = PWinGravity;
  142. sizehints.win_gravity = NorthWestGravity;
  143. /* determine window offsets, width & height */
  144. if (options->geometry == NULL)
  145. gmask = 0;
  146. else
  147. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  148. &win->w, &win->h);
  149. if ((gmask & WidthValue) != 0)
  150. sizehints.flags |= USSize;
  151. else
  152. win->w = WIN_WIDTH;
  153. if ((gmask & HeightValue) != 0)
  154. sizehints.flags |= USSize;
  155. else
  156. win->h = WIN_HEIGHT;
  157. if ((gmask & XValue) != 0) {
  158. if ((gmask & XNegative) != 0) {
  159. win->x += e->scrw - win->w;
  160. sizehints.win_gravity = NorthEastGravity;
  161. }
  162. sizehints.flags |= USPosition;
  163. } else {
  164. win->x = 0;
  165. }
  166. if ((gmask & YValue) != 0) {
  167. if ((gmask & YNegative) != 0) {
  168. win->y += e->scrh - win->h;
  169. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  170. ? SouthEastGravity : SouthWestGravity;
  171. }
  172. sizehints.flags |= USPosition;
  173. } else {
  174. win->y = 0;
  175. }
  176. win->xwin = XCreateWindow(e->dpy, parent,
  177. win->x, win->y, win->w, win->h, 0,
  178. e->depth, InputOutput, e->vis, 0, NULL);
  179. if (win->xwin == None)
  180. error(EXIT_FAILURE, 0, "Error creating X window");
  181. XSelectInput(e->dpy, win->xwin,
  182. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  183. PointerMotionMask | StructureNotifyMask);
  184. for (i = 0; i < ARRLEN(cursors); i++) {
  185. if (i != CURSOR_NONE)
  186. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  187. }
  188. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  189. &col, &col) == 0)
  190. {
  191. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  192. }
  193. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  194. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  195. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  196. n = icons[ARRLEN(icons)-1].size;
  197. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  198. for (i = 0; i < ARRLEN(icons); i++) {
  199. n = 0;
  200. icon_data[n++] = icons[i].size;
  201. icon_data[n++] = icons[i].size;
  202. for (j = 0; j < icons[i].cnt; j++) {
  203. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  204. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  205. }
  206. XChangeProperty(e->dpy, win->xwin,
  207. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  208. i == 0 ? PropModeReplace : PropModeAppend,
  209. (unsigned char *) icon_data, n);
  210. }
  211. free(icon_data);
  212. win_set_title(win, "sxiv");
  213. classhint.res_class = "Sxiv";
  214. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  215. XSetClassHint(e->dpy, win->xwin, &classhint);
  216. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  217. sizehints.width = win->w;
  218. sizehints.height = win->h;
  219. sizehints.x = win->x;
  220. sizehints.y = win->y;
  221. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  222. win->h -= win->bar.h;
  223. win->buf.w = e->scrw;
  224. win->buf.h = e->scrh;
  225. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  226. win->buf.w, win->buf.h, e->depth);
  227. XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  228. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  229. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  230. XMapWindow(e->dpy, win->xwin);
  231. XFlush(e->dpy);
  232. if (fullscreen)
  233. win_toggle_fullscreen(win);
  234. }
  235. CLEANUP void win_close(win_t *win)
  236. {
  237. int i;
  238. for (i = 0; i < ARRLEN(cursors); i++)
  239. XFreeCursor(win->env.dpy, cursors[i].icon);
  240. XFreeGC(win->env.dpy, gc);
  241. XDestroyWindow(win->env.dpy, win->xwin);
  242. XCloseDisplay(win->env.dpy);
  243. }
  244. bool win_configure(win_t *win, XConfigureEvent *c)
  245. {
  246. bool changed;
  247. changed = win->w != c->width || win->h + win->bar.h != c->height;
  248. win->x = c->x;
  249. win->y = c->y;
  250. win->w = c->width;
  251. win->h = c->height - win->bar.h;
  252. win->bw = c->border_width;
  253. return changed;
  254. }
  255. void win_toggle_fullscreen(win_t *win)
  256. {
  257. XEvent ev;
  258. XClientMessageEvent *cm;
  259. if (!fs_support) {
  260. if (!fs_warned) {
  261. error(0, 0, "No fullscreen support");
  262. fs_warned = True;
  263. }
  264. return;
  265. }
  266. win->fullscreen = !win->fullscreen;
  267. memset(&ev, 0, sizeof(ev));
  268. ev.type = ClientMessage;
  269. cm = &ev.xclient;
  270. cm->window = win->xwin;
  271. cm->message_type = atoms[ATOM__NET_WM_STATE];
  272. cm->format = 32;
  273. cm->data.l[0] = win->fullscreen;
  274. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  275. cm->data.l[2] = cm->data.l[3] = 0;
  276. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  277. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  278. }
  279. void win_toggle_bar(win_t *win)
  280. {
  281. if (win->bar.h != 0) {
  282. win->h += win->bar.h;
  283. win->bar.h = 0;
  284. } else {
  285. win->bar.h = barheight;
  286. win->h -= win->bar.h;
  287. }
  288. }
  289. void win_clear(win_t *win)
  290. {
  291. win_env_t *e;
  292. e = &win->env;
  293. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  294. XFreePixmap(e->dpy, win->buf.pm);
  295. win->buf.w = MAX(win->buf.w, win->w);
  296. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  297. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  298. win->buf.w, win->buf.h, e->depth);
  299. }
  300. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  301. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  302. }
  303. void win_draw_bar_text(win_t *win, XftDraw *d, XftColor *color, XftFont *font, int x, int y, char *text, int maxlen, int maximum_x)
  304. {
  305. size_t len = 0;
  306. int xshift = 0, newshift;
  307. long codep;
  308. char *p, *nextp;
  309. FcCharSet* fccharset;
  310. XftFont* fallback = NULL;
  311. for (p = text; *p && (len < maxlen); p = nextp, len++) {
  312. nextp = utf8codepoint(p, &codep);
  313. if (!XftCharExists(win->env.dpy, font, codep)) {
  314. fccharset = FcCharSetCreate();
  315. FcCharSetAddChar(fccharset, codep);
  316. fallback = XftFontOpen(win->env.dpy, win->env.scr,
  317. FC_CHARSET, FcTypeCharSet, fccharset,
  318. FC_SCALABLE, FcTypeBool, FcTrue,
  319. NULL);
  320. FcCharSetDestroy(fccharset);
  321. }
  322. newshift = win_textwidth(&win->env, p, (int) (nextp-p), false, (fallback ? fallback : font));
  323. if (xshift + newshift <= maximum_x)
  324. XftDrawStringUtf8(d, color, (fallback ? fallback : font), x + xshift, y, (XftChar8*)p, (int) (nextp-p));
  325. xshift += newshift;
  326. if (fallback) {
  327. XftFontClose(win->env.dpy, fallback);
  328. fallback = NULL;
  329. }
  330. }
  331. }
  332. void win_draw_bar(win_t *win)
  333. {
  334. int len, olen, x, y, w, tw, maximum_x;
  335. char rest[3];
  336. const char *dots = "...";
  337. win_env_t *e;
  338. win_bar_t *l, *r;
  339. XftDraw *d;
  340. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  341. return;
  342. e = &win->env;
  343. y = win->h + font->ascent + V_TEXT_PAD;
  344. w = win->w;
  345. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  346. DefaultColormap(e->dpy, e->scr));
  347. XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
  348. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  349. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  350. XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
  351. if ((len = strlen(r->buf)) > 0) {
  352. if ((tw = win_textwidth(e, r->buf, len, true, font)) > w)
  353. return;
  354. x = win->w - tw + H_TEXT_PAD;
  355. w -= tw;
  356. XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)r->buf, len);
  357. }
  358. if ((len = strlen(l->buf)) > 0) {
  359. olen = len;
  360. while (len > 0 && (tw = win_textwidth(e, l->buf, len, true, font)) > w)
  361. len--;
  362. if (len > 0) {
  363. maximum_x = w;
  364. if (len != olen) {
  365. w = strlen(dots);
  366. if (len <= w)
  367. return;
  368. memcpy(rest, l->buf + len - w, w);
  369. memcpy(l->buf + len - w, dots, w);
  370. }
  371. x = H_TEXT_PAD;
  372. win_draw_bar_text(win, d, &win->bar.fgcol, font, x, y, l->buf, len, maximum_x);
  373. if (len != olen)
  374. memcpy(l->buf + len - w, rest, w);
  375. }
  376. }
  377. XftDrawDestroy(d);
  378. }
  379. void win_draw(win_t *win)
  380. {
  381. if (win->bar.h > 0)
  382. win_draw_bar(win);
  383. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  384. XClearWindow(win->env.dpy, win->xwin);
  385. XFlush(win->env.dpy);
  386. }
  387. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  388. unsigned long col)
  389. {
  390. XGCValues gcval;
  391. gcval.line_width = lw;
  392. gcval.foreground = col;
  393. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  394. if (fill)
  395. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  396. else
  397. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  398. }
  399. int win_textwidth(const win_env_t *e, const char *text, unsigned int len, bool with_padding, XftFont *fnt)
  400. {
  401. XGlyphInfo ext;
  402. if(!fnt)
  403. fnt = font;
  404. XftTextExtentsUtf8(e->dpy, fnt, (XftChar8*)text, len, &ext);
  405. return ext.xOff + (with_padding ? 2 * H_TEXT_PAD : 0);
  406. }
  407. void win_set_title(win_t *win, const char *title)
  408. {
  409. if (title == NULL)
  410. title = "sxiv";
  411. XStoreName(win->env.dpy, win->xwin, title);
  412. XSetIconName(win->env.dpy, win->xwin, title);
  413. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  414. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  415. PropModeReplace, (unsigned char *) title, strlen(title));
  416. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  417. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  418. PropModeReplace, (unsigned char *) title, strlen(title));
  419. }
  420. void win_set_cursor(win_t *win, cursor_t cursor)
  421. {
  422. if (cursor >= 0 && cursor < ARRLEN(cursors)) {
  423. XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
  424. XFlush(win->env.dpy);
  425. }
  426. }
  427. void win_cursor_pos(win_t *win, int *x, int *y)
  428. {
  429. int i;
  430. unsigned int ui;
  431. Window w;
  432. if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
  433. *x = *y = 0;
  434. }