A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

window.c 7.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* sxiv: window.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <string.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/cursorfont.h>
  21. #include "config.h"
  22. #include "options.h"
  23. #include "util.h"
  24. #include "window.h"
  25. static Cursor carrow;
  26. static Cursor cnone;
  27. static Cursor chand;
  28. static Cursor cwatch;
  29. static GC gc;
  30. Atom wm_delete_win;
  31. void win_set_sizehints(win_t *win) {
  32. XSizeHints sizehints;
  33. if (!win)
  34. return;
  35. sizehints.flags = PMinSize | PMaxSize;
  36. sizehints.min_width = win->w;
  37. sizehints.max_width = win->w;
  38. sizehints.min_height = win->h;
  39. sizehints.max_height = win->h;
  40. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  41. }
  42. void win_open(win_t *win) {
  43. win_env_t *e;
  44. XClassHint classhint;
  45. XColor col;
  46. char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
  47. Pixmap none;
  48. int gmask;
  49. if (!win)
  50. return;
  51. e = &win->env;
  52. if (!(e->dpy = XOpenDisplay(NULL)))
  53. die("could not open display");
  54. e->scr = DefaultScreen(e->dpy);
  55. e->scrw = DisplayWidth(e->dpy, e->scr);
  56. e->scrh = DisplayHeight(e->dpy, e->scr);
  57. e->vis = DefaultVisual(e->dpy, e->scr);
  58. e->cmap = DefaultColormap(e->dpy, e->scr);
  59. e->depth = DefaultDepth(e->dpy, e->scr);
  60. win->black = BlackPixel(e->dpy, e->scr);
  61. win->white = WhitePixel(e->dpy, e->scr);
  62. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  63. &col, &col))
  64. win->bgcol = col.pixel;
  65. else
  66. die("could not allocate color: %s", BG_COLOR);
  67. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
  68. &col, &col))
  69. win->selcol = col.pixel;
  70. else
  71. die("could not allocate color: %s", BG_COLOR);
  72. win->pm = 0;
  73. win->fullscreen = 0;
  74. /* determine window offsets, width & height */
  75. if (!options->geometry)
  76. gmask = 0;
  77. else
  78. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  79. &win->w, &win->h);
  80. if (!(gmask & WidthValue))
  81. win->w = WIN_WIDTH;
  82. if (win->w > e->scrw)
  83. win->w = e->scrw;
  84. if (!(gmask & HeightValue))
  85. win->h = WIN_HEIGHT;
  86. if (win->h > e->scrh)
  87. win->h = e->scrh;
  88. if (!(gmask & XValue))
  89. win->x = (e->scrw - win->w) / 2;
  90. else if (gmask & XNegative)
  91. win->x += e->scrw - win->w;
  92. if (!(gmask & YValue))
  93. win->y = (e->scrh - win->h) / 2;
  94. else if (gmask & YNegative)
  95. win->y += e->scrh - win->h;
  96. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  97. win->x, win->y, win->w, win->h, 0,
  98. e->depth, InputOutput, e->vis, 0, None);
  99. if (win->xwin == None)
  100. die("could not create window");
  101. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  102. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  103. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  104. chand = XCreateFontCursor(e->dpy, XC_fleur);
  105. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  106. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  107. &col, &col))
  108. die("could not allocate color: black");
  109. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  110. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  111. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  112. win_set_title(win, "sxiv");
  113. classhint.res_name = "sxiv";
  114. classhint.res_class = "sxiv";
  115. XSetClassHint(e->dpy, win->xwin, &classhint);
  116. if (options->fixed)
  117. win_set_sizehints(win);
  118. XMapWindow(e->dpy, win->xwin);
  119. XFlush(e->dpy);
  120. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  121. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  122. if (options->fullscreen)
  123. win_toggle_fullscreen(win);
  124. }
  125. void win_close(win_t *win) {
  126. if (!win)
  127. return;
  128. XFreeCursor(win->env.dpy, carrow);
  129. XFreeCursor(win->env.dpy, cnone);
  130. XFreeCursor(win->env.dpy, chand);
  131. XFreeCursor(win->env.dpy, cwatch);
  132. XFreeGC(win->env.dpy, gc);
  133. XDestroyWindow(win->env.dpy, win->xwin);
  134. XCloseDisplay(win->env.dpy);
  135. }
  136. int win_configure(win_t *win, XConfigureEvent *c) {
  137. int changed;
  138. if (!win)
  139. return 0;
  140. changed = win->w != c->width || win->h != c->height;
  141. win->x = c->x;
  142. win->y = c->y;
  143. win->w = c->width;
  144. win->h = c->height;
  145. win->bw = c->border_width;
  146. return changed;
  147. }
  148. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  149. if (!win)
  150. return 0;
  151. x = MAX(0, x);
  152. y = MAX(0, y);
  153. w = MIN(w, win->env.scrw - 2 * win->bw);
  154. h = MIN(h, win->env.scrh - 2 * win->bw);
  155. if (win->x == x && win->y == y && win->w == w && win->h == h)
  156. return 0;
  157. win->x = x;
  158. win->y = y;
  159. win->w = w;
  160. win->h = h;
  161. if (options->fixed)
  162. win_set_sizehints(win);
  163. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  164. return 1;
  165. }
  166. void win_toggle_fullscreen(win_t *win) {
  167. XEvent ev;
  168. XClientMessageEvent *cm;
  169. if (!win)
  170. return;
  171. win->fullscreen ^= 1;
  172. memset(&ev, 0, sizeof(ev));
  173. ev.type = ClientMessage;
  174. cm = &ev.xclient;
  175. cm->window = win->xwin;
  176. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  177. cm->format = 32;
  178. cm->data.l[0] = win->fullscreen;
  179. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  180. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  181. cm->data.l[3] = 0;
  182. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  183. SubstructureNotifyMask, &ev);
  184. }
  185. void win_clear(win_t *win) {
  186. win_env_t *e;
  187. XGCValues gcval;
  188. if (!win)
  189. return;
  190. e = &win->env;
  191. gcval.foreground = win->fullscreen ? win->black : win->bgcol;
  192. if (win->pm)
  193. XFreePixmap(e->dpy, win->pm);
  194. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  195. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  196. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  197. }
  198. void win_draw(win_t *win) {
  199. if (!win)
  200. return;
  201. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  202. XClearWindow(win->env.dpy, win->xwin);
  203. }
  204. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  205. Bool fill, int lw, unsigned long col) {
  206. XGCValues gcval;
  207. if (!win || !pm)
  208. return;
  209. gcval.line_width = lw;
  210. gcval.foreground = col;
  211. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  212. if (fill)
  213. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  214. else
  215. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  216. }
  217. void win_set_title(win_t *win, const char *title) {
  218. if (!win)
  219. return;
  220. if (!title)
  221. title = "sxiv";
  222. XStoreName(win->env.dpy, win->xwin, title);
  223. XSetIconName(win->env.dpy, win->xwin, title);
  224. XChangeProperty(win->env.dpy, win->xwin,
  225. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  226. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  227. PropModeReplace, (unsigned char *) title, strlen(title));
  228. XChangeProperty(win->env.dpy, win->xwin,
  229. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  230. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  231. PropModeReplace, (unsigned char *) title, strlen(title));
  232. }
  233. void win_set_cursor(win_t *win, win_cur_t cursor) {
  234. if (!win)
  235. return;
  236. switch (cursor) {
  237. case CURSOR_NONE:
  238. XDefineCursor(win->env.dpy, win->xwin, cnone);
  239. break;
  240. case CURSOR_HAND:
  241. XDefineCursor(win->env.dpy, win->xwin, chand);
  242. break;
  243. case CURSOR_WATCH:
  244. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  245. break;
  246. case CURSOR_ARROW:
  247. default:
  248. XDefineCursor(win->env.dpy, win->xwin, carrow);
  249. break;
  250. }
  251. XFlush(win->env.dpy);
  252. }