Krolyxon
Commit 15041f6

Branches

Refs →

nsxivwindow.c

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