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.

window.c 14 KiB

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