dwm-meillo
view client.c @ 56:b72b7086c385
continued with man page
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Fri, 14 Jul 2006 08:45:30 +0200 |
parents | a64602deac83 |
children | f005d46462e8 |
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;
53 sel->w = sw - 2 * sel->border;
54 sel->h = sh - 2 * sel->border;
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 }
72 void
73 tappend(Arg *arg)
74 {
75 if(!sel)
76 return;
78 sel->tags[arg->i] = tags[arg->i];
79 arrange(NULL);
80 }
82 void
83 ttrunc(Arg *arg)
84 {
85 int i;
86 if(!sel)
87 return;
89 for(i = 0; i < TLast; i++)
90 sel->tags[i] = NULL;
91 tappend(arg);
92 }
94 static void
95 ban_client(Client *c)
96 {
97 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
98 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
99 }
101 void
102 floating(Arg *arg)
103 {
104 Client *c;
106 arrange = floating;
107 for(c = clients; c; c = c->next) {
108 if(c->tags[tsel])
109 resize(c, True);
110 else
111 ban_client(c);
112 }
113 if(sel && !sel->tags[tsel]) {
114 if((sel = next(clients))) {
115 craise(sel);
116 focus(sel);
117 }
118 }
119 discard_events(EnterWindowMask);
120 }
122 void
123 tiling(Arg *arg)
124 {
125 Client *c;
126 int n, i, w, h;
128 w = sw - mw;
129 arrange = tiling;
130 for(n = 0, c = clients; c; c = c->next)
131 if(c->tags[tsel] && !c->floating)
132 n++;
134 h = (n > 1) ? sh / (n - 1) : sh;
136 for(i = 0, c = clients; c; c = c->next) {
137 if(c->tags[tsel]) {
138 if(c->floating) {
139 craise(c);
140 resize(c, True);
141 continue;
142 }
143 if(n == 1) {
144 c->x = sx;
145 c->y = sy;
146 c->w = sw - 2 * c->border;
147 c->h = sh - 2 * c->border;
148 }
149 else if(i == 0) {
150 c->x = sx;
151 c->y = sy;
152 c->w = mw - 2 * c->border;
153 c->h = sh - 2 * c->border;
154 }
155 else {
156 c->x = sx + mw;
157 c->y = sy + (i - 1) * h;
158 c->w = w - 2 * c->border;
159 c->h = h - 2 * c->border;
160 }
161 resize(c, False);
162 i++;
163 }
164 else
165 ban_client(c);
166 }
167 if(sel && !sel->tags[tsel]) {
168 if((sel = next(clients))) {
169 craise(sel);
170 focus(sel);
171 }
172 }
173 discard_events(EnterWindowMask);
174 }
176 void
177 prevc(Arg *arg)
178 {
179 Client *c;
181 if(!sel)
182 return;
184 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
185 craise(c);
186 focus(c);
187 }
188 }
190 void
191 nextc(Arg *arg)
192 {
193 Client *c;
195 if(!sel)
196 return;
198 if(!(c = next(sel->next)))
199 c = next(clients);
200 if(c) {
201 craise(c);
202 c->revert = sel;
203 focus(c);
204 }
205 }
207 void
208 ckill(Arg *arg)
209 {
210 if(!sel)
211 return;
212 if(sel->proto & WM_PROTOCOL_DELWIN)
213 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
214 else
215 XKillClient(dpy, sel->win);
216 }
218 static void
219 resize_title(Client *c)
220 {
221 int i;
223 c->tw = 0;
224 for(i = 0; i < TLast; i++)
225 if(c->tags[i])
226 c->tw += textw(c->tags[i]) + dc.font.height;
227 c->tw += textw(c->name) + dc.font.height;
228 if(c->tw > c->w)
229 c->tw = c->w + 2;
230 c->tx = c->x + c->w - c->tw + 2;
231 c->ty = c->y;
232 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
233 }
235 void
236 update_name(Client *c)
237 {
238 XTextProperty name;
239 int n;
240 char **list = NULL;
242 name.nitems = 0;
243 c->name[0] = 0;
244 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
245 if(!name.nitems)
246 XGetWMName(dpy, c->win, &name);
247 if(!name.nitems)
248 return;
249 if(name.encoding == XA_STRING)
250 strncpy(c->name, (char *)name.value, sizeof(c->name));
251 else {
252 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
253 && n > 0 && *list)
254 {
255 strncpy(c->name, *list, sizeof(c->name));
256 XFreeStringList(list);
257 }
258 }
259 XFree(name.value);
260 resize_title(c);
261 }
263 void
264 update_size(Client *c)
265 {
266 XSizeHints size;
267 long msize;
268 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
269 size.flags = PSize;
270 c->flags = size.flags;
271 if(c->flags & PBaseSize) {
272 c->basew = size.base_width;
273 c->baseh = size.base_height;
274 }
275 else
276 c->basew = c->baseh = 0;
277 if(c->flags & PResizeInc) {
278 c->incw = size.width_inc;
279 c->inch = size.height_inc;
280 }
281 else
282 c->incw = c->inch = 0;
283 if(c->flags & PMaxSize) {
284 c->maxw = size.max_width;
285 c->maxh = size.max_height;
286 }
287 else
288 c->maxw = c->maxh = 0;
289 if(c->flags & PMinSize) {
290 c->minw = size.min_width;
291 c->minh = size.min_height;
292 }
293 else
294 c->minw = c->minh = 0;
295 if(c->flags & PWinGravity)
296 c->grav = size.win_gravity;
297 else
298 c->grav = NorthWestGravity;
299 }
301 void
302 craise(Client *c)
303 {
304 XRaiseWindow(dpy, c->win);
305 XRaiseWindow(dpy, c->title);
306 }
308 void
309 lower(Client *c)
310 {
311 XLowerWindow(dpy, c->title);
312 XLowerWindow(dpy, c->win);
313 }
315 void
316 focus(Client *c)
317 {
318 Client *old = sel;
320 sel = c;
321 if(old && old != c)
322 draw_client(old);
323 draw_client(c);
324 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
325 XFlush(dpy);
326 discard_events(EnterWindowMask);
327 }
329 static void
330 init_tags(Client *c)
331 {
332 XClassHint ch;
333 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
334 unsigned int i, j;
335 Bool matched = False;
337 if(!len) {
338 c->tags[tsel] = tags[tsel];
339 return;
340 }
342 if(XGetClassHint(dpy, c->win, &ch)) {
343 if(ch.res_class && ch.res_name) {
344 for(i = 0; i < len; i++)
345 if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
346 && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
347 {
348 for(j = 0; j < TLast; j++)
349 c->tags[j] = rule[i].tags[j];
350 c->floating = rule[i].floating;
351 matched = True;
352 break;
353 }
354 }
355 if(ch.res_class)
356 XFree(ch.res_class);
357 if(ch.res_name)
358 XFree(ch.res_name);
359 }
361 if(!matched)
362 c->tags[tsel] = tags[tsel];
363 }
365 void
366 manage(Window w, XWindowAttributes *wa)
367 {
368 Client *c, **l;
369 XSetWindowAttributes twa;
370 Window trans;
372 c = emallocz(sizeof(Client));
373 c->win = w;
374 c->tx = c->x = wa->x;
375 c->ty = c->y = wa->y;
376 c->tw = c->w = wa->width;
377 c->h = wa->height;
378 c->th = th;
379 c->border = 1;
380 c->proto = win_proto(c->win);
381 update_size(c);
382 XSelectInput(dpy, c->win,
383 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
384 XGetTransientForHint(dpy, c->win, &trans);
385 twa.override_redirect = 1;
386 twa.background_pixmap = ParentRelative;
387 twa.event_mask = ExposureMask;
389 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
390 0, DefaultDepth(dpy, screen), CopyFromParent,
391 DefaultVisual(dpy, screen),
392 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
394 update_name(c);
395 init_tags(c);
397 for(l = &clients; *l; l = &(*l)->next);
398 c->next = *l; /* *l == nil */
399 *l = c;
401 XMapRaised(dpy, c->win);
402 XMapRaised(dpy, c->title);
403 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
404 GrabModeAsync, GrabModeSync, None, None);
405 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
406 GrabModeAsync, GrabModeSync, None, None);
407 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
408 GrabModeAsync, GrabModeSync, None, None);
410 if(!c->floating)
411 c->floating = trans
412 || ((c->maxw == c->minw) && (c->maxh == c->minh));
414 arrange(NULL);
415 if(c->tags[tsel])
416 focus(c);
417 else
418 ban_client(c);
419 }
421 void
422 gravitate(Client *c, Bool invert)
423 {
424 int dx = 0, dy = 0;
426 switch(c->grav) {
427 case StaticGravity:
428 case NorthWestGravity:
429 case NorthGravity:
430 case NorthEastGravity:
431 dy = c->border;
432 break;
433 case EastGravity:
434 case CenterGravity:
435 case WestGravity:
436 dy = -(c->h / 2) + c->border;
437 break;
438 case SouthEastGravity:
439 case SouthGravity:
440 case SouthWestGravity:
441 dy = -c->h;
442 break;
443 default:
444 break;
445 }
447 switch (c->grav) {
448 case StaticGravity:
449 case NorthWestGravity:
450 case WestGravity:
451 case SouthWestGravity:
452 dx = c->border;
453 break;
454 case NorthGravity:
455 case CenterGravity:
456 case SouthGravity:
457 dx = -(c->w / 2) + c->border;
458 break;
459 case NorthEastGravity:
460 case EastGravity:
461 case SouthEastGravity:
462 dx = -(c->w + c->border);
463 break;
464 default:
465 break;
466 }
468 if(invert) {
469 dx = -dx;
470 dy = -dy;
471 }
472 c->x += dx;
473 c->y += dy;
474 }
477 void
478 resize(Client *c, Bool inc)
479 {
480 XConfigureEvent e;
482 if(inc) {
483 if(c->incw)
484 c->w -= (c->w - c->basew) % c->incw;
485 if(c->inch)
486 c->h -= (c->h - c->baseh) % c->inch;
487 }
488 if(c->minw && c->w < c->minw)
489 c->w = c->minw;
490 if(c->minh && c->h < c->minh)
491 c->h = c->minh;
492 if(c->maxw && c->w > c->maxw)
493 c->w = c->maxw;
494 if(c->maxh && c->h > c->maxh)
495 c->h = c->maxh;
496 resize_title(c);
497 XSetWindowBorderWidth(dpy, c->win, 1);
498 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
499 e.type = ConfigureNotify;
500 e.event = c->win;
501 e.window = c->win;
502 e.x = c->x;
503 e.y = c->y;
504 e.width = c->w;
505 e.height = c->h;
506 e.border_width = c->border;
507 e.above = None;
508 e.override_redirect = False;
509 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
510 XFlush(dpy);
511 }
513 static int
514 dummy_error_handler(Display *dsply, XErrorEvent *err)
515 {
516 return 0;
517 }
519 void
520 unmanage(Client *c)
521 {
522 Client **l;
524 XGrabServer(dpy);
525 XSetErrorHandler(dummy_error_handler);
527 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
528 XDestroyWindow(dpy, c->title);
530 for(l = &clients; *l && *l != c; l = &(*l)->next);
531 *l = c->next;
532 for(l = &clients; *l; l = &(*l)->next)
533 if((*l)->revert == c)
534 (*l)->revert = NULL;
535 if(sel == c)
536 sel = sel->revert ? sel->revert : clients;
538 free(c);
540 XFlush(dpy);
541 XSetErrorHandler(error_handler);
542 XUngrabServer(dpy);
543 arrange(NULL);
544 if(sel)
545 focus(sel);
546 }
548 Client *
549 gettitle(Window w)
550 {
551 Client *c;
552 for(c = clients; c; c = c->next)
553 if(c->title == w)
554 return c;
555 return NULL;
556 }
558 Client *
559 getclient(Window w)
560 {
561 Client *c;
562 for(c = clients; c; c = c->next)
563 if(c->win == w)
564 return c;
565 return NULL;
566 }
568 void
569 draw_client(Client *c)
570 {
571 int i;
572 if(c == sel) {
573 XUnmapWindow(dpy, c->title);
574 XSetWindowBorder(dpy, c->win, dc.fg);
575 return;
576 }
578 XSetWindowBorder(dpy, c->win, dc.bg);
579 XMapWindow(dpy, c->title);
581 dc.x = dc.y = 0;
582 dc.h = c->th;
584 dc.w = 0;
585 for(i = 0; i < TLast; i++) {
586 if(c->tags[i]) {
587 dc.x += dc.w;
588 dc.w = textw(c->tags[i]) + dc.font.height;
589 draw(True, c->tags[i]);
590 }
591 }
592 dc.x += dc.w;
593 dc.w = textw(c->name) + dc.font.height;
594 draw(True, c->name);
595 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
596 0, 0, c->tw, c->th, 0, 0);
597 XFlush(dpy);
598 }