A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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