Krolyxon
Commit ff013dd

Branches

Refs →

nsxivwindow.c

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