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.
 
 
 
 
 
 

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