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