A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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