aewl

view client.c @ 71:7681ef838201

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