A Simple X Image Viewer
 
 
 
 
 
 

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