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.
 
 
 
 
 
 

479 lines
13 KiB

  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", "#110e0d");
  100. fg = win_res(e->dpy, RES_CLASS ".foreground", "#252221");
  101. win_alloc_color(e, bg, &win->bg);
  102. win_alloc_color(e, fg, &win->fg);
  103. // Kanon trying to get bar colors back
  104. win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
  105. win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
  106. win->bar.l.size = BAR_L_LEN;
  107. win->bar.r.size = BAR_R_LEN;
  108. /* 3 padding bytes needed by utf8_decode */
  109. win->bar.l.buf = emalloc(win->bar.l.size + 3);
  110. win->bar.l.buf[0] = '\0';
  111. win->bar.r.buf = emalloc(win->bar.r.size + 3);
  112. win->bar.r.buf[0] = '\0';
  113. win->bar.h = options->hide_bar ? 0 : barheight;
  114. INIT_ATOM_(WM_DELETE_WINDOW);
  115. INIT_ATOM_(_NET_WM_NAME);
  116. INIT_ATOM_(_NET_WM_ICON_NAME);
  117. INIT_ATOM_(_NET_WM_ICON);
  118. INIT_ATOM_(_NET_WM_STATE);
  119. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  120. }
  121. void win_open(win_t *win)
  122. {
  123. int c, i, j, n;
  124. long parent;
  125. win_env_t *e;
  126. XClassHint classhint;
  127. unsigned long *icon_data;
  128. XColor col;
  129. Cursor *cnone = &cursors[CURSOR_NONE].icon;
  130. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  131. Pixmap none;
  132. int gmask;
  133. XSizeHints sizehints;
  134. e = &win->env;
  135. parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
  136. sizehints.flags = PWinGravity;
  137. sizehints.win_gravity = NorthWestGravity;
  138. /* determine window offsets, width & height */
  139. if (options->geometry == NULL)
  140. gmask = 0;
  141. else
  142. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  143. &win->w, &win->h);
  144. if ((gmask & WidthValue) != 0)
  145. sizehints.flags |= USSize;
  146. else
  147. win->w = WIN_WIDTH;
  148. if ((gmask & HeightValue) != 0)
  149. sizehints.flags |= USSize;
  150. else
  151. win->h = WIN_HEIGHT;
  152. if ((gmask & XValue) != 0) {
  153. if ((gmask & XNegative) != 0) {
  154. win->x += e->scrw - win->w;
  155. sizehints.win_gravity = NorthEastGravity;
  156. }
  157. sizehints.flags |= USPosition;
  158. } else {
  159. win->x = 0;
  160. }
  161. if ((gmask & YValue) != 0) {
  162. if ((gmask & YNegative) != 0) {
  163. win->y += e->scrh - win->h;
  164. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  165. ? SouthEastGravity : SouthWestGravity;
  166. }
  167. sizehints.flags |= USPosition;
  168. } else {
  169. win->y = 0;
  170. }
  171. win->xwin = XCreateWindow(e->dpy, parent,
  172. win->x, win->y, win->w, win->h, 0,
  173. e->depth, InputOutput, e->vis, 0, NULL);
  174. if (win->xwin == None)
  175. error(EXIT_FAILURE, 0, "Error creating X window");
  176. XSelectInput(e->dpy, win->xwin,
  177. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  178. PointerMotionMask | StructureNotifyMask);
  179. for (i = 0; i < ARRLEN(cursors); i++) {
  180. if (i != CURSOR_NONE)
  181. cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
  182. }
  183. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  184. &col, &col) == 0)
  185. {
  186. error(EXIT_FAILURE, 0, "Error allocating color 'black'");
  187. }
  188. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  189. *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  190. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  191. n = icons[ARRLEN(icons)-1].size;
  192. icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
  193. for (i = 0; i < ARRLEN(icons); i++) {
  194. n = 0;
  195. icon_data[n++] = icons[i].size;
  196. icon_data[n++] = icons[i].size;
  197. for (j = 0; j < icons[i].cnt; j++) {
  198. for (c = icons[i].data[j] >> 4; c >= 0; c--)
  199. icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
  200. }
  201. XChangeProperty(e->dpy, win->xwin,
  202. atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
  203. i == 0 ? PropModeReplace : PropModeAppend,
  204. (unsigned char *) icon_data, n);
  205. }
  206. free(icon_data);
  207. win_set_title(win, "sxiv");
  208. classhint.res_class = RES_CLASS;
  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, &atoms[ATOM_WM_DELETE_WINDOW], 1);
  212. sizehints.width = win->w;
  213. sizehints.height = win->h;
  214. sizehints.x = win->x;
  215. sizehints.y = win->y;
  216. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  217. win->h -= win->bar.h;
  218. win->buf.w = e->scrw;
  219. win->buf.h = e->scrh;
  220. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  221. win->buf.w, win->buf.h, e->depth);
  222. XSetForeground(e->dpy, gc, win->bg.pixel);
  223. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  224. XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
  225. XMapWindow(e->dpy, win->xwin);
  226. XFlush(e->dpy);
  227. if (options->fullscreen)
  228. win_toggle_fullscreen(win);
  229. }
  230. CLEANUP void win_close(win_t *win)
  231. {
  232. int i;
  233. for (i = 0; i < ARRLEN(cursors); i++)
  234. XFreeCursor(win->env.dpy, cursors[i].icon);
  235. XFreeGC(win->env.dpy, gc);
  236. XDestroyWindow(win->env.dpy, win->xwin);
  237. XCloseDisplay(win->env.dpy);
  238. }
  239. bool win_configure(win_t *win, XConfigureEvent *c)
  240. {
  241. bool changed;
  242. changed = win->w != c->width || win->h + win->bar.h != c->height;
  243. win->x = c->x;
  244. win->y = c->y;
  245. win->w = c->width;
  246. win->h = c->height - win->bar.h;
  247. win->bw = c->border_width;
  248. return changed;
  249. }
  250. void win_toggle_fullscreen(win_t *win)
  251. {
  252. XEvent ev;
  253. XClientMessageEvent *cm;
  254. memset(&ev, 0, sizeof(ev));
  255. ev.type = ClientMessage;
  256. cm = &ev.xclient;
  257. cm->window = win->xwin;
  258. cm->message_type = atoms[ATOM__NET_WM_STATE];
  259. cm->format = 32;
  260. cm->data.l[0] = 2; // toggle
  261. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  262. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  263. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  264. }
  265. void win_toggle_bar(win_t *win)
  266. {
  267. if (win->bar.h != 0) {
  268. win->h += win->bar.h;
  269. win->bar.h = 0;
  270. } else {
  271. win->bar.h = barheight;
  272. win->h -= win->bar.h;
  273. }
  274. }
  275. void win_clear(win_t *win)
  276. {
  277. win_env_t *e;
  278. e = &win->env;
  279. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  280. XFreePixmap(e->dpy, win->buf.pm);
  281. win->buf.w = MAX(win->buf.w, win->w);
  282. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  283. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  284. win->buf.w, win->buf.h, e->depth);
  285. }
  286. XSetForeground(e->dpy, gc, win->bg.pixel);
  287. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  288. }
  289. #define TEXTWIDTH(win, text, len) \
  290. win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
  291. int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, int x, int y,
  292. char *text, int len, int w)
  293. {
  294. int err, tw = 0;
  295. char *t, *next;
  296. uint32_t rune;
  297. XftFont *f;
  298. FcCharSet *fccharset;
  299. XGlyphInfo ext;
  300. for (t = text; t - text < len; t = next) {
  301. next = utf8_decode(t, &rune, &err);
  302. if (XftCharExists(win->env.dpy, font, rune)) {
  303. f = font;
  304. } else { /* fallback font */
  305. fccharset = FcCharSetCreate();
  306. FcCharSetAddChar(fccharset, rune);
  307. f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
  308. fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
  309. FC_SIZE, FcTypeDouble, fontsize, NULL);
  310. FcCharSetDestroy(fccharset);
  311. }
  312. XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
  313. tw += ext.xOff;
  314. if (tw <= w) {
  315. XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
  316. x += ext.xOff;
  317. }
  318. if (f != font)
  319. XftFontClose(win->env.dpy, f);
  320. }
  321. return tw;
  322. }
  323. void win_draw_bar(win_t *win)
  324. {
  325. int len, x, y, w, tw;
  326. win_env_t *e;
  327. win_bar_t *l, *r;
  328. XftDraw *d;
  329. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  330. return;
  331. e = &win->env;
  332. y = win->h + font->ascent + V_TEXT_PAD;
  333. w = win->w - 2*H_TEXT_PAD;
  334. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  335. DefaultColormap(e->dpy, e->scr));
  336. XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
  337. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  338. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  339. XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
  340. if ((len = strlen(r->buf)) > 0) {
  341. if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
  342. return;
  343. x = win->w - tw - H_TEXT_PAD;
  344. w -= tw;
  345. win_draw_text(win, d, &win->bar.fgcol, x, y, r->buf, len, tw);
  346. }
  347. if ((len = strlen(l->buf)) > 0) {
  348. x = H_TEXT_PAD;
  349. w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
  350. win_draw_text(win, d, &win->bar.fgcol, x, y, l->buf, len, w);
  351. }
  352. XftDrawDestroy(d);
  353. }
  354. void win_draw(win_t *win)
  355. {
  356. if (win->bar.h > 0)
  357. win_draw_bar(win);
  358. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  359. XClearWindow(win->env.dpy, win->xwin);
  360. XFlush(win->env.dpy);
  361. }
  362. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  363. unsigned long col)
  364. {
  365. XGCValues gcval;
  366. gcval.line_width = lw;
  367. gcval.foreground = col;
  368. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  369. if (fill)
  370. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  371. else
  372. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  373. }
  374. void win_set_title(win_t *win, const char *title)
  375. {
  376. XStoreName(win->env.dpy, win->xwin, title);
  377. XSetIconName(win->env.dpy, win->xwin, title);
  378. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
  379. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  380. PropModeReplace, (unsigned char *) title, strlen(title));
  381. XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
  382. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  383. PropModeReplace, (unsigned char *) title, strlen(title));
  384. }
  385. void win_set_cursor(win_t *win, cursor_t cursor)
  386. {
  387. if (cursor >= 0 && cursor < ARRLEN(cursors)) {
  388. XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
  389. XFlush(win->env.dpy);
  390. }
  391. }
  392. void win_cursor_pos(win_t *win, int *x, int *y)
  393. {
  394. int i;
  395. unsigned int ui;
  396. Window w;
  397. if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
  398. *x = *y = 0;
  399. }