Krolyxon
Commit 3a4f386

Branches

Refs →

nsxivwindow.c

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