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