aewl

view client.c @ 60:24f9c674d03f

made stdin reader more robust
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 12:08:32 +0200
parents f005d46462e8
children 0a4b066ce254
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, *old;
33 if(!(old = sel))
34 return;
36 for(l = &clients; *l && *l != sel; l = &(*l)->next);
37 *l = sel->next;
39 old->next = clients; /* pop */
40 clients = old;
41 sel = old;
42 arrange(NULL);
43 focus(sel);
44 }
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 }
60 void
61 view(Arg *arg)
62 {
63 Client *c;
65 tsel = arg->i;
66 arrange(NULL);
68 for(c = clients; c; c = next(c->next))
69 draw_client(c);
70 draw_bar();
71 }
73 void
74 tappend(Arg *arg)
75 {
76 if(!sel)
77 return;
79 sel->tags[arg->i] = tags[arg->i];
80 arrange(NULL);
81 }
83 void
84 ttrunc(Arg *arg)
85 {
86 int i;
87 if(!sel)
88 return;
90 for(i = 0; i < TLast; i++)
91 sel->tags[i] = NULL;
92 tappend(arg);
93 }
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 }
102 void
103 floating(Arg *arg)
104 {
105 Client *c;
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 }
123 void
124 tiling(Arg *arg)
125 {
126 Client *c;
127 int n, i, w, h;
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++;
135 if(n > 1)
136 h = (sh - bh) / (n - 1);
137 else
138 h = sh - bh;
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 }
180 void
181 prevc(Arg *arg)
182 {
183 Client *c;
185 if(!sel)
186 return;
188 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
189 craise(c);
190 focus(c);
191 }
192 }
194 void
195 nextc(Arg *arg)
196 {
197 Client *c;
199 if(!sel)
200 return;
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 }
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 }
222 static void
223 resize_title(Client *c)
224 {
225 int i;
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 }
239 void
240 update_name(Client *c)
241 {
242 XTextProperty name;
243 int n;
244 char **list = NULL;
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 }
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 }
305 void
306 craise(Client *c)
307 {
308 XRaiseWindow(dpy, c->win);
309 XRaiseWindow(dpy, c->title);
310 }
312 void
313 lower(Client *c)
314 {
315 XLowerWindow(dpy, c->title);
316 XLowerWindow(dpy, c->win);
317 }
319 void
320 focus(Client *c)
321 {
322 Client *old = sel;
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 }
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;
341 if(!len) {
342 c->tags[tsel] = tags[tsel];
343 return;
344 }
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 }
365 if(!matched)
366 c->tags[tsel] = tags[tsel];
367 }
369 void
370 manage(Window w, XWindowAttributes *wa)
371 {
372 Client *c, **l;
373 XSetWindowAttributes twa;
374 Window trans;
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;
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);
400 update_name(c);
401 init_tags(c);
403 for(l = &clients; *l; l = &(*l)->next);
404 c->next = *l; /* *l == nil */
405 *l = c;
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);
414 if(!c->floating)
415 c->floating = trans
416 || ((c->maxw == c->minw) && (c->maxh == c->minh));
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 }
432 void
433 gravitate(Client *c, Bool invert)
434 {
435 int dx = 0, dy = 0;
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 }
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 }
479 if(invert) {
480 dx = -dx;
481 dy = -dy;
482 }
483 c->x += dx;
484 c->y += dy;
485 }
488 void
489 resize(Client *c, Bool inc)
490 {
491 XConfigureEvent e;
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 }
524 static int
525 dummy_error_handler(Display *dsply, XErrorEvent *err)
526 {
527 return 0;
528 }
530 void
531 unmanage(Client *c)
532 {
533 Client **l;
535 XGrabServer(dpy);
536 XSetErrorHandler(dummy_error_handler);
538 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
539 XDestroyWindow(dpy, c->title);
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;
549 free(c);
551 XFlush(dpy);
552 XSetErrorHandler(error_handler);
553 XUngrabServer(dpy);
554 arrange(NULL);
555 if(sel)
556 focus(sel);
557 }
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 }
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 }
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 }
590 XSetWindowBorder(dpy, c->win, dc.bg);
591 XMapWindow(dpy, c->title);
593 dc.x = dc.y = 0;
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 }