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.
 
 
 
 
 
 

173 lines
4.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 "sxiv.h"
  23. #include "options.h"
  24. #include "window.h"
  25. GC bgc;
  26. void win_open(win_t *win) {
  27. win_env_t *e;
  28. XClassHint *classhint;
  29. XColor bgcol;
  30. if (!win)
  31. return;
  32. e = &win->env;
  33. if (!(e->dpy = XOpenDisplay(NULL)))
  34. DIE("could not open display");
  35. e->scr = DefaultScreen(e->dpy);
  36. e->scrw = DisplayWidth(e->dpy, e->scr);
  37. e->scrh = DisplayHeight(e->dpy, e->scr);
  38. e->vis = DefaultVisual(e->dpy, e->scr);
  39. e->cmap = DefaultColormap(e->dpy, e->scr);
  40. e->depth = DefaultDepth(e->dpy, e->scr);
  41. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  42. &bgcol, &bgcol))
  43. DIE("could not allocate color: %s", BG_COLOR);
  44. win->bgcol = bgcol.pixel;
  45. win->pm = 0;
  46. win->w = MIN(options->winw, e->scrw);
  47. win->h = MIN(options->winh, e->scrh);
  48. win->x = (e->scrw - win->w) / 2;
  49. win->y = (e->scrh - win->h) / 2;
  50. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  51. win->x, win->y, win->w, win->h, 0,
  52. e->depth, InputOutput, e->vis, 0, None);
  53. if (win->xwin == None)
  54. DIE("could not create window");
  55. XSelectInput(e->dpy, win->xwin,
  56. StructureNotifyMask | KeyPressMask | ButtonPressMask);
  57. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  58. win_set_title(win, "sxiv");
  59. if ((classhint = XAllocClassHint())) {
  60. classhint->res_name = "sxiv";
  61. classhint->res_class = "sxiv";
  62. XSetClassHint(e->dpy, win->xwin, classhint);
  63. XFree(classhint);
  64. }
  65. XMapWindow(e->dpy, win->xwin);
  66. XFlush(e->dpy);
  67. }
  68. void win_close(win_t *win) {
  69. if (!win)
  70. return;
  71. XDestroyWindow(win->env.dpy, win->xwin);
  72. XCloseDisplay(win->env.dpy);
  73. }
  74. void win_set_title(win_t *win, const char *title) {
  75. if (!win)
  76. return;
  77. if (!title)
  78. title = "sxiv";
  79. XStoreName(win->env.dpy, win->xwin, title);
  80. XSetIconName(win->env.dpy, win->xwin, title);
  81. }
  82. int win_configure(win_t *win, XConfigureEvent *c) {
  83. int changed;
  84. if (!win)
  85. return 0;
  86. changed = win->w != c->width || win->h != c->height;
  87. win->x = c->x;
  88. win->y = c->y;
  89. win->w = c->width;
  90. win->h = c->height;
  91. win->bw = c->border_width;
  92. return changed;
  93. }
  94. void win_toggle_fullscreen(win_t *win) {
  95. XEvent ev;
  96. XClientMessageEvent *cm;
  97. if (!win)
  98. return;
  99. win->fullscreen ^= 1;
  100. memset(&ev, 0, sizeof(ev));
  101. ev.type = ClientMessage;
  102. cm = &ev.xclient;
  103. cm->window = win->xwin;
  104. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  105. cm->format = 32;
  106. cm->data.l[0] = win->fullscreen;
  107. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  108. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  109. cm->data.l[3] = 0;
  110. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  111. SubstructureNotifyMask, &ev);
  112. }
  113. void win_clear(win_t *win) {
  114. win_env_t *e;
  115. XGCValues gcval;
  116. if (!win)
  117. return;
  118. e = &win->env;
  119. if (win->pm)
  120. XFreePixmap(e->dpy, win->pm);
  121. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  122. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  123. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  124. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  125. }
  126. void win_draw(win_t *win) {
  127. if (!win)
  128. return;
  129. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  130. XClearWindow(win->env.dpy, win->xwin);
  131. }