A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

14 anos atrás
14 anos atrás
13 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
14 anos atrás
13 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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->white = WhitePixel(e->dpy, e->scr);
  104. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  105. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  106. win->selcol = win_alloc_color(win, SEL_COLOR);
  107. win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
  108. win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
  109. win->sizehints.flags = PWinGravity;
  110. win->sizehints.win_gravity = NorthWestGravity;
  111. if (options->fixed_win)
  112. /* actual min/max values set in win_update_sizehints() */
  113. win->sizehints.flags |= PMinSize | PMaxSize;
  114. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  115. warn("no locale support");
  116. win_init_font(e->dpy, BAR_FONT);
  117. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  118. }
  119. void win_update_sizehints(win_t *win)
  120. {
  121. if (win == NULL || win->xwin == None)
  122. return;
  123. if ((win->sizehints.flags & USSize) != 0) {
  124. win->sizehints.width = win->w;
  125. win->sizehints.height = win->h;
  126. }
  127. if ((win->sizehints.flags & USPosition) != 0) {
  128. win->sizehints.x = win->x;
  129. win->sizehints.y = win->y;
  130. }
  131. if (options->fixed_win) {
  132. win->sizehints.min_width = win->sizehints.max_width = win->w;
  133. win->sizehints.min_height = win->sizehints.max_height = win->h + win->bar.h;
  134. }
  135. XSetWMNormalHints(win->env.dpy, win->xwin, &win->sizehints);
  136. }
  137. void win_open(win_t *win)
  138. {
  139. win_env_t *e;
  140. XClassHint classhint;
  141. XColor col;
  142. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  143. Pixmap none;
  144. int gmask;
  145. if (win == NULL)
  146. return;
  147. e = &win->env;
  148. /* determine window offsets, width & height */
  149. if (options->geometry == NULL)
  150. gmask = 0;
  151. else
  152. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  153. &win->w, &win->h);
  154. if ((gmask & WidthValue) != 0)
  155. win->sizehints.flags |= USSize;
  156. else
  157. win->w = WIN_WIDTH;
  158. if ((gmask & HeightValue) != 0)
  159. win->sizehints.flags |= USSize;
  160. else
  161. win->h = WIN_HEIGHT;
  162. if ((gmask & XValue) != 0) {
  163. if ((gmask & XNegative) != 0) {
  164. win->x += e->scrw - win->w;
  165. win->sizehints.win_gravity = NorthEastGravity;
  166. }
  167. win->sizehints.flags |= USPosition;
  168. } else {
  169. win->x = (e->scrw - win->w) / 2;
  170. }
  171. if ((gmask & YValue) != 0) {
  172. if ((gmask & YNegative) != 0) {
  173. win->y += e->scrh - win->h;
  174. if (win->sizehints.win_gravity == NorthEastGravity)
  175. win->sizehints.win_gravity = SouthEastGravity;
  176. else
  177. win->sizehints.win_gravity = SouthWestGravity;
  178. }
  179. win->sizehints.flags |= USPosition;
  180. } else {
  181. win->y = (e->scrh - win->h) / 2;
  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. win_update_sizehints(win);
  212. XMapWindow(e->dpy, win->xwin);
  213. XFlush(e->dpy);
  214. if (options->fullscreen)
  215. win_toggle_fullscreen(win);
  216. }
  217. void win_close(win_t *win)
  218. {
  219. if (win == NULL || win->xwin == None)
  220. return;
  221. XFreeCursor(win->env.dpy, carrow);
  222. XFreeCursor(win->env.dpy, cnone);
  223. XFreeCursor(win->env.dpy, chand);
  224. XFreeCursor(win->env.dpy, cwatch);
  225. XFreeGC(win->env.dpy, gc);
  226. XDestroyWindow(win->env.dpy, win->xwin);
  227. XCloseDisplay(win->env.dpy);
  228. }
  229. bool win_configure(win_t *win, XConfigureEvent *c)
  230. {
  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. {
  249. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  250. return;
  251. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  252. e->x, e->y, e->width, e->height, e->x, e->y);
  253. }
  254. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
  255. {
  256. if (win == NULL || win->xwin == None)
  257. return false;
  258. /* caller knows nothing about the bar */
  259. h += win->bar.h;
  260. x = MAX(0, x);
  261. y = MAX(0, y);
  262. w = MIN(w, win->env.scrw - 2 * win->bw);
  263. h = MIN(h, win->env.scrh - 2 * win->bw);
  264. if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
  265. return false;
  266. win->x = x;
  267. win->y = y;
  268. win->w = w;
  269. win->h = h - win->bar.h;
  270. win_update_sizehints(win);
  271. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  272. if (win->pm != None) {
  273. XFreePixmap(win->env.dpy, win->pm);
  274. win->pm = None;
  275. }
  276. return true;
  277. }
  278. void win_toggle_fullscreen(win_t *win)
  279. {
  280. XEvent ev;
  281. XClientMessageEvent *cm;
  282. if (win == NULL || win->xwin == None)
  283. return;
  284. win->fullscreen = !win->fullscreen;
  285. memset(&ev, 0, sizeof(ev));
  286. ev.type = ClientMessage;
  287. cm = &ev.xclient;
  288. cm->window = win->xwin;
  289. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  290. cm->format = 32;
  291. cm->data.l[0] = win->fullscreen;
  292. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  293. cm->data.l[2] = cm->data.l[3] = 0;
  294. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  295. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  296. }
  297. void win_toggle_bar(win_t *win)
  298. {
  299. if (win == NULL || win->xwin == None)
  300. return;
  301. if (win->bar.h != 0) {
  302. win->h += win->bar.h;
  303. win->bar.h = 0;
  304. } else {
  305. win->bar.h = barheight;
  306. win->h -= win->bar.h;
  307. }
  308. }
  309. void win_clear(win_t *win)
  310. {
  311. int h;
  312. win_env_t *e;
  313. if (win == NULL || win->xwin == None)
  314. return;
  315. h = win->h + win->bar.h;
  316. e = &win->env;
  317. if (win->pm == None)
  318. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  319. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  320. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  321. }
  322. void win_draw_bar(win_t *win)
  323. {
  324. int len, olen, x, y, w, tw;
  325. char rest[3];
  326. const char *dots = "...";
  327. win_env_t *e;
  328. if (win == NULL || win->xwin == None || win->pm == None)
  329. return;
  330. e = &win->env;
  331. y = win->h + font.ascent + V_TEXT_PAD;
  332. w = win->w;
  333. XSetForeground(e->dpy, gc, win->bar.bgcol);
  334. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
  335. XSetForeground(e->dpy, gc, win->bar.fgcol);
  336. XSetBackground(e->dpy, gc, win->bar.bgcol);
  337. if (win->bar.r != NULL) {
  338. len = strlen(win->bar.r);
  339. if (len > 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. }
  350. if (win->bar.l != NULL) {
  351. olen = len = strlen(win->bar.l);
  352. while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
  353. len--;
  354. if (len > 0) {
  355. if (len != olen) {
  356. w = strlen(dots);
  357. if (len <= w)
  358. return;
  359. memcpy(rest, win->bar.l + len - w, w);
  360. memcpy(win->bar.l + len - w, dots, w);
  361. }
  362. x = H_TEXT_PAD;
  363. if (font.set)
  364. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
  365. else
  366. XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
  367. if (len != olen)
  368. memcpy(win->bar.l + len - w, rest, w);
  369. }
  370. }
  371. }
  372. void win_draw(win_t *win)
  373. {
  374. if (win == NULL || win->xwin == None || win->pm == None)
  375. return;
  376. if (win->bar.h > 0)
  377. win_draw_bar(win);
  378. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  379. 0, 0, win->w, win->h + win->bar.h, 0, 0);
  380. }
  381. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  382. bool fill, int lw, unsigned long col)
  383. {
  384. XGCValues gcval;
  385. if (win == NULL || pm == None)
  386. return;
  387. gcval.line_width = lw;
  388. gcval.foreground = col;
  389. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  390. if (fill)
  391. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  392. else
  393. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  394. }
  395. int win_textwidth(const char *text, unsigned int len, bool with_padding)
  396. {
  397. XRectangle r;
  398. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  399. if (font.set) {
  400. XmbTextExtents(font.set, text, len, NULL, &r);
  401. return r.width + padding;
  402. } else {
  403. return XTextWidth(font.xfont, text, len) + padding;
  404. }
  405. }
  406. void win_set_title(win_t *win, const char *title)
  407. {
  408. if (win == NULL || win->xwin == None)
  409. return;
  410. if (title == NULL)
  411. title = "sxiv";
  412. XStoreName(win->env.dpy, win->xwin, title);
  413. XSetIconName(win->env.dpy, win->xwin, title);
  414. XChangeProperty(win->env.dpy, win->xwin,
  415. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  416. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  417. PropModeReplace, (unsigned char *) title, strlen(title));
  418. XChangeProperty(win->env.dpy, win->xwin,
  419. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  420. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  421. PropModeReplace, (unsigned char *) title, strlen(title));
  422. }
  423. void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
  424. {
  425. if (win != NULL) {
  426. win->bar.l = linfo;
  427. win->bar.r = rinfo;
  428. }
  429. }
  430. void win_set_cursor(win_t *win, cursor_t cursor)
  431. {
  432. if (win == NULL || win->xwin == None)
  433. return;
  434. switch (cursor) {
  435. case CURSOR_NONE:
  436. XDefineCursor(win->env.dpy, win->xwin, cnone);
  437. break;
  438. case CURSOR_HAND:
  439. XDefineCursor(win->env.dpy, win->xwin, chand);
  440. break;
  441. case CURSOR_WATCH:
  442. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  443. break;
  444. case CURSOR_ARROW:
  445. default:
  446. XDefineCursor(win->env.dpy, win->xwin, carrow);
  447. break;
  448. }
  449. XFlush(win->env.dpy);
  450. }