Krolyxon
Commit bd87ae9

Branches

Refs →

nsxivwindow.c

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