A Simple X Image Viewer
 
 
 
 
 
 

533 行
13 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. win->sizehints.flags = PWinGravity;
  108. win->sizehints.win_gravity = NorthWestGravity;
  109. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  110. warn("no locale support");
  111. win_init_font(e->dpy, BAR_FONT);
  112. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  113. }
  114. void win_set_sizehints(win_t *win) {
  115. if (win == NULL || win->xwin == None)
  116. return;
  117. if ((win->sizehints.flags & PMinSize) == 1) {
  118. win->sizehints.min_width = win->w;
  119. win->sizehints.min_height = win->h + win->bar.h;
  120. }
  121. if ((win->sizehints.flags & PMaxSize) == 1) {
  122. win->sizehints.max_width = win->w;
  123. win->sizehints.max_height = win->h + win->bar.h;
  124. }
  125. if ((win->sizehints.flags & USPosition) == 1) {
  126. win->sizehints.x = win->x;
  127. win->sizehints.y = win->y;
  128. }
  129. XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
  130. }
  131. void win_open(win_t *win) {
  132. win_env_t *e;
  133. XClassHint classhint;
  134. XColor col;
  135. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  136. Pixmap none;
  137. int gmask;
  138. if (win == NULL)
  139. return;
  140. e = &win->env;
  141. /* determine window offsets, width & height */
  142. if (options->geometry == NULL)
  143. gmask = 0;
  144. else
  145. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  146. &win->w, &win->h);
  147. if ((gmask & WidthValue) == 0) {
  148. win->w = WIN_WIDTH;
  149. } else {
  150. win->sizehints.flags |= USSize;
  151. }
  152. if (win->w > e->scrw)
  153. win->w = e->scrw;
  154. if ((gmask & HeightValue) == 0) {
  155. win->h = WIN_HEIGHT;
  156. } else {
  157. win->sizehints.flags |= USSize;
  158. }
  159. if (win->h > e->scrh)
  160. win->h = e->scrh;
  161. if ((gmask & XValue) == 0) {
  162. win->x = (e->scrw - win->w) / 2;
  163. } else {
  164. if ((gmask & XNegative) != 0) {
  165. win->x += e->scrw - win->w;
  166. win->sizehints.win_gravity = NorthEastGravity;
  167. }
  168. win->sizehints.flags |= USPosition;
  169. }
  170. if ((gmask & YValue) == 0) {
  171. win->y = (e->scrh - win->h) / 2;
  172. } else {
  173. if ((gmask & YNegative) != 0) {
  174. win->y += e->scrh - win->h;
  175. if (win->sizehints.win_gravity == NorthEastGravity) {
  176. win->sizehints.win_gravity = SouthEastGravity;
  177. } else {
  178. win->sizehints.win_gravity = SouthWestGravity;
  179. }
  180. }
  181. win->sizehints.flags |= USPosition;
  182. }
  183. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  184. win->x, win->y, win->w, win->h, 0,
  185. e->depth, InputOutput, e->vis, 0, None);
  186. if (win->xwin == None)
  187. die("could not create window");
  188. XSelectInput(e->dpy, win->xwin,
  189. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  190. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  191. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  192. chand = XCreateFontCursor(e->dpy, XC_fleur);
  193. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  194. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  195. &col, &col) == 0)
  196. {
  197. die("could not allocate color: black");
  198. }
  199. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  200. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  201. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  202. win_set_title(win, "sxiv");
  203. classhint.res_class = "Sxiv";
  204. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  205. XSetClassHint(e->dpy, win->xwin, &classhint);
  206. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  207. if (!options->hide_bar) {
  208. win->bar.h = barheight;
  209. win->h -= win->bar.h;
  210. }
  211. if (options->fixed_win)
  212. win->sizehints.flags |= PMinSize | PMaxSize;
  213. win_set_sizehints(win);
  214. XMapWindow(e->dpy, win->xwin);
  215. XFlush(e->dpy);
  216. if (options->fullscreen)
  217. win_toggle_fullscreen(win);
  218. }
  219. void win_close(win_t *win) {
  220. if (win == NULL || win->xwin == None)
  221. return;
  222. XFreeCursor(win->env.dpy, carrow);
  223. XFreeCursor(win->env.dpy, cnone);
  224. XFreeCursor(win->env.dpy, chand);
  225. XFreeCursor(win->env.dpy, cwatch);
  226. XFreeGC(win->env.dpy, gc);
  227. XDestroyWindow(win->env.dpy, win->xwin);
  228. XCloseDisplay(win->env.dpy);
  229. }
  230. bool win_configure(win_t *win, XConfigureEvent *c) {
  231. bool changed;
  232. if (win == NULL || c == NULL)
  233. return false;
  234. if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
  235. if (win->pm != None) {
  236. XFreePixmap(win->env.dpy, win->pm);
  237. win->pm = None;
  238. }
  239. }
  240. win->x = c->x;
  241. win->y = c->y;
  242. win->w = c->width;
  243. win->h = c->height - win->bar.h;
  244. win->bw = c->border_width;
  245. return changed;
  246. }
  247. void win_expose(win_t *win, XExposeEvent *e) {
  248. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  249. return;
  250. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  251. e->x, e->y, e->width, e->height, e->x, e->y);
  252. }
  253. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  254. if (win == NULL || win->xwin == None)
  255. return false;
  256. x = MAX(0, x);
  257. y = MAX(0, y);
  258. w = MIN(w, win->env.scrw - 2 * win->bw);
  259. h = MIN(h, win->env.scrh - 2 * win->bw);
  260. if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
  261. return false;
  262. win->x = x;
  263. win->y = y;
  264. win->w = w;
  265. win->h = h - win->bar.h;
  266. win_set_sizehints(win);
  267. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  268. return true;
  269. }
  270. void win_toggle_fullscreen(win_t *win) {
  271. XEvent ev;
  272. XClientMessageEvent *cm;
  273. if (win == NULL || win->xwin == None)
  274. return;
  275. win->fullscreen = !win->fullscreen;
  276. memset(&ev, 0, sizeof(ev));
  277. ev.type = ClientMessage;
  278. cm = &ev.xclient;
  279. cm->window = win->xwin;
  280. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  281. cm->format = 32;
  282. cm->data.l[0] = win->fullscreen;
  283. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  284. cm->data.l[2] = cm->data.l[3] = 0;
  285. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  286. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  287. }
  288. void win_toggle_bar(win_t *win) {
  289. if (win == NULL || win->xwin == None)
  290. return;
  291. if (win->bar.h != 0) {
  292. win->h += win->bar.h;
  293. win->bar.h = 0;
  294. } else {
  295. win->bar.h = barheight;
  296. win->h -= win->bar.h;
  297. }
  298. }
  299. void win_clear(win_t *win) {
  300. int h;
  301. win_env_t *e;
  302. if (win == NULL || win->xwin == None)
  303. return;
  304. h = win->h + win->bar.h;
  305. e = &win->env;
  306. if (win->pm == None)
  307. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  308. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  309. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  310. }
  311. void win_draw_bar(win_t *win) {
  312. int len, olen, x, y, w, tw;
  313. char rest[3];
  314. const char *dots = "...";
  315. win_env_t *e;
  316. if (win == NULL || win->xwin == None || win->pm == None)
  317. return;
  318. e = &win->env;
  319. y = win->h + font.ascent + V_TEXT_PAD;
  320. w = win->w;
  321. XSetForeground(e->dpy, gc, win->bar.bgcol);
  322. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  323. XSetForeground(e->dpy, gc, win->bar.fgcol);
  324. XSetBackground(e->dpy, gc, win->bar.bgcol);
  325. if (win->bar.r != NULL) {
  326. len = strlen(win->bar.r);
  327. if (len > 0) {
  328. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  329. return;
  330. x = win->w - tw + H_TEXT_PAD;
  331. w -= tw;
  332. if (font.set)
  333. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  334. else
  335. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  336. }
  337. }
  338. if (win->bar.l != NULL) {
  339. olen = len = strlen(win->bar.l);
  340. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  341. len--;
  342. if (len > 0) {
  343. if (len != olen) {
  344. w = strlen(dots);
  345. if (len <= w)
  346. return;
  347. memcpy(rest, win->bar.l + len - w, w);
  348. memcpy(win->bar.l + len - w, dots, w);
  349. }
  350. x = H_TEXT_PAD;
  351. if (font.set)
  352. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  353. else
  354. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  355. if (len != olen)
  356. memcpy(win->bar.l + len - w, rest, w);
  357. }
  358. }
  359. }
  360. void win_draw(win_t *win) {
  361. if (win == NULL || win->xwin == None || win->pm == None)
  362. return;
  363. if (win->bar.h > 0)
  364. win_draw_bar(win);
  365. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  366. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  367. }
  368. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  369. bool fill, int lw, unsigned long col)
  370. {
  371. XGCValues gcval;
  372. if (win == NULL || pm == None)
  373. return;
  374. gcval.line_width = lw;
  375. gcval.foreground = col;
  376. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  377. if (fill)
  378. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  379. else
  380. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  381. }
  382. int win_textwidth(const char *text, unsigned int len, bool with_padding) {
  383. XRectangle r;
  384. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  385. if (font.set) {
  386. XmbTextExtents(font.set, text, len, NULL, &r);
  387. return r.width + padding;
  388. } else {
  389. return XTextWidth(font.xfont, text, len) + padding;
  390. }
  391. }
  392. void win_set_title(win_t *win, const char *title) {
  393. if (win == NULL || win->xwin == None)
  394. return;
  395. if (title == NULL)
  396. title = "sxiv";
  397. XStoreName(win->env.dpy, win->xwin, title);
  398. XSetIconName(win->env.dpy, win->xwin, title);
  399. XChangeProperty(win->env.dpy, win->xwin,
  400. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  401. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  402. PropModeReplace, (unsigned char *) title, strlen(title));
  403. XChangeProperty(win->env.dpy, win->xwin,
  404. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  405. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  406. PropModeReplace, (unsigned char *) title, strlen(title));
  407. }
  408. void win_set_bar_info(win_t *win, char *linfo, char *rinfo) {
  409. if (win != NULL) {
  410. win->bar.l = linfo;
  411. win->bar.r = rinfo;
  412. }
  413. }
  414. void win_set_cursor(win_t *win, cursor_t cursor) {
  415. if (win == NULL || win->xwin == None)
  416. return;
  417. switch (cursor) {
  418. case CURSOR_NONE:
  419. XDefineCursor(win->env.dpy, win->xwin, cnone);
  420. break;
  421. case CURSOR_HAND:
  422. XDefineCursor(win->env.dpy, win->xwin, chand);
  423. break;
  424. case CURSOR_WATCH:
  425. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  426. break;
  427. case CURSOR_ARROW:
  428. default:
  429. XDefineCursor(win->env.dpy, win->xwin, carrow);
  430. break;
  431. }
  432. XFlush(win->env.dpy);
  433. }