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.
 
 
 
 
 
 

591 linhas
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. #define _POSIX_C_SOURCE 200112L
  19. #define _WINDOW_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <locale.h>
  23. #include <X11/cursorfont.h>
  24. #include <X11/Xatom.h>
  25. #include "options.h"
  26. #include "util.h"
  27. #include "window.h"
  28. #include "config.h"
  29. #include "icon/data.h"
  30. enum {
  31. H_TEXT_PAD = 5,
  32. V_TEXT_PAD = 1
  33. };
  34. static Cursor carrow;
  35. static Cursor cnone;
  36. static Cursor chand;
  37. static Cursor cwatch;
  38. static GC gc;
  39. static struct {
  40. int ascent;
  41. int descent;
  42. XFontStruct *xfont;
  43. XFontSet set;
  44. } font;
  45. static int fontheight;
  46. static int barheight;
  47. Atom atoms[ATOM_COUNT];
  48. static Bool fs_support;
  49. static Bool fs_warned;
  50. void win_init_font(Display *dpy, const char *fontstr)
  51. {
  52. int n;
  53. char *def, **missing;
  54. font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  55. if (missing)
  56. XFreeStringList(missing);
  57. if (font.set) {
  58. XFontStruct **xfonts;
  59. char **font_names;
  60. font.ascent = font.descent = 0;
  61. XExtentsOfFontSet(font.set);
  62. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  63. while (n--) {
  64. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  65. font.descent = MAX(font.descent,(*xfonts)->descent);
  66. xfonts++;
  67. }
  68. } else {
  69. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  70. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  71. {
  72. die("could not load font: %s", fontstr);
  73. }
  74. font.ascent = font.xfont->ascent;
  75. font.descent = font.xfont->descent;
  76. }
  77. fontheight = font.ascent + font.descent;
  78. barheight = fontheight + 2 * V_TEXT_PAD;
  79. }
  80. unsigned long win_alloc_color(win_t *win, const char *name)
  81. {
  82. XColor col;
  83. if (win == NULL)
  84. return 0UL;
  85. if (XAllocNamedColor(win->env.dpy,
  86. DefaultColormap(win->env.dpy, win->env.scr),
  87. name, &col, &col) == 0)
  88. {
  89. die("could not allocate color: %s", name);
  90. }
  91. return col.pixel;
  92. }
  93. void win_check_wm_support(Display *dpy, Window root)
  94. {
  95. int format;
  96. long offset = 0, length = 16;
  97. Atom *data, type;
  98. unsigned long i, nitems, bytes_left;
  99. Bool found = False;
  100. while (!found && length > 0) {
  101. if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
  102. offset, length, False, XA_ATOM, &type, &format,
  103. &nitems, &bytes_left, (unsigned char**) &data))
  104. {
  105. break;
  106. }
  107. if (type == XA_ATOM && format == 32) {
  108. for (i = 0; i < nitems; i++) {
  109. if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
  110. found = True;
  111. fs_support = True;
  112. break;
  113. }
  114. }
  115. }
  116. XFree(data);
  117. offset += nitems;
  118. length = MIN(length, bytes_left / 4);
  119. }
  120. }
  121. #define INIT_ATOM_(atom) \
  122. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  123. void win_init(win_t *win)
  124. {
  125. win_env_t *e;
  126. if (win == NULL)
  127. return;
  128. memset(win, 0, sizeof(win_t));
  129. e = &win->env;
  130. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  131. die("could not open display");
  132. e->scr = DefaultScreen(e->dpy);
  133. e->scrw = DisplayWidth(e->dpy, e->scr);
  134. e->scrh = DisplayHeight(e->dpy, e->scr);
  135. e->vis = DefaultVisual(e->dpy, e->scr);
  136. e->cmap = DefaultColormap(e->dpy, e->scr);
  137. e->depth = DefaultDepth(e->dpy, e->scr);
  138. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  139. warn("no locale support");
  140. win_init_font(e->dpy, BAR_FONT);
  141. win->white = WhitePixel(e->dpy, e->scr);
  142. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  143. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  144. win->selcol = win_alloc_color(win, SEL_COLOR);
  145. win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
  146. win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
  147. win->bar.h = options->hide_bar ? 0 : barheight;
  148. win->sizehints.flags = PWinGravity;
  149. win->sizehints.win_gravity = NorthWestGravity;
  150. if (options->fixed_win)
  151. /* actual min/max values set in win_update_sizehints() */
  152. win->sizehints.flags |= PMinSize | PMaxSize;
  153. INIT_ATOM_(WM_DELETE_WINDOW);
  154. INIT_ATOM_(_NET_WM_NAME);
  155. INIT_ATOM_(_NET_WM_ICON_NAME);
  156. INIT_ATOM_(_NET_WM_ICON);
  157. INIT_ATOM_(_NET_WM_STATE);
  158. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  159. INIT_ATOM_(_NET_SUPPORTED);
  160. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  161. }
  162. void win_update_sizehints(win_t *win)
  163. {
  164. if (win == NULL || win->xwin == None)
  165. return;
  166. if ((win->sizehints.flags & USSize) != 0) {
  167. win->sizehints.width = win->w;
  168. win->sizehints.height = win->h + win->bar.h;
  169. }
  170. if ((win->sizehints.flags & USPosition) != 0) {
  171. win->sizehints.x = win->x;
  172. win->sizehints.y = win->y;
  173. }
  174. if (options->fixed_win) {
  175. win->sizehints.min_width = win->sizehints.max_width = win->w;
  176. win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
  177. }
  178. XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
  179. }
  180. void win_open(win_t *win)
  181. {
  182. int c, i, j, n;
  183. win_env_t *e;
  184. XClassHint classhint;
  185. XSetWindowAttributes attr;
  186. unsigned long attr_mask;
  187. unsigned long *icon_data;
  188. XColor col;
  189. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  190. Pixmap none;
  191. int gmask;
  192. if (win == NULL)
  193. return;
  194. e = &win->env;
  195. /* determine window offsets, width & height */
  196. if (options->geometry == NULL)
  197. gmask = 0;
  198. else
  199. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  200. &win->w, &win->h);
  201. if ((gmask & WidthValue) != 0)
  202. win->sizehints.flags |= USSize;
  203. else
  204. win->w = WIN_WIDTH;
  205. if ((gmask & HeightValue) != 0)
  206. win->sizehints.flags |= USSize;
  207. else
  208. win->h = WIN_HEIGHT;
  209. if ((gmask & XValue) != 0) {
  210. if ((gmask & XNegative) != 0) {
  211. win->x += e->scrw - win->w;
  212. win->sizehints.win_gravity = NorthEastGravity;
  213. }
  214. win->sizehints.flags |= USPosition;
  215. } else {
  216. win->x = (e->scrw - win->w) / 2;
  217. }
  218. if ((gmask & YValue) != 0) {
  219. if ((gmask & YNegative) != 0) {
  220. win->y += e->scrh - win->h;
  221. if (win->sizehints.win_gravity == NorthEastGravity)
  222. win->sizehints.win_gravity = SouthEastGravity;
  223. else
  224. win->sizehints.win_gravity = SouthWestGravity;
  225. }
  226. win->sizehints.flags |= USPosition;
  227. } else {
  228. win->y = (e->scrh - win->h) / 2;
  229. }
  230. attr.background_pixel = win->bgcol;
  231. attr_mask = CWBackPixel;
  232. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  233. win->x, win->y, win->w, win->h, 0,
  234. e->depth, InputOutput, e->vis, attr_mask, &attr);
  235. if (win->xwin == None)
  236. die("could not create window");
  237. XSelectInput(e->dpy, win->xwin,
  238. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  239. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  240. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  241. chand = XCreateFontCursor(e->dpy, XC_fleur);
  242. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  243. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  244. &col, &col) == 0)
  245. {
  246. die("could not allocate color: black");
  247. }
  248. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  249. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  250. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  251. n = icons[ARRLEN(icons)-1].size;
  252. icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
  253. for (i = 0; i < ARRLEN(icons); i++) {
  254. n = 0;
  255. icon_data[n++] = icons[i].size;
  256. icon_data[n++] = icons[i].size;
  257. for (j = 0; j < icons[i].cnt; j++) {
  258. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  259. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  260. }
  261. XChangeProperty(e->dpy, win->xwin,
  262. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  263. i == 0 ? PropModeReplace : PropModeAppend,
  264. (unsigned char *) icon_data, n);
  265. }
  266. free(icon_data);
  267. win_set_title(win, "sxiv");
  268. classhint.res_class = "Sxiv";
  269. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  270. XSetClassHint(e->dpy, win->xwin, &classhint);
  271. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  272. win->h -= win->bar.h;
  273. win_update_sizehints(win);
  274. XMapWindow(e->dpy, win->xwin);
  275. XFlush(e->dpy);
  276. if (options->fullscreen)
  277. win_toggle_fullscreen(win);
  278. }
  279. void win_close(win_t *win)
  280. {
  281. if (win == NULL || win->xwin == None)
  282. return;
  283. XFreeCursor(win->env.dpy, carrow);
  284. XFreeCursor(win->env.dpy, cnone);
  285. XFreeCursor(win->env.dpy, chand);
  286. XFreeCursor(win->env.dpy, cwatch);
  287. XFreeGC(win->env.dpy, gc);
  288. XDestroyWindow(win->env.dpy, win->xwin);
  289. XCloseDisplay(win->env.dpy);
  290. }
  291. bool win_configure(win_t *win, XConfigureEvent *c)
  292. {
  293. bool changed;
  294. if (win == NULL || c == NULL)
  295. return false;
  296. if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
  297. if (win->pm != None) {
  298. XFreePixmap(win->env.dpy, win->pm);
  299. win->pm = None;
  300. }
  301. }
  302. win->x = c->x;
  303. win->y = c->y;
  304. win->w = c->width;
  305. win->h = c->height - win->bar.h;
  306. win->bw = c->border_width;
  307. return changed;
  308. }
  309. void win_expose(win_t *win, XExposeEvent *e)
  310. {
  311. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  312. return;
  313. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  314. e->x, e->y, e->width, e->height, e->x, e->y);
  315. }
  316. void win_toggle_fullscreen(win_t *win)
  317. {
  318. XEvent ev;
  319. XClientMessageEvent *cm;
  320. if (win == NULL || win->xwin == None)
  321. return;
  322. if (!fs_support) {
  323. if (!fs_warned) {
  324. warn("window manager does not support fullscreen");
  325. fs_warned = True;
  326. }
  327. return;
  328. }
  329. win->fullscreen = !win->fullscreen;
  330. memset(&ev, 0, sizeof(ev));
  331. ev.type = ClientMessage;
  332. cm = &ev.xclient;
  333. cm->window = win->xwin;
  334. cm->message_type = atoms[ATOM__NET_WM_STATE];
  335. cm->format = 32;
  336. cm->data.l[0] = win->fullscreen;
  337. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  338. cm->data.l[2] = cm->data.l[3] = 0;
  339. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  340. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  341. }
  342. void win_toggle_bar(win_t *win)
  343. {
  344. if (win == NULL || win->xwin == None)
  345. return;
  346. if (win->bar.h != 0) {
  347. win->h += win->bar.h;
  348. win->bar.h = 0;
  349. } else {
  350. win->bar.h = barheight;
  351. win->h -= win->bar.h;
  352. }
  353. }
  354. void win_clear(win_t *win)
  355. {
  356. int h;
  357. win_env_t *e;
  358. if (win == NULL || win->xwin == None)
  359. return;
  360. h = win->h + win->bar.h;
  361. e = &win->env;
  362. if (win->pm == None)
  363. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  364. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  365. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  366. }
  367. void win_draw_bar(win_t *win)
  368. {
  369. int len, olen, x, y, w, tw;
  370. char rest[3];
  371. const char *dots = "...";
  372. win_env_t *e;
  373. if (win == NULL || win->xwin == None || win->pm == None)
  374. return;
  375. e = &win->env;
  376. y = win->h + font.ascent + V_TEXT_PAD;
  377. w = win->w;
  378. XSetForeground(e->dpy, gc, win->bar.bgcol);
  379. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  380. XSetForeground(e->dpy, gc, win->bar.fgcol);
  381. XSetBackground(e->dpy, gc, win->bar.bgcol);
  382. if ((len = strlen(win->bar.r)) > 0) {
  383. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  384. return;
  385. x = win->w - tw + H_TEXT_PAD;
  386. w -= tw;
  387. if (font.set)
  388. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  389. else
  390. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  391. }
  392. if ((len = strlen(win->bar.l)) > 0) {
  393. olen = len;
  394. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  395. len--;
  396. if (len > 0) {
  397. if (len != olen) {
  398. w = strlen(dots);
  399. if (len <= w)
  400. return;
  401. memcpy(rest, win->bar.l + len - w, w);
  402. memcpy(win->bar.l + len - w, dots, w);
  403. }
  404. x = H_TEXT_PAD;
  405. if (font.set)
  406. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  407. else
  408. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  409. if (len != olen)
  410. memcpy(win->bar.l + len - w, rest, w);
  411. }
  412. }
  413. }
  414. void win_draw(win_t *win)
  415. {
  416. if (win == NULL || win->xwin == None || win->pm == None)
  417. return;
  418. if (win->bar.h > 0)
  419. win_draw_bar(win);
  420. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  421. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  422. }
  423. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  424. bool fill, int lw, unsigned long col)
  425. {
  426. XGCValues gcval;
  427. if (win == NULL || pm == None)
  428. return;
  429. gcval.line_width = lw;
  430. gcval.foreground = col;
  431. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  432. if (fill)
  433. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  434. else
  435. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  436. }
  437. void win_update_bar(win_t *win)
  438. {
  439. if (win == NULL || win->xwin == None || win->pm == None)
  440. return;
  441. if (win->bar.h > 0) {
  442. win_draw_bar(win);
  443. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  444. 0, win->h, win->w, win->bar.h, 0, win->h);
  445. }
  446. }
  447. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  448. {
  449. XRectangle r;
  450. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  451. if (font.set) {
  452. XmbTextExtents(font.set, text, len, NULL, &r);
  453. return r.width + padding;
  454. } else {
  455. return XTextWidth(font.xfont, text, len) + padding;
  456. }
  457. }
  458. void win_set_title(win_t *win, const char *title)
  459. {
  460. if (win == NULL || win->xwin == None)
  461. return;
  462. if (title == NULL)
  463. title = "sxiv";
  464. XStoreName(win->env.dpy, win->xwin, title);
  465. XSetIconName(win->env.dpy, win->xwin, title);
  466. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  467. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  468. PropModeReplace, (unsigned char *) title, strlen(title));
  469. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  470. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  471. PropModeReplace, (unsigned char *) title, strlen(title));
  472. }
  473. void win_set_cursor(win_t *win, cursor_t cursor)
  474. {
  475. if (win == NULL || win->xwin == None)
  476. return;
  477. switch (cursor) {
  478. case CURSOR_NONE:
  479. XDefineCursor(win->env.dpy, win->xwin, cnone);
  480. break;
  481. case CURSOR_HAND:
  482. XDefineCursor(win->env.dpy, win->xwin, chand);
  483. break;
  484. case CURSOR_WATCH:
  485. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  486. break;
  487. case CURSOR_ARROW:
  488. default:
  489. XDefineCursor(win->env.dpy, win->xwin, carrow);
  490. break;
  491. }
  492. XFlush(win->env.dpy);
  493. }