A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

496 行
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.l.buf[0] = '\0';
  117. win->bar.r.buf = emalloc(win->bar.r.size + 3);
  118. win->bar.r.buf[0] = '\0';
  119. win->bar.h = options->hide_bar ? 0 : barheight;
  120. INIT_ATOM_(WM_DELETE_WINDOW);
  121. INIT_ATOM_(_NET_WM_NAME);
  122. INIT_ATOM_(_NET_WM_ICON_NAME);
  123. INIT_ATOM_(_NET_WM_ICON);
  124. INIT_ATOM_(_NET_WM_STATE);
  125. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  126. INIT_ATOM_(_NET_SUPPORTED);
  127. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  128. }
  129. void win_open(win_t *win)
  130. {
  131. int c, i, j, n;
  132. long parent;
  133. win_env_t *e;
  134. XClassHint classhint;
  135. unsigned long *icon_data;
  136. XColor col;
  137. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  138. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  139. Pixmap none;
  140. int gmask;
  141. XSizeHints sizehints;
  142. Bool fullscreen = options->fullscreen && fs_support;
  143. e = &win->env;
  144. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  145. sizehints.flags = PWinGravity;
  146. sizehints.win_gravity = NorthWestGravity;
  147. /* determine window offsets, width & height */
  148. if (options->geometry == NULL)
  149. gmask = 0;
  150. else
  151. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  152. &win->w, &win->h);
  153. if ((gmask & WidthValue) != 0)
  154. sizehints.flags |= USSize;
  155. else
  156. win->w = WIN_WIDTH;
  157. if ((gmask & HeightValue) != 0)
  158. sizehints.flags |= USSize;
  159. else
  160. win->h = WIN_HEIGHT;
  161. if ((gmask & XValue) != 0) {
  162. if ((gmask & XNegative) != 0) {
  163. win->x += e->scrw - win->w;
  164. sizehints.win_gravity = NorthEastGravity;
  165. }
  166. sizehints.flags |= USPosition;
  167. } else {
  168. win->x = 0;
  169. }
  170. if ((gmask & YValue) != 0) {
  171. if ((gmask & YNegative) != 0) {
  172. win->y += e->scrh - win->h;
  173. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  174. ? SouthEastGravity : SouthWestGravity;
  175. }
  176. sizehints.flags |= USPosition;
  177. } else {
  178. win->y = 0;
  179. }
  180. win->xwin = XCreateWindow(e->dpy, parent,
  181. win->x, win->y, win->w, win->h, 0,
  182. e->depth, InputOutput, e->vis, 0, NULL);
  183. if (win->xwin == None)
  184. error(EXIT_FAILURE, 0, "Error creating X window");
  185. XSelectInput(e->dpy, win->xwin,
  186. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  187. PointerMotionMask | StructureNotifyMask);
  188. for (i = 0; i < ARRLEN(cursors); i++) {
  189. if (i != CURSOR_NONE)
  190. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  191. }
  192. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  193. &col, &col) == 0)
  194. {
  195. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  196. }
  197. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  198. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  199. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  200. n = icons[ARRLEN(icons)-1].size;
  201. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  202. for (i = 0; i < ARRLEN(icons); i++) {
  203. n = 0;
  204. icon_data[n++] = icons[i].size;
  205. icon_data[n++] = icons[i].size;
  206. for (j = 0; j < icons[i].cnt; j++) {
  207. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  208. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  209. }
  210. XChangeProperty(e->dpy, win->xwin,
  211. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  212. i == 0 ? PropModeReplace : PropModeAppend,
  213. (unsigned char *) icon_data, n);
  214. }
  215. free(icon_data);
  216. win_set_title(win, "sxiv");
  217. classhint.res_class = "Sxiv";
  218. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  219. XSetClassHint(e->dpy, win->xwin, &classhint);
  220. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  221. sizehints.width = win->w;
  222. sizehints.height = win->h;
  223. sizehints.x = win->x;
  224. sizehints.y = win->y;
  225. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  226. win->h -= win->bar.h;
  227. win->buf.w = e->scrw;
  228. win->buf.h = e->scrh;
  229. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  230. win->buf.w, win->buf.h, e->depth);
  231. XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  232. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  233. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  234. XMapWindow(e->dpy, win->xwin);
  235. XFlush(e->dpy);
  236. if (fullscreen)
  237. win_toggle_fullscreen(win);
  238. }
  239. CLEANUP void win_close(win_t *win)
  240. {
  241. int i;
  242. for (i = 0; i < ARRLEN(cursors); i++)
  243. XFreeCursor(win->env.dpy, cursors[i].icon);
  244. XFreeGC(win->env.dpy, gc);
  245. XDestroyWindow(win->env.dpy, win->xwin);
  246. XCloseDisplay(win->env.dpy);
  247. }
  248. bool win_configure(win_t *win, XConfigureEvent *c)
  249. {
  250. bool changed;
  251. changed = win->w != c->width || win->h + win->bar.h != c->height;
  252. win->x = c->x;
  253. win->y = c->y;
  254. win->w = c->width;
  255. win->h = c->height - win->bar.h;
  256. win->bw = c->border_width;
  257. return changed;
  258. }
  259. void win_toggle_fullscreen(win_t *win)
  260. {
  261. XEvent ev;
  262. XClientMessageEvent *cm;
  263. if (!fs_support) {
  264. if (!fs_warned) {
  265. error(0, 0, "No fullscreen support");
  266. fs_warned = True;
  267. }
  268. return;
  269. }
  270. win->fullscreen = !win->fullscreen;
  271. memset(&ev, 0, sizeof(ev));
  272. ev.type = ClientMessage;
  273. cm = &ev.xclient;
  274. cm->window = win->xwin;
  275. cm->message_type = atoms[ATOM__NET_WM_STATE];
  276. cm->format = 32;
  277. cm->data.l[0] = win->fullscreen;
  278. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  279. cm->data.l[2] = cm->data.l[3] = 0;
  280. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  281. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  282. }
  283. void win_toggle_bar(win_t *win)
  284. {
  285. if (win->bar.h != 0) {
  286. win->h += win->bar.h;
  287. win->bar.h = 0;
  288. } else {
  289. win->bar.h = barheight;
  290. win->h -= win->bar.h;
  291. }
  292. }
  293. void win_clear(win_t *win)
  294. {
  295. win_env_t *e;
  296. e = &win->env;
  297. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  298. XFreePixmap(e->dpy, win->buf.pm);
  299. win->buf.w = MAX(win->buf.w, win->w);
  300. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  301. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  302. win->buf.w, win->buf.h, e->depth);
  303. }
  304. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  305. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  306. }
  307. #define TEXTWIDTH(win, text, len) \
  308. win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
  309. int win_draw_text(win_t *win, XftDraw *d, XftColor *color, int x, int y,
  310. char *text, int len, int w)
  311. {
  312. int err, tw = 0;
  313. char *t, *next;
  314. uint32_t rune;
  315. XftFont *f;
  316. FcCharSet *fccharset;
  317. XGlyphInfo ext;
  318. for (t = text; t - text < len; t = next) {
  319. next = utf8_decode(t, &rune, &err);
  320. if (XftCharExists(win->env.dpy, font, rune)) {
  321. f = font;
  322. } else { /* fallback font */
  323. fccharset = FcCharSetCreate();
  324. FcCharSetAddChar(fccharset, rune);
  325. f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
  326. fccharset, FC_SCALABLE, FcTypeBool, FcTrue, NULL);
  327. FcCharSetDestroy(fccharset);
  328. }
  329. XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
  330. tw += ext.xOff;
  331. if (tw <= w) {
  332. XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
  333. x += ext.xOff;
  334. }
  335. if (f != font)
  336. XftFontClose(win->env.dpy, f);
  337. }
  338. return tw;
  339. }
  340. void win_draw_bar(win_t *win)
  341. {
  342. int len, x, y, w, tw;
  343. win_env_t *e;
  344. win_bar_t *l, *r;
  345. XftDraw *d;
  346. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  347. return;
  348. e = &win->env;
  349. y = win->h + font->ascent + V_TEXT_PAD;
  350. w = win->w - 2*H_TEXT_PAD;
  351. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  352. DefaultColormap(e->dpy, e->scr));
  353. XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
  354. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  355. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  356. XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
  357. if ((len = strlen(r->buf)) > 0) {
  358. if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
  359. return;
  360. x = win->w - tw - H_TEXT_PAD;
  361. w -= tw;
  362. win_draw_text(win, d, &win->bar.fgcol, x, y, r->buf, len, tw);
  363. }
  364. if ((len = strlen(l->buf)) > 0) {
  365. x = H_TEXT_PAD;
  366. w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
  367. win_draw_text(win, d, &win->bar.fgcol, x, y, l->buf, len, w);
  368. }
  369. XftDrawDestroy(d);
  370. }
  371. void win_draw(win_t *win)
  372. {
  373. if (win->bar.h > 0)
  374. win_draw_bar(win);
  375. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  376. XClearWindow(win->env.dpy, win->xwin);
  377. XFlush(win->env.dpy);
  378. }
  379. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  380. unsigned long col)
  381. {
  382. XGCValues gcval;
  383. gcval.line_width = lw;
  384. gcval.foreground = col;
  385. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  386. if (fill)
  387. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  388. else
  389. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  390. }
  391. void win_set_title(win_t *win, const char *title)
  392. {
  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. }