A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

339 строки
8.5 KiB

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