renamed floating into swimming (this does not clash with C naming conventions and...
[dwm.git] / client.c
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>
9
10 /* static */
11
12 static void
13 attachstack(Client *c) {
14         c->snext = stack;
15         stack = c;
16 }
17
18 static void
19 detachstack(Client *c) {
20         Client **tc;
21
22         for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
23         *tc = c->snext;
24 }
25
26 static void
27 grabbuttons(Client *c, Bool focused) {
28         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
29
30         if(focused) {
31                 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
32                                 GrabModeAsync, GrabModeSync, None, None);
33                 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
34                                 GrabModeAsync, GrabModeSync, None, None);
35                 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
36                                 GrabModeAsync, GrabModeSync, None, None);
37                 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
38                                 GrabModeAsync, GrabModeSync, None, None);
39
40                 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
41                                 GrabModeAsync, GrabModeSync, None, None);
42                 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
43                                 GrabModeAsync, GrabModeSync, None, None);
44                 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
45                                 GrabModeAsync, GrabModeSync, None, None);
46                 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
47                                 GrabModeAsync, GrabModeSync, None, None);
48
49                 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
50                                 GrabModeAsync, GrabModeSync, None, None);
51                 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
52                                 GrabModeAsync, GrabModeSync, None, None);
53                 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
54                                 GrabModeAsync, GrabModeSync, None, None);
55                 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
56                                 GrabModeAsync, GrabModeSync, None, None);
57         }
58         else
59                 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
60                                 GrabModeAsync, GrabModeSync, None, None);
61 }
62
63 static Bool
64 isprotodel(Client *c) {
65         int i, n;
66         Atom *protocols;
67         Bool ret = False;
68
69         if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
70                 for(i = 0; !ret && i < n; i++)
71                         if(protocols[i] == wmatom[WMDelete])
72                                 ret = True;
73                 XFree(protocols);
74         }
75         return ret;
76 }
77
78 static void
79 setclientstate(Client *c, long state) {
80         long data[] = {state, None};
81
82         XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
83                         PropModeReplace, (unsigned char *)data, 2);
84 }
85
86 static void
87 togglemax(Client *c) {
88         XEvent ev;
89
90         if(c->isfixed)
91                 return;
92         if((c->ismax = !c->ismax)) {
93                 c->rx = c->x;
94                 c->ry = c->y;
95                 c->rw = c->w;
96                 c->rh = c->h;
97                 resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
98         }
99         else
100                 resize(c, c->rx, c->ry, c->rw, c->rh, True);
101         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
102 }
103
104 static int
105 xerrordummy(Display *dsply, XErrorEvent *ee) {
106         return 0;
107 }
108
109 /* extern */
110
111 void
112 attach(Client *c) {
113         if(clients)
114                 clients->prev = c;
115         c->next = clients;
116         clients = c;
117 }
118
119 void
120 configure(Client *c) {
121         XConfigureEvent ce;
122
123         ce.type = ConfigureNotify;
124         ce.display = dpy;
125         ce.event = c->win;
126         ce.window = c->win;
127         ce.x = c->x;
128         ce.y = c->y;
129         ce.width = c->w;
130         ce.height = c->h;
131         ce.border_width = c->border;
132         ce.above = None;
133         ce.override_redirect = False;
134         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
135 }
136
137 void
138 detach(Client *c) {
139         if(c->prev)
140                 c->prev->next = c->next;
141         if(c->next)
142                 c->next->prev = c->prev;
143         if(c == clients)
144                 clients = c->next;
145         c->next = c->prev = NULL;
146 }
147
148 void
149 focus(Client *c) {
150         if(c && !isvisible(c))
151                 return;
152         if(sel && sel != c) {
153                 grabbuttons(sel, False);
154                 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
155         }
156         if(c) {
157                 detachstack(c);
158                 attachstack(c);
159                 grabbuttons(c, True);
160         }
161         sel = c;
162         drawstatus();
163         if(!selscreen)
164                 return;
165         if(c) {
166                 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
167                 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
168         }
169         else
170                 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
171 }
172
173 void
174 focusnext(Arg *arg) {
175         Client *c;
176    
177         if(!sel)
178                 return;
179         for(c = sel->next; c && !isvisible(c); c = c->next);
180         if(!c)
181                 for(c = clients; c && !isvisible(c); c = c->next);
182         if(c) {
183                 focus(c);
184                 restack();
185         }
186 }
187
188 void
189 focusprev(Arg *arg) {
190         Client *c;
191
192         if(!sel)
193                 return;
194         for(c = sel->prev; c && !isvisible(c); c = c->prev);
195         if(!c) {
196                 for(c = clients; c && c->next; c = c->next);
197                 for(; c && !isvisible(c); c = c->prev);
198         }
199         if(c) {
200                 focus(c);
201                 restack();
202         }
203 }
204
205 void
206 killclient(Arg *arg) {
207         if(!sel)
208                 return;
209         if(isprotodel(sel))
210                 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
211         else
212                 XKillClient(dpy, sel->win);
213 }
214
215 void
216 manage(Window w, XWindowAttributes *wa) {
217         Client *c, *t;
218         Window trans;
219         XWindowChanges wc;
220
221         c = emallocz(sizeof(Client));
222         c->tags = emallocz(ntags * sizeof(Bool));
223         c->win = w;
224         c->x = wa->x;
225         c->y = wa->y;
226         c->w = wa->width;
227         c->h = wa->height;
228         if(c->w == sw && c->h == sh) {
229                 c->border = 0;
230                 c->x = sx;
231                 c->y = sy;
232         }
233         else {
234                 c->border = BORDERPX;
235                 if(c->x + c->w + 2 * c->border > wax + waw)
236                         c->x = wax + waw - c->w - 2 * c->border;
237                 if(c->y + c->h + 2 * c->border > way + wah)
238                         c->y = way + wah - c->h - 2 * c->border;
239                 if(c->x < wax)
240                         c->x = wax;
241                 if(c->y < way)
242                         c->y = way;
243         }
244         updatesizehints(c);
245         XSelectInput(dpy, w,
246                 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
247         XGetTransientForHint(dpy, w, &trans);
248         grabbuttons(c, False);
249         wc.border_width = c->border;
250         XConfigureWindow(dpy, w, CWBorderWidth, &wc);
251         XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
252         configure(c); /* propagates border_width, if size doesn't change */
253         updatetitle(c);
254         for(t = clients; t && t->win != trans; t = t->next);
255         settags(c, t);
256         if(!c->swimming)
257                 c->swimming = (t != NULL) || c->isfixed;
258         attach(c);
259         attachstack(c);
260         c->isbanned = True;
261         XMoveWindow(dpy, w, c->x + 2 * sw, c->y);
262         XMapWindow(dpy, w);
263         setclientstate(c, NormalState);
264         if(isvisible(c))
265                 focus(c);
266         lt->arrange();
267 }
268
269 Client *
270 nexttiled(Client *c) {
271         for(; c && (c->swimming || !isvisible(c)); c = c->next);
272         return c;
273 }
274
275 void
276 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
277         float actual, dx, dy, max, min;
278         XWindowChanges wc;
279
280         if(w <= 0 || h <= 0)
281                 return;
282         if(sizehints) {
283                 if(c->minw && w < c->minw)
284                         w = c->minw;
285                 if(c->minh && h < c->minh)
286                         h = c->minh;
287                 if(c->maxw && w > c->maxw)
288                         w = c->maxw;
289                 if(c->maxh && h > c->maxh)
290                         h = c->maxh;
291                 /* inspired by algorithm from fluxbox */
292                 if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
293                         dx = (float)(w - c->basew);
294                         dy = (float)(h - c->baseh);
295                         min = (float)(c->minax) / (float)(c->minay);
296                         max = (float)(c->maxax) / (float)(c->maxay);
297                         actual = dx / dy;
298                         if(max > 0 && min > 0 && actual > 0) {
299                                 if(actual < min) {
300                                         dy = (dx * min + dy) / (min * min + 1);
301                                         dx = dy * min;
302                                         w = (int)dx + c->basew;
303                                         h = (int)dy + c->baseh;
304                                 }
305                                 else if(actual > max) {
306                                         dy = (dx * min + dy) / (max * max + 1);
307                                         dx = dy * min;
308                                         w = (int)dx + c->basew;
309                                         h = (int)dy + c->baseh;
310                                 }
311                         }
312                 }
313                 if(c->incw)
314                         w -= (w - c->basew) % c->incw;
315                 if(c->inch)
316                         h -= (h - c->baseh) % c->inch;
317         }
318         if(w == sw && h == sh)
319                 c->border = 0;
320         else
321                 c->border = BORDERPX;
322         /* offscreen appearance fixes */
323         if(x > sw)
324                 x = sw - w - 2 * c->border;
325         if(y > sh)
326                 y = sh - h - 2 * c->border;
327         if(x + w + 2 * c->border < sx)
328                 x = sx;
329         if(y + h + 2 * c->border < sy)
330                 y = sy;
331         if(c->x != x || c->y != y || c->w != w || c->h != h) {
332                 c->x = wc.x = x;
333                 c->y = wc.y = y;
334                 c->w = wc.width = w;
335                 c->h = wc.height = h;
336                 wc.border_width = c->border;
337                 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
338                 configure(c);
339                 XSync(dpy, False);
340         }
341 }
342
343 void
344 updatesizehints(Client *c) {
345         long msize;
346         XSizeHints size;
347
348         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
349                 size.flags = PSize;
350         c->flags = size.flags;
351         if(c->flags & PBaseSize) {
352                 c->basew = size.base_width;
353                 c->baseh = size.base_height;
354         }
355         else
356                 c->basew = c->baseh = 0;
357         if(c->flags & PResizeInc) {
358                 c->incw = size.width_inc;
359                 c->inch = size.height_inc;
360         }
361         else
362                 c->incw = c->inch = 0;
363         if(c->flags & PMaxSize) {
364                 c->maxw = size.max_width;
365                 c->maxh = size.max_height;
366         }
367         else
368                 c->maxw = c->maxh = 0;
369         if(c->flags & PMinSize) {
370                 c->minw = size.min_width;
371                 c->minh = size.min_height;
372         }
373         else
374                 c->minw = c->minh = 0;
375         if(c->flags & PAspect) {
376                 c->minax = size.min_aspect.x;
377                 c->minay = size.min_aspect.y;
378                 c->maxax = size.max_aspect.x;
379                 c->maxay = size.max_aspect.y;
380         }
381         else
382                 c->minax = c->minay = c->maxax = c->maxay = 0;
383         c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
384                         && c->maxw == c->minw && c->maxh == c->minh);
385 }
386
387 void
388 updatetitle(Client *c) {
389         char **list = NULL;
390         int n;
391         XTextProperty name;
392
393         name.nitems = 0;
394         c->name[0] = 0;
395         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
396         if(!name.nitems)
397                 XGetWMName(dpy, c->win, &name);
398         if(!name.nitems)
399                 return;
400         if(name.encoding == XA_STRING)
401                 strncpy(c->name, (char *)name.value, sizeof c->name);
402         else {
403                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
404                 && n > 0 && *list)
405                 {
406                         strncpy(c->name, *list, sizeof c->name);
407                         XFreeStringList(list);
408                 }
409         }
410         XFree(name.value);
411 }
412
413 void
414 unmanage(Client *c) {
415         Client *nc;
416
417         /* The server grab construct avoids race conditions. */
418         XGrabServer(dpy);
419         XSetErrorHandler(xerrordummy);
420         detach(c);
421         detachstack(c);
422         if(sel == c) {
423                 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
424                 focus(nc);
425         }
426         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
427         setclientstate(c, WithdrawnState);
428         free(c->tags);
429         free(c);
430         XSync(dpy, False);
431         XSetErrorHandler(xerror);
432         XUngrabServer(dpy);
433         lt->arrange();
434 }
435
436 void
437 zoom(Arg *arg) {
438         unsigned int n;
439         Client *c;
440
441         if(!sel)
442                 return;
443         if(sel->swimming || (lt->arrange == swim)) {
444                 togglemax(sel);
445                 return;
446         }
447         for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
448                 n++;
449         if((c = sel) == nexttiled(clients))
450                 if(!(c = nexttiled(c->next)))
451                         return;
452         detach(c);
453         attach(c);
454         focus(c);
455         lt->arrange();
456 }