A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

14 роки тому
13 роки тому
14 роки тому
13 роки тому
13 роки тому
13 роки тому
14 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
13 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
13 роки тому
14 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
13 роки тому
13 роки тому
14 роки тому
13 роки тому
14 роки тому
13 роки тому
13 роки тому
14 роки тому
14 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
13 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
14 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* sxiv: window.c
  2. * Copyright (c) 2011 Bert Muennich <be.muennich at googlemail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _WINDOW_CONFIG
  20. #include <string.h>
  21. #include <locale.h>
  22. #include <X11/Xutil.h>
  23. #include <X11/cursorfont.h>
  24. #include "options.h"
  25. #include "util.h"
  26. #include "window.h"
  27. #include "config.h"
  28. enum {
  29. H_TEXT_PAD = 5,
  30. V_TEXT_PAD = 1
  31. };
  32. static Cursor carrow;
  33. static Cursor cnone;
  34. static Cursor chand;
  35. static Cursor cwatch;
  36. static GC gc;
  37. Atom wm_delete_win;
  38. struct {
  39. int ascent;
  40. int descent;
  41. XFontStruct *xfont;
  42. XFontSet set;
  43. } font;
  44. void win_init_font(Display *dpy, const char *fontstr) {
  45. int n;
  46. char *def, **missing;
  47. font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  48. if (missing) {
  49. while (n--)
  50. warn("missing fontset: %s", missing[n]);
  51. XFreeStringList(missing);
  52. }
  53. if (font.set) {
  54. XFontStruct **xfonts;
  55. char **font_names;
  56. font.ascent = font.descent = 0;
  57. XExtentsOfFontSet(font.set);
  58. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  59. while (n--) {
  60. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  61. font.descent = MAX(font.descent,(*xfonts)->descent);
  62. xfonts++;
  63. }
  64. } else {
  65. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  66. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  67. {
  68. die("could not load font: %s", fontstr);
  69. }
  70. font.ascent = font.xfont->ascent;
  71. font.descent = font.xfont->descent;
  72. }
  73. }
  74. unsigned long win_alloc_color(win_t *win, const char *name) {
  75. XColor col;
  76. if (win == NULL)
  77. return 0UL;
  78. if (XAllocNamedColor(win->env.dpy,
  79. DefaultColormap(win->env.dpy, win->env.scr),
  80. name, &col, &col) == 0)
  81. {
  82. die("could not allocate color: %s", name);
  83. }
  84. return col.pixel;
  85. }
  86. void win_init(win_t *win) {
  87. win_env_t *e;
  88. if (win == NULL)
  89. return;
  90. e = &win->env;
  91. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  92. die("could not open display");
  93. e->scr = DefaultScreen(e->dpy);
  94. e->scrw = DisplayWidth(e->dpy, e->scr);
  95. e->scrh = DisplayHeight(e->dpy, e->scr);
  96. e->vis = DefaultVisual(e->dpy, e->scr);
  97. e->cmap = DefaultColormap(e->dpy, e->scr);
  98. e->depth = DefaultDepth(e->dpy, e->scr);
  99. win->black = BlackPixel(e->dpy, e->scr);
  100. win->white = WhitePixel(e->dpy, e->scr);
  101. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  102. win->selcol = win_alloc_color(win, SEL_COLOR);
  103. win->barbgcol = win_alloc_color(win, BAR_BG_COLOR);
  104. win->barfgcol = win_alloc_color(win, BAR_FG_COLOR);
  105. win->xwin = 0;
  106. win->pm = 0;
  107. win->fullscreen = false;
  108. win->lbar = NULL;
  109. win->rbar = NULL;
  110. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  111. warn("no locale support");
  112. win_init_font(e->dpy, BAR_FONT);
  113. }
  114. void win_set_sizehints(win_t *win) {
  115. XSizeHints sizehints;
  116. if (win == NULL || win->xwin == None)
  117. return;
  118. sizehints.flags = PMinSize | PMaxSize;
  119. sizehints.min_width = win->w;
  120. sizehints.max_width = win->w;
  121. sizehints.min_height = win->h + win->barh;
  122. sizehints.max_height = win->h + win->barh;
  123. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  124. }
  125. void win_open(win_t *win) {
  126. win_env_t *e;
  127. XClassHint classhint;
  128. XColor col;
  129. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  130. Pixmap none;
  131. int gmask;
  132. if (win == NULL)
  133. return;
  134. e = &win->env;
  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. win->w = WIN_WIDTH;
  143. if (win->w > e->scrw)
  144. win->w = e->scrw;
  145. if ((gmask & HeightValue) == 0)
  146. win->h = WIN_HEIGHT;
  147. if (win->h > e->scrh)
  148. win->h = e->scrh;
  149. if ((gmask & XValue) == 0)
  150. win->x = (e->scrw - win->w) / 2;
  151. else if ((gmask & XNegative) != 0)
  152. win->x += e->scrw - win->w;
  153. if ((gmask & YValue) == 0)
  154. win->y = (e->scrh - win->h) / 2;
  155. else if ((gmask & YNegative) != 0)
  156. win->y += e->scrh - win->h;
  157. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  158. win->x, win->y, win->w, win->h, 0,
  159. e->depth, InputOutput, e->vis, 0, None);
  160. if (win->xwin == None)
  161. die("could not create window");
  162. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  163. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  164. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  165. chand = XCreateFontCursor(e->dpy, XC_fleur);
  166. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  167. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  168. &col, &col) == 0)
  169. {
  170. die("could not allocate color: black");
  171. }
  172. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  173. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  174. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  175. win_set_title(win, "sxiv");
  176. classhint.res_name = "sxiv";
  177. classhint.res_class = "sxiv";
  178. XSetClassHint(e->dpy, win->xwin, &classhint);
  179. win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
  180. win->h -= win->barh;
  181. if (options->fixed_win)
  182. win_set_sizehints(win);
  183. XMapWindow(e->dpy, win->xwin);
  184. XFlush(e->dpy);
  185. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  186. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  187. if (options->fullscreen)
  188. win_toggle_fullscreen(win);
  189. }
  190. void win_close(win_t *win) {
  191. if (win == NULL || win->xwin == None)
  192. return;
  193. XFreeCursor(win->env.dpy, carrow);
  194. XFreeCursor(win->env.dpy, cnone);
  195. XFreeCursor(win->env.dpy, chand);
  196. XFreeCursor(win->env.dpy, cwatch);
  197. XFreeGC(win->env.dpy, gc);
  198. XDestroyWindow(win->env.dpy, win->xwin);
  199. XCloseDisplay(win->env.dpy);
  200. }
  201. bool win_configure(win_t *win, XConfigureEvent *c) {
  202. bool changed;
  203. if (win == NULL)
  204. return false;
  205. changed = win->w != c->width || win->h + win->barh != c->height;
  206. win->x = c->x;
  207. win->y = c->y;
  208. win->w = c->width;
  209. win->h = c->height - win->barh;
  210. win->bw = c->border_width;
  211. return changed;
  212. }
  213. bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  214. if (win == NULL || win->xwin == None)
  215. return false;
  216. x = MAX(0, x);
  217. y = MAX(0, y);
  218. w = MIN(w, win->env.scrw - 2 * win->bw);
  219. h = MIN(h, win->env.scrh - 2 * win->bw);
  220. if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h)
  221. return false;
  222. win->x = x;
  223. win->y = y;
  224. win->w = w;
  225. win->h = h - win->barh;
  226. if (options->fixed_win)
  227. win_set_sizehints(win);
  228. XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
  229. return true;
  230. }
  231. void win_toggle_fullscreen(win_t *win) {
  232. XEvent ev;
  233. XClientMessageEvent *cm;
  234. if (win == NULL || win->xwin == None)
  235. return;
  236. win->fullscreen = !win->fullscreen;
  237. memset(&ev, 0, sizeof(ev));
  238. ev.type = ClientMessage;
  239. cm = &ev.xclient;
  240. cm->window = win->xwin;
  241. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  242. cm->format = 32;
  243. cm->data.l[0] = win->fullscreen;
  244. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  245. cm->data.l[2] = cm->data.l[3] = 0;
  246. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  247. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  248. }
  249. void win_clear(win_t *win) {
  250. win_env_t *e;
  251. if (win == NULL || win->xwin == None)
  252. return;
  253. e = &win->env;
  254. if (win->pm != None)
  255. XFreePixmap(e->dpy, win->pm);
  256. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  257. XSetForeground(e->dpy, gc, win->fullscreen ? win->black : win->bgcol);
  258. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  259. }
  260. int win_textwidth(const char *text, unsigned int len) {
  261. XRectangle r;
  262. if (font.set) {
  263. XmbTextExtents(font.set, text, len, NULL, &r);
  264. return r.width;
  265. } else {
  266. return XTextWidth(font.xfont, text, len);
  267. }
  268. }
  269. void win_draw_bar(win_t *win) {
  270. win_env_t *e;
  271. int len, x, y, w, tw = 0;
  272. const char *rt;
  273. if (win == NULL || win->xwin == None)
  274. return;
  275. e = &win->env;
  276. x = H_TEXT_PAD;
  277. y = win->h + font.ascent + V_TEXT_PAD;
  278. w = win->w - 2 * H_TEXT_PAD;
  279. XSetForeground(e->dpy, gc, win->barbgcol);
  280. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh);
  281. XSetForeground(e->dpy, gc, win->barfgcol);
  282. XSetBackground(e->dpy, gc, win->barbgcol);
  283. if (win->lbar != NULL) {
  284. len = strlen(win->lbar);
  285. while (len > 0 && (tw = win_textwidth(win->lbar, len)) > w)
  286. len--;
  287. w -= tw + 2 * H_TEXT_PAD;
  288. if (font.set)
  289. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len);
  290. else
  291. XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len);
  292. }
  293. if (win->rbar != NULL) {
  294. len = strlen(win->rbar);
  295. rt = win->rbar;
  296. while (len > 0 && (tw = win_textwidth(rt, len)) > w) {
  297. rt = strstr(rt, " ");
  298. if (rt != NULL) {
  299. rt += 2;
  300. len = strlen(rt);
  301. } else {
  302. len = 0;
  303. }
  304. }
  305. if (len > 0) {
  306. x = win->w - tw - H_TEXT_PAD;
  307. if (font.set)
  308. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len);
  309. else
  310. XDrawString(e->dpy, win->pm, gc, x, y, rt, len);
  311. }
  312. }
  313. }
  314. void win_draw(win_t *win) {
  315. if (win == NULL || win->xwin == None)
  316. return;
  317. win_draw_bar(win);
  318. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  319. XClearWindow(win->env.dpy, win->xwin);
  320. }
  321. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  322. bool fill, int lw, unsigned long col) {
  323. XGCValues gcval;
  324. if (win == NULL || pm == None)
  325. return;
  326. gcval.line_width = lw;
  327. gcval.foreground = col;
  328. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  329. if (fill)
  330. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  331. else
  332. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  333. }
  334. void win_set_title(win_t *win, const char *title) {
  335. if (win == NULL || win->xwin == None)
  336. return;
  337. if (title == NULL)
  338. title = "sxiv";
  339. XStoreName(win->env.dpy, win->xwin, title);
  340. XSetIconName(win->env.dpy, win->xwin, title);
  341. XChangeProperty(win->env.dpy, win->xwin,
  342. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  343. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  344. PropModeReplace, (unsigned char *) title, strlen(title));
  345. XChangeProperty(win->env.dpy, win->xwin,
  346. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  347. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  348. PropModeReplace, (unsigned char *) title, strlen(title));
  349. }
  350. void win_set_bar_info(win_t *win, const char *li, const char *ri) {
  351. if (win != NULL) {
  352. win->lbar = li;
  353. win->rbar = ri;
  354. }
  355. }
  356. void win_set_cursor(win_t *win, cursor_t cursor) {
  357. if (win == NULL || win->xwin == None)
  358. return;
  359. switch (cursor) {
  360. case CURSOR_NONE:
  361. XDefineCursor(win->env.dpy, win->xwin, cnone);
  362. break;
  363. case CURSOR_HAND:
  364. XDefineCursor(win->env.dpy, win->xwin, chand);
  365. break;
  366. case CURSOR_WATCH:
  367. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  368. break;
  369. case CURSOR_ARROW:
  370. default:
  371. XDefineCursor(win->env.dpy, win->xwin, carrow);
  372. break;
  373. }
  374. XFlush(win->env.dpy);
  375. }