Krolyxon
Commit 2194df8

Branches

Refs →

nsxivwindow.c

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