JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
45acf2e619acec3ae5c89adf4505513c65e52f94
[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 & 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         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
276         if(sizehints) {
277                 if(c->incw)
278                         c->w -= (c->w - c->basew) % c->incw;
279                 if(c->inch)
280                         c->h -= (c->h - c->baseh) % c->inch;
281                 if(c->minw && c->w < c->minw)
282                         c->w = c->minw;
283                 if(c->minh && c->h < c->minh)
284                         c->h = c->minh;
285                 if(c->maxw && c->w > c->maxw)
286                         c->w = c->maxw;
287                 if(c->maxh && c->h > c->maxh)
288                         c->h = c->maxh;
289         }
290         if(c->x > sw) /* might happen on restart */
291                 c->x = sw - c->w;
292         if(c->y > sh)
293                 c->y = sh - c->h;
294         if(sticky == TopRight || sticky == BotRight)
295                 c->x = right - c->w;
296         if(sticky == BotLeft || sticky == BotRight)
297                 c->y = bottom - c->h;
298
299         resizetitle(c);
300         XSetWindowBorderWidth(dpy, c->win, 1);
301         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
302
303         e.type = ConfigureNotify;
304         e.event = c->win;
305         e.window = c->win;
306         e.x = c->x;
307         e.y = c->y;
308         e.width = c->w;
309         e.height = c->h;
310         e.border_width = c->border;
311         e.above = None;
312         e.override_redirect = False;
313         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
314         XSync(dpy, False);
315 }
316
317 void
318 setsize(Client *c)
319 {
320         long msize;
321         XSizeHints size;
322
323         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
324                 size.flags = PSize;
325         c->flags = size.flags;
326         if(c->flags & PBaseSize) {
327                 c->basew = size.base_width;
328                 c->baseh = size.base_height;
329         }
330         else
331                 c->basew = c->baseh = 0;
332         if(c->flags & PResizeInc) {
333                 c->incw = size.width_inc;
334                 c->inch = size.height_inc;
335         }
336         else
337                 c->incw = c->inch = 0;
338         if(c->flags & PMaxSize) {
339                 c->maxw = size.max_width;
340                 c->maxh = size.max_height;
341         }
342         else
343                 c->maxw = c->maxh = 0;
344         if(c->flags & PMinSize) {
345                 c->minw = size.min_width;
346                 c->minh = size.min_height;
347         }
348         else
349                 c->minw = c->minh = 0;
350         if(c->flags & PWinGravity)
351                 c->grav = size.win_gravity;
352         else
353                 c->grav = NorthWestGravity;
354 }
355
356 void
357 settitle(Client *c)
358 {
359         char **list = NULL;
360         int n;
361         XTextProperty name;
362
363         name.nitems = 0;
364         c->name[0] = 0;
365         XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
366         if(!name.nitems)
367                 XGetWMName(dpy, c->win, &name);
368         if(!name.nitems)
369                 return;
370         if(name.encoding == XA_STRING)
371                 strncpy(c->name, (char *)name.value, sizeof(c->name));
372         else {
373                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
374                                 && n > 0 && *list)
375                 {
376                         strncpy(c->name, *list, sizeof(c->name));
377                         XFreeStringList(list);
378                 }
379         }
380         XFree(name.value);
381         resizetitle(c);
382 }
383
384 void
385 togglemax(Arg *arg)
386 {
387         int ox, oy, ow, oh;
388         XEvent ev;
389
390         if(!sel)
391                 return;
392
393         if((sel->ismax = !sel->ismax)) {
394                 ox = sel->x;
395                 oy = sel->y;
396                 ow = sel->w;
397                 oh = sel->h;
398                 sel->x = sx;
399                 sel->y = sy + bh;
400                 sel->w = sw - 2 * sel->border;
401                 sel->h = sh - 2 * sel->border - bh;
402
403                 higher(sel);
404                 resize(sel, False, TopLeft);
405
406                 sel->x = ox;
407                 sel->y = oy;
408                 sel->w = ow;
409                 sel->h = oh;
410         }
411         else
412                 resize(sel, False, TopLeft);
413         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
414 }
415
416 void
417 unmanage(Client *c)
418 {
419         Client **l;
420
421         XGrabServer(dpy);
422         XSetErrorHandler(xerrordummy);
423
424         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
425         XDestroyWindow(dpy, c->title);
426
427         for(l = &clients; *l && *l != c; l = &(*l)->next);
428         if(c->prev)
429                 c->prev->next = c->next;
430         if(c->next)
431                 c->next->prev = c->prev;
432         *l = c->next;
433         if(sel == c) {
434                 sel = getnext(c->next);
435                 if(!sel)
436                         sel = getprev(c->prev);
437                 if(!sel)
438                         sel = clients;
439         }
440         free(c);
441
442         XSync(dpy, False);
443         XSetErrorHandler(xerror);
444         XUngrabServer(dpy);
445         arrange(NULL);
446         if(sel)
447                 focus(sel);
448 }
449
450 void
451 zoom(Arg *arg)
452 {
453         Client *c, **l;
454
455         if(!sel)
456                 return;
457
458         if(sel == getnext(clients) && sel->next)  {
459                 if((c = getnext(sel->next)))
460                         sel = c;
461         }
462
463         /* pop */
464         for(l = &clients; *l && *l != sel; l = &(*l)->next);
465         if(sel->prev)
466                 sel->prev->next = sel->next;
467         if(sel->next)
468                 sel->next->prev = sel->prev;
469         *l = sel->next;
470
471         sel->prev = NULL;
472         if(clients)
473                 clients->prev = sel;
474         sel->next = clients;
475         clients = sel;
476         arrange(NULL);
477         focus(sel);
478 }