A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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