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.
 
 
 
 
 
 

501 lines
12 KiB

  1. /* sxiv: window.c
  2. * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  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. int n;
  48. char *def, **missing;
  49. font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  50. if (missing)
  51. XFreeStringList(missing);
  52. if (font.set) {
  53. XFontStruct **xfonts;
  54. char **font_names;
  55. font.ascent = font.descent = 0;
  56. XExtentsOfFontSet(font.set);
  57. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  58. while (n--) {
  59. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  60. font.descent = MAX(font.descent,(*xfonts)->descent);
  61. xfonts++;
  62. }
  63. } else {
  64. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  65. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  66. {
  67. die("could not load font: %s", fontstr);
  68. }
  69. font.ascent = font.xfont->ascent;
  70. font.descent = font.xfont->descent;
  71. }
  72. fontheight = font.ascent + font.descent;
  73. barheight = fontheight + 2 * V_TEXT_PAD;
  74. }
  75. unsigned long win_alloc_color(win_t *win, const char *name) {
  76. XColor col;
  77. if (win == NULL)
  78. return 0UL;
  79. if (XAllocNamedColor(win->env.dpy,
  80. DefaultColormap(win->env.dpy, win->env.scr),
  81. name, &col, &col) == 0)
  82. {
  83. die("could not allocate color: %s", name);
  84. }
  85. return col.pixel;
  86. }
  87. void win_init(win_t *win) {
  88. win_env_t *e;
  89. if (win == NULL)
  90. return;
  91. memset(win, 0, sizeof(win_t));
  92. e = &win->env;
  93. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  94. die("could not open display");
  95. e->scr = DefaultScreen(e->dpy);
  96. e->scrw = DisplayWidth(e->dpy, e->scr);
  97. e->scrh = DisplayHeight(e->dpy, e->scr);
  98. e->vis = DefaultVisual(e->dpy, e->scr);
  99. e->cmap = DefaultColormap(e->dpy, e->scr);
  100. e->depth = DefaultDepth(e->dpy, e->scr);
  101. win->white = WhitePixel(e->dpy, e->scr);
  102. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  103. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  104. win->selcol = win_alloc_color(win, SEL_COLOR);
  105. win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
  106. win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
  107. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  108. warn("no locale support");
  109. win_init_font(e->dpy, BAR_FONT);
  110. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  111. }
  112. void win_set_sizehints(win_t *win) {
  113. XSizeHints sizehints;
  114. if (win == NULL || win->xwin == None)
  115. return;
  116. sizehints.flags = PMinSize | PMaxSize;
  117. sizehints.min_width = win->w;
  118. sizehints.max_width = win->w;
  119. sizehints.min_height = win->h + win->bar.h;
  120. sizehints.max_height = win->h + win->bar.h;
  121. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  122. }
  123. void win_open(win_t *win) {
  124. win_env_t *e;
  125. XClassHint classhint;
  126. XColor col;
  127. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  128. Pixmap none;
  129. int gmask;
  130. if (win == NULL)
  131. return;
  132. e = &win->env;
  133. /* determine window offsets, width & height */
  134. if (options->geometry == NULL)
  135. gmask = 0;
  136. else
  137. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  138. &win->w, &win->h);
  139. if ((gmask & WidthValue) == 0)
  140. win->w = WIN_WIDTH;
  141. if (win->w > e->scrw)
  142. win->w = e->scrw;
  143. if ((gmask & HeightValue) == 0)
  144. win->h = WIN_HEIGHT;
  145. if (win->h > e->scrh)
  146. win->h = e->scrh;
  147. if ((gmask & XValue) == 0)
  148. win->x = (e->scrw - win->w) / 2;
  149. else if ((gmask & XNegative) != 0)
  150. win->x += e->scrw - win->w;
  151. if ((gmask & YValue) == 0)
  152. win->y = (e->scrh - win->h) / 2;
  153. else if ((gmask & YNegative) != 0)
  154. win->y += e->scrh - win->h;
  155. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  156. win->x, win->y, win->w, win->h, 0,
  157. e->depth, InputOutput, e->vis, 0, None);
  158. if (win->xwin == None)
  159. die("could not create window");
  160. XSelectInput(e->dpy, win->xwin,
  161. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  162. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  163. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  164. chand = XCreateFontCursor(e->dpy, XC_fleur);
  165. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  166. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  167. &col, &col) == 0)
  168. {
  169. die("could not allocate color: black");
  170. }
  171. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  172. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  173. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  174. win_set_title(win, "sxiv");
  175. classhint.res_class = "Sxiv";
  176. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  177. XSetClassHint(e->dpy, win->xwin, &classhint);
  178. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  179. if (!options->hide_bar) {
  180. win->bar.h = barheight;
  181. win->h -= win->bar.h;
  182. }
  183. if (options->fixed_win)
  184. win_set_sizehints(win);
  185. XMapWindow(e->dpy, win->xwin);
  186. XFlush(e->dpy);
  187. if (options->fullscreen)
  188. win_toggle_fullscreen(win);
  189. }
  190. void win_close(win_t *win) {
  191. if (win == NULL || win->xwin == None)
  192. return;
  193. XFreeCursor(win->env.dpy, carrow);
  194. XFreeCursor(win->env.dpy, cnone);
  195. XFreeCursor(win->env.dpy, chand);
  196. XFreeCursor(win->env.dpy, cwatch);
  197. XFreeGC(win->env.dpy, gc);
  198. XDestroyWindow(win->env.dpy, win->xwin);
  199. XCloseDisplay(win->env.dpy);
  200. }
  201. bool win_configure(win_t *win, XConfigureEvent *c) {
  202. bool changed;
  203. if (win == NULL || c == NULL)
  204. return false;
  205. if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
  206. if (win->pm != None) {
  207. XFreePixmap(win->env.dpy, win->pm);
  208. win->pm = None;
  209. }
  210. }
  211. win->x = c->x;
  212. win->y = c->y;
  213. win->w = c->width;
  214. win->h = c->height - win->bar.h;
  215. win->bw = c->border_width;
  216. return changed;
  217. }
  218. void win_expose(win_t *win, XExposeEvent *e) {
  219. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  220. return;
  221. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  222. e->x, e->y, e->width, e->height, e->x, e->y);
  223. }
  224. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  225. if (win == NULL || win->xwin == None)
  226. return false;
  227. x = MAX(0, x);
  228. y = MAX(0, y);
  229. w = MIN(w, win->env.scrw - 2 * win->bw);
  230. h = MIN(h, win->env.scrh - 2 * win->bw);
  231. if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
  232. return false;
  233. win->x = x;
  234. win->y = y;
  235. win->w = w;
  236. win->h = h - win->bar.h;
  237. if (options->fixed_win)
  238. win_set_sizehints(win);
  239. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  240. return true;
  241. }
  242. void win_toggle_fullscreen(win_t *win) {
  243. XEvent ev;
  244. XClientMessageEvent *cm;
  245. if (win == NULL || win->xwin == None)
  246. return;
  247. win->fullscreen = !win->fullscreen;
  248. memset(&ev, 0, sizeof(ev));
  249. ev.type = ClientMessage;
  250. cm = &ev.xclient;
  251. cm->window = win->xwin;
  252. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  253. cm->format = 32;
  254. cm->data.l[0] = win->fullscreen;
  255. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  256. cm->data.l[2] = cm->data.l[3] = 0;
  257. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  258. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  259. }
  260. void win_toggle_bar(win_t *win) {
  261. if (win == NULL || win->xwin == None)
  262. return;
  263. if (win->bar.h != 0) {
  264. win->h += win->bar.h;
  265. win->bar.h = 0;
  266. } else {
  267. win->bar.h = barheight;
  268. win->h -= win->bar.h;
  269. }
  270. }
  271. void win_clear(win_t *win) {
  272. int h;
  273. win_env_t *e;
  274. if (win == NULL || win->xwin == None)
  275. return;
  276. h = win->h + win->bar.h;
  277. e = &win->env;
  278. if (win->pm == None)
  279. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  280. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  281. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  282. }
  283. void win_draw_bar(win_t *win) {
  284. int len, olen, x, y, w, tw;
  285. char rest[3];
  286. const char *dots = "...";
  287. win_env_t *e;
  288. if (win == NULL || win->xwin == None || win->pm == None)
  289. return;
  290. e = &win->env;
  291. y = win->h + font.ascent + V_TEXT_PAD;
  292. w = win->w;
  293. XSetForeground(e->dpy, gc, win->bar.bgcol);
  294. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  295. XSetForeground(e->dpy, gc, win->bar.fgcol);
  296. XSetBackground(e->dpy, gc, win->bar.bgcol);
  297. if (win->bar.r != NULL) {
  298. len = strlen(win->bar.r);
  299. if (len > 0) {
  300. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  301. return;
  302. x = win->w - tw + H_TEXT_PAD;
  303. w -= tw;
  304. if (font.set)
  305. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  306. else
  307. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  308. }
  309. }
  310. if (win->bar.l != NULL) {
  311. olen = len = strlen(win->bar.l);
  312. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  313. len--;
  314. if (len > 0) {
  315. if (len != olen) {
  316. w = strlen(dots);
  317. if (len <= w)
  318. return;
  319. memcpy(rest, win->bar.l + len - w, w);
  320. memcpy(win->bar.l + len - w, dots, w);
  321. }
  322. x = H_TEXT_PAD;
  323. if (font.set)
  324. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  325. else
  326. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  327. if (len != olen)
  328. memcpy(win->bar.l + len - w, rest, w);
  329. }
  330. }
  331. }
  332. void win_draw(win_t *win) {
  333. if (win == NULL || win->xwin == None || win->pm == None)
  334. return;
  335. if (win->bar.h > 0)
  336. win_draw_bar(win);
  337. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  338. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  339. }
  340. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  341. bool fill, int lw, unsigned long col)
  342. {
  343. XGCValues gcval;
  344. if (win == NULL || pm == None)
  345. return;
  346. gcval.line_width = lw;
  347. gcval.foreground = col;
  348. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  349. if (fill)
  350. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  351. else
  352. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  353. }
  354. int win_textwidth(const char *text, unsigned int len, bool with_padding) {
  355. XRectangle r;
  356. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  357. if (font.set) {
  358. XmbTextExtents(font.set, text, len, NULL, &r);
  359. return r.width + padding;
  360. } else {
  361. return XTextWidth(font.xfont, text, len) + padding;
  362. }
  363. }
  364. void win_set_title(win_t *win, const char *title) {
  365. if (win == NULL || win->xwin == None)
  366. return;
  367. if (title == NULL)
  368. title = "sxiv";
  369. XStoreName(win->env.dpy, win->xwin, title);
  370. XSetIconName(win->env.dpy, win->xwin, title);
  371. XChangeProperty(win->env.dpy, win->xwin,
  372. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  373. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  374. PropModeReplace, (unsigned char *) title, strlen(title));
  375. XChangeProperty(win->env.dpy, win->xwin,
  376. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  377. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  378. PropModeReplace, (unsigned char *) title, strlen(title));
  379. }
  380. void win_set_bar_info(win_t *win, char *linfo, char *rinfo) {
  381. if (win != NULL) {
  382. win->bar.l = linfo;
  383. win->bar.r = rinfo;
  384. }
  385. }
  386. void win_set_cursor(win_t *win, cursor_t cursor) {
  387. if (win == NULL || win->xwin == None)
  388. return;
  389. switch (cursor) {
  390. case CURSOR_NONE:
  391. XDefineCursor(win->env.dpy, win->xwin, cnone);
  392. break;
  393. case CURSOR_HAND:
  394. XDefineCursor(win->env.dpy, win->xwin, chand);
  395. break;
  396. case CURSOR_WATCH:
  397. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  398. break;
  399. case CURSOR_ARROW:
  400. default:
  401. XDefineCursor(win->env.dpy, win->xwin, carrow);
  402. break;
  403. }
  404. XFlush(win->env.dpy);
  405. }