JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
focus on view change as well
[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
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
11
12 #include "dwm.h"
13
14 void (*arrange)(Arg *) = tiling;
15
16 static Rule rule[] = {
17         /* class                        instance        tags                                            floating */
18         { "Firefox-bin",        "Gecko",        { [Twww] = "www" },                     False },
19 };
20
21 static Client *
22 next(Client *c)
23 {
24         for(; c && !c->tags[tsel]; c = c->next);
25         return c;
26 }
27
28 void
29 zoom(Arg *arg)
30 {
31         Client **l, *old;
32
33         if(!(old = sel))
34                 return;
35
36         for(l = &clients; *l && *l != sel; l = &(*l)->next);
37         *l = sel->next;
38
39         old->next = clients; /* pop */
40         clients = old;
41         sel = old;
42         arrange(NULL);
43         focus(sel);
44 }
45
46 void
47 max(Arg *arg)
48 {
49         if(!sel)
50                 return;
51         sel->x = sx;
52         sel->y = sy + bh;
53         sel->w = sw - 2 * sel->border;
54         sel->h = sh - 2 * sel->border - bh;
55         craise(sel);
56         resize(sel, False);
57         discard_events(EnterWindowMask);
58 }
59
60 void
61 view(Arg *arg)
62 {
63         Client *c;
64
65         tsel = arg->i;
66         arrange(NULL);
67
68         if((c = next(clients)))
69                 focus(c);
70
71         for(c = clients; c; c = next(c->next))
72                 draw_client(c);
73         draw_bar();
74 }
75
76 void
77 tappend(Arg *arg)
78 {
79         if(!sel)
80                 return;
81
82         sel->tags[arg->i] = tags[arg->i];
83         arrange(NULL);
84 }
85
86 void
87 ttrunc(Arg *arg)
88 {
89         int i;
90         if(!sel)
91                 return;
92
93         for(i = 0; i < TLast; i++)
94                 sel->tags[i] = NULL;
95         tappend(arg);
96 }
97
98 static void
99 ban_client(Client *c)
100 {
101         XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
102         XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
103 }
104
105 void
106 floating(Arg *arg)
107 {
108         Client *c;
109
110         arrange = floating;
111         for(c = clients; c; c = c->next) {
112                 if(c->tags[tsel])
113                         resize(c, True);
114                 else
115                         ban_client(c);
116         }
117         if(sel && !sel->tags[tsel]) {
118                 if((sel = next(clients))) {
119                         craise(sel);
120                         focus(sel);
121                 }
122         }
123         discard_events(EnterWindowMask);
124 }
125
126 void
127 tiling(Arg *arg)
128 {
129         Client *c;
130         int n, i, w, h;
131
132         w = sw - mw;
133         arrange = tiling;
134         for(n = 0, c = clients; c; c = c->next)
135                 if(c->tags[tsel] && !c->floating)
136                         n++;
137
138         if(n > 1)
139                 h = (sh - bh) / (n - 1);
140         else
141                 h = sh - bh;
142
143         for(i = 0, c = clients; c; c = c->next) {
144                 if(c->tags[tsel]) {
145                         if(c->floating) {
146                                 craise(c);
147                                 resize(c, True);
148                                 continue;
149                         }
150                         if(n == 1) {
151                                 c->x = sx;
152                                 c->y = sy + bh;
153                                 c->w = sw - 2 * c->border;
154                                 c->h = sh - 2 * c->border - bh;
155                         }
156                         else if(i == 0) {
157                                 c->x = sx;
158                                 c->y = sy + bh;
159                                 c->w = mw - 2 * c->border;
160                                 c->h = sh - 2 * c->border - bh;
161                         }
162                         else {
163                                 c->x = sx + mw;
164                                 c->y = sy + (i - 1) * h + bh;
165                                 c->w = w - 2 * c->border;
166                                 c->h = h - 2 * c->border;
167                         }
168                         resize(c, False);
169                         i++;
170                 }
171                 else
172                         ban_client(c);
173         }
174         if(sel && !sel->tags[tsel]) {
175                 if((sel = next(clients))) {
176                         craise(sel);
177                         focus(sel);
178                 }
179         }
180         discard_events(EnterWindowMask);
181 }
182
183 void
184 prevc(Arg *arg)
185 {
186         Client *c;
187
188         if(!sel)
189                 return;
190
191         if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
192                 craise(c);
193                 focus(c);
194         }
195 }
196
197 void
198 nextc(Arg *arg)
199 {
200         Client *c;
201    
202         if(!sel)
203                 return;
204
205         if(!(c = next(sel->next)))
206                 c = next(clients);
207         if(c) {
208                 craise(c);
209                 c->revert = sel;
210                 focus(c);
211         }
212 }
213
214 void
215 ckill(Arg *arg)
216 {
217         if(!sel)
218                 return;
219         if(sel->proto & WM_PROTOCOL_DELWIN)
220                 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
221         else
222                 XKillClient(dpy, sel->win);
223 }
224
225 static void
226 resize_title(Client *c)
227 {
228         int i;
229
230         c->tw = 0;
231         for(i = 0; i < TLast; i++)
232                 if(c->tags[i])
233                         c->tw += textw(c->tags[i]) + dc.font.height;
234         c->tw += textw(c->name) + dc.font.height;
235         if(c->tw > c->w)
236                 c->tw = c->w + 2;
237         c->tx = c->x + c->w - c->tw + 2;
238         c->ty = c->y;
239         XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
240 }
241
242 void
243 update_name(Client *c)
244 {
245         XTextProperty name;
246         int n;
247         char **list = NULL;
248
249         name.nitems = 0;
250         c->name[0] = 0;
251         XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
252         if(!name.nitems)
253                 XGetWMName(dpy, c->win, &name);
254         if(!name.nitems)
255                 return;
256         if(name.encoding == XA_STRING)
257                 strncpy(c->name, (char *)name.value, sizeof(c->name));
258         else {
259                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
260                                 && n > 0 && *list)
261                 {
262                         strncpy(c->name, *list, sizeof(c->name));
263                         XFreeStringList(list);
264                 }
265         }
266         XFree(name.value);
267         resize_title(c);
268 }
269
270 void
271 update_size(Client *c)
272 {
273         XSizeHints size;
274         long msize;
275         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
276                 size.flags = PSize;
277         c->flags = size.flags;
278         if(c->flags & PBaseSize) {
279                 c->basew = size.base_width;
280                 c->baseh = size.base_height;
281         }
282         else
283                 c->basew = c->baseh = 0;
284         if(c->flags & PResizeInc) {
285                 c->incw = size.width_inc;
286                 c->inch = size.height_inc;
287         }
288         else
289                 c->incw = c->inch = 0;
290         if(c->flags & PMaxSize) {
291                 c->maxw = size.max_width;
292                 c->maxh = size.max_height;
293         }
294         else
295                 c->maxw = c->maxh = 0;
296         if(c->flags & PMinSize) {
297                 c->minw = size.min_width;
298                 c->minh = size.min_height;
299         }
300         else
301                 c->minw = c->minh = 0;
302         if(c->flags & PWinGravity)
303                 c->grav = size.win_gravity;
304         else
305                 c->grav = NorthWestGravity;
306 }
307
308 void
309 craise(Client *c)
310 {
311         XRaiseWindow(dpy, c->win);
312         XRaiseWindow(dpy, c->title);
313 }
314
315 void
316 lower(Client *c)
317 {
318         XLowerWindow(dpy, c->title);
319         XLowerWindow(dpy, c->win);
320 }
321
322 void
323 focus(Client *c)
324 {
325         Client *old = sel;
326
327         sel = c;
328         if(old && old != c)
329                 draw_client(old);
330         draw_client(c);
331         XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
332         XFlush(dpy);
333         discard_events(EnterWindowMask);
334 }
335
336 static void
337 init_tags(Client *c)
338 {
339         XClassHint ch;
340         static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
341         unsigned int i, j;
342         Bool matched = False;
343
344         if(!len) {
345                 c->tags[tsel] = tags[tsel];
346                 return;
347         }
348
349         if(XGetClassHint(dpy, c->win, &ch)) {
350                 if(ch.res_class && ch.res_name) {
351                         for(i = 0; i < len; i++)
352                                 if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
353                                         && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
354                                 {
355                                         for(j = 0; j < TLast; j++)
356                                                 c->tags[j] = rule[i].tags[j];
357                                         c->floating = rule[i].floating;
358                                         matched = True;
359                                         break;
360                                 }
361                 }
362                 if(ch.res_class)
363                         XFree(ch.res_class);
364                 if(ch.res_name)
365                         XFree(ch.res_name);
366         }
367
368         if(!matched)
369                 c->tags[tsel] = tags[tsel];
370 }
371
372 void
373 manage(Window w, XWindowAttributes *wa)
374 {
375         Client *c, **l;
376         XSetWindowAttributes twa;
377         Window trans;
378
379         c = emallocz(sizeof(Client));
380         c->win = w;
381         c->tx = c->x = wa->x;
382         c->ty = c->y = wa->y;
383         if(c->y < bh)
384                 c->ty = c->y += bh;
385         c->tw = c->w = wa->width;
386         c->h = wa->height;
387         c->th = bh;
388         c->border = 1;
389         c->proto = win_proto(c->win);
390         update_size(c);
391         XSelectInput(dpy, c->win,
392                         StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
393         XGetTransientForHint(dpy, c->win, &trans);
394         twa.override_redirect = 1;
395         twa.background_pixmap = ParentRelative;
396         twa.event_mask = ExposureMask;
397
398         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
399                         0, DefaultDepth(dpy, screen), CopyFromParent,
400                         DefaultVisual(dpy, screen),
401                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
402
403         update_name(c);
404         init_tags(c);
405
406         for(l = &clients; *l; l = &(*l)->next);
407         c->next = *l; /* *l == nil */
408         *l = c;
409
410         XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
411                         GrabModeAsync, GrabModeSync, None, None);
412         XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
413                         GrabModeAsync, GrabModeSync, None, None);
414         XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
415                         GrabModeAsync, GrabModeSync, None, None);
416
417         if(!c->floating)
418                 c->floating = trans
419                         || ((c->maxw == c->minw) && (c->maxh == c->minh));
420
421         arrange(NULL);
422         /* mapping the window now prevents flicker */
423         if(c->tags[tsel]) {
424                 XMapRaised(dpy, c->win);
425                 XMapRaised(dpy, c->title);
426                 focus(c);
427         }
428         else {
429                 ban_client(c);
430                 XMapRaised(dpy, c->win);
431                 XMapRaised(dpy, c->title);
432         }
433 }
434
435 void
436 gravitate(Client *c, Bool invert)
437 {
438         int dx = 0, dy = 0;
439
440         switch(c->grav) {
441         case StaticGravity:
442         case NorthWestGravity:
443         case NorthGravity:
444         case NorthEastGravity:
445                 dy = c->border;
446                 break;
447         case EastGravity:
448         case CenterGravity:
449         case WestGravity:
450                 dy = -(c->h / 2) + c->border;
451                 break;
452         case SouthEastGravity:
453         case SouthGravity:
454         case SouthWestGravity:
455                 dy = -c->h;
456                 break;
457         default:
458                 break;
459         }
460
461         switch (c->grav) {
462         case StaticGravity:
463         case NorthWestGravity:
464         case WestGravity:
465         case SouthWestGravity:
466                 dx = c->border;
467                 break;
468         case NorthGravity:
469         case CenterGravity:
470         case SouthGravity:
471                 dx = -(c->w / 2) + c->border;
472                 break;
473         case NorthEastGravity:
474         case EastGravity:
475         case SouthEastGravity:
476                 dx = -(c->w + c->border);
477                 break;
478         default:
479                 break;
480         }
481
482         if(invert) {
483                 dx = -dx;
484                 dy = -dy;
485         }
486         c->x += dx;
487         c->y += dy;
488 }
489
490
491 void
492 resize(Client *c, Bool inc)
493 {
494         XConfigureEvent e;
495
496         if(inc) {
497                 if(c->incw)
498                         c->w -= (c->w - c->basew) % c->incw;
499                 if(c->inch)
500                         c->h -= (c->h - c->baseh) % c->inch;
501         }
502         if(c->minw && c->w < c->minw)
503                 c->w = c->minw;
504         if(c->minh && c->h < c->minh)
505                 c->h = c->minh;
506         if(c->maxw && c->w > c->maxw)
507                 c->w = c->maxw;
508         if(c->maxh && c->h > c->maxh)
509                 c->h = c->maxh;
510         resize_title(c);
511         XSetWindowBorderWidth(dpy, c->win, 1);
512         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
513         e.type = ConfigureNotify;
514         e.event = c->win;
515         e.window = c->win;
516         e.x = c->x;
517         e.y = c->y;
518         e.width = c->w;
519         e.height = c->h;
520         e.border_width = c->border;
521         e.above = None;
522         e.override_redirect = False;
523         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
524         XFlush(dpy);
525 }
526
527 static int
528 dummy_error_handler(Display *dsply, XErrorEvent *err)
529 {
530         return 0;
531 }
532
533 void
534 unmanage(Client *c)
535 {
536         Client **l;
537
538         XGrabServer(dpy);
539         XSetErrorHandler(dummy_error_handler);
540
541         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
542         XDestroyWindow(dpy, c->title);
543
544         for(l = &clients; *l && *l != c; l = &(*l)->next);
545         *l = c->next;
546         for(l = &clients; *l; l = &(*l)->next)
547                 if((*l)->revert == c)
548                         (*l)->revert = NULL;
549         if(sel == c)
550                 sel = sel->revert ? sel->revert : clients;
551
552         free(c);
553
554         XFlush(dpy);
555         XSetErrorHandler(error_handler);
556         XUngrabServer(dpy);
557         arrange(NULL);
558         if(sel)
559                 focus(sel);
560 }
561
562 Client *
563 gettitle(Window w)
564 {
565         Client *c;
566         for(c = clients; c; c = c->next)
567                 if(c->title == w)
568                         return c;
569         return NULL;
570 }
571
572 Client *
573 getclient(Window w)
574 {
575         Client *c;
576         for(c = clients; c; c = c->next)
577                 if(c->win == w)
578                         return c;
579         return NULL;
580 }
581
582 void
583 draw_client(Client *c)
584 {
585         int i;
586         if(c == sel) {
587                 draw_bar();
588                 XUnmapWindow(dpy, c->title);
589                 XSetWindowBorder(dpy, c->win, dc.fg);
590                 return;
591         }
592
593         XSetWindowBorder(dpy, c->win, dc.bg);
594         XMapWindow(dpy, c->title);
595
596         dc.x = dc.y = 0;
597
598         dc.w = 0;
599         for(i = 0; i < TLast; i++) {
600                 if(c->tags[i]) {
601                         dc.x += dc.w;
602                         dc.w = textw(c->tags[i]) + dc.font.height;
603                         drawtext(c->tags[i], True);
604                 }
605         }
606         dc.x += dc.w;
607         dc.w = textw(c->name) + dc.font.height;
608         drawtext(c->name, True);
609         XCopyArea(dpy, dc.drawable, c->title, dc.gc,
610                         0, 0, c->tw, c->th, 0, 0);
611         XFlush(dpy);
612 }