aewl

view client.c @ 709:6c2fcf88dd9f

this variant is known to work, but focus() is ugly - we need in general a better way to handle multihead, this issel-stuff looks awkward (maybe it might be a good idea to set sel to NULL but to introduce a Client *revert which is set if a screen is unfocused, have to think about it further).
author Anselm R. Garbe <arg@suckless.org>
date Mon, 22 Jan 2007 10:35:58 +0100
parents a2d568a5cdb8
children b40134b93de3
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
10 /* static functions */
12 static void
13 detachstack(Client *c) {
14 Client **tc;
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
16 *tc = c->snext;
17 }
19 static void
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
23 if(focused) {
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
50 }
51 else
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee) {
58 return 0;
59 }
61 /* extern functions */
63 void
64 configure(Client *c) {
65 XEvent synev;
67 synev.type = ConfigureNotify;
68 synev.xconfigure.display = dpy;
69 synev.xconfigure.event = c->win;
70 synev.xconfigure.window = c->win;
71 synev.xconfigure.x = c->x;
72 synev.xconfigure.y = c->y;
73 synev.xconfigure.width = c->w;
74 synev.xconfigure.height = c->h;
75 synev.xconfigure.border_width = c->border;
76 synev.xconfigure.above = None;
77 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
78 }
80 void
81 focus(Client *c) {
82 Client *old = sel;
84 if(c && !isvisible(c))
85 return;
87 if(old && old != c) {
88 grabbuttons(old, False);
89 XSetWindowBorder(dpy, old->win, dc.norm[ColBorder]);
90 }
91 if(c) {
92 if(issel) {
93 detachstack(c);
94 c->snext = stack;
95 stack = c;
96 grabbuttons(c, True);
97 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
98 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
99 }
100 else
101 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
102 }
103 else if(issel)
104 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
105 sel = c;
106 drawstatus();
107 }
109 Client *
110 getclient(Window w) {
111 Client *c;
113 for(c = clients; c; c = c->next)
114 if(c->win == w)
115 return c;
116 return NULL;
117 }
119 void
120 killclient(Arg *arg) {
121 if(!sel)
122 return;
123 if(sel->proto & PROTODELWIN)
124 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
125 else
126 XKillClient(dpy, sel->win);
127 }
129 void
130 manage(Window w, XWindowAttributes *wa) {
131 Client *c;
132 Window trans;
134 c = emallocz(sizeof(Client));
135 c->tags = emallocz(ntags * sizeof(Bool));
136 c->win = w;
137 c->x = wa->x;
138 c->y = wa->y;
139 c->w = wa->width;
140 c->h = wa->height;
141 if(c->w == sw && c->h == sh) {
142 c->border = 0;
143 c->x = sx;
144 c->y = sy;
145 }
146 else {
147 c->border = BORDERPX;
148 if(c->x < wax)
149 c->x = wax;
150 if(c->y < way)
151 c->y = way;
152 if(c->x + c->w + 2 * c->border > wax + waw)
153 c->x = wax + waw - c->w - 2 * c->border;
154 if(c->y + c->h + 2 * c->border > way + wah)
155 c->y = way + wah - c->h - 2 * c->border;
156 }
157 updatesizehints(c);
158 c->proto = getproto(c->win);
159 XSelectInput(dpy, c->win,
160 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
161 XGetTransientForHint(dpy, c->win, &trans);
162 grabbuttons(c, False);
163 updatetitle(c);
164 settags(c, getclient(trans));
165 if(!c->isfloat)
166 c->isfloat = trans || c->isfixed;
167 if(clients)
168 clients->prev = c;
169 c->next = clients;
170 c->snext = stack;
171 stack = clients = c;
172 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
173 XMapWindow(dpy, c->win);
174 if(isvisible(c))
175 focus(c);
176 arrange();
177 }
179 void
180 resize(Client *c, Bool sizehints) {
181 XWindowChanges wc;
183 if(sizehints) {
184 if(c->incw)
185 c->w -= (c->w - c->basew) % c->incw;
186 if(c->inch)
187 c->h -= (c->h - c->baseh) % c->inch;
188 if(c->minw && c->w < c->minw)
189 c->w = c->minw;
190 if(c->minh && c->h < c->minh)
191 c->h = c->minh;
192 if(c->maxw && c->w > c->maxw)
193 c->w = c->maxw;
194 if(c->maxh && c->h > c->maxh)
195 c->h = c->maxh;
196 }
197 if(c->w == sw && c->h == sh)
198 c->border = 0;
199 else
200 c->border = BORDERPX;
201 /* offscreen appearance fixes */
202 if(c->x + c->w + 2 * c->border < sx)
203 c->x = sx;
204 if(c->y + c->h + 2 * c->border < sy)
205 c->y = sy;
206 if(c->x > sw)
207 c->x = sw - c->w - 2 * c->border;
208 if(c->y > sh)
209 c->y = sh - c->h - 2 * c->border;
210 wc.x = c->x;
211 wc.y = c->y;
212 wc.width = c->w;
213 wc.height = c->h;
214 wc.border_width = c->border;
215 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
216 configure(c);
217 XSync(dpy, False);
218 }
220 void
221 updatesizehints(Client *c) {
222 long msize;
223 XSizeHints size;
225 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
226 size.flags = PSize;
227 c->flags = size.flags;
228 if(c->flags & PBaseSize) {
229 c->basew = size.base_width;
230 c->baseh = size.base_height;
231 }
232 else
233 c->basew = c->baseh = 0;
234 if(c->flags & PResizeInc) {
235 c->incw = size.width_inc;
236 c->inch = size.height_inc;
237 }
238 else
239 c->incw = c->inch = 0;
240 if(c->flags & PMaxSize) {
241 c->maxw = size.max_width;
242 c->maxh = size.max_height;
243 }
244 else
245 c->maxw = c->maxh = 0;
246 if(c->flags & PMinSize) {
247 c->minw = size.min_width;
248 c->minh = size.min_height;
249 }
250 else
251 c->minw = c->minh = 0;
252 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
253 c->maxw == c->minw && c->maxh == c->minh);
254 }
256 void
257 updatetitle(Client *c) {
258 char **list = NULL;
259 int n;
260 XTextProperty name;
262 name.nitems = 0;
263 c->name[0] = 0;
264 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
265 if(!name.nitems)
266 XGetWMName(dpy, c->win, &name);
267 if(!name.nitems)
268 return;
269 if(name.encoding == XA_STRING)
270 strncpy(c->name, (char *)name.value, sizeof c->name);
271 else {
272 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
273 && n > 0 && *list)
274 {
275 strncpy(c->name, *list, sizeof c->name);
276 XFreeStringList(list);
277 }
278 }
279 XFree(name.value);
280 }
282 void
283 unmanage(Client *c) {
284 Client *nc;
286 /* The server grab construct avoids race conditions. */
287 XGrabServer(dpy);
288 XSetErrorHandler(xerrordummy);
289 detach(c);
290 detachstack(c);
291 if(sel == c) {
292 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
293 focus(nc);
294 }
295 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
296 free(c->tags);
297 free(c);
298 XSync(dpy, False);
299 XSetErrorHandler(xerror);
300 XUngrabServer(dpy);
301 arrange();
302 }