A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

480 строки
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 <stdlib.h>
  19. #include <string.h>
  20. #include <locale.h>
  21. #include <X11/cursorfont.h>
  22. #include <X11/Xatom.h>
  23. #include "options.h"
  24. #include "util.h"
  25. #include "window.h"
  26. #include "icon/data.h"
  27. #define _WINDOW_CONFIG
  28. #include "config.h"
  29. enum {
  30. H_TEXT_PAD = 5,
  31. V_TEXT_PAD = 1
  32. };
  33. static Cursor carrow;
  34. static Cursor cnone;
  35. static Cursor chand;
  36. static Cursor cwatch;
  37. static GC gc;
  38. static XftFont *font;
  39. static int fontheight;
  40. static int barheight;
  41. Atom atoms[ATOM_COUNT];
  42. static Bool fs_support;
  43. static Bool fs_warned;
  44. void win_init_font(const win_env_t *e, const char *fontstr)
  45. {
  46. if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
  47. error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
  48. fontheight = font->ascent + font->descent;
  49. barheight = fontheight + 2 * V_TEXT_PAD;
  50. }
  51. void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
  52. {
  53. if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
  54. DefaultColormap(e->dpy, e->scr), name, col))
  55. {
  56. error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
  57. }
  58. }
  59. void win_check_wm_support(Display *dpy, Window root)
  60. {
  61. int format;
  62. long offset = 0, length = 16;
  63. Atom *data, type;
  64. unsigned long i, nitems, bytes_left;
  65. Bool found = False;
  66. while (!found && length > 0) {
  67. if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
  68. offset, length, False, XA_ATOM, &type, &format,
  69. &nitems, &bytes_left, (unsigned char**) &data))
  70. {
  71. break;
  72. }
  73. if (type == XA_ATOM && format == 32) {
  74. for (i = 0; i < nitems; i++) {
  75. if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
  76. found = True;
  77. fs_support = True;
  78. break;
  79. }
  80. }
  81. }
  82. XFree(data);
  83. offset += nitems;
  84. length = MIN(length, bytes_left / 4);
  85. }
  86. }
  87. #define INIT_ATOM_(atom) \
  88. atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
  89. void win_init(win_t *win)
  90. {
  91. win_env_t *e;
  92. memset(win, 0, sizeof(win_t));
  93. e = &win->env;
  94. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  95. error(EXIT_FAILURE, 0, "Error opening X display");
  96. e->scr = DefaultScreen(e->dpy);
  97. e->scrw = DisplayWidth(e->dpy, e->scr);
  98. e->scrh = DisplayHeight(e->dpy, e->scr);
  99. e->vis = DefaultVisual(e->dpy, e->scr);
  100. e->cmap = DefaultColormap(e->dpy, e->scr);
  101. e->depth = DefaultDepth(e->dpy, e->scr);
  102. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  103. error(0, 0, "No locale support");
  104. win_init_font(e, BAR_FONT);
  105. win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
  106. win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
  107. win_alloc_color(e, SEL_COLOR, &win->selcol);
  108. win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
  109. win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
  110. win->bar.l.size = BAR_L_LEN;
  111. win->bar.r.size = BAR_R_LEN;
  112. win->bar.l.buf = emalloc(win->bar.l.size);
  113. win->bar.r.buf = emalloc(win->bar.r.size);
  114. win->bar.h = options->hide_bar ? 0 : barheight;
  115. INIT_ATOM_(WM_DELETE_WINDOW);
  116. INIT_ATOM_(_NET_WM_NAME);
  117. INIT_ATOM_(_NET_WM_ICON_NAME);
  118. INIT_ATOM_(_NET_WM_ICON);
  119. INIT_ATOM_(_NET_WM_STATE);
  120. INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
  121. INIT_ATOM_(_NET_SUPPORTED);
  122. win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
  123. }
  124. void win_open(win_t *win)
  125. {
  126. int c, i, j, n;
  127. win_env_t *e;
  128. XClassHint classhint;
  129. unsigned long *icon_data;
  130. XColor col;
  131. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  132. Pixmap none;
  133. int gmask;
  134. XSizeHints sizehints;
  135. Bool fullscreen = options->fullscreen && fs_support;
  136. e = &win->env;
  137. sizehints.flags = PWinGravity;
  138. sizehints.win_gravity = NorthWestGravity;
  139. /* determine window offsets, width & height */
  140. if (options->geometry == NULL)
  141. gmask = 0;
  142. else
  143. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  144. &win->w, &win->h);
  145. if ((gmask & WidthValue) != 0)
  146. sizehints.flags |= USSize;
  147. else
  148. win->w = WIN_WIDTH;
  149. if ((gmask & HeightValue) != 0)
  150. sizehints.flags |= USSize;
  151. else
  152. win->h = WIN_HEIGHT;
  153. if ((gmask & XValue) != 0) {
  154. if ((gmask & XNegative) != 0) {
  155. win->x += e->scrw - win->w;
  156. sizehints.win_gravity = NorthEastGravity;
  157. }
  158. sizehints.flags |= USPosition;
  159. } else {
  160. win->x = 0;
  161. }
  162. if ((gmask & YValue) != 0) {
  163. if ((gmask & YNegative) != 0) {
  164. win->y += e->scrh - win->h;
  165. sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
  166. ? SouthEastGravity : SouthWestGravity;
  167. }
  168. sizehints.flags |= USPosition;
  169. } else {
  170. win->y = 0;
  171. }
  172. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  173. win->x, win->y, win->w, win->h, 0,
  174. e->depth, InputOutput, e->vis, 0, NULL);
  175. if (win->xwin == None)
  176. error(EXIT_FAILURE, 0, "Error creating X window");
  177. XSelectInput(e->dpy, win->xwin,
  178. ButtonReleaseMask | ButtonPressMask | KeyPressMask |
  179. PointerMotionMask | StructureNotifyMask);
  180. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  181. chand = XCreateFontCursor(e->dpy, XC_fleur);
  182. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  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 = "Sxiv";
  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, fullscreen ? win->fscol.pixel : win->bgcol.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 (fullscreen)
  228. win_toggle_fullscreen(win);
  229. }
  230. CLEANUP void win_close(win_t *win)
  231. {
  232. XFreeCursor(win->env.dpy, carrow);
  233. XFreeCursor(win->env.dpy, cnone);
  234. XFreeCursor(win->env.dpy, chand);
  235. XFreeCursor(win->env.dpy, cwatch);
  236. XFreeGC(win->env.dpy, gc);
  237. XDestroyWindow(win->env.dpy, win->xwin);
  238. XCloseDisplay(win->env.dpy);
  239. }
  240. bool win_configure(win_t *win, XConfigureEvent *c)
  241. {
  242. bool changed;
  243. changed = win->w != c->width || win->h + win->bar.h != c->height;
  244. win->x = c->x;
  245. win->y = c->y;
  246. win->w = c->width;
  247. win->h = c->height - win->bar.h;
  248. win->bw = c->border_width;
  249. return changed;
  250. }
  251. void win_toggle_fullscreen(win_t *win)
  252. {
  253. XEvent ev;
  254. XClientMessageEvent *cm;
  255. if (!fs_support) {
  256. if (!fs_warned) {
  257. error(0, 0, "No fullscreen support");
  258. fs_warned = True;
  259. }
  260. return;
  261. }
  262. win->fullscreen = !win->fullscreen;
  263. memset(&ev, 0, sizeof(ev));
  264. ev.type = ClientMessage;
  265. cm = &ev.xclient;
  266. cm->window = win->xwin;
  267. cm->message_type = atoms[ATOM__NET_WM_STATE];
  268. cm->format = 32;
  269. cm->data.l[0] = win->fullscreen;
  270. cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
  271. cm->data.l[2] = cm->data.l[3] = 0;
  272. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  273. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  274. }
  275. void win_toggle_bar(win_t *win)
  276. {
  277. if (win->bar.h != 0) {
  278. win->h += win->bar.h;
  279. win->bar.h = 0;
  280. } else {
  281. win->bar.h = barheight;
  282. win->h -= win->bar.h;
  283. }
  284. }
  285. void win_clear(win_t *win)
  286. {
  287. win_env_t *e;
  288. e = &win->env;
  289. if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
  290. XFreePixmap(e->dpy, win->buf.pm);
  291. win->buf.w = MAX(win->buf.w, win->w);
  292. win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
  293. win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
  294. win->buf.w, win->buf.h, e->depth);
  295. }
  296. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
  297. XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
  298. }
  299. void win_draw_bar(win_t *win)
  300. {
  301. int len, olen, x, y, w, tw;
  302. char rest[3];
  303. const char *dots = "...";
  304. win_env_t *e;
  305. win_bar_t *l, *r;
  306. XftDraw *d;
  307. if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
  308. return;
  309. e = &win->env;
  310. y = win->h + font->ascent + V_TEXT_PAD;
  311. w = win->w;
  312. d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
  313. DefaultColormap(e->dpy, e->scr));
  314. XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
  315. XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
  316. XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
  317. XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
  318. if ((len = strlen(r->buf)) > 0) {
  319. if ((tw = win_textwidth(e, r->buf, len, true)) > w)
  320. return;
  321. x = win->w - tw + H_TEXT_PAD;
  322. w -= tw;
  323. XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)r->buf, len);
  324. }
  325. if ((len = strlen(l->buf)) > 0) {
  326. olen = len;
  327. while (len > 0 && (tw = win_textwidth(e, l->buf, len, true)) > w)
  328. len--;
  329. if (len > 0) {
  330. if (len != olen) {
  331. w = strlen(dots);
  332. if (len <= w)
  333. return;
  334. memcpy(rest, l->buf + len - w, w);
  335. memcpy(l->buf + len - w, dots, w);
  336. }
  337. x = H_TEXT_PAD;
  338. XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)l->buf, len);
  339. if (len != olen)
  340. memcpy(l->buf + len - w, rest, w);
  341. }
  342. }
  343. XftDrawDestroy(d);
  344. }
  345. void win_draw(win_t *win)
  346. {
  347. if (win->bar.h > 0)
  348. win_draw_bar(win);
  349. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
  350. XClearWindow(win->env.dpy, win->xwin);
  351. XFlush(win->env.dpy);
  352. }
  353. void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
  354. unsigned long col)
  355. {
  356. XGCValues gcval;
  357. gcval.line_width = lw;
  358. gcval.foreground = col;
  359. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  360. if (fill)
  361. XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  362. else
  363. XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
  364. }
  365. int win_textwidth(const win_env_t *e, const char *text, unsigned int len, bool with_padding)
  366. {
  367. XGlyphInfo ext;
  368. XftTextExtentsUtf8(e->dpy, font, (XftChar8*)text, len, &ext);
  369. return ext.xOff + (with_padding ? 2 * H_TEXT_PAD : 0);
  370. }
  371. void win_set_title(win_t *win, const char *title)
  372. {
  373. if (title == NULL)
  374. title = "sxiv";
  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. switch (cursor) {
  387. case CURSOR_NONE:
  388. XDefineCursor(win->env.dpy, win->xwin, cnone);
  389. break;
  390. case CURSOR_HAND:
  391. XDefineCursor(win->env.dpy, win->xwin, chand);
  392. break;
  393. case CURSOR_WATCH:
  394. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  395. break;
  396. case CURSOR_ARROW:
  397. default:
  398. XDefineCursor(win->env.dpy, win->xwin, carrow);
  399. break;
  400. }
  401. XFlush(win->env.dpy);
  402. }