Krolyxon
Commit 800143a

Branches

Refs →

nsxivwindow.c

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