A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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