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.

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
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
  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_init(win_t *win) {
  32. win_env_t *e;
  33. XColor col;
  34. if (!win)
  35. return;
  36. e = &win->env;
  37. if (!(e->dpy = XOpenDisplay(NULL)))
  38. die("could not open display");
  39. e->scr = DefaultScreen(e->dpy);
  40. e->scrw = DisplayWidth(e->dpy, e->scr);
  41. e->scrh = DisplayHeight(e->dpy, e->scr);
  42. e->vis = DefaultVisual(e->dpy, e->scr);
  43. e->cmap = DefaultColormap(e->dpy, e->scr);
  44. e->depth = DefaultDepth(e->dpy, e->scr);
  45. win->black = BlackPixel(e->dpy, e->scr);
  46. win->white = WhitePixel(e->dpy, e->scr);
  47. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  48. &col, &col))
  49. {
  50. win->bgcol = col.pixel;
  51. } else {
  52. die("could not allocate color: %s", BG_COLOR);
  53. }
  54. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
  55. &col, &col))
  56. {
  57. win->selcol = col.pixel;
  58. } else {
  59. die("could not allocate color: %s", SEL_COLOR);
  60. }
  61. win->xwin = 0;
  62. win->pm = 0;
  63. win->fullscreen = 0;
  64. }
  65. void win_set_sizehints(win_t *win) {
  66. XSizeHints sizehints;
  67. if (!win || !win->xwin)
  68. return;
  69. sizehints.flags = PMinSize | PMaxSize;
  70. sizehints.min_width = win->w;
  71. sizehints.max_width = win->w;
  72. sizehints.min_height = win->h;
  73. sizehints.max_height = win->h;
  74. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  75. }
  76. void win_open(win_t *win) {
  77. win_env_t *e;
  78. XClassHint classhint;
  79. XColor col;
  80. char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
  81. Pixmap none;
  82. int gmask;
  83. if (!win)
  84. return;
  85. e = &win->env;
  86. /* determine window offsets, width & height */
  87. if (!options->geometry)
  88. gmask = 0;
  89. else
  90. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  91. &win->w, &win->h);
  92. if (!(gmask & WidthValue))
  93. win->w = WIN_WIDTH;
  94. if (win->w > e->scrw)
  95. win->w = e->scrw;
  96. if (!(gmask & HeightValue))
  97. win->h = WIN_HEIGHT;
  98. if (win->h > e->scrh)
  99. win->h = e->scrh;
  100. if (!(gmask & XValue))
  101. win->x = (e->scrw - win->w) / 2;
  102. else if (gmask & XNegative)
  103. win->x += e->scrw - win->w;
  104. if (!(gmask & YValue))
  105. win->y = (e->scrh - win->h) / 2;
  106. else if (gmask & YNegative)
  107. win->y += e->scrh - win->h;
  108. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  109. win->x, win->y, win->w, win->h, 0,
  110. e->depth, InputOutput, e->vis, 0, None);
  111. if (win->xwin == None)
  112. die("could not create window");
  113. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  114. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  115. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  116. chand = XCreateFontCursor(e->dpy, XC_fleur);
  117. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  118. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  119. &col, &col))
  120. {
  121. die("could not allocate color: black");
  122. }
  123. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  124. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  125. gc = XCreateGC(e->dpy, win->xwin, 0, None);
  126. win_set_title(win, "sxiv");
  127. classhint.res_name = "sxiv";
  128. classhint.res_class = "sxiv";
  129. XSetClassHint(e->dpy, win->xwin, &classhint);
  130. if (options->fixed)
  131. win_set_sizehints(win);
  132. XMapWindow(e->dpy, win->xwin);
  133. XFlush(e->dpy);
  134. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  135. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  136. if (options->fullscreen)
  137. win_toggle_fullscreen(win);
  138. }
  139. void win_close(win_t *win) {
  140. if (!win || !win->xwin)
  141. return;
  142. XFreeCursor(win->env.dpy, carrow);
  143. XFreeCursor(win->env.dpy, cnone);
  144. XFreeCursor(win->env.dpy, chand);
  145. XFreeCursor(win->env.dpy, cwatch);
  146. XFreeGC(win->env.dpy, gc);
  147. XDestroyWindow(win->env.dpy, win->xwin);
  148. XCloseDisplay(win->env.dpy);
  149. }
  150. int win_configure(win_t *win, XConfigureEvent *c) {
  151. int changed;
  152. if (!win)
  153. return 0;
  154. changed = win->w != c->width || win->h != c->height;
  155. win->x = c->x;
  156. win->y = c->y;
  157. win->w = c->width;
  158. win->h = c->height;
  159. win->bw = c->border_width;
  160. return changed;
  161. }
  162. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  163. if (!win || !win->xwin)
  164. return 0;
  165. x = MAX(0, x);
  166. y = MAX(0, y);
  167. w = MIN(w, win->env.scrw - 2 * win->bw);
  168. h = MIN(h, win->env.scrh - 2 * win->bw);
  169. if (win->x == x && win->y == y && win->w == w && win->h == h)
  170. return 0;
  171. win->x = x;
  172. win->y = y;
  173. win->w = w;
  174. win->h = h;
  175. if (options->fixed)
  176. win_set_sizehints(win);
  177. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  178. return 1;
  179. }
  180. void win_toggle_fullscreen(win_t *win) {
  181. XEvent ev;
  182. XClientMessageEvent *cm;
  183. if (!win || !win->xwin)
  184. return;
  185. win->fullscreen ^= 1;
  186. memset(&ev, 0, sizeof(ev));
  187. ev.type = ClientMessage;
  188. cm = &ev.xclient;
  189. cm->window = win->xwin;
  190. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  191. cm->format = 32;
  192. cm->data.l[0] = win->fullscreen;
  193. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  194. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  195. cm->data.l[3] = 0;
  196. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  197. SubstructureNotifyMask, &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, win_cur_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. }