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 8.2 KiB

14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 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. #include <string.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/cursorfont.h>
  21. #include "options.h"
  22. #include "util.h"
  23. #include "window.h"
  24. #define _WINDOW_CONFIG
  25. #include "config.h"
  26. static Cursor carrow;
  27. static Cursor cnone;
  28. static Cursor chand;
  29. static Cursor cwatch;
  30. static GC gc;
  31. Atom wm_delete_win;
  32. void win_init(win_t *win) {
  33. win_env_t *e;
  34. XColor col;
  35. if (!win)
  36. return;
  37. e = &win->env;
  38. if (!(e->dpy = XOpenDisplay(NULL)))
  39. die("could not open display");
  40. e->scr = DefaultScreen(e->dpy);
  41. e->scrw = DisplayWidth(e->dpy, e->scr);
  42. e->scrh = DisplayHeight(e->dpy, e->scr);
  43. e->vis = DefaultVisual(e->dpy, e->scr);
  44. e->cmap = DefaultColormap(e->dpy, e->scr);
  45. e->depth = DefaultDepth(e->dpy, e->scr);
  46. win->black = BlackPixel(e->dpy, e->scr);
  47. win->white = WhitePixel(e->dpy, e->scr);
  48. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  49. &col, &col))
  50. {
  51. win->bgcol = col.pixel;
  52. } else {
  53. die("could not allocate color: %s", BG_COLOR);
  54. }
  55. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
  56. &col, &col))
  57. {
  58. win->selcol = col.pixel;
  59. } else {
  60. die("could not allocate color: %s", SEL_COLOR);
  61. }
  62. win->xwin = 0;
  63. win->pm = 0;
  64. win->fullscreen = 0;
  65. }
  66. void win_set_sizehints(win_t *win) {
  67. XSizeHints sizehints;
  68. if (!win || !win->xwin)
  69. return;
  70. sizehints.flags = PMinSize | PMaxSize;
  71. sizehints.min_width = win->w;
  72. sizehints.max_width = win->w;
  73. sizehints.min_height = win->h;
  74. sizehints.max_height = win->h;
  75. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  76. }
  77. void win_open(win_t *win) {
  78. win_env_t *e;
  79. XClassHint classhint;
  80. XColor col;
  81. char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
  82. Pixmap none;
  83. int gmask;
  84. if (!win)
  85. return;
  86. e = &win->env;
  87. /* determine window offsets, width & height */
  88. if (!options->geometry)
  89. gmask = 0;
  90. else
  91. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  92. &win->w, &win->h);
  93. if (!(gmask & WidthValue))
  94. win->w = WIN_WIDTH;
  95. if (win->w > e->scrw)
  96. win->w = e->scrw;
  97. if (!(gmask & HeightValue))
  98. win->h = WIN_HEIGHT;
  99. if (win->h > e->scrh)
  100. win->h = e->scrh;
  101. if (!(gmask & XValue))
  102. win->x = (e->scrw - win->w) / 2;
  103. else if (gmask & XNegative)
  104. win->x += e->scrw - win->w;
  105. if (!(gmask & YValue))
  106. win->y = (e->scrh - win->h) / 2;
  107. else if (gmask & YNegative)
  108. win->y += e->scrh - win->h;
  109. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  110. win->x, win->y, win->w, win->h, 0,
  111. e->depth, InputOutput, e->vis, 0, None);
  112. if (win->xwin == None)
  113. die("could not create window");
  114. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  115. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  116. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  117. chand = XCreateFontCursor(e->dpy, XC_fleur);
  118. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  119. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  120. &col, &col))
  121. {
  122. die("could not allocate color: black");
  123. }
  124. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  125. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  126. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  127. win_set_title(win, "sxiv");
  128. classhint.res_name = "sxiv";
  129. classhint.res_class = "sxiv";
  130. XSetClassHint(e->dpy, win->xwin, &classhint);
  131. if (options->fixed)
  132. win_set_sizehints(win);
  133. XMapWindow(e->dpy, win->xwin);
  134. XFlush(e->dpy);
  135. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  136. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  137. if (options->fullscreen)
  138. win_toggle_fullscreen(win);
  139. }
  140. void win_close(win_t *win) {
  141. if (!win || !win->xwin)
  142. return;
  143. XFreeCursor(win->env.dpy, carrow);
  144. XFreeCursor(win->env.dpy, cnone);
  145. XFreeCursor(win->env.dpy, chand);
  146. XFreeCursor(win->env.dpy, cwatch);
  147. XFreeGC(win->env.dpy, gc);
  148. XDestroyWindow(win->env.dpy, win->xwin);
  149. XCloseDisplay(win->env.dpy);
  150. }
  151. int win_configure(win_t *win, XConfigureEvent *c) {
  152. int changed;
  153. if (!win)
  154. return 0;
  155. changed = win->w != c->width || win->h != c->height;
  156. win->x = c->x;
  157. win->y = c->y;
  158. win->w = c->width;
  159. win->h = c->height;
  160. win->bw = c->border_width;
  161. return changed;
  162. }
  163. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  164. if (!win || !win->xwin)
  165. return 0;
  166. x = MAX(0, x);
  167. y = MAX(0, y);
  168. w = MIN(w, win->env.scrw - 2 * win->bw);
  169. h = MIN(h, win->env.scrh - 2 * win->bw);
  170. if (win->x == x && win->y == y && win->w == w && win->h == h)
  171. return 0;
  172. win->x = x;
  173. win->y = y;
  174. win->w = w;
  175. win->h = h;
  176. if (options->fixed)
  177. win_set_sizehints(win);
  178. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  179. return 1;
  180. }
  181. void win_toggle_fullscreen(win_t *win) {
  182. XEvent ev;
  183. XClientMessageEvent *cm;
  184. if (!win || !win->xwin)
  185. return;
  186. win->fullscreen ^= 1;
  187. memset(&ev, 0, sizeof(ev));
  188. ev.type = ClientMessage;
  189. cm = &ev.xclient;
  190. cm->window = win->xwin;
  191. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  192. cm->format = 32;
  193. cm->data.l[0] = win->fullscreen;
  194. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  195. cm->data.l[2] = cm->data.l[3] = 0;
  196. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  197. SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  198. }
  199. void win_clear(win_t *win) {
  200. win_env_t *e;
  201. XGCValues gcval;
  202. if (!win || !win->xwin)
  203. return;
  204. e = &win->env;
  205. gcval.foreground = win->fullscreen ? win->black : win->bgcol;
  206. if (win->pm)
  207. XFreePixmap(e->dpy, win->pm);
  208. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  209. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  210. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  211. }
  212. void win_draw(win_t *win) {
  213. if (!win || !win->xwin)
  214. return;
  215. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  216. XClearWindow(win->env.dpy, win->xwin);
  217. }
  218. void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
  219. Bool fill, int lw, unsigned long col) {
  220. XGCValues gcval;
  221. if (!win || !pm)
  222. return;
  223. gcval.line_width = lw;
  224. gcval.foreground = col;
  225. XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
  226. if (fill)
  227. XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
  228. else
  229. XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
  230. }
  231. void win_set_title(win_t *win, const char *title) {
  232. if (!win || !win->xwin)
  233. return;
  234. if (!title)
  235. title = "sxiv";
  236. XStoreName(win->env.dpy, win->xwin, title);
  237. XSetIconName(win->env.dpy, win->xwin, title);
  238. XChangeProperty(win->env.dpy, win->xwin,
  239. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  240. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  241. PropModeReplace, (unsigned char *) title, strlen(title));
  242. XChangeProperty(win->env.dpy, win->xwin,
  243. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  244. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  245. PropModeReplace, (unsigned char *) title, strlen(title));
  246. }
  247. void win_set_cursor(win_t *win, cursor_t cursor) {
  248. if (!win || !win->xwin)
  249. return;
  250. switch (cursor) {
  251. case CURSOR_NONE:
  252. XDefineCursor(win->env.dpy, win->xwin, cnone);
  253. break;
  254. case CURSOR_HAND:
  255. XDefineCursor(win->env.dpy, win->xwin, chand);
  256. break;
  257. case CURSOR_WATCH:
  258. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  259. break;
  260. case CURSOR_ARROW:
  261. default:
  262. XDefineCursor(win->env.dpy, win->xwin, carrow);
  263. break;
  264. }
  265. XFlush(win->env.dpy);
  266. }