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.
 
 
 
 
 
 

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