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.
 
 
 
 
 
 

219 lignes
5.1 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. if (!(gmask & YValue))
  67. win->y = (e->scrh - win->h) / 2;
  68. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  69. win->x, win->y, win->w, win->h, 0,
  70. e->depth, InputOutput, e->vis, 0, None);
  71. if (win->xwin == None)
  72. DIE("could not create window");
  73. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  74. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  75. arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  76. hand = XCreateFontCursor(e->dpy, XC_fleur);
  77. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  78. win_set_title(win, "sxiv");
  79. if ((classhint = XAllocClassHint())) {
  80. classhint->res_name = "sxiv";
  81. classhint->res_class = "sxiv";
  82. XSetClassHint(e->dpy, win->xwin, classhint);
  83. XFree(classhint);
  84. }
  85. XMapWindow(e->dpy, win->xwin);
  86. XFlush(e->dpy);
  87. if (options->fullscreen)
  88. win_toggle_fullscreen(win);
  89. }
  90. void win_close(win_t *win) {
  91. if (!win)
  92. return;
  93. XFreeCursor(win->env.dpy, arrow);
  94. XFreeCursor(win->env.dpy, hand);
  95. XFreeGC(win->env.dpy, bgc);
  96. XDestroyWindow(win->env.dpy, win->xwin);
  97. XCloseDisplay(win->env.dpy);
  98. }
  99. void win_set_title(win_t *win, const char *title) {
  100. if (!win)
  101. return;
  102. if (!title)
  103. title = "sxiv";
  104. XStoreName(win->env.dpy, win->xwin, title);
  105. XSetIconName(win->env.dpy, win->xwin, title);
  106. }
  107. int win_configure(win_t *win, XConfigureEvent *c) {
  108. int changed;
  109. if (!win)
  110. return 0;
  111. changed = win->w != c->width || win->h != c->height;
  112. win->x = c->x;
  113. win->y = c->y;
  114. win->w = c->width;
  115. win->h = c->height;
  116. win->bw = c->border_width;
  117. return changed;
  118. }
  119. void win_toggle_fullscreen(win_t *win) {
  120. XEvent ev;
  121. XClientMessageEvent *cm;
  122. if (!win)
  123. return;
  124. win->fullscreen ^= 1;
  125. memset(&ev, 0, sizeof(ev));
  126. ev.type = ClientMessage;
  127. cm = &ev.xclient;
  128. cm->window = win->xwin;
  129. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  130. cm->format = 32;
  131. cm->data.l[0] = win->fullscreen;
  132. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  133. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  134. cm->data.l[3] = 0;
  135. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  136. SubstructureNotifyMask, &ev);
  137. }
  138. void win_clear(win_t *win) {
  139. win_env_t *e;
  140. XGCValues gcval;
  141. if (!win)
  142. return;
  143. e = &win->env;
  144. if (win->pm)
  145. XFreePixmap(e->dpy, win->pm);
  146. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  147. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  148. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  149. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  150. }
  151. void win_draw(win_t *win) {
  152. if (!win)
  153. return;
  154. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  155. XClearWindow(win->env.dpy, win->xwin);
  156. }
  157. void win_set_cursor(win_t *win, win_cur_t cursor) {
  158. if (!win)
  159. return;
  160. switch (cursor) {
  161. case CURSOR_HAND:
  162. XDefineCursor(win->env.dpy, win->xwin, hand);
  163. break;
  164. case CURSOR_ARROW:
  165. default:
  166. XDefineCursor(win->env.dpy, win->xwin, arrow);
  167. break;
  168. }
  169. }