A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

522 строки
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. #define INIT_ATOM_(atom) \
  108. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  109. void win_init(win_t *win)
  110. {
  111. win_env_t *e;
  112. memset(win, 0, sizeof(win_t));
  113. e = &win->env;
  114. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  115. error(EXIT_FAILURE, 0, "Error opening X display");
  116. e->scr = DefaultScreen(e->dpy);
  117. e->scrw = DisplayWidth(e->dpy, e->scr);
  118. e->scrh = DisplayHeight(e->dpy, e->scr);
  119. e->vis = DefaultVisual(e->dpy, e->scr);
  120. e->cmap = DefaultColormap(e->dpy, e->scr);
  121. e->depth = DefaultDepth(e->dpy, e->scr);
  122. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  123. error(0, 0, "No locale support");
  124. win_init_font(e, BAR_FONT);
  125. win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", WIN_BG_COLOR),
  126. &win->bgcol);
  127. win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
  128. win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", SEL_COLOR),
  129. &win->selcol);
  130. win_alloc_color(e, win_res(e->dpy, RES_CLASS ".foreground", BAR_BG_COLOR),
  131. &win->bar.bgcol);
  132. win_alloc_color(e, win_res(e->dpy, RES_CLASS ".background", BAR_FG_COLOR),
  133. &win->bar.fgcol);
  134. win->bar.l.size = BAR_L_LEN;
  135. win->bar.r.size = BAR_R_LEN;
  136. /* 3 padding bytes needed by utf8_decode */
  137. win->bar.l.buf = emalloc(win->bar.l.size + 3);
  138. win->bar.l.buf[0] = '\0';
  139. win->bar.r.buf = emalloc(win->bar.r.size + 3);
  140. win->bar.r.buf[0] = '\0';
  141. win->bar.h = options->hide_bar ? 0 : barheight;
  142. INIT_ATOM_(WM_DELETE_WINDOW);
  143. INIT_ATOM_(_NET_WM_NAME);
  144. INIT_ATOM_(_NET_WM_ICON_NAME);
  145. INIT_ATOM_(_NET_WM_ICON);
  146. INIT_ATOM_(_NET_WM_STATE);
  147. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  148. INIT_ATOM_(_NET_SUPPORTED);
  149. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  150. }
  151. void win_open(win_t *win)
  152. {
  153. int c, i, j, n;
  154. long parent;
  155. win_env_t *e;
  156. XClassHint classhint;
  157. unsigned long *icon_data;
  158. XColor col;
  159. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  160. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  161. Pixmap none;
  162. int gmask;
  163. XSizeHints sizehints;
  164. Bool fullscreen = options->fullscreen && fs_support;
  165. e = &win->env;
  166. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  167. sizehints.flags = PWinGravity;
  168. sizehints.win_gravity = NorthWestGravity;
  169. /* determine window offsets, width & height */
  170. if (options->geometry == NULL)
  171. gmask = 0;
  172. else
  173. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  174. &win->w, &win->h);
  175. if ((gmask & WidthValue) != 0)
  176. sizehints.flags |= USSize;
  177. else
  178. win->w = WIN_WIDTH;
  179. if ((gmask & HeightValue) != 0)
  180. sizehints.flags |= USSize;
  181. else
  182. win->h = WIN_HEIGHT;
  183. if ((gmask & XValue) != 0) {
  184. if ((gmask & XNegative) != 0) {
  185. win->x += e->scrw - win->w;
  186. sizehints.win_gravity = NorthEastGravity;
  187. }
  188. sizehints.flags |= USPosition;
  189. } else {
  190. win->x = 0;
  191. }
  192. if ((gmask & YValue) != 0) {
  193. if ((gmask & YNegative) != 0) {
  194. win->y += e->scrh - win->h;
  195. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  196. ? SouthEastGravity : SouthWestGravity;
  197. }
  198. sizehints.flags |= USPosition;
  199. } else {
  200. win->y = 0;
  201. }
  202. win->xwin = XCreateWindow(e->dpy, parent,
  203. win->x, win->y, win->w, win->h, 0,
  204. e->depth, InputOutput, e->vis, 0, NULL);
  205. if (win->xwin == None)
  206. error(EXIT_FAILURE, 0, "Error creating X window");
  207. XSelectInput(e->dpy, win->xwin,
  208. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  209. PointerMotionMask | StructureNotifyMask);
  210. for (i = 0; i < ARRLEN(cursors); i++) {
  211. if (i != CURSOR_NONE)
  212. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  213. }
  214. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  215. &col, &col) == 0)
  216. {
  217. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  218. }
  219. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  220. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  221. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  222. n = icons[ARRLEN(icons)-1].size;
  223. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  224. for (i = 0; i < ARRLEN(icons); i++) {
  225. n = 0;
  226. icon_data[n++] = icons[i].size;
  227. icon_data[n++] = icons[i].size;
  228. for (j = 0; j < icons[i].cnt; j++) {
  229. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  230. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  231. }
  232. XChangeProperty(e->dpy, win->xwin,
  233. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  234. i == 0 ? PropModeReplace : PropModeAppend,
  235. (unsigned char *) icon_data, n);
  236. }
  237. free(icon_data);
  238. win_set_title(win, "sxiv");
  239. classhint.res_class = RES_CLASS;
  240. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  241. XSetClassHint(e->dpy, win->xwin, &classhint);
  242. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  243. sizehints.width = win->w;
  244. sizehints.height = win->h;
  245. sizehints.x = win->x;
  246. sizehints.y = win->y;
  247. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  248. win->h -= win->bar.h;
  249. win->buf.w = e->scrw;
  250. win->buf.h = e->scrh;
  251. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  252. win->buf.w, win->buf.h, e->depth);
  253. XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  254. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  255. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  256. XMapWindow(e->dpy, win->xwin);
  257. XFlush(e->dpy);
  258. if (fullscreen)
  259. win_toggle_fullscreen(win);
  260. }
  261. CLEANUP void win_close(win_t *win)
  262. {
  263. int i;
  264. for (i = 0; i < ARRLEN(cursors); i++)
  265. XFreeCursor(win->env.dpy, cursors[i].icon);
  266. XFreeGC(win->env.dpy, gc);
  267. XDestroyWindow(win->env.dpy, win->xwin);
  268. XCloseDisplay(win->env.dpy);
  269. }
  270. bool win_configure(win_t *win, XConfigureEvent *c)
  271. {
  272. bool changed;
  273. changed = win->w != c->width || win->h + win->bar.h != c->height;
  274. win->x = c->x;
  275. win->y = c->y;
  276. win->w = c->width;
  277. win->h = c->height - win->bar.h;
  278. win->bw = c->border_width;
  279. return changed;
  280. }
  281. void win_toggle_fullscreen(win_t *win)
  282. {
  283. XEvent ev;
  284. XClientMessageEvent *cm;
  285. if (!fs_support) {
  286. if (!fs_warned) {
  287. error(0, 0, "No fullscreen support");
  288. fs_warned = True;
  289. }
  290. return;
  291. }
  292. win->fullscreen = !win->fullscreen;
  293. memset(&ev, 0, sizeof(ev));
  294. ev.type = ClientMessage;
  295. cm = &ev.xclient;
  296. cm->window = win->xwin;
  297. cm->message_type = atoms[ATOM__NET_WM_STATE];
  298. cm->format = 32;
  299. cm->data.l[0] = win->fullscreen;
  300. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  301. cm->data.l[2] = cm->data.l[3] = 0;
  302. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  303. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  304. }
  305. void win_toggle_bar(win_t *win)
  306. {
  307. if (win->bar.h != 0) {
  308. win->h += win->bar.h;
  309. win->bar.h = 0;
  310. } else {
  311. win->bar.h = barheight;
  312. win->h -= win->bar.h;
  313. }
  314. }
  315. void win_clear(win_t *win)
  316. {
  317. win_env_t *e;
  318. e = &win->env;
  319. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  320. XFreePixmap(e->dpy, win->buf.pm);
  321. win->buf.w = MAX(win->buf.w, win->w);
  322. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  323. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  324. win->buf.w, win->buf.h, e->depth);
  325. }
  326. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  327. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  328. }
  329. #define TEXTWIDTH(win, text, len) \
  330. win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
  331. int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
  332. char *text, int len, int w)
  333. {
  334. int err, tw = 0;
  335. char *t, *next;
  336. uint32_t rune;
  337. XftFont *f;
  338. FcCharSet *fccharset;
  339. XGlyphInfo ext;
  340. for (t = text; t - text < len; t = next) {
  341. next = utf8_decode(t, &rune, &err);
  342. if (XftCharExists(win->env.dpy, font, rune)) {
  343. f = font;
  344. } else { /* fallback font */
  345. fccharset = FcCharSetCreate();
  346. FcCharSetAddChar(fccharset, rune);
  347. f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
  348. fccharset, FC_SCALABLE, FcTypeBool, FcTrue, 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->bar.bgcol.pixel);
  376. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  377. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  378. XSetBackground(e->dpy, gc, win->bar.bgcol.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->bar.fgcol, 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->bar.fgcol, 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. }