Krolyxon
Commit c5f6b2c

Branches

Refs →

nsxivwindow.c

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