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ů.
 
 
 
 
 
 

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