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