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