A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

550 wiersze
13 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/cursorfont.h>
  23. #include "options.h"
  24. #include "util.h"
  25. #include "window.h"
  26. #include "config.h"
  27. enum {
  28. H_TEXT_PAD = 5,
  29. V_TEXT_PAD = 1
  30. };
  31. static Cursor carrow;
  32. static Cursor cnone;
  33. static Cursor chand;
  34. static Cursor cwatch;
  35. static GC gc;
  36. Atom wm_delete_win;
  37. static struct {
  38. int ascent;
  39. int descent;
  40. XFontStruct *xfont;
  41. XFontSet set;
  42. } font;
  43. static int fontheight;
  44. static int barheight;
  45. void win_init_font(Display *dpy, const char *fontstr)
  46. {
  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. {
  77. XColor col;
  78. if (win == NULL)
  79. return 0UL;
  80. if (XAllocNamedColor(win->env.dpy,
  81. DefaultColormap(win->env.dpy, win->env.scr),
  82. name, &col, &col) == 0)
  83. {
  84. die("could not allocate color: %s", name);
  85. }
  86. return col.pixel;
  87. }
  88. void win_init(win_t *win)
  89. {
  90. win_env_t *e;
  91. if (win == NULL)
  92. return;
  93. memset(win, 0, sizeof(win_t));
  94. e = &win->env;
  95. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  96. die("could not open display");
  97. e->scr = DefaultScreen(e->dpy);
  98. e->scrw = DisplayWidth(e->dpy, e->scr);
  99. e->scrh = DisplayHeight(e->dpy, e->scr);
  100. e->vis = DefaultVisual(e->dpy, e->scr);
  101. e->cmap = DefaultColormap(e->dpy, e->scr);
  102. e->depth = DefaultDepth(e->dpy, e->scr);
  103. win_init_font(e->dpy, BAR_FONT);
  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. win->bar.h = options->hide_bar ? 0 : barheight;
  111. win->sizehints.flags = PWinGravity;
  112. win->sizehints.win_gravity = NorthWestGravity;
  113. if (options->fixed_win)
  114. /* actual min/max values set in win_update_sizehints() */
  115. win->sizehints.flags |= PMinSize | PMaxSize;
  116. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  117. warn("no locale support");
  118. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  119. }
  120. void win_update_sizehints(win_t *win)
  121. {
  122. if (win == NULL || win->xwin == None)
  123. return;
  124. if ((win->sizehints.flags & USSize) != 0) {
  125. win->sizehints.width = win->w;
  126. win->sizehints.height = win->h;
  127. }
  128. if ((win->sizehints.flags & USPosition) != 0) {
  129. win->sizehints.x = win->x;
  130. win->sizehints.y = win->y;
  131. }
  132. if (options->fixed_win) {
  133. win->sizehints.min_width = win->sizehints.max_width = win->w;
  134. win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
  135. }
  136. XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
  137. }
  138. void win_open(win_t *win)
  139. {
  140. win_env_t *e;
  141. XClassHint classhint;
  142. XColor col;
  143. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  144. Pixmap none;
  145. int gmask;
  146. if (win == NULL)
  147. return;
  148. e = &win->env;
  149. /* determine window offsets, width & height */
  150. if (options->geometry == NULL)
  151. gmask = 0;
  152. else
  153. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  154. &win->w, &win->h);
  155. if ((gmask & WidthValue) != 0)
  156. win->sizehints.flags |= USSize;
  157. else
  158. win->w = WIN_WIDTH;
  159. if ((gmask & HeightValue) != 0)
  160. win->sizehints.flags |= USSize;
  161. else
  162. win->h = WIN_HEIGHT;
  163. if ((gmask & XValue) != 0) {
  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. } else {
  170. win->x = (e->scrw - win->w) / 2;
  171. }
  172. if ((gmask & YValue) != 0) {
  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. win->sizehints.flags |= USPosition;
  181. } else {
  182. win->y = (e->scrh - win->h) / 2;
  183. }
  184. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  185. win->x, win->y, win->w, win->h, 0,
  186. e->depth, InputOutput, e->vis, 0, None);
  187. if (win->xwin == None)
  188. die("could not create window");
  189. XSelectInput(e->dpy, win->xwin,
  190. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  191. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  192. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  193. chand = XCreateFontCursor(e->dpy, XC_fleur);
  194. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  195. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  196. &col, &col) == 0)
  197. {
  198. die("could not allocate color: black");
  199. }
  200. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  201. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  202. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  203. win_set_title(win, "sxiv");
  204. classhint.res_class = "Sxiv";
  205. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  206. XSetClassHint(e->dpy, win->xwin, &classhint);
  207. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  208. if (!options->hide_bar) {
  209. win->bar.h = barheight;
  210. win->h -= win->bar.h;
  211. }
  212. win_update_sizehints(win);
  213. XMapWindow(e->dpy, win->xwin);
  214. XFlush(e->dpy);
  215. if (options->fullscreen)
  216. win_toggle_fullscreen(win);
  217. }
  218. void win_close(win_t *win)
  219. {
  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. {
  232. bool changed;
  233. if (win == NULL || c == NULL)
  234. return false;
  235. if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
  236. if (win->pm != None) {
  237. XFreePixmap(win->env.dpy, win->pm);
  238. win->pm = None;
  239. }
  240. }
  241. win->x = c->x;
  242. win->y = c->y;
  243. win->w = c->width;
  244. win->h = c->height - win->bar.h;
  245. win->bw = c->border_width;
  246. return changed;
  247. }
  248. void win_expose(win_t *win, XExposeEvent *e)
  249. {
  250. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  251. return;
  252. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  253. e->x, e->y, e->width, e->height, e->x, e->y);
  254. }
  255. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
  256. {
  257. if (win == NULL || win->xwin == None)
  258. return false;
  259. /* caller knows nothing about the bar */
  260. h += win->bar.h;
  261. x = MAX(0, x);
  262. y = MAX(0, y);
  263. w = MIN(w, win->env.scrw - 2 * win->bw);
  264. h = MIN(h, win->env.scrh - 2 * win->bw);
  265. if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
  266. return false;
  267. win->x = x;
  268. win->y = y;
  269. win->w = w;
  270. win->h = h - win->bar.h;
  271. win_update_sizehints(win);
  272. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  273. if (win->pm != None) {
  274. XFreePixmap(win->env.dpy, win->pm);
  275. win->pm = None;
  276. }
  277. return true;
  278. }
  279. void win_toggle_fullscreen(win_t *win)
  280. {
  281. XEvent ev;
  282. XClientMessageEvent *cm;
  283. if (win == NULL || win->xwin == None)
  284. return;
  285. win->fullscreen = !win->fullscreen;
  286. memset(&ev, 0, sizeof(ev));
  287. ev.type = ClientMessage;
  288. cm = &ev.xclient;
  289. cm->window = win->xwin;
  290. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  291. cm->format = 32;
  292. cm->data.l[0] = win->fullscreen;
  293. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  294. cm->data.l[2] = cm->data.l[3] = 0;
  295. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  296. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  297. }
  298. void win_toggle_bar(win_t *win)
  299. {
  300. if (win == NULL || win->xwin == None)
  301. return;
  302. if (win->bar.h != 0) {
  303. win->h += win->bar.h;
  304. win->bar.h = 0;
  305. } else {
  306. win->bar.h = barheight;
  307. win->h -= win->bar.h;
  308. }
  309. }
  310. void win_clear(win_t *win)
  311. {
  312. int h;
  313. win_env_t *e;
  314. if (win == NULL || win->xwin == None)
  315. return;
  316. h = win->h + win->bar.h;
  317. e = &win->env;
  318. if (win->pm == None)
  319. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  320. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  321. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  322. }
  323. void win_draw_bar(win_t *win)
  324. {
  325. int len, olen, x, y, w, tw;
  326. char rest[3];
  327. const char *dots = "...";
  328. win_env_t *e;
  329. if (win == NULL || win->xwin == None || win->pm == None)
  330. return;
  331. e = &win->env;
  332. y = win->h + font.ascent + V_TEXT_PAD;
  333. w = win->w;
  334. XSetForeground(e->dpy, gc, win->bar.bgcol);
  335. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  336. XSetForeground(e->dpy, gc, win->bar.fgcol);
  337. XSetBackground(e->dpy, gc, win->bar.bgcol);
  338. if ((len = strlen(win->bar.r)) > 0) {
  339. if ((tw = win_textwidth(win->bar.r, len, true)) > w)
  340. return;
  341. x = win->w - tw + H_TEXT_PAD;
  342. w -= tw;
  343. if (font.set)
  344. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
  345. else
  346. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
  347. }
  348. if ((len = strlen(win->bar.l)) > 0) {
  349. olen = len;
  350. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  351. len--;
  352. if (len > 0) {
  353. if (len != olen) {
  354. w = strlen(dots);
  355. if (len <= w)
  356. return;
  357. memcpy(rest, win->bar.l + len - w, w);
  358. memcpy(win->bar.l + len - w, dots, w);
  359. }
  360. x = H_TEXT_PAD;
  361. if (font.set)
  362. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  363. else
  364. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  365. if (len != olen)
  366. memcpy(win->bar.l + len - w, rest, w);
  367. }
  368. }
  369. }
  370. void win_draw(win_t *win)
  371. {
  372. if (win == NULL || win->xwin == None || win->pm == None)
  373. return;
  374. if (win->bar.h > 0)
  375. win_draw_bar(win);
  376. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  377. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  378. }
  379. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  380. bool fill, int lw, unsigned long col)
  381. {
  382. XGCValues gcval;
  383. if (win == NULL || pm == None)
  384. return;
  385. gcval.line_width = lw;
  386. gcval.foreground = col;
  387. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  388. if (fill)
  389. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  390. else
  391. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  392. }
  393. void win_update_bar(win_t *win)
  394. {
  395. if (win == NULL || win->xwin == None || win->pm == None)
  396. return;
  397. if (win->bar.h > 0) {
  398. win_draw_bar(win);
  399. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  400. 0, win->h, win->w, win->bar.h, 0, win->h);
  401. }
  402. }
  403. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  404. {
  405. XRectangle r;
  406. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  407. if (font.set) {
  408. XmbTextExtents(font.set, text, len, NULL, &r);
  409. return r.width + padding;
  410. } else {
  411. return XTextWidth(font.xfont, text, len) + padding;
  412. }
  413. }
  414. void win_set_title(win_t *win, const char *title)
  415. {
  416. if (win == NULL || win->xwin == None)
  417. return;
  418. if (title == NULL)
  419. title = "sxiv";
  420. XStoreName(win->env.dpy, win->xwin, title);
  421. XSetIconName(win->env.dpy, win->xwin, title);
  422. XChangeProperty(win->env.dpy, win->xwin,
  423. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  424. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  425. PropModeReplace, (unsigned char *) title, strlen(title));
  426. XChangeProperty(win->env.dpy, win->xwin,
  427. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  428. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  429. PropModeReplace, (unsigned char *) title, strlen(title));
  430. }
  431. void win_set_cursor(win_t *win, cursor_t cursor)
  432. {
  433. if (win == NULL || win->xwin == None)
  434. return;
  435. switch (cursor) {
  436. case CURSOR_NONE:
  437. XDefineCursor(win->env.dpy, win->xwin, cnone);
  438. break;
  439. case CURSOR_HAND:
  440. XDefineCursor(win->env.dpy, win->xwin, chand);
  441. break;
  442. case CURSOR_WATCH:
  443. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  444. break;
  445. case CURSOR_ARROW:
  446. default:
  447. XDefineCursor(win->env.dpy, win->xwin, carrow);
  448. break;
  449. }
  450. XFlush(win->env.dpy);
  451. }