aewl

view client.c @ 67:a0272d97ec7c

fixed crash on zoom if only 1 client exists
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 18:11:36 +0200
parents 50450aa24a46
children 7681ef838201
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xutil.h>
12 #include "dwm.h"
14 void (*arrange)(Arg *) = tiling;
16 static Rule rule[] = {
17 /* class instance tags floating */
18 { "Firefox-bin", "Gecko", { [Twww] = "www" }, False },
19 };
21 static Client *
22 next(Client *c)
23 {
24 for(; c && !c->tags[tsel]; c = c->next);
25 return c;
26 }
28 void
29 zoom(Arg *arg)
30 {
31 Client **l, *c;
33 if(!sel)
34 return;
36 if(sel == next(clients) && sel->next) {
37 if((c = next(sel->next)))
38 sel = c;
39 }
41 for(l = &clients; *l && *l != sel; l = &(*l)->next);
42 *l = sel->next;
44 sel->next = clients; /* pop */
45 clients = sel;
46 arrange(NULL);
47 focus(sel);
48 }
50 void
51 max(Arg *arg)
52 {
53 if(!sel)
54 return;
55 sel->x = sx;
56 sel->y = sy + bh;
57 sel->w = sw - 2 * sel->border;
58 sel->h = sh - 2 * sel->border - bh;
59 craise(sel);
60 resize(sel, False);
61 }
63 void
64 view(Arg *arg)
65 {
66 Client *c;
68 tsel = arg->i;
69 arrange(NULL);
71 for(c = clients; c; c = next(c->next))
72 draw_client(c);
73 draw_bar();
74 }
76 void
77 tappend(Arg *arg)
78 {
79 if(!sel)
80 return;
82 sel->tags[arg->i] = tags[arg->i];
83 arrange(NULL);
84 }
86 void
87 ttrunc(Arg *arg)
88 {
89 int i;
90 if(!sel)
91 return;
93 for(i = 0; i < TLast; i++)
94 sel->tags[i] = NULL;
95 tappend(arg);
96 }
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 }
105 void
106 floating(Arg *arg)
107 {
108 Client *c;
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 }
125 void
126 tiling(Arg *arg)
127 {
128 Client *c;
129 int n, i, w, h;
131 w = sw - mw;
132 arrange = tiling;
133 for(n = 0, c = clients; c; c = c->next)
134 if(c->tags[tsel] && !c->floating)
135 n++;
137 if(n > 1)
138 h = (sh - bh) / (n - 1);
139 else
140 h = sh - bh;
142 for(i = 0, c = clients; c; c = c->next) {
143 if(c->tags[tsel]) {
144 if(c->floating) {
145 craise(c);
146 resize(c, True);
147 continue;
148 }
149 if(n == 1) {
150 c->x = sx;
151 c->y = sy + bh;
152 c->w = sw - 2 * c->border;
153 c->h = sh - 2 * c->border - bh;
154 }
155 else if(i == 0) {
156 c->x = sx;
157 c->y = sy + bh;
158 c->w = mw - 2 * c->border;
159 c->h = sh - 2 * c->border - bh;
160 }
161 else {
162 c->x = sx + mw;
163 c->y = sy + (i - 1) * h + bh;
164 c->w = w - 2 * c->border;
165 c->h = h - 2 * c->border;
166 }
167 resize(c, False);
168 i++;
169 }
170 else
171 ban_client(c);
172 }
173 if(!sel || (sel && !sel->tags[tsel])) {
174 if((sel = next(clients))) {
175 craise(sel);
176 focus(sel);
177 }
178 }
179 }
181 void
182 prevc(Arg *arg)
183 {
184 Client *c;
186 if(!sel)
187 return;
189 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
190 craise(c);
191 focus(c);
192 }
193 }
195 void
196 nextc(Arg *arg)
197 {
198 Client *c;
200 if(!sel)
201 return;
203 if(!(c = next(sel->next)))
204 c = next(clients);
205 if(c) {
206 craise(c);
207 c->revert = sel;
208 focus(c);
209 }
210 }
212 void
213 ckill(Arg *arg)
214 {
215 if(!sel)
216 return;
217 if(sel->proto & WM_PROTOCOL_DELWIN)
218 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
219 else
220 XKillClient(dpy, sel->win);
221 }
223 static void
224 resize_title(Client *c)
225 {
226 int i;
228 c->tw = 0;
229 for(i = 0; i < TLast; i++)
230 if(c->tags[i])
231 c->tw += textw(c->tags[i]) + dc.font.height;
232 c->tw += textw(c->name) + dc.font.height;
233 if(c->tw > c->w)
234 c->tw = c->w + 2;
235 c->tx = c->x + c->w - c->tw + 2;
236 c->ty = c->y;
237 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
238 }
240 void
241 update_name(Client *c)
242 {
243 XTextProperty name;
244 int n;
245 char **list = NULL;
247 name.nitems = 0;
248 c->name[0] = 0;
249 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
250 if(!name.nitems)
251 XGetWMName(dpy, c->win, &name);
252 if(!name.nitems)
253 return;
254 if(name.encoding == XA_STRING)
255 strncpy(c->name, (char *)name.value, sizeof(c->name));
256 else {
257 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
258 && n > 0 && *list)
259 {
260 strncpy(c->name, *list, sizeof(c->name));
261 XFreeStringList(list);
262 }
263 }
264 XFree(name.value);
265 resize_title(c);
266 }
268 void
269 update_size(Client *c)
270 {
271 XSizeHints size;
272 long msize;
273 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
274 size.flags = PSize;
275 c->flags = size.flags;
276 if(c->flags & PBaseSize) {
277 c->basew = size.base_width;
278 c->baseh = size.base_height;
279 }
280 else
281 c->basew = c->baseh = 0;
282 if(c->flags & PResizeInc) {
283 c->incw = size.width_inc;
284 c->inch = size.height_inc;
285 }
286 else
287 c->incw = c->inch = 0;
288 if(c->flags & PMaxSize) {
289 c->maxw = size.max_width;
290 c->maxh = size.max_height;
291 }
292 else
293 c->maxw = c->maxh = 0;
294 if(c->flags & PMinSize) {
295 c->minw = size.min_width;
296 c->minh = size.min_height;
297 }
298 else
299 c->minw = c->minh = 0;
300 if(c->flags & PWinGravity)
301 c->grav = size.win_gravity;
302 else
303 c->grav = NorthWestGravity;
304 }
306 void
307 craise(Client *c)
308 {
309 XRaiseWindow(dpy, c->win);
310 XRaiseWindow(dpy, c->title);
311 }
313 void
314 lower(Client *c)
315 {
316 XLowerWindow(dpy, c->title);
317 XLowerWindow(dpy, c->win);
318 }
320 void
321 focus(Client *c)
322 {
323 Client *old = sel;
324 XEvent ev;
326 XFlush(dpy);
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 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
334 }
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;
344 if(!len) {
345 c->tags[tsel] = tags[tsel];
346 return;
347 }
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 }
368 if(!matched)
369 c->tags[tsel] = tags[tsel];
370 }
372 void
373 manage(Window w, XWindowAttributes *wa)
374 {
375 Client *c, **l;
376 XSetWindowAttributes twa;
377 Window trans;
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;
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);
403 update_name(c);
404 init_tags(c);
406 for(l = &clients; *l; l = &(*l)->next);
407 c->next = *l; /* *l == nil */
408 *l = c;
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);
417 if(!c->floating)
418 c->floating = trans
419 || ((c->maxw == c->minw) && (c->maxh == c->minh));
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 }
435 void
436 gravitate(Client *c, Bool invert)
437 {
438 int dx = 0, dy = 0;
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 }
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 }
482 if(invert) {
483 dx = -dx;
484 dy = -dy;
485 }
486 c->x += dx;
487 c->y += dy;
488 }
491 void
492 resize(Client *c, Bool inc)
493 {
494 XConfigureEvent e;
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->x > sw) /* might happen on restart */
503 c->x = sw - c->w;
504 if(c->y > sh)
505 c->ty = c->y = sh - c->h;
506 if(c->minw && c->w < c->minw)
507 c->w = c->minw;
508 if(c->minh && c->h < c->minh)
509 c->h = c->minh;
510 if(c->maxw && c->w > c->maxw)
511 c->w = c->maxw;
512 if(c->maxh && c->h > c->maxh)
513 c->h = c->maxh;
514 resize_title(c);
515 XSetWindowBorderWidth(dpy, c->win, 1);
516 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
517 e.type = ConfigureNotify;
518 e.event = c->win;
519 e.window = c->win;
520 e.x = c->x;
521 e.y = c->y;
522 e.width = c->w;
523 e.height = c->h;
524 e.border_width = c->border;
525 e.above = None;
526 e.override_redirect = False;
527 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
528 XFlush(dpy);
529 }
531 static int
532 dummy_error_handler(Display *dsply, XErrorEvent *err)
533 {
534 return 0;
535 }
537 void
538 unmanage(Client *c)
539 {
540 Client **l;
542 XGrabServer(dpy);
543 XSetErrorHandler(dummy_error_handler);
545 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
546 XDestroyWindow(dpy, c->title);
548 for(l = &clients; *l && *l != c; l = &(*l)->next);
549 *l = c->next;
550 for(l = &clients; *l; l = &(*l)->next)
551 if((*l)->revert == c)
552 (*l)->revert = NULL;
553 if(sel == c)
554 sel = sel->revert ? sel->revert : clients;
556 free(c);
558 XFlush(dpy);
559 XSetErrorHandler(error_handler);
560 XUngrabServer(dpy);
561 arrange(NULL);
562 if(sel)
563 focus(sel);
564 }
566 Client *
567 gettitle(Window w)
568 {
569 Client *c;
570 for(c = clients; c; c = c->next)
571 if(c->title == w)
572 return c;
573 return NULL;
574 }
576 Client *
577 getclient(Window w)
578 {
579 Client *c;
580 for(c = clients; c; c = c->next)
581 if(c->win == w)
582 return c;
583 return NULL;
584 }
586 void
587 draw_client(Client *c)
588 {
589 int i;
590 if(c == sel) {
591 draw_bar();
592 XUnmapWindow(dpy, c->title);
593 XSetWindowBorder(dpy, c->win, dc.fg);
594 return;
595 }
597 XSetWindowBorder(dpy, c->win, dc.bg);
598 XMapWindow(dpy, c->title);
600 dc.x = dc.y = 0;
602 dc.w = 0;
603 for(i = 0; i < TLast; i++) {
604 if(c->tags[i]) {
605 dc.x += dc.w;
606 dc.w = textw(c->tags[i]) + dc.font.height;
607 drawtext(c->tags[i], False, True);
608 }
609 }
610 dc.x += dc.w;
611 dc.w = textw(c->name) + dc.font.height;
612 drawtext(c->name, False, True);
613 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
614 0, 0, c->tw, c->th, 0, 0);
615 XFlush(dpy);
616 }