A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

498 řádky
12 KiB

  1. /* sxiv: window.c
  2. * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  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. struct {
  39. int ascent;
  40. int descent;
  41. XFontStruct *xfont;
  42. XFontSet set;
  43. } font;
  44. void win_init_font(Display *dpy, const char *fontstr) {
  45. int n;
  46. char *def, **missing;
  47. font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  48. if (missing)
  49. XFreeStringList(missing);
  50. if (font.set) {
  51. XFontStruct **xfonts;
  52. char **font_names;
  53. font.ascent = font.descent = 0;
  54. XExtentsOfFontSet(font.set);
  55. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  56. while (n--) {
  57. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  58. font.descent = MAX(font.descent,(*xfonts)->descent);
  59. xfonts++;
  60. }
  61. } else {
  62. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  63. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  64. {
  65. die("could not load font: %s", fontstr);
  66. }
  67. font.ascent = font.xfont->ascent;
  68. font.descent = font.xfont->descent;
  69. }
  70. }
  71. unsigned long win_alloc_color(win_t *win, const char *name) {
  72. XColor col;
  73. if (win == NULL)
  74. return 0UL;
  75. if (XAllocNamedColor(win->env.dpy,
  76. DefaultColormap(win->env.dpy, win->env.scr),
  77. name, &col, &col) == 0)
  78. {
  79. die("could not allocate color: %s", name);
  80. }
  81. return col.pixel;
  82. }
  83. void win_init(win_t *win) {
  84. win_env_t *e;
  85. if (win == NULL)
  86. return;
  87. e = &win->env;
  88. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  89. die("could not open display");
  90. e->scr = DefaultScreen(e->dpy);
  91. e->scrw = DisplayWidth(e->dpy, e->scr);
  92. e->scrh = DisplayHeight(e->dpy, e->scr);
  93. e->vis = DefaultVisual(e->dpy, e->scr);
  94. e->cmap = DefaultColormap(e->dpy, e->scr);
  95. e->depth = DefaultDepth(e->dpy, e->scr);
  96. win->white = WhitePixel(e->dpy, e->scr);
  97. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  98. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  99. win->selcol = win_alloc_color(win, SEL_COLOR);
  100. win->barbgcol = win_alloc_color(win, BAR_BG_COLOR);
  101. win->barfgcol = win_alloc_color(win, BAR_FG_COLOR);
  102. win->xwin = 0;
  103. win->pm = 0;
  104. win->fullscreen = false;
  105. win->barh = 0;
  106. win->lbar = NULL;
  107. win->rbar = NULL;
  108. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  109. warn("no locale support");
  110. win_init_font(e->dpy, BAR_FONT);
  111. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  112. }
  113. void win_set_sizehints(win_t *win) {
  114. XSizeHints sizehints;
  115. if (win == NULL || win->xwin == None)
  116. return;
  117. sizehints.flags = PMinSize | PMaxSize;
  118. sizehints.min_width = win->w;
  119. sizehints.max_width = win->w;
  120. sizehints.min_height = win->h + win->barh;
  121. sizehints.max_height = win->h + win->barh;
  122. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  123. }
  124. void win_open(win_t *win) {
  125. win_env_t *e;
  126. XClassHint classhint;
  127. XColor col;
  128. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  129. Pixmap none;
  130. int gmask;
  131. if (win == NULL)
  132. return;
  133. e = &win->env;
  134. /* determine window offsets, width & height */
  135. if (options->geometry == NULL)
  136. gmask = 0;
  137. else
  138. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  139. &win->w, &win->h);
  140. if ((gmask & WidthValue) == 0)
  141. win->w = WIN_WIDTH;
  142. if (win->w > e->scrw)
  143. win->w = e->scrw;
  144. if ((gmask & HeightValue) == 0)
  145. win->h = WIN_HEIGHT;
  146. if (win->h > e->scrh)
  147. win->h = e->scrh;
  148. if ((gmask & XValue) == 0)
  149. win->x = (e->scrw - win->w) / 2;
  150. else if ((gmask & XNegative) != 0)
  151. win->x += e->scrw - win->w;
  152. if ((gmask & YValue) == 0)
  153. win->y = (e->scrh - win->h) / 2;
  154. else if ((gmask & YNegative) != 0)
  155. win->y += e->scrh - win->h;
  156. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  157. win->x, win->y, win->w, win->h, 0,
  158. e->depth, InputOutput, e->vis, 0, None);
  159. if (win->xwin == None)
  160. die("could not create window");
  161. XSelectInput(e->dpy, win->xwin,
  162. ExposureMask | ButtonReleaseMask | ButtonPressMask |
  163. KeyPressMask | PointerMotionMask | StructureNotifyMask);
  164. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  165. chand = XCreateFontCursor(e->dpy, XC_fleur);
  166. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  167. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  168. &col, &col) == 0)
  169. {
  170. die("could not allocate color: black");
  171. }
  172. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  173. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  174. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  175. win_set_title(win, "sxiv");
  176. classhint.res_class = "Sxiv";
  177. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  178. XSetClassHint(e->dpy, win->xwin, &classhint);
  179. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  180. if (!options->hide_bar) {
  181. win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
  182. win->h -= win->barh;
  183. }
  184. if (options->fixed_win)
  185. win_set_sizehints(win);
  186. XMapWindow(e->dpy, win->xwin);
  187. XFlush(e->dpy);
  188. if (options->fullscreen)
  189. win_toggle_fullscreen(win);
  190. }
  191. void win_close(win_t *win) {
  192. if (win == NULL || win->xwin == None)
  193. return;
  194. XFreeCursor(win->env.dpy, carrow);
  195. XFreeCursor(win->env.dpy, cnone);
  196. XFreeCursor(win->env.dpy, chand);
  197. XFreeCursor(win->env.dpy, cwatch);
  198. XFreeGC(win->env.dpy, gc);
  199. XDestroyWindow(win->env.dpy, win->xwin);
  200. XCloseDisplay(win->env.dpy);
  201. }
  202. bool win_configure(win_t *win, XConfigureEvent *c) {
  203. bool changed;
  204. if (win == NULL || c == NULL)
  205. return false;
  206. if ((changed = win->w != c->width || win->h + win->barh != c->height)) {
  207. if (win->pm != None) {
  208. XFreePixmap(win->env.dpy, win->pm);
  209. win->pm = None;
  210. }
  211. }
  212. win->x = c->x;
  213. win->y = c->y;
  214. win->w = c->width;
  215. win->h = c->height - win->barh;
  216. win->bw = c->border_width;
  217. return changed;
  218. }
  219. void win_expose(win_t *win, XExposeEvent *e) {
  220. if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
  221. return;
  222. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  223. e->x, e->y, e->width, e->height, e->x, e->y);
  224. }
  225. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  226. if (win == NULL || win->xwin == None)
  227. return false;
  228. x = MAX(0, x);
  229. y = MAX(0, y);
  230. w = MIN(w, win->env.scrw - 2 * win->bw);
  231. h = MIN(h, win->env.scrh - 2 * win->bw);
  232. if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h)
  233. return false;
  234. win->x = x;
  235. win->y = y;
  236. win->w = w;
  237. win->h = h - win->barh;
  238. if (options->fixed_win)
  239. win_set_sizehints(win);
  240. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  241. return true;
  242. }
  243. void win_toggle_fullscreen(win_t *win) {
  244. XEvent ev;
  245. XClientMessageEvent *cm;
  246. if (win == NULL || win->xwin == None)
  247. return;
  248. win->fullscreen = !win->fullscreen;
  249. memset(&ev, 0, sizeof(ev));
  250. ev.type = ClientMessage;
  251. cm = &ev.xclient;
  252. cm->window = win->xwin;
  253. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  254. cm->format = 32;
  255. cm->data.l[0] = win->fullscreen;
  256. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  257. cm->data.l[2] = cm->data.l[3] = 0;
  258. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  259. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  260. }
  261. void win_toggle_bar(win_t *win) {
  262. if (win == NULL || win->xwin == None)
  263. return;
  264. if (win->barh != 0) {
  265. win->h += win->barh;
  266. win->barh = 0;
  267. } else {
  268. win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
  269. win->h -= win->barh;
  270. }
  271. }
  272. void win_clear(win_t *win) {
  273. int h;
  274. win_env_t *e;
  275. if (win == NULL || win->xwin == None)
  276. return;
  277. h = win->h + win->barh;
  278. e = &win->env;
  279. if (win->pm == None)
  280. win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
  281. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  282. XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
  283. }
  284. void win_draw_bar(win_t *win) {
  285. win_env_t *e;
  286. int len, x, y, w, tw = 0, seplen;
  287. const char *rt;
  288. if (win == NULL || win->xwin == None || win->pm == None)
  289. return;
  290. e = &win->env;
  291. x = H_TEXT_PAD;
  292. y = win->h + font.ascent + V_TEXT_PAD;
  293. w = win->w - 2 * H_TEXT_PAD;
  294. XSetForeground(e->dpy, gc, win->barbgcol);
  295. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh);
  296. XSetForeground(e->dpy, gc, win->barfgcol);
  297. XSetBackground(e->dpy, gc, win->barbgcol);
  298. if (win->lbar != NULL) {
  299. len = strlen(win->lbar);
  300. while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
  301. len--;
  302. w -= tw + 2 * H_TEXT_PAD;
  303. if (font.set)
  304. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len);
  305. else
  306. XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len);
  307. }
  308. if (win->rbar != NULL) {
  309. len = strlen(win->rbar);
  310. seplen = strlen(BAR_SEPARATOR);
  311. rt = win->rbar;
  312. while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
  313. rt = strstr(rt, BAR_SEPARATOR);
  314. if (rt != NULL) {
  315. rt += seplen;
  316. len = strlen(rt);
  317. } else {
  318. len = 0;
  319. }
  320. }
  321. if (len > 0) {
  322. x = win->w - tw - H_TEXT_PAD;
  323. if (font.set)
  324. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len);
  325. else
  326. XDrawString(e->dpy, win->pm, gc, x, y, rt, len);
  327. }
  328. }
  329. }
  330. void win_draw(win_t *win) {
  331. if (win == NULL || win->xwin == None || win->pm == None)
  332. return;
  333. if (win->barh > 0)
  334. win_draw_bar(win);
  335. XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
  336. 0, 0, win->w, win->h + win->barh, 0, 0);
  337. }
  338. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  339. bool fill, int lw, unsigned long col)
  340. {
  341. XGCValues gcval;
  342. if (win == NULL || pm == None)
  343. return;
  344. gcval.line_width = lw;
  345. gcval.foreground = col;
  346. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  347. if (fill)
  348. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  349. else
  350. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  351. }
  352. int win_textwidth(const char *text, unsigned int len, bool with_padding) {
  353. XRectangle r;
  354. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  355. if (font.set) {
  356. XmbTextExtents(font.set, text, len, NULL, &r);
  357. return r.width + padding;
  358. } else {
  359. return XTextWidth(font.xfont, text, len) + padding;
  360. }
  361. }
  362. void win_set_title(win_t *win, const char *title) {
  363. if (win == NULL || win->xwin == None)
  364. return;
  365. if (title == NULL)
  366. title = "sxiv";
  367. XStoreName(win->env.dpy, win->xwin, title);
  368. XSetIconName(win->env.dpy, win->xwin, title);
  369. XChangeProperty(win->env.dpy, win->xwin,
  370. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  371. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  372. PropModeReplace, (unsigned char *) title, strlen(title));
  373. XChangeProperty(win->env.dpy, win->xwin,
  374. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  375. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  376. PropModeReplace, (unsigned char *) title, strlen(title));
  377. }
  378. void win_set_bar_info(win_t *win, const char *li, const char *ri) {
  379. if (win != NULL) {
  380. win->lbar = li;
  381. win->rbar = ri;
  382. }
  383. }
  384. void win_set_cursor(win_t *win, cursor_t cursor) {
  385. if (win == NULL || win->xwin == None)
  386. return;
  387. switch (cursor) {
  388. case CURSOR_NONE:
  389. XDefineCursor(win->env.dpy, win->xwin, cnone);
  390. break;
  391. case CURSOR_HAND:
  392. XDefineCursor(win->env.dpy, win->xwin, chand);
  393. break;
  394. case CURSOR_WATCH:
  395. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  396. break;
  397. case CURSOR_ARROW:
  398. default:
  399. XDefineCursor(win->env.dpy, win->xwin, carrow);
  400. break;
  401. }
  402. XFlush(win->env.dpy);
  403. }