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.

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