A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

335 lignes
8.3 KiB

  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. XGCValues gcval;
  47. char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
  48. Pixmap none;
  49. int gmask;
  50. if (!win)
  51. return;
  52. e = &win->env;
  53. if (!(e->dpy = XOpenDisplay(NULL)))
  54. die("could not open display");
  55. e->scr = DefaultScreen(e->dpy);
  56. e->scrw = DisplayWidth(e->dpy, e->scr);
  57. e->scrh = DisplayHeight(e->dpy, e->scr);
  58. e->vis = DefaultVisual(e->dpy, e->scr);
  59. e->cmap = DefaultColormap(e->dpy, e->scr);
  60. e->depth = DefaultDepth(e->dpy, e->scr);
  61. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  62. &col, &col))
  63. win->bgcol = col.pixel;
  64. else
  65. die("could not allocate color: %s", BG_COLOR);
  66. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
  67. &col, &col))
  68. win->selcol = col.pixel;
  69. else
  70. die("could not allocate color: %s", BG_COLOR);
  71. win->pm = 0;
  72. win->fullscreen = 0;
  73. /* determine window offsets, width & height */
  74. if (!options->geometry)
  75. gmask = 0;
  76. else
  77. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  78. &win->w, &win->h);
  79. if (!(gmask & WidthValue))
  80. win->w = WIN_WIDTH;
  81. if (win->w > e->scrw)
  82. win->w = e->scrw;
  83. if (!(gmask & HeightValue))
  84. win->h = WIN_HEIGHT;
  85. if (win->h > e->scrh)
  86. win->h = e->scrh;
  87. if (!(gmask & XValue))
  88. win->x = (e->scrw - win->w) / 2;
  89. else if (gmask & XNegative)
  90. win->x += e->scrw - win->w;
  91. if (!(gmask & YValue))
  92. win->y = (e->scrh - win->h) / 2;
  93. else if (gmask & YNegative)
  94. win->y += e->scrh - win->h;
  95. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  96. win->x, win->y, win->w, win->h, 0,
  97. e->depth, InputOutput, e->vis, 0, None);
  98. if (win->xwin == None)
  99. die("could not create window");
  100. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  101. ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  102. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  103. chand = XCreateFontCursor(e->dpy, XC_fleur);
  104. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  105. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
  106. &col, &col))
  107. die("could not allocate color: black");
  108. none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
  109. cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
  110. gcval.line_width = 2;
  111. gc = XCreateGC(e->dpy, win->xwin, GCLineWidth, &gcval);
  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. Pixmap win_create_pixmap(win_t *win, int w, int h) {
  186. if (!win)
  187. return 0;
  188. return XCreatePixmap(win->env.dpy, win->xwin, w, h, win->env.depth);
  189. }
  190. void win_free_pixmap(win_t *win, Pixmap pm) {
  191. if (win && pm)
  192. XFreePixmap(win->env.dpy, pm);
  193. }
  194. void win_clear(win_t *win) {
  195. win_env_t *e;
  196. XGCValues gcval;
  197. if (!win)
  198. return;
  199. e = &win->env;
  200. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) :
  201. win->bgcol;
  202. if (win->pm)
  203. XFreePixmap(e->dpy, win->pm);
  204. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  205. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  206. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  207. }
  208. void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) {
  209. if (win)
  210. XCopyArea(win->env.dpy, pm, win->pm, gc, 0, 0, w, h, x, y);
  211. }
  212. void win_draw_rect(win_t *win, int x, int y, int w, int h, Bool sel) {
  213. win_env_t *e;
  214. XGCValues gcval;
  215. if (!win)
  216. return;
  217. e = &win->env;
  218. if (sel)
  219. gcval.foreground = win->selcol;
  220. else
  221. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) :
  222. win->bgcol;
  223. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  224. XDrawRectangle(e->dpy, win->pm, gc, x, y, w, h);
  225. }
  226. void win_draw(win_t *win) {
  227. if (!win)
  228. return;
  229. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  230. XClearWindow(win->env.dpy, win->xwin);
  231. }
  232. void win_set_title(win_t *win, const char *title) {
  233. if (!win)
  234. return;
  235. if (!title)
  236. title = "sxiv";
  237. XStoreName(win->env.dpy, win->xwin, title);
  238. XSetIconName(win->env.dpy, win->xwin, title);
  239. XChangeProperty(win->env.dpy, win->xwin,
  240. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  241. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  242. PropModeReplace, (unsigned char *) title, strlen(title));
  243. XChangeProperty(win->env.dpy, win->xwin,
  244. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  245. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  246. PropModeReplace, (unsigned char *) title, strlen(title));
  247. }
  248. void win_set_cursor(win_t *win, win_cur_t cursor) {
  249. if (!win)
  250. return;
  251. switch (cursor) {
  252. case CURSOR_NONE:
  253. XDefineCursor(win->env.dpy, win->xwin, cnone);
  254. break;
  255. case CURSOR_HAND:
  256. XDefineCursor(win->env.dpy, win->xwin, chand);
  257. break;
  258. case CURSOR_WATCH:
  259. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  260. break;
  261. case CURSOR_ARROW:
  262. default:
  263. XDefineCursor(win->env.dpy, win->xwin, carrow);
  264. break;
  265. }
  266. }