A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

14 anos atrás
14 anos atrás
14 anos atrás
13 anos atrás
14 anos atrás
14 anos atrás
14 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
13 anos atrás
14 anos atrás
13 anos atrás
13 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* sxiv: window.c
  2. * Copyright (c) 2012 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. XFreeStringList(missing);
  50. if (font.set) {
  51. XFontStruct **xfonts;
  52. char **font_names;
  53. font.ascent = font.descent = 0;
  54. XExtentsOfFontSet(font.set);
  55. n = XFontsOfFontSet(font.set, &xfonts, &font_names);
  56. while (n--) {
  57. font.ascent = MAX(font.ascent, (*xfonts)->ascent);
  58. font.descent = MAX(font.descent,(*xfonts)->descent);
  59. xfonts++;
  60. }
  61. } else {
  62. if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
  63. (font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
  64. {
  65. die("could not load font: %s", fontstr);
  66. }
  67. font.ascent = font.xfont->ascent;
  68. font.descent = font.xfont->descent;
  69. }
  70. }
  71. unsigned long win_alloc_color(win_t *win, const char *name) {
  72. XColor col;
  73. if (win == NULL)
  74. return 0UL;
  75. if (XAllocNamedColor(win->env.dpy,
  76. DefaultColormap(win->env.dpy, win->env.scr),
  77. name, &col, &col) == 0)
  78. {
  79. die("could not allocate color: %s", name);
  80. }
  81. return col.pixel;
  82. }
  83. void win_init(win_t *win) {
  84. win_env_t *e;
  85. if (win == NULL)
  86. return;
  87. e = &win->env;
  88. if ((e->dpy = XOpenDisplay(NULL)) == NULL)
  89. die("could not open display");
  90. e->scr = DefaultScreen(e->dpy);
  91. e->scrw = DisplayWidth(e->dpy, e->scr);
  92. e->scrh = DisplayHeight(e->dpy, e->scr);
  93. e->vis = DefaultVisual(e->dpy, e->scr);
  94. e->cmap = DefaultColormap(e->dpy, e->scr);
  95. e->depth = DefaultDepth(e->dpy, e->scr);
  96. win->white = WhitePixel(e->dpy, e->scr);
  97. win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
  98. win->fscol = win_alloc_color(win, WIN_FS_COLOR);
  99. win->selcol = win_alloc_color(win, SEL_COLOR);
  100. win->barbgcol = win_alloc_color(win, BAR_BG_COLOR);
  101. win->barfgcol = win_alloc_color(win, BAR_FG_COLOR);
  102. win->xwin = 0;
  103. win->pm = 0;
  104. win->fullscreen = false;
  105. win->barh = 0;
  106. win->lbar = NULL;
  107. win->rbar = NULL;
  108. if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
  109. warn("no locale support");
  110. win_init_font(e->dpy, BAR_FONT);
  111. }
  112. void win_set_sizehints(win_t *win) {
  113. XSizeHints sizehints;
  114. if (win == NULL || win->xwin == None)
  115. return;
  116. sizehints.flags = PMinSize | PMaxSize;
  117. sizehints.min_width = win->w;
  118. sizehints.max_width = win->w;
  119. sizehints.min_height = win->h + win->barh;
  120. sizehints.max_height = win->h + win->barh;
  121. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  122. }
  123. void win_open(win_t *win) {
  124. win_env_t *e;
  125. XClassHint classhint;
  126. XColor col;
  127. char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  128. Pixmap none;
  129. int gmask;
  130. if (win == NULL)
  131. return;
  132. e = &win->env;
  133. /* determine window offsets, width & height */
  134. if (options->geometry == NULL)
  135. gmask = 0;
  136. else
  137. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  138. &win->w, &win->h);
  139. if ((gmask & WidthValue) == 0)
  140. win->w = WIN_WIDTH;
  141. if (win->w > e->scrw)
  142. win->w = e->scrw;
  143. if ((gmask & HeightValue) == 0)
  144. win->h = WIN_HEIGHT;
  145. if (win->h > e->scrh)
  146. win->h = e->scrh;
  147. if ((gmask & XValue) == 0)
  148. win->x = (e->scrw - win->w) / 2;
  149. else if ((gmask & XNegative) != 0)
  150. win->x += e->scrw - win->w;
  151. if ((gmask & YValue) == 0)
  152. win->y = (e->scrh - win->h) / 2;
  153. else if ((gmask & YNegative) != 0)
  154. win->y += e->scrh - win->h;
  155. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  156. win->x, win->y, win->w, win->h, 0,
  157. e->depth, InputOutput, e->vis, 0, None);
  158. if (win->xwin == None)
  159. die("could not create window");
  160. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  161. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  162. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  163. chand = XCreateFontCursor(e->dpy, XC_fleur);
  164. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  165. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  166. &col, &col) == 0)
  167. {
  168. die("could not allocate color: black");
  169. }
  170. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  171. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  172. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  173. win_set_title(win, "sxiv");
  174. classhint.res_name = "sxiv";
  175. classhint.res_class = "Sxiv";
  176. XSetClassHint(e->dpy, win->xwin, &classhint);
  177. if (!options->hide_bar) {
  178. win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
  179. win->h -= win->barh;
  180. }
  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_toggle_bar(win_t *win) {
  250. if (win == NULL || win->xwin == None)
  251. return;
  252. if (win->barh != 0) {
  253. win->h += win->barh;
  254. win->barh = 0;
  255. } else {
  256. win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD;
  257. win->h -= win->barh;
  258. }
  259. }
  260. void win_clear(win_t *win) {
  261. win_env_t *e;
  262. if (win == NULL || win->xwin == None)
  263. return;
  264. e = &win->env;
  265. if (win->pm != None)
  266. XFreePixmap(e->dpy, win->pm);
  267. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  268. XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
  269. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  270. }
  271. void win_draw_bar(win_t *win) {
  272. win_env_t *e;
  273. int len, x, y, w, tw = 0, seplen;
  274. const char *rt;
  275. if (win == NULL || win->xwin == None)
  276. return;
  277. e = &win->env;
  278. x = H_TEXT_PAD;
  279. y = win->h + font.ascent + V_TEXT_PAD;
  280. w = win->w - 2 * H_TEXT_PAD;
  281. XSetForeground(e->dpy, gc, win->barbgcol);
  282. XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh);
  283. XSetForeground(e->dpy, gc, win->barfgcol);
  284. XSetBackground(e->dpy, gc, win->barbgcol);
  285. if (win->lbar != NULL) {
  286. len = strlen(win->lbar);
  287. while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w)
  288. len--;
  289. w -= tw + 2 * H_TEXT_PAD;
  290. if (font.set)
  291. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len);
  292. else
  293. XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len);
  294. }
  295. if (win->rbar != NULL) {
  296. len = strlen(win->rbar);
  297. seplen = strlen(BAR_SEPARATOR);
  298. rt = win->rbar;
  299. while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) {
  300. rt = strstr(rt, BAR_SEPARATOR);
  301. if (rt != NULL) {
  302. rt += seplen;
  303. len = strlen(rt);
  304. } else {
  305. len = 0;
  306. }
  307. }
  308. if (len > 0) {
  309. x = win->w - tw - H_TEXT_PAD;
  310. if (font.set)
  311. XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len);
  312. else
  313. XDrawString(e->dpy, win->pm, gc, x, y, rt, len);
  314. }
  315. }
  316. }
  317. void win_draw(win_t *win) {
  318. if (win == NULL || win->xwin == None)
  319. return;
  320. if (win->barh > 0)
  321. win_draw_bar(win);
  322. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  323. XClearWindow(win->env.dpy, win->xwin);
  324. }
  325. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  326. bool fill, int lw, unsigned long col) {
  327. XGCValues gcval;
  328. if (win == NULL || pm == None)
  329. return;
  330. gcval.line_width = lw;
  331. gcval.foreground = col;
  332. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  333. if (fill)
  334. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  335. else
  336. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  337. }
  338. int win_textwidth(const char *text, unsigned int len, bool with_padding) {
  339. XRectangle r;
  340. int padding = with_padding ? 2 * H_TEXT_PAD : 0;
  341. if (font.set) {
  342. XmbTextExtents(font.set, text, len, NULL, &r);
  343. return r.width + padding;
  344. } else {
  345. return XTextWidth(font.xfont, text, len) + padding;
  346. }
  347. }
  348. void win_set_title(win_t *win, const char *title) {
  349. if (win == NULL || win->xwin == None)
  350. return;
  351. if (title == NULL)
  352. title = "sxiv";
  353. XStoreName(win->env.dpy, win->xwin, title);
  354. XSetIconName(win->env.dpy, win->xwin, title);
  355. XChangeProperty(win->env.dpy, win->xwin,
  356. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  357. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  358. PropModeReplace, (unsigned char *) title, strlen(title));
  359. XChangeProperty(win->env.dpy, win->xwin,
  360. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  361. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  362. PropModeReplace, (unsigned char *) title, strlen(title));
  363. }
  364. void win_set_bar_info(win_t *win, const char *li, const char *ri) {
  365. if (win != NULL) {
  366. win->lbar = li;
  367. win->rbar = ri;
  368. }
  369. }
  370. void win_set_cursor(win_t *win, cursor_t cursor) {
  371. if (win == NULL || win->xwin == None)
  372. return;
  373. switch (cursor) {
  374. case CURSOR_NONE:
  375. XDefineCursor(win->env.dpy, win->xwin, cnone);
  376. break;
  377. case CURSOR_HAND:
  378. XDefineCursor(win->env.dpy, win->xwin, chand);
  379. break;
  380. case CURSOR_WATCH:
  381. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  382. break;
  383. case CURSOR_ARROW:
  384. default:
  385. XDefineCursor(win->env.dpy, win->xwin, carrow);
  386. break;
  387. }
  388. XFlush(win->env.dpy);
  389. }