A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

519 líneas
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. return true;
  250. }
  251. void win_toggle_fullscreen(win_t *win)
  252. {
  253. XEvent ev;
  254. XClientMessageEvent *cm;
  255. if (win == NULL || win->xwin == None)
  256. return;
  257. win->fullscreen = !win->fullscreen;
  258. memset(&ev, 0, sizeof(ev));
  259. ev.type = ClientMessage;
  260. cm = &ev.xclient;
  261. cm->window = win->xwin;
  262. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  263. cm->format = 32;
  264. cm->data.l[0] = win->fullscreen;
  265. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  266. cm->data.l[2] = cm->data.l[3] = 0;
  267. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  268. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  269. }
  270. void win_toggle_bar(win_t *win)
  271. {
  272. if (win == NULL || win->xwin == None)
  273. return;
  274. if (win->bar.h != 0) {
  275. win->h += win->bar.h;
  276. win->bar.h = 0;
  277. } else {
  278. win->bar.h = barheight;
  279. win->h -= win->bar.h;
  280. }
  281. }
  282. void win_clear(win_t *win)
  283. {
  284. int h;
  285. win_env_t *e;
  286. if (win == NULL || win->xwin == None)
  287. return;
  288. h = win->h + win->bar.h;
  289. e = &win->env;
  290. if (win->pm == None)
  291. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  292. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  293. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  294. }
  295. void win_draw_bar(win_t *win)
  296. {
  297. int len, olen, x, y, w, tw;
  298. char rest[3];
  299. const char *dots = "...";
  300. win_env_t *e;
  301. if (win == NULL || win->xwin == None || win->pm == None)
  302. return;
  303. e = &win->env;
  304. y = win->h + font.ascent + V_TEXT_PAD;
  305. w = win->w;
  306. XSetForeground(e->dpy, gc, win->bar.bgcol);
  307. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  308. XSetForeground(e->dpy, gc, win->bar.fgcol);
  309. XSetBackground(e->dpy, gc, win->bar.bgcol);
  310. if (win->bar.r != NULL) {
  311. len = strlen(win->bar.r);
  312. if (len > 0) {
  313. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  314. return;
  315. x = win->w - tw + H_TEXT_PAD;
  316. w -= tw;
  317. if (font.set)
  318. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  319. else
  320. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  321. }
  322. }
  323. if (win->bar.l != NULL) {
  324. olen = len = strlen(win->bar.l);
  325. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  326. len--;
  327. if (len > 0) {
  328. if (len != olen) {
  329. w = strlen(dots);
  330. if (len <= w)
  331. return;
  332. memcpy(rest, win->bar.l + len - w, w);
  333. memcpy(win->bar.l + len - w, dots, w);
  334. }
  335. x = H_TEXT_PAD;
  336. if (font.set)
  337. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  338. else
  339. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  340. if (len != olen)
  341. memcpy(win->bar.l + len - w, rest, w);
  342. }
  343. }
  344. }
  345. void win_draw(win_t *win)
  346. {
  347. if (win == NULL || win->xwin == None || win->pm == None)
  348. return;
  349. if (win->bar.h > 0)
  350. win_draw_bar(win);
  351. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  352. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  353. }
  354. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  355. bool fill, int lw, unsigned long col)
  356. {
  357. XGCValues gcval;
  358. if (win == NULL || pm == None)
  359. return;
  360. gcval.line_width = lw;
  361. gcval.foreground = col;
  362. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  363. if (fill)
  364. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  365. else
  366. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  367. }
  368. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  369. {
  370. XRectangle r;
  371. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  372. if (font.set) {
  373. XmbTextExtents(font.set, text, len, NULL, &r);
  374. return r.width + padding;
  375. } else {
  376. return XTextWidth(font.xfont, text, len) + padding;
  377. }
  378. }
  379. void win_set_title(win_t *win, const char *title)
  380. {
  381. if (win == NULL || win->xwin == None)
  382. return;
  383. if (title == NULL)
  384. title = "sxiv";
  385. XStoreName(win->env.dpy, win->xwin, title);
  386. XSetIconName(win->env.dpy, win->xwin, title);
  387. XChangeProperty(win->env.dpy, win->xwin,
  388. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  389. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  390. PropModeReplace, (unsigned char *) title, strlen(title));
  391. XChangeProperty(win->env.dpy, win->xwin,
  392. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  393. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  394. PropModeReplace, (unsigned char *) title, strlen(title));
  395. }
  396. void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
  397. {
  398. if (win != NULL) {
  399. win->bar.l = linfo;
  400. win->bar.r = rinfo;
  401. }
  402. }
  403. void win_set_cursor(win_t *win, cursor_t cursor)
  404. {
  405. if (win == NULL || win->xwin == None)
  406. return;
  407. switch (cursor) {
  408. case CURSOR_NONE:
  409. XDefineCursor(win->env.dpy, win->xwin, cnone);
  410. break;
  411. case CURSOR_HAND:
  412. XDefineCursor(win->env.dpy, win->xwin, chand);
  413. break;
  414. case CURSOR_WATCH:
  415. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  416. break;
  417. case CURSOR_ARROW:
  418. default:
  419. XDefineCursor(win->env.dpy, win->xwin, carrow);
  420. break;
  421. }
  422. XFlush(win->env.dpy);
  423. }