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.

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