JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
using double-linked list in order to get correct prev focus handling
[dwm.git] / client.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5 #include "dwm.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
11
12 /* static functions */
13
14 static void
15 resizetitle(Client *c)
16 {
17         int i;
18
19         c->tw = 0;
20         for(i = 0; i < TLast; i++)
21                 if(c->tags[i])
22                         c->tw += textw(c->tags[i]);
23         c->tw += textw(c->name);
24         if(c->tw > c->w)
25                 c->tw = c->w + 2;
26         c->tx = c->x + c->w - c->tw + 2;
27         c->ty = c->y;
28         if(c->tags[tsel])
29                 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
30         else
31                 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
32
33 }
34
35 static int
36 xerrordummy(Display *dsply, XErrorEvent *ee)
37 {
38         return 0;
39 }
40
41 /* extern functions */
42
43 void
44 ban(Client *c)
45 {
46         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
47         XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
48 }
49
50 void
51 focus(Client *c)
52 {
53         Client *old = sel;
54         XEvent ev;
55
56         sel = c;
57         if(old && old != c)
58                 drawtitle(old);
59         drawtitle(c);
60         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
61         XSync(dpy, False);
62         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
63 }
64
65 void
66 focusnext(Arg *arg)
67 {
68         Client *c;
69    
70         if(!sel)
71                 return;
72
73         if(sel->ismax)
74                 togglemax(NULL);
75
76         if(!(c = getnext(sel->next, tsel)))
77                 c = getnext(clients, tsel);
78         if(c) {
79                 higher(c);
80                 focus(c);
81         }
82 }
83
84 void
85 focusprev(Arg *arg)
86 {
87         Client *c;
88
89         if(!sel)
90                 return;
91
92         if(sel->ismax)
93                 togglemax(NULL);
94
95         if(!(c = getprev(sel->prev))) {
96                 for(c = clients; c && c->next; c = c->next);
97                 c = getprev(c);
98         }
99         if(c) {
100                 higher(c);
101                 focus(c);
102         }
103 }
104
105 Client *
106 getclient(Window w)
107 {
108         Client *c;
109
110         for(c = clients; c; c = c->next)
111                 if(c->win == w)
112                         return c;
113         return NULL;
114 }
115
116 Client *
117 getctitle(Window w)
118 {
119         Client *c;
120
121         for(c = clients; c; c = c->next)
122                 if(c->title == w)
123                         return c;
124         return NULL;
125 }
126
127 void
128 gravitate(Client *c, Bool invert)
129 {
130         int dx = 0, dy = 0;
131
132         switch(c->grav) {
133         default:
134                 break;
135         case StaticGravity:
136         case NorthWestGravity:
137         case NorthGravity:
138         case NorthEastGravity:
139                 dy = c->border;
140                 break;
141         case EastGravity:
142         case CenterGravity:
143         case WestGravity:
144                 dy = -(c->h / 2) + c->border;
145                 break;
146         case SouthEastGravity:
147         case SouthGravity:
148         case SouthWestGravity:
149                 dy = -(c->h);
150                 break;
151         }
152
153         switch (c->grav) {
154         default:
155                 break;
156         case StaticGravity:
157         case NorthWestGravity:
158         case WestGravity:
159         case SouthWestGravity:
160                 dx = c->border;
161                 break;
162         case NorthGravity:
163         case CenterGravity:
164         case SouthGravity:
165                 dx = -(c->w / 2) + c->border;
166                 break;
167         case NorthEastGravity:
168         case EastGravity:
169         case SouthEastGravity:
170                 dx = -(c->w + c->border);
171                 break;
172         }
173
174         if(invert) {
175                 dx = -dx;
176                 dy = -dy;
177         }
178         c->x += dx;
179         c->y += dy;
180 }
181
182 void
183 higher(Client *c)
184 {
185         XRaiseWindow(dpy, c->win);
186         XRaiseWindow(dpy, c->title);
187 }
188
189 void
190 killclient(Arg *arg)
191 {
192         if(!sel)
193                 return;
194         if(sel->proto & WM_PROTOCOL_DELWIN)
195                 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
196         else
197                 XKillClient(dpy, sel->win);
198 }
199
200 void
201 lower(Client *c)
202 {
203         XLowerWindow(dpy, c->title);
204         XLowerWindow(dpy, c->win);
205 }
206
207 void
208 manage(Window w, XWindowAttributes *wa)
209 {
210         Client *c;
211         Window trans;
212         XSetWindowAttributes twa;
213
214         c = emallocz(sizeof(Client));
215         c->win = w;
216         c->x = c->tx = wa->x;
217         c->y = c->ty = wa->y;
218         c->w = c->tw = wa->width;
219         c->h = wa->height;
220         c->th = bh;
221
222         if(c->y < bh)
223                 c->y = c->ty = bh;
224
225         c->border = 1;
226         c->proto = getproto(c->win);
227         setsize(c);
228         XSelectInput(dpy, c->win,
229                 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
230         XGetTransientForHint(dpy, c->win, &trans);
231         twa.override_redirect = 1;
232         twa.background_pixmap = ParentRelative;
233         twa.event_mask = ExposureMask;
234
235         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
236                         0, DefaultDepth(dpy, screen), CopyFromParent,
237                         DefaultVisual(dpy, screen),
238                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
239
240         settags(c);
241
242         if(clients)
243                 clients->prev = c;
244         c->next = clients;
245         clients = c;
246
247         XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask,
248                         GrabModeAsync, GrabModeSync, None, None);
249         XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask,
250                         GrabModeAsync, GrabModeSync, None, None);
251         XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask,
252                         GrabModeAsync, GrabModeSync, None, None);
253
254         if(!c->isfloat)
255                 c->isfloat = trans || (c->maxw && c->minw &&
256                                 (c->maxw == c->minw) && (c->maxh == c->minh));
257
258
259         settitle(c);
260         arrange(NULL);
261
262         /* mapping the window now prevents flicker */
263         if(c->tags[tsel]) {
264                 XMapRaised(dpy, c->win);
265                 XMapRaised(dpy, c->title);
266                 focus(c);
267         }
268         else {
269                 XMapRaised(dpy, c->win);
270                 XMapRaised(dpy, c->title);
271
272         }
273 }
274
275 void
276 pop(Client *c)
277 {
278         Client **l;
279
280         for(l = &clients; *l && *l != c; l = &(*l)->next);
281         if(c->prev)
282                 c->prev->next = c->next;
283         if(c->next)
284                 c->next->prev = c->prev;
285         *l = c->next;
286
287         if(clients)
288                 clients->prev = c;
289         c->next = clients;
290         clients = c;
291         arrange(NULL);
292 }
293
294 void
295 resize(Client *c, Bool inc, Corner sticky)
296 {
297         int bottom = c->y + c->h;
298         int right = c->x + c->w;
299         XConfigureEvent e;
300
301         if(inc) {
302                 if(c->incw)
303                         c->w -= (c->w - c->basew) % c->incw;
304                 if(c->inch)
305                         c->h -= (c->h - c->baseh) % c->inch;
306         }
307         if(c->x > sw) /* might happen on restart */
308                 c->x = sw - c->w;
309         if(c->y > sh)
310                 c->y = sh - c->h;
311         if(c->minw && c->w < c->minw)
312                 c->w = c->minw;
313         if(c->minh && c->h < c->minh)
314                 c->h = c->minh;
315         if(c->maxw && c->w > c->maxw)
316                 c->w = c->maxw;
317         if(c->maxh && c->h > c->maxh)
318                 c->h = c->maxh;
319         if(sticky == TopRight || sticky == BotRight)
320                 c->x = right - c->w;
321         if(sticky == BotLeft || sticky == BotRight)
322                 c->y = bottom - c->h;
323
324         resizetitle(c);
325         XSetWindowBorderWidth(dpy, c->win, 1);
326         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
327
328         e.type = ConfigureNotify;
329         e.event = c->win;
330         e.window = c->win;
331         e.x = c->x;
332         e.y = c->y;
333         e.width = c->w;
334         e.height = c->h;
335         e.border_width = c->border;
336         e.above = None;
337         e.override_redirect = False;
338         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
339         XSync(dpy, False);
340 }
341
342 void
343 setsize(Client *c)
344 {
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 & PWinGravity)
376                 c->grav = size.win_gravity;
377         else
378                 c->grav = NorthWestGravity;
379 }
380
381 void
382 settitle(Client *c)
383 {
384         char **list = NULL;
385         int n;
386         XTextProperty name;
387
388         name.nitems = 0;
389         c->name[0] = 0;
390         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
391         if(!name.nitems)
392                 XGetWMName(dpy, c->win, &name);
393         if(!name.nitems)
394                 return;
395         if(name.encoding == XA_STRING)
396                 strncpy(c->name, (char *)name.value, sizeof(c->name));
397         else {
398                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
399                                 && n > 0 && *list)
400                 {
401                         strncpy(c->name, *list, sizeof(c->name));
402                         XFreeStringList(list);
403                 }
404         }
405         XFree(name.value);
406         resizetitle(c);
407 }
408
409 void
410 togglemax(Arg *arg)
411 {
412         int ox, oy, ow, oh;
413         XEvent ev;
414
415         if(!sel)
416                 return;
417
418         if((sel->ismax = !sel->ismax)) {
419                 ox = sel->x;
420                 oy = sel->y;
421                 ow = sel->w;
422                 oh = sel->h;
423                 sel->x = sx;
424                 sel->y = sy + bh;
425                 sel->w = sw - 2 * sel->border;
426                 sel->h = sh - 2 * sel->border - bh;
427
428                 higher(sel);
429                 resize(sel, False, TopLeft);
430
431                 sel->x = ox;
432                 sel->y = oy;
433                 sel->w = ow;
434                 sel->h = oh;
435         }
436         else
437                 resize(sel, False, TopLeft);
438         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
439 }
440
441 void
442 unmanage(Client *c)
443 {
444         Client **l;
445
446         XGrabServer(dpy);
447         XSetErrorHandler(xerrordummy);
448
449         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
450         XDestroyWindow(dpy, c->title);
451
452         for(l = &clients; *l && *l != c; l = &(*l)->next);
453         if(c->prev)
454                 c->prev->next = c->next;
455         if(c->next)
456                 c->next->prev = c->prev;
457         *l = c->next;
458         if(sel == c) {
459                 sel = getnext(c->next, tsel);
460                 if(!sel)
461                         sel = getprev(c->prev);
462                 if(!sel)
463                         sel = clients;
464         }
465         free(c);
466
467         XSync(dpy, False);
468         XSetErrorHandler(xerror);
469         XUngrabServer(dpy);
470         arrange(NULL);
471         if(sel)
472                 focus(sel);
473 }
474
475 void
476 zoom(Arg *arg)
477 {
478         Client *c;
479
480         if(!sel)
481                 return;
482
483         if(sel == getnext(clients, tsel) && sel->next)  {
484                 if((c = getnext(sel->next, tsel)))
485                         sel = c;
486         }
487
488         pop(sel);
489         focus(sel);
490 }