A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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