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.
 
 
 
 
 
 

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