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

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