2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/Xutil.h>
14 void (*arrange)(Arg *) = tiling;
16 static Rule rule[] = {
17 /* class instance tags floating */
18 { "Firefox-bin", "Gecko", { [Twww] = "www" }, False },
24 for(; c && !c->tags[tsel]; c = c->next);
36 if(sel == next(clients) && sel->next) {
37 if((c = next(sel->next)))
41 for(l = &clients; *l && *l != sel; l = &(*l)->next);
44 sel->next = clients; /* pop */
57 sel->w = sw - 2 * sel->border;
58 sel->h = sh - 2 * sel->border - bh;
71 for(c = clients; c; c = next(c->next))
82 sel->tags[arg->i] = tags[arg->i];
93 for(i = 0; i < TLast; i++)
101 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
102 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
111 for(c = clients; c; c = c->next) {
117 if(sel && !sel->tags[tsel]) {
118 if((sel = next(clients))) {
133 for(n = 0, c = clients; c; c = c->next)
134 if(c->tags[tsel] && !c->floating)
138 h = (sh - bh) / (n - 1);
142 for(i = 0, c = clients; c; c = c->next) {
152 c->w = sw - 2 * c->border;
153 c->h = sh - 2 * c->border - bh;
158 c->w = mw - 2 * c->border;
159 c->h = sh - 2 * c->border - bh;
163 c->y = sy + (i - 1) * h + bh;
164 c->w = w - 2 * c->border;
165 c->h = h - 2 * c->border;
173 if(!sel || (sel && !sel->tags[tsel])) {
174 if((sel = next(clients))) {
189 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
203 if(!(c = next(sel->next)))
217 if(sel->proto & WM_PROTOCOL_DELWIN)
218 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
220 XKillClient(dpy, sel->win);
224 resize_title(Client *c)
229 for(i = 0; i < TLast; i++)
231 c->tw += textw(c->tags[i]) + dc.font.height;
232 c->tw += textw(c->name) + dc.font.height;
235 c->tx = c->x + c->w - c->tw + 2;
237 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
241 update_name(Client *c)
249 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
251 XGetWMName(dpy, c->win, &name);
254 if(name.encoding == XA_STRING)
255 strncpy(c->name, (char *)name.value, sizeof(c->name));
257 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
260 strncpy(c->name, *list, sizeof(c->name));
261 XFreeStringList(list);
269 update_size(Client *c)
273 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
275 c->flags = size.flags;
276 if(c->flags & PBaseSize) {
277 c->basew = size.base_width;
278 c->baseh = size.base_height;
281 c->basew = c->baseh = 0;
282 if(c->flags & PResizeInc) {
283 c->incw = size.width_inc;
284 c->inch = size.height_inc;
287 c->incw = c->inch = 0;
288 if(c->flags & PMaxSize) {
289 c->maxw = size.max_width;
290 c->maxh = size.max_height;
293 c->maxw = c->maxh = 0;
294 if(c->flags & PMinSize) {
295 c->minw = size.min_width;
296 c->minh = size.min_height;
299 c->minw = c->minh = 0;
300 if(c->flags & PWinGravity)
301 c->grav = size.win_gravity;
303 c->grav = NorthWestGravity;
309 XRaiseWindow(dpy, c->win);
310 XRaiseWindow(dpy, c->title);
316 XLowerWindow(dpy, c->title);
317 XLowerWindow(dpy, c->win);
331 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
333 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
340 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
342 Bool matched = False;
345 c->tags[tsel] = tags[tsel];
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)))
355 for(j = 0; j < TLast; j++)
356 c->tags[j] = rule[i].tags[j];
357 c->floating = rule[i].floating;
369 c->tags[tsel] = tags[tsel];
373 manage(Window w, XWindowAttributes *wa)
376 XSetWindowAttributes twa;
379 c = emallocz(sizeof(Client));
381 c->tx = c->x = wa->x;
382 c->ty = c->y = wa->y;
385 c->tw = c->w = wa->width;
389 c->proto = win_proto(c->win);
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);
406 for(l = &clients; *l; l = &(*l)->next);
407 c->next = *l; /* *l == nil */
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);
419 || ((c->maxw == c->minw) && (c->maxh == c->minh));
422 /* mapping the window now prevents flicker */
424 XMapRaised(dpy, c->win);
425 XMapRaised(dpy, c->title);
430 XMapRaised(dpy, c->win);
431 XMapRaised(dpy, c->title);
436 gravitate(Client *c, Bool invert)
442 case NorthWestGravity:
444 case NorthEastGravity:
450 dy = -(c->h / 2) + c->border;
452 case SouthEastGravity:
454 case SouthWestGravity:
463 case NorthWestGravity:
465 case SouthWestGravity:
471 dx = -(c->w / 2) + c->border;
473 case NorthEastGravity:
475 case SouthEastGravity:
476 dx = -(c->w + c->border);
492 resize(Client *c, Bool inc)
498 c->w -= (c->w - c->basew) % c->incw;
500 c->h -= (c->h - c->baseh) % c->inch;
502 if(c->x > sw) /* might happen on restart */
505 c->ty = c->y = sh - c->h;
506 if(c->minw && c->w < c->minw)
508 if(c->minh && c->h < c->minh)
510 if(c->maxw && c->w > c->maxw)
512 if(c->maxh && c->h > c->maxh)
515 XSetWindowBorderWidth(dpy, c->win, 1);
516 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
517 e.type = ConfigureNotify;
524 e.border_width = c->border;
526 e.override_redirect = False;
527 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
532 dummy_error_handler(Display *dsply, XErrorEvent *err)
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);
550 for(l = &clients; *l; l = &(*l)->next)
551 if((*l)->revert == c)
554 sel = sel->revert ? sel->revert : clients;
559 XSetErrorHandler(error_handler);
570 for(c = clients; c; c = c->next)
580 for(c = clients; c; c = c->next)
587 draw_client(Client *c)
592 XUnmapWindow(dpy, c->title);
593 XSetWindowBorder(dpy, c->win, dc.fg);
597 XSetWindowBorder(dpy, c->win, dc.bg);
598 XMapWindow(dpy, c->title);
603 for(i = 0; i < TLast; i++) {
606 dc.w = textw(c->tags[i]) + dc.font.height;
607 drawtext(c->tags[i], False, True);
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);