A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

572 řádky
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. INIT_ATOM_(WM_DELETE_WINDOW);
  149. INIT_ATOM_(_NET_WM_NAME);
  150. INIT_ATOM_(_NET_WM_ICON_NAME);
  151. INIT_ATOM_(_NET_WM_ICON);
  152. INIT_ATOM_(_NET_WM_STATE);
  153. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  154. INIT_ATOM_(_NET_SUPPORTED);
  155. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  156. }
  157. void win_open(win_t *win)
  158. {
  159. int c, i, j, n;
  160. win_env_t *e;
  161. XClassHint classhint;
  162. XSetWindowAttributes attr;
  163. unsigned long attr_mask;
  164. unsigned long *icon_data;
  165. XColor col;
  166. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  167. Pixmap none;
  168. int gmask;
  169. XSizeHints sizehints;
  170. if (win == NULL)
  171. return;
  172. e = &win->env;
  173. sizehints.flags = PWinGravity;
  174. sizehints.win_gravity = NorthWestGravity;
  175. /* determine window offsets, width & height */
  176. if (options->geometry == NULL)
  177. gmask = 0;
  178. else
  179. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  180. &win->w, &win->h);
  181. if ((gmask & WidthValue) != 0)
  182. sizehints.flags |= USSize;
  183. else
  184. win->w = WIN_WIDTH;
  185. if ((gmask & HeightValue) != 0)
  186. sizehints.flags |= USSize;
  187. else
  188. win->h = WIN_HEIGHT;
  189. if ((gmask & XValue) != 0) {
  190. if ((gmask & XNegative) != 0) {
  191. win->x += e->scrw - win->w;
  192. sizehints.win_gravity = NorthEastGravity;
  193. }
  194. sizehints.flags |= USPosition;
  195. } else {
  196. win->x = (e->scrw - win->w) / 2;
  197. }
  198. if ((gmask & YValue) != 0) {
  199. if ((gmask & YNegative) != 0) {
  200. win->y += e->scrh - win->h;
  201. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  202. ? SouthEastGravity : SouthWestGravity;
  203. }
  204. sizehints.flags |= USPosition;
  205. } else {
  206. win->y = (e->scrh - win->h) / 2;
  207. }
  208. attr.background_pixel = win->bgcol;
  209. attr_mask = CWBackPixel;
  210. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  211. win->x, win->y, win->w, win->h, 0,
  212. e->depth, InputOutput, e->vis, attr_mask, &attr);
  213. if (win->xwin == None)
  214. die("could not create window");
  215. XSelectInput(e->dpy, win->xwin,
  216. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  217. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  218. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  219. chand = XCreateFontCursor(e->dpy, XC_fleur);
  220. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  221. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  222. &col, &col) == 0)
  223. {
  224. die("could not allocate color: black");
  225. }
  226. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  227. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  228. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  229. n = icons[ARRLEN(icons)-1].size;
  230. icon_data = s_malloc((n * n + 2) * sizeof(*icon_data));
  231. for (i = 0; i < ARRLEN(icons); i++) {
  232. n = 0;
  233. icon_data[n++] = icons[i].size;
  234. icon_data[n++] = icons[i].size;
  235. for (j = 0; j < icons[i].cnt; j++) {
  236. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  237. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  238. }
  239. XChangeProperty(e->dpy, win->xwin,
  240. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  241. i == 0 ? PropModeReplace : PropModeAppend,
  242. (unsigned char *) icon_data, n);
  243. }
  244. free(icon_data);
  245. win_set_title(win, "sxiv");
  246. classhint.res_class = "Sxiv";
  247. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  248. XSetClassHint(e->dpy, win->xwin, &classhint);
  249. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  250. sizehints.width = win->w;
  251. sizehints.height = win->h;
  252. sizehints.x = win->x;
  253. sizehints.y = win->y;
  254. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  255. win->h -= win->bar.h;
  256. XMapWindow(e->dpy, win->xwin);
  257. XFlush(e->dpy);
  258. if (options->fullscreen)
  259. win_toggle_fullscreen(win);
  260. }
  261. void win_close(win_t *win)
  262. {
  263. if (win == NULL || win->xwin == None)
  264. return;
  265. XFreeCursor(win->env.dpy, carrow);
  266. XFreeCursor(win->env.dpy, cnone);
  267. XFreeCursor(win->env.dpy, chand);
  268. XFreeCursor(win->env.dpy, cwatch);
  269. XFreeGC(win->env.dpy, gc);
  270. XDestroyWindow(win->env.dpy, win->xwin);
  271. XCloseDisplay(win->env.dpy);
  272. }
  273. bool win_configure(win_t *win, XConfigureEvent *c)
  274. {
  275. bool changed;
  276. if (win == NULL || c == NULL)
  277. return false;
  278. if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
  279. if (win->pm != None) {
  280. XFreePixmap(win->env.dpy, win->pm);
  281. win->pm = None;
  282. }
  283. }
  284. win->x = c->x;
  285. win->y = c->y;
  286. win->w = c->width;
  287. win->h = c->height - win->bar.h;
  288. win->bw = c->border_width;
  289. return changed;
  290. }
  291. void win_expose(win_t *win, XExposeEvent *e)
  292. {
  293. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  294. return;
  295. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  296. e->x, e->y, e->width, e->height, e->x, e->y);
  297. }
  298. void win_toggle_fullscreen(win_t *win)
  299. {
  300. XEvent ev;
  301. XClientMessageEvent *cm;
  302. if (win == NULL || win->xwin == None)
  303. return;
  304. if (!fs_support) {
  305. if (!fs_warned) {
  306. warn("window manager does not support fullscreen");
  307. fs_warned = True;
  308. }
  309. return;
  310. }
  311. win->fullscreen = !win->fullscreen;
  312. memset(&ev, 0, sizeof(ev));
  313. ev.type = ClientMessage;
  314. cm = &ev.xclient;
  315. cm->window = win->xwin;
  316. cm->message_type = atoms[ATOM__NET_WM_STATE];
  317. cm->format = 32;
  318. cm->data.l[0] = win->fullscreen;
  319. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  320. cm->data.l[2] = cm->data.l[3] = 0;
  321. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  322. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  323. }
  324. void win_toggle_bar(win_t *win)
  325. {
  326. if (win == NULL || win->xwin == None)
  327. return;
  328. if (win->bar.h != 0) {
  329. win->h += win->bar.h;
  330. win->bar.h = 0;
  331. } else {
  332. win->bar.h = barheight;
  333. win->h -= win->bar.h;
  334. }
  335. }
  336. void win_clear(win_t *win)
  337. {
  338. int h;
  339. win_env_t *e;
  340. if (win == NULL || win->xwin == None)
  341. return;
  342. h = win->h + win->bar.h;
  343. e = &win->env;
  344. if (win->pm == None)
  345. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  346. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  347. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  348. }
  349. void win_draw_bar(win_t *win)
  350. {
  351. int len, olen, x, y, w, tw;
  352. char rest[3];
  353. const char *dots = "...";
  354. win_env_t *e;
  355. if (win == NULL || win->xwin == None || win->pm == None)
  356. return;
  357. e = &win->env;
  358. y = win->h + font.ascent + V_TEXT_PAD;
  359. w = win->w;
  360. XSetForeground(e->dpy, gc, win->bar.bgcol);
  361. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  362. XSetForeground(e->dpy, gc, win->bar.fgcol);
  363. XSetBackground(e->dpy, gc, win->bar.bgcol);
  364. if ((len = strlen(win->bar.r)) > 0) {
  365. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  366. return;
  367. x = win->w - tw + H_TEXT_PAD;
  368. w -= tw;
  369. if (font.set)
  370. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  371. else
  372. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  373. }
  374. if ((len = strlen(win->bar.l)) > 0) {
  375. olen = len;
  376. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  377. len--;
  378. if (len > 0) {
  379. if (len != olen) {
  380. w = strlen(dots);
  381. if (len <= w)
  382. return;
  383. memcpy(rest, win->bar.l + len - w, w);
  384. memcpy(win->bar.l + len - w, dots, w);
  385. }
  386. x = H_TEXT_PAD;
  387. if (font.set)
  388. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  389. else
  390. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  391. if (len != olen)
  392. memcpy(win->bar.l + len - w, rest, w);
  393. }
  394. }
  395. }
  396. void win_draw(win_t *win)
  397. {
  398. if (win == NULL || win->xwin == None || win->pm == None)
  399. return;
  400. if (win->bar.h > 0)
  401. win_draw_bar(win);
  402. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  403. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  404. }
  405. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  406. bool fill, int lw, unsigned long col)
  407. {
  408. XGCValues gcval;
  409. if (win == NULL || pm == None)
  410. return;
  411. gcval.line_width = lw;
  412. gcval.foreground = col;
  413. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  414. if (fill)
  415. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  416. else
  417. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  418. }
  419. void win_update_bar(win_t *win)
  420. {
  421. if (win == NULL || win->xwin == None || win->pm == None)
  422. return;
  423. if (win->bar.h > 0) {
  424. win_draw_bar(win);
  425. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  426. 0, win->h, win->w, win->bar.h, 0, win->h);
  427. }
  428. }
  429. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  430. {
  431. XRectangle r;
  432. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  433. if (font.set) {
  434. XmbTextExtents(font.set, text, len, NULL, &r);
  435. return r.width + padding;
  436. } else {
  437. return XTextWidth(font.xfont, text, len) + padding;
  438. }
  439. }
  440. void win_set_title(win_t *win, const char *title)
  441. {
  442. if (win == NULL || win->xwin == None)
  443. return;
  444. if (title == NULL)
  445. title = "sxiv";
  446. XStoreName(win->env.dpy, win->xwin, title);
  447. XSetIconName(win->env.dpy, win->xwin, title);
  448. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  449. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  450. PropModeReplace, (unsigned char *) title, strlen(title));
  451. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  452. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  453. PropModeReplace, (unsigned char *) title, strlen(title));
  454. }
  455. void win_set_cursor(win_t *win, cursor_t cursor)
  456. {
  457. if (win == NULL || win->xwin == None)
  458. return;
  459. switch (cursor) {
  460. case CURSOR_NONE:
  461. XDefineCursor(win->env.dpy, win->xwin, cnone);
  462. break;
  463. case CURSOR_HAND:
  464. XDefineCursor(win->env.dpy, win->xwin, chand);
  465. break;
  466. case CURSOR_WATCH:
  467. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  468. break;
  469. case CURSOR_ARROW:
  470. default:
  471. XDefineCursor(win->env.dpy, win->xwin, carrow);
  472. break;
  473. }
  474. XFlush(win->env.dpy);
  475. }