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.
 
 
 
 
 
 

223 lines
5.2 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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/cursorfont.h>
  23. #include "sxiv.h"
  24. #include "window.h"
  25. static Cursor arrow;
  26. static Cursor hand;
  27. static GC bgc;
  28. void win_open(win_t *win) {
  29. win_env_t *e;
  30. XClassHint *classhint;
  31. XColor bgcol;
  32. int gmask;
  33. if (!win)
  34. return;
  35. e = &win->env;
  36. if (!(e->dpy = XOpenDisplay(NULL)))
  37. DIE("could not open display");
  38. e->scr = DefaultScreen(e->dpy);
  39. e->scrw = DisplayWidth(e->dpy, e->scr);
  40. e->scrh = DisplayHeight(e->dpy, e->scr);
  41. e->vis = DefaultVisual(e->dpy, e->scr);
  42. e->cmap = DefaultColormap(e->dpy, e->scr);
  43. e->depth = DefaultDepth(e->dpy, e->scr);
  44. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  45. &bgcol, &bgcol))
  46. DIE("could not allocate color: %s", BG_COLOR);
  47. win->bgcol = bgcol.pixel;
  48. win->pm = 0;
  49. win->fullscreen = 0;
  50. /* determine window offsets, width & height */
  51. if (!options->geometry)
  52. gmask = 0;
  53. else
  54. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  55. &win->w, &win->h);
  56. if (!(gmask & WidthValue))
  57. win->w = WIN_WIDTH;
  58. if (win->w > e->scrw)
  59. win->w = e->scrw;
  60. if (!(gmask & HeightValue))
  61. win->h = WIN_HEIGHT;
  62. if (win->h > e->scrh)
  63. win->h = e->scrh;
  64. if (!(gmask & XValue))
  65. win->x = (e->scrw - win->w) / 2;
  66. else if (gmask & XNegative)
  67. win->x += e->scrw - win->w;
  68. if (!(gmask & YValue))
  69. win->y = (e->scrh - win->h) / 2;
  70. else if (gmask & YNegative)
  71. win->y += e->scrh - win->h;
  72. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  73. win->x, win->y, win->w, win->h, 0,
  74. e->depth, InputOutput, e->vis, 0, None);
  75. if (win->xwin == None)
  76. DIE("could not create window");
  77. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  78. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  79. arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  80. hand = XCreateFontCursor(e->dpy, XC_fleur);
  81. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  82. win_set_title(win, "sxiv");
  83. if ((classhint = XAllocClassHint())) {
  84. classhint->res_name = "sxiv";
  85. classhint->res_class = "sxiv";
  86. XSetClassHint(e->dpy, win->xwin, classhint);
  87. XFree(classhint);
  88. }
  89. XMapWindow(e->dpy, win->xwin);
  90. XFlush(e->dpy);
  91. if (options->fullscreen)
  92. win_toggle_fullscreen(win);
  93. }
  94. void win_close(win_t *win) {
  95. if (!win)
  96. return;
  97. XFreeCursor(win->env.dpy, arrow);
  98. XFreeCursor(win->env.dpy, hand);
  99. XFreeGC(win->env.dpy, bgc);
  100. XDestroyWindow(win->env.dpy, win->xwin);
  101. XCloseDisplay(win->env.dpy);
  102. }
  103. void win_set_title(win_t *win, const char *title) {
  104. if (!win)
  105. return;
  106. if (!title)
  107. title = "sxiv";
  108. XStoreName(win->env.dpy, win->xwin, title);
  109. XSetIconName(win->env.dpy, win->xwin, title);
  110. }
  111. int win_configure(win_t *win, XConfigureEvent *c) {
  112. int changed;
  113. if (!win)
  114. return 0;
  115. changed = win->w != c->width || win->h != c->height;
  116. win->x = c->x;
  117. win->y = c->y;
  118. win->w = c->width;
  119. win->h = c->height;
  120. win->bw = c->border_width;
  121. return changed;
  122. }
  123. void win_toggle_fullscreen(win_t *win) {
  124. XEvent ev;
  125. XClientMessageEvent *cm;
  126. if (!win)
  127. return;
  128. win->fullscreen ^= 1;
  129. memset(&ev, 0, sizeof(ev));
  130. ev.type = ClientMessage;
  131. cm = &ev.xclient;
  132. cm->window = win->xwin;
  133. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  134. cm->format = 32;
  135. cm->data.l[0] = win->fullscreen;
  136. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  137. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  138. cm->data.l[3] = 0;
  139. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  140. SubstructureNotifyMask, &ev);
  141. }
  142. void win_clear(win_t *win) {
  143. win_env_t *e;
  144. XGCValues gcval;
  145. if (!win)
  146. return;
  147. e = &win->env;
  148. if (win->pm)
  149. XFreePixmap(e->dpy, win->pm);
  150. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  151. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  152. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  153. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  154. }
  155. void win_draw(win_t *win) {
  156. if (!win)
  157. return;
  158. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  159. XClearWindow(win->env.dpy, win->xwin);
  160. }
  161. void win_set_cursor(win_t *win, win_cur_t cursor) {
  162. if (!win)
  163. return;
  164. switch (cursor) {
  165. case CURSOR_HAND:
  166. XDefineCursor(win->env.dpy, win->xwin, hand);
  167. break;
  168. case CURSOR_ARROW:
  169. default:
  170. XDefineCursor(win->env.dpy, win->xwin, arrow);
  171. break;
  172. }
  173. }