JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new stuff
[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)))
77                 c = getnext(clients);
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 & PROTODELWIN)
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         c->border = 1;
223         setsize(c);
224
225         if(c->h != sh && c->y < bh)
226                 c->y = c->ty = bh;
227
228         c->proto = getproto(c->win);
229         XSelectInput(dpy, c->win,
230                 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
231         XGetTransientForHint(dpy, c->win, &trans);
232         twa.override_redirect = 1;
233         twa.background_pixmap = ParentRelative;
234         twa.event_mask = ExposureMask | EnterWindowMask;
235
236         c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
237                         0, DefaultDepth(dpy, screen), CopyFromParent,
238                         DefaultVisual(dpy, screen),
239                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
240
241         if(clients)
242                 clients->prev = c;
243         c->next = clients;
244         clients = c;
245
246         XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
247                         GrabModeAsync, GrabModeSync, None, None);
248         XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
249                         GrabModeAsync, GrabModeSync, None, None);
250         XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
251                         GrabModeAsync, GrabModeSync, None, None);
252
253         settags(c);
254         if(!c->isfloat)
255                 c->isfloat = trans
256                         || (c->maxw && c->minw &&
257                                 c->maxw == c->minw && c->maxh == c->minh)
258                         || (c->w == sw && c->h == sh);
259         settitle(c);
260         arrange(NULL);
261
262         /* mapping the window now prevents flicker */
263         XMapRaised(dpy, c->win);
264         XMapRaised(dpy, c->title);
265         if(c->tags[tsel])
266                 focus(c);
267 }
268
269 void
270 resize(Client *c, Bool sizehints, Corner sticky)
271 {
272         int bottom = c->y + c->h;
273         int right = c->x + c->w;
274         XConfigureEvent e;
275         XWindowChanges wc;
276
277         if(sizehints) {
278                 if(c->incw)
279                         c->w -= (c->w - c->basew) % c->incw;
280                 if(c->inch)
281                         c->h -= (c->h - c->baseh) % c->inch;
282                 if(c->minw && c->w < c->minw)
283                         c->w = c->minw;
284                 if(c->minh && c->h < c->minh)
285                         c->h = c->minh;
286                 if(c->maxw && c->w > c->maxw)
287                         c->w = c->maxw;
288                 if(c->maxh && c->h > c->maxh)
289                         c->h = c->maxh;
290         }
291         if(c->x > right) /* might happen on restart */
292                 c->x = right - c->w;
293         if(c->y > bottom)
294                 c->y = bottom - c->h;
295         if(sticky == TopRight || sticky == BotRight)
296                 c->x = right - c->w;
297         if(sticky == BotLeft || sticky == BotRight)
298                 c->y = bottom - c->h;
299
300         resizetitle(c);
301         wc.x = c->x;
302         wc.y = c->y;
303         wc.width = c->w;
304         wc.height = c->h;
305         wc.border_width = 1;
306         XConfigureWindow(dpy, c->win,
307                         CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
308
309         e.type = ConfigureNotify;
310         e.event = c->win;
311         e.window = c->win;
312         e.x = c->x;
313         e.y = c->y;
314         e.width = c->w;
315         e.height = c->h;
316         e.border_width = c->border;
317         e.above = None;
318         e.override_redirect = False;
319         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
320         XSync(dpy, False);
321 }
322
323 void
324 setsize(Client *c)
325 {
326         long msize;
327         XSizeHints size;
328
329         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
330                 size.flags = PSize;
331         c->flags = size.flags;
332         if(c->flags & PBaseSize) {
333                 c->basew = size.base_width;
334                 c->baseh = size.base_height;
335         }
336         else
337                 c->basew = c->baseh = 0;
338         if(c->flags & PResizeInc) {
339                 c->incw = size.width_inc;
340                 c->inch = size.height_inc;
341         }
342         else
343                 c->incw = c->inch = 0;
344         if(c->flags & PMaxSize) {
345                 c->maxw = size.max_width;
346                 c->maxh = size.max_height;
347         }
348         else
349                 c->maxw = c->maxh = 0;
350         if(c->flags & PMinSize) {
351                 c->minw = size.min_width;
352                 c->minh = size.min_height;
353         }
354         else
355                 c->minw = c->minh = 0;
356         if(c->flags & PWinGravity)
357                 c->grav = size.win_gravity;
358         else
359                 c->grav = NorthWestGravity;
360 }
361
362 void
363 settitle(Client *c)
364 {
365         char **list = NULL;
366         int n;
367         XTextProperty name;
368
369         name.nitems = 0;
370         c->name[0] = 0;
371         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
372         if(!name.nitems)
373                 XGetWMName(dpy, c->win, &name);
374         if(!name.nitems)
375                 return;
376         if(name.encoding == XA_STRING)
377                 strncpy(c->name, (char *)name.value, sizeof(c->name));
378         else {
379                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
380                                 && n > 0 && *list)
381                 {
382                         strncpy(c->name, *list, sizeof(c->name));
383                         XFreeStringList(list);
384                 }
385         }
386         XFree(name.value);
387         resizetitle(c);
388 }
389
390 void
391 togglemax(Arg *arg)
392 {
393         int ox, oy, ow, oh;
394         XEvent ev;
395
396         if(!sel)
397                 return;
398
399         if((sel->ismax = !sel->ismax)) {
400                 ox = sel->x;
401                 oy = sel->y;
402                 ow = sel->w;
403                 oh = sel->h;
404                 sel->x = sx;
405                 sel->y = sy + bh;
406                 sel->w = sw - 2 * sel->border;
407                 sel->h = sh - 2 * sel->border - bh;
408
409                 higher(sel);
410                 resize(sel, False, TopLeft);
411
412                 sel->x = ox;
413                 sel->y = oy;
414                 sel->w = ow;
415                 sel->h = oh;
416         }
417         else
418                 resize(sel, False, TopLeft);
419         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
420 }
421
422 void
423 unmanage(Client *c)
424 {
425         XGrabServer(dpy);
426         XSetErrorHandler(xerrordummy);
427
428         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
429         XDestroyWindow(dpy, c->title);
430
431         if(c->prev)
432                 c->prev->next = c->next;
433         if(c->next)
434                 c->next->prev = c->prev;
435         if(c == clients)
436                 clients = c->next;
437         if(sel == c) {
438                 sel = getnext(c->next);
439                 if(!sel)
440                         sel = getprev(c->prev);
441                 if(!sel)
442                         sel = clients;
443         }
444         free(c);
445
446         XSync(dpy, False);
447         XSetErrorHandler(xerror);
448         XUngrabServer(dpy);
449         arrange(NULL);
450         if(sel)
451                 focus(sel);
452 }
453
454 void
455 zoom(Arg *arg)
456 {
457         Client *c;
458
459         if(!sel)
460                 return;
461
462         if(sel == getnext(clients) && sel->next)  {
463                 if((c = getnext(sel->next)))
464                         sel = c;
465         }
466
467         /* pop */
468         if(sel->prev)
469                 sel->prev->next = sel->next;
470         if(sel->next)
471                 sel->next->prev = sel->prev;
472         sel->prev = NULL;
473         if(clients)
474                 clients->prev = sel;
475         sel->next = clients;
476         clients = sel;
477         arrange(NULL);
478         focus(sel);
479 }