Krolyxon
Commit 6e575b0

Branches

Refs →

nsxivwindow.c

339 lines · 8.2 KB · C

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 it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20
21#include <string.h>
22#include <X11/Xutil.h>
23#include <X11/cursorfont.h>
24
25#include "options.h"
26#include "util.h"
27#include "window.h"
28
29#define _WINDOW_CONFIG
30#include "config.h"
31
32static Cursor carrow;
33static Cursor cnone;
34static Cursor chand;
35static Cursor cwatch;
36static GC gc;
37
38Atom wm_delete_win;
39
40void win_init(win_t *win) {
41 win_env_t *e;
42 XColor col;
43
44 if (!win)
45 return;
46
47 e = &win->env;
48 if (!(e->dpy = XOpenDisplay(NULL)))
49 die("could not open display");
50
51 e->scr = DefaultScreen(e->dpy);
52 e->scrw = DisplayWidth(e->dpy, e->scr);
53 e->scrh = DisplayHeight(e->dpy, e->scr);
54 e->vis = DefaultVisual(e->dpy, e->scr);
55 e->cmap = DefaultColormap(e->dpy, e->scr);
56 e->depth = DefaultDepth(e->dpy, e->scr);
57
58 win->black = BlackPixel(e->dpy, e->scr);
59 win->white = WhitePixel(e->dpy, e->scr);
60
61 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
62 &col, &col))
63 {
64 win->bgcol = col.pixel;
65 } else {
66 die("could not allocate color: %s", BG_COLOR);
67 }
68
69 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
70 &col, &col))
71 {
72 win->selcol = col.pixel;
73 } else {
74 die("could not allocate color: %s", SEL_COLOR);
75 }
76
77 win->xwin = 0;
78 win->pm = 0;
79 win->fullscreen = 0;
80}
81
82void win_set_sizehints(win_t *win) {
83 XSizeHints sizehints;
84
85 if (!win || !win->xwin)
86 return;
87
88 sizehints.flags = PMinSize | PMaxSize;
89 sizehints.min_width = win->w;
90 sizehints.max_width = win->w;
91 sizehints.min_height = win->h;
92 sizehints.max_height = win->h;
93 XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
94}
95
96void win_open(win_t *win) {
97 win_env_t *e;
98 XClassHint classhint;
99 XColor col;
100 char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
101 Pixmap none;
102 int gmask;
103
104 if (!win)
105 return;
106
107 e = &win->env;
108
109 /* determine window offsets, width & height */
110 if (!options->geometry)
111 gmask = 0;
112 else
113 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
114 &win->w, &win->h);
115 if (!(gmask & WidthValue))
116 win->w = WIN_WIDTH;
117 if (win->w > e->scrw)
118 win->w = e->scrw;
119 if (!(gmask & HeightValue))
120 win->h = WIN_HEIGHT;
121 if (win->h > e->scrh)
122 win->h = e->scrh;
123 if (!(gmask & XValue))
124 win->x = (e->scrw - win->w) / 2;
125 else if (gmask & XNegative)
126 win->x += e->scrw - win->w;
127 if (!(gmask & YValue))
128 win->y = (e->scrh - win->h) / 2;
129 else if (gmask & YNegative)
130 win->y += e->scrh - win->h;
131
132 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
133 win->x, win->y, win->w, win->h, 0,
134 e->depth, InputOutput, e->vis, 0, None);
135 if (win->xwin == None)
136 die("could not create window");
137
138 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
139 ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
140
141 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
142 chand = XCreateFontCursor(e->dpy, XC_fleur);
143 cwatch = XCreateFontCursor(e->dpy, XC_watch);
144
145 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
146 &col, &col))
147 {
148 die("could not allocate color: black");
149 }
150 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
151 cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
152
153 gc = XCreateGC(e->dpy, win->xwin, 0, None);
154
155 win_set_title(win, "sxiv");
156
157 classhint.res_name = "sxiv";
158 classhint.res_class = "sxiv";
159 XSetClassHint(e->dpy, win->xwin, &classhint);
160
161 if (options->fixed)
162 win_set_sizehints(win);
163
164 XMapWindow(e->dpy, win->xwin);
165 XFlush(e->dpy);
166
167 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
168 XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
169
170 if (options->fullscreen)
171 win_toggle_fullscreen(win);
172}
173
174void win_close(win_t *win) {
175 if (!win || !win->xwin)
176 return;
177
178 XFreeCursor(win->env.dpy, carrow);
179 XFreeCursor(win->env.dpy, cnone);
180 XFreeCursor(win->env.dpy, chand);
181 XFreeCursor(win->env.dpy, cwatch);
182
183 XFreeGC(win->env.dpy, gc);
184
185 XDestroyWindow(win->env.dpy, win->xwin);
186 XCloseDisplay(win->env.dpy);
187}
188
189int win_configure(win_t *win, XConfigureEvent *c) {
190 int changed;
191
192 if (!win)
193 return 0;
194
195 changed = win->w != c->width || win->h != c->height;
196
197 win->x = c->x;
198 win->y = c->y;
199 win->w = c->width;
200 win->h = c->height;
201 win->bw = c->border_width;
202
203 return changed;
204}
205
206int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
207 if (!win || !win->xwin)
208 return 0;
209
210 x = MAX(0, x);
211 y = MAX(0, y);
212 w = MIN(w, win->env.scrw - 2 * win->bw);
213 h = MIN(h, win->env.scrh - 2 * win->bw);
214
215 if (win->x == x && win->y == y && win->w == w && win->h == h)
216 return 0;
217
218 win->x = x;
219 win->y = y;
220 win->w = w;
221 win->h = h;
222
223 if (options->fixed)
224 win_set_sizehints(win);
225
226 XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
227
228 return 1;
229}
230
231void win_toggle_fullscreen(win_t *win) {
232 XEvent ev;
233 XClientMessageEvent *cm;
234
235 if (!win || !win->xwin)
236 return;
237
238 win->fullscreen ^= 1;
239
240 memset(&ev, 0, sizeof(ev));
241 ev.type = ClientMessage;
242
243 cm = &ev.xclient;
244 cm->window = win->xwin;
245 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
246 cm->format = 32;
247 cm->data.l[0] = win->fullscreen;
248 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
249 cm->data.l[2] = cm->data.l[3] = 0;
250
251 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
252 SubstructureNotifyMask | SubstructureRedirectMask, &ev);
253}
254
255void win_clear(win_t *win) {
256 win_env_t *e;
257 XGCValues gcval;
258
259 if (!win || !win->xwin)
260 return;
261
262 e = &win->env;
263 gcval.foreground = win->fullscreen ? win->black : win->bgcol;
264
265 if (win->pm)
266 XFreePixmap(e->dpy, win->pm);
267 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
268
269 XChangeGC(e->dpy, gc, GCForeground, &gcval);
270 XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
271}
272
273void win_draw(win_t *win) {
274 if (!win || !win->xwin)
275 return;
276
277 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
278 XClearWindow(win->env.dpy, win->xwin);
279}
280
281void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
282 Bool fill, int lw, unsigned long col) {
283 XGCValues gcval;
284
285 if (!win || !pm)
286 return;
287
288 gcval.line_width = lw;
289 gcval.foreground = col;
290 XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
291
292 if (fill)
293 XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
294 else
295 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
296}
297
298void win_set_title(win_t *win, const char *title) {
299 if (!win || !win->xwin)
300 return;
301
302 if (!title)
303 title = "sxiv";
304
305 XStoreName(win->env.dpy, win->xwin, title);
306 XSetIconName(win->env.dpy, win->xwin, title);
307
308 XChangeProperty(win->env.dpy, win->xwin,
309 XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
310 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
311 PropModeReplace, (unsigned char *) title, strlen(title));
312 XChangeProperty(win->env.dpy, win->xwin,
313 XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
314 XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
315 PropModeReplace, (unsigned char *) title, strlen(title));
316}
317
318void win_set_cursor(win_t *win, cursor_t cursor) {
319 if (!win || !win->xwin)
320 return;
321
322 switch (cursor) {
323 case CURSOR_NONE:
324 XDefineCursor(win->env.dpy, win->xwin, cnone);
325 break;
326 case CURSOR_HAND:
327 XDefineCursor(win->env.dpy, win->xwin, chand);
328 break;
329 case CURSOR_WATCH:
330 XDefineCursor(win->env.dpy, win->xwin, cwatch);
331 break;
332 case CURSOR_ARROW:
333 default:
334 XDefineCursor(win->env.dpy, win->xwin, carrow);
335 break;
336 }
337
338 XFlush(win->env.dpy);
339}