A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

271 satır
6.5 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 arrow;
  26. static Cursor hand;
  27. static GC bgc;
  28. void win_set_sizehints(win_t *win) {
  29. XSizeHints sizehints;
  30. if (!win)
  31. return;
  32. sizehints.flags = PMinSize | PMaxSize;
  33. sizehints.min_width = win->w;
  34. sizehints.max_width = win->w;
  35. sizehints.min_height = win->h;
  36. sizehints.max_height = win->h;
  37. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  38. }
  39. void win_open(win_t *win) {
  40. win_env_t *e;
  41. XClassHint classhint;
  42. XColor bgcol;
  43. int gmask;
  44. if (!win)
  45. return;
  46. e = &win->env;
  47. if (!(e->dpy = XOpenDisplay(NULL)))
  48. die("could not open display");
  49. e->scr = DefaultScreen(e->dpy);
  50. e->scrw = DisplayWidth(e->dpy, e->scr);
  51. e->scrh = DisplayHeight(e->dpy, e->scr);
  52. e->vis = DefaultVisual(e->dpy, e->scr);
  53. e->cmap = DefaultColormap(e->dpy, e->scr);
  54. e->depth = DefaultDepth(e->dpy, e->scr);
  55. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  56. &bgcol, &bgcol))
  57. die("could not allocate color: %s", BG_COLOR);
  58. win->bgcol = bgcol.pixel;
  59. win->pm = 0;
  60. win->fullscreen = 0;
  61. /* determine window offsets, width & height */
  62. if (!options->geometry)
  63. gmask = 0;
  64. else
  65. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  66. &win->w, &win->h);
  67. if (!(gmask & WidthValue))
  68. win->w = WIN_WIDTH;
  69. if (win->w > e->scrw)
  70. win->w = e->scrw;
  71. if (!(gmask & HeightValue))
  72. win->h = WIN_HEIGHT;
  73. if (win->h > e->scrh)
  74. win->h = e->scrh;
  75. if (!(gmask & XValue))
  76. win->x = (e->scrw - win->w) / 2;
  77. else if (gmask & XNegative)
  78. win->x += e->scrw - win->w;
  79. if (!(gmask & YValue))
  80. win->y = (e->scrh - win->h) / 2;
  81. else if (gmask & YNegative)
  82. win->y += e->scrh - win->h;
  83. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  84. win->x, win->y, win->w, win->h, 0,
  85. e->depth, InputOutput, e->vis, 0, None);
  86. if (win->xwin == None)
  87. die("could not create window");
  88. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  89. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  90. arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  91. hand = XCreateFontCursor(e->dpy, XC_fleur);
  92. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  93. win_set_title(win, "sxiv");
  94. classhint.res_name = "sxiv";
  95. classhint.res_class = "sxiv";
  96. XSetClassHint(e->dpy, win->xwin, &classhint);
  97. if (options->fixed)
  98. win_set_sizehints(win);
  99. XMapWindow(e->dpy, win->xwin);
  100. XFlush(e->dpy);
  101. if (options->fullscreen)
  102. win_toggle_fullscreen(win);
  103. }
  104. void win_close(win_t *win) {
  105. if (!win)
  106. return;
  107. XFreeCursor(win->env.dpy, arrow);
  108. XFreeCursor(win->env.dpy, hand);
  109. XFreeGC(win->env.dpy, bgc);
  110. XDestroyWindow(win->env.dpy, win->xwin);
  111. XCloseDisplay(win->env.dpy);
  112. }
  113. int win_configure(win_t *win, XConfigureEvent *c) {
  114. int changed;
  115. if (!win)
  116. return 0;
  117. changed = win->w != c->width || win->h != c->height;
  118. win->x = c->x;
  119. win->y = c->y;
  120. win->w = c->width;
  121. win->h = c->height;
  122. win->bw = c->border_width;
  123. return changed;
  124. }
  125. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  126. if (!win)
  127. return 0;
  128. x = MAX(0, x);
  129. y = MAX(0, y);
  130. w = MIN(w, win->env.scrw - 2 * win->bw);
  131. h = MIN(h, win->env.scrh - 2 * win->bw);
  132. if (win->x == x && win->y == y && win->w == w && win->h == h)
  133. return 0;
  134. win->x = x;
  135. win->y = y;
  136. win->w = w;
  137. win->h = h;
  138. if (options->fixed)
  139. win_set_sizehints(win);
  140. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  141. return 1;
  142. }
  143. void win_toggle_fullscreen(win_t *win) {
  144. XEvent ev;
  145. XClientMessageEvent *cm;
  146. if (!win)
  147. return;
  148. win->fullscreen ^= 1;
  149. memset(&ev, 0, sizeof(ev));
  150. ev.type = ClientMessage;
  151. cm = &ev.xclient;
  152. cm->window = win->xwin;
  153. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  154. cm->format = 32;
  155. cm->data.l[0] = win->fullscreen;
  156. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  157. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  158. cm->data.l[3] = 0;
  159. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  160. SubstructureNotifyMask, &ev);
  161. }
  162. void win_clear(win_t *win) {
  163. win_env_t *e;
  164. XGCValues gcval;
  165. if (!win)
  166. return;
  167. e = &win->env;
  168. if (win->pm)
  169. XFreePixmap(e->dpy, win->pm);
  170. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  171. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  172. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  173. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  174. }
  175. void win_draw(win_t *win) {
  176. if (!win)
  177. return;
  178. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  179. XClearWindow(win->env.dpy, win->xwin);
  180. }
  181. void win_set_title(win_t *win, const char *title) {
  182. if (!win)
  183. return;
  184. if (!title)
  185. title = "sxiv";
  186. XStoreName(win->env.dpy, win->xwin, title);
  187. XSetIconName(win->env.dpy, win->xwin, title);
  188. XChangeProperty(win->env.dpy, win->xwin,
  189. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  190. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  191. PropModeReplace, (unsigned char *) title, strlen(title));
  192. XChangeProperty(win->env.dpy, win->xwin,
  193. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  194. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  195. PropModeReplace, (unsigned char *) title, strlen(title));
  196. }
  197. void win_set_cursor(win_t *win, win_cur_t cursor) {
  198. if (!win)
  199. return;
  200. switch (cursor) {
  201. case CURSOR_HAND:
  202. XDefineCursor(win->env.dpy, win->xwin, hand);
  203. break;
  204. case CURSOR_ARROW:
  205. default:
  206. XDefineCursor(win->env.dpy, win->xwin, arrow);
  207. break;
  208. }
  209. }