A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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