A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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