A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

window.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include "sxiv.h"
  19. #define _WINDOW_CONFIG
  20. #include "config.h"
  21. #include "icon/data.h"
  22. #include "utf8.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <locale.h>
  26. #include <X11/cursorfont.h>
  27. #include <X11/Xatom.h>
  28. #include <X11/Xresource.h>
  29. #define RES_CLASS "Sxiv"
  30. enum {
  31. H_TEXT_PAD = 5,
  32. V_TEXT_PAD = 1
  33. };
  34. static struct {
  35. int name;
  36. Cursor icon;
  37. } cursors[CURSOR_COUNT] = {
  38. { XC_left_ptr }, { XC_dotbox }, { XC_watch },
  39. { XC_sb_left_arrow }, { XC_sb_right_arrow }
  40. };
  41. static GC gc;
  42. static XftFont *font;
  43. static int fontheight;
  44. static double fontsize;
  45. static int barheight;
  46. Atom atoms[ATOM_COUNT];
  47. void win_init_font(const win_env_t *e, const char *fontstr)
  48. {
  49. if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
  50. error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
  51. fontheight = font->ascent + font->descent;
  52. FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
  53. barheight = fontheight + 2 * V_TEXT_PAD;
  54. }
  55. void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
  56. {
  57. if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
  58. DefaultColormap(e->dpy, e->scr), name, col))
  59. {
  60. error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
  61. }
  62. }
  63. const char* win_res(Display *dpy, const char *name, const char *def)
  64. {
  65. char *type;
  66. XrmValue ret;
  67. XrmDatabase db;
  68. char *res_man;
  69. XrmInitialize();
  70. if ((res_man = XResourceManagerString(dpy)) != NULL &&
  71. (db = XrmGetStringDatabase(res_man)) != NULL &&
  72. XrmGetResource(db, name, name, &type, &ret) && STREQ(type, "String"))
  73. {
  74. return ret.addr;
  75. } else {
  76. return def;
  77. }
  78. }
  79. #define INIT_ATOM_(atom) \
  80. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  81. void win_init(win_t *win)
  82. {
  83. win_env_t *e;
  84. const char *bg, *fg, *f;
  85. memset(win, 0, sizeof(win_t));
  86. e = &win->env;
  87. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  88. error(EXIT_FAILURE, 0, "Error opening X display");
  89. e->scr = DefaultScreen(e->dpy);
  90. e->scrw = DisplayWidth(e->dpy, e->scr);
  91. e->scrh = DisplayHeight(e->dpy, e->scr);
  92. e->vis = DefaultVisual(e->dpy, e->scr);
  93. e->cmap = DefaultColormap(e->dpy, e->scr);
  94. e->depth = DefaultDepth(e->dpy, e->scr);
  95. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  96. error(0, 0, "No locale support");
  97. f = win_res(e->dpy, RES_CLASS ".font", "monospace-8");
  98. win_init_font(e, f);
  99. bg = win_res(e->dpy, RES_CLASS ".background", "white");
  100. fg = win_res(e->dpy, RES_CLASS ".foreground", "black");
  101. win_alloc_color(e, bg, &win->bg);
  102. win_alloc_color(e, fg, &win->fg);
  103. win->bar.l.size = BAR_L_LEN;
  104. win->bar.r.size = BAR_R_LEN;
  105. /* 3 padding bytes needed by utf8_decode */
  106. win->bar.l.buf = emalloc(win->bar.l.size + 3);
  107. win->bar.l.buf[0] = '\0';
  108. win->bar.r.buf = emalloc(win->bar.r.size + 3);
  109. win->bar.r.buf[0] = '\0';
  110. win->bar.h = options->hide_bar ? 0 : barheight;
  111. INIT_ATOM_(WM_DELETE_WINDOW);
  112. INIT_ATOM_(_NET_WM_NAME);
  113. INIT_ATOM_(_NET_WM_ICON_NAME);
  114. INIT_ATOM_(_NET_WM_ICON);
  115. INIT_ATOM_(_NET_WM_STATE);
  116. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  117. }
  118. void win_open(win_t *win)
  119. {
  120. int c, i, j, n;
  121. long parent;
  122. win_env_t *e;
  123. XClassHint classhint;
  124. unsigned long *icon_data;
  125. XColor col;
  126. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  127. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  128. Pixmap none;
  129. int gmask;
  130. XSizeHints sizehints;
  131. e = &win->env;
  132. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  133. sizehints.flags = PWinGravity;
  134. sizehints.win_gravity = NorthWestGravity;
  135. /* determine window offsets, width & height */
  136. if (options->geometry == NULL)
  137. gmask = 0;
  138. else
  139. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  140. &win->w, &win->h);
  141. if ((gmask & WidthValue) != 0)
  142. sizehints.flags |= USSize;
  143. else
  144. win->w = WIN_WIDTH;
  145. if ((gmask & HeightValue) != 0)
  146. sizehints.flags |= USSize;
  147. else
  148. win->h = WIN_HEIGHT;
  149. if ((gmask & XValue) != 0) {
  150. if ((gmask & XNegative) != 0) {
  151. win->x += e->scrw - win->w;
  152. sizehints.win_gravity = NorthEastGravity;
  153. }
  154. sizehints.flags |= USPosition;
  155. } else {
  156. win->x = 0;
  157. }
  158. if ((gmask & YValue) != 0) {
  159. if ((gmask & YNegative) != 0) {
  160. win->y += e->scrh - win->h;
  161. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  162. ? SouthEastGravity : SouthWestGravity;
  163. }
  164. sizehints.flags |= USPosition;
  165. } else {
  166. win->y = 0;
  167. }
  168. win->xwin = XCreateWindow(e->dpy, parent,
  169. win->x, win->y, win->w, win->h, 0,
  170. e->depth, InputOutput, e->vis, 0, NULL);
  171. if (win->xwin == None)
  172. error(EXIT_FAILURE, 0, "Error creating X window");
  173. XSelectInput(e->dpy, win->xwin,
  174. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  175. PointerMotionMask | StructureNotifyMask);
  176. for (i = 0; i < ARRLEN(cursors); i++) {
  177. if (i != CURSOR_NONE)
  178. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  179. }
  180. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  181. &col, &col) == 0)
  182. {
  183. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  184. }
  185. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  186. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  187. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  188. n = icons[ARRLEN(icons)-1].size;
  189. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  190. for (i = 0; i < ARRLEN(icons); i++) {
  191. n = 0;
  192. icon_data[n++] = icons[i].size;
  193. icon_data[n++] = icons[i].size;
  194. for (j = 0; j < icons[i].cnt; j++) {
  195. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  196. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  197. }
  198. XChangeProperty(e->dpy, win->xwin,
  199. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  200. i == 0 ? PropModeReplace : PropModeAppend,
  201. (unsigned char *) icon_data, n);
  202. }
  203. free(icon_data);
  204. win_set_title(win, "sxiv");
  205. classhint.res_class = RES_CLASS;
  206. classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
  207. XSetClassHint(e->dpy, win->xwin, &classhint);
  208. XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  209. sizehints.width = win->w;
  210. sizehints.height = win->h;
  211. sizehints.x = win->x;
  212. sizehints.y = win->y;
  213. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  214. win->h -= win->bar.h;
  215. win->buf.w = e->scrw;
  216. win->buf.h = e->scrh;
  217. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  218. win->buf.w, win->buf.h, e->depth);
  219. XSetForeground(e->dpy, gc, win->bg.pixel);
  220. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  221. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  222. XMapWindow(e->dpy, win->xwin);
  223. XFlush(e->dpy);
  224. if (options->fullscreen)
  225. win_toggle_fullscreen(win);
  226. }
  227. CLEANUP void win_close(win_t *win)
  228. {
  229. int i;
  230. for (i = 0; i < ARRLEN(cursors); i++)
  231. XFreeCursor(win->env.dpy, cursors[i].icon);
  232. XFreeGC(win->env.dpy, gc);
  233. XDestroyWindow(win->env.dpy, win->xwin);
  234. XCloseDisplay(win->env.dpy);
  235. }
  236. bool win_configure(win_t *win, XConfigureEvent *c)
  237. {
  238. bool changed;
  239. changed = win->w != c->width || win->h + win->bar.h != c->height;
  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_toggle_fullscreen(win_t *win)
  248. {
  249. XEvent ev;
  250. XClientMessageEvent *cm;
  251. memset(&ev, 0, sizeof(ev));
  252. ev.type = ClientMessage;
  253. cm = &ev.xclient;
  254. cm->window = win->xwin;
  255. cm->message_type = atoms[ATOM__NET_WM_STATE];
  256. cm->format = 32;
  257. cm->data.l[0] = 2; // toggle
  258. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  259. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  260. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  261. }
  262. void win_toggle_bar(win_t *win)
  263. {
  264. if (win->bar.h != 0) {
  265. win->h += win->bar.h;
  266. win->bar.h = 0;
  267. } else {
  268. win->bar.h = barheight;
  269. win->h -= win->bar.h;
  270. }
  271. }
  272. void win_clear(win_t *win)
  273. {
  274. win_env_t *e;
  275. e = &win->env;
  276. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  277. XFreePixmap(e->dpy, win->buf.pm);
  278. win->buf.w = MAX(win->buf.w, win->w);
  279. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  280. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  281. win->buf.w, win->buf.h, e->depth);
  282. }
  283. XSetForeground(e->dpy, gc, win->bg.pixel);
  284. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  285. }
  286. #define TEXTWIDTH(win, text, len) \
  287. win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
  288. int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
  289. char *text, int len, int w)
  290. {
  291. int err, tw = 0;
  292. char *t, *next;
  293. uint32_t rune;
  294. XftFont *f;
  295. FcCharSet *fccharset;
  296. XGlyphInfo ext;
  297. for (t = text; t - text < len; t = next) {
  298. next = utf8_decode(t, &rune, &err);
  299. if (XftCharExists(win->env.dpy, font, rune)) {
  300. f = font;
  301. } else { /* fallback font */
  302. fccharset = FcCharSetCreate();
  303. FcCharSetAddChar(fccharset, rune);
  304. f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
  305. fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
  306. FC_SIZE, FcTypeDouble, fontsize, NULL);
  307. FcCharSetDestroy(fccharset);
  308. }
  309. XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
  310. tw += ext.xOff;
  311. if (tw <= w) {
  312. XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
  313. x += ext.xOff;
  314. }
  315. if (f != font)
  316. XftFontClose(win->env.dpy, f);
  317. }
  318. return tw;
  319. }
  320. void win_draw_bar(win_t *win)
  321. {
  322. int len, x, y, w, tw;
  323. win_env_t *e;
  324. win_bar_t *l, *r;
  325. XftDraw *d;
  326. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  327. return;
  328. e = &win->env;
  329. y = win->h + font->ascent + V_TEXT_PAD;
  330. w = win->w - 2*H_TEXT_PAD;
  331. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  332. DefaultColormap(e->dpy, e->scr));
  333. XSetForeground(e->dpy, gc, win->fg.pixel);
  334. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  335. XSetForeground(e->dpy, gc, win->bg.pixel);
  336. XSetBackground(e->dpy, gc, win->fg.pixel);
  337. if ((len = strlen(r->buf)) > 0) {
  338. if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
  339. return;
  340. x = win->w - tw - H_TEXT_PAD;
  341. w -= tw;
  342. win_draw_text(win, d, &win->bg, x, y, r->buf, len, tw);
  343. }
  344. if ((len = strlen(l->buf)) > 0) {
  345. x = H_TEXT_PAD;
  346. w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
  347. win_draw_text(win, d, &win->bg, x, y, l->buf, len, w);
  348. }
  349. XftDrawDestroy(d);
  350. }
  351. void win_draw(win_t *win)
  352. {
  353. if (win->bar.h > 0)
  354. win_draw_bar(win);
  355. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  356. XClearWindow(win->env.dpy, win->xwin);
  357. XFlush(win->env.dpy);
  358. }
  359. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  360. unsigned long col)
  361. {
  362. XGCValues gcval;
  363. gcval.line_width = lw;
  364. gcval.foreground = col;
  365. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  366. if (fill)
  367. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  368. else
  369. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  370. }
  371. void win_set_title(win_t *win, const char *title)
  372. {
  373. XStoreName(win->env.dpy, win->xwin, title);
  374. XSetIconName(win->env.dpy, win->xwin, title);
  375. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  376. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  377. PropModeReplace, (unsigned char *) title, strlen(title));
  378. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  379. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  380. PropModeReplace, (unsigned char *) title, strlen(title));
  381. }
  382. void win_set_cursor(win_t *win, cursor_t cursor)
  383. {
  384. if (cursor >= 0 && cursor < ARRLEN(cursors)) {
  385. XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
  386. XFlush(win->env.dpy);
  387. }
  388. }
  389. void win_cursor_pos(win_t *win, int *x, int *y)
  390. {
  391. int i;
  392. unsigned int ui;
  393. Window w;
  394. if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
  395. *x = *y = 0;
  396. }