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 for(l = &clients; *l && *l != sel; l = &(*l)->next);
39 old->next = clients; /* pop */
53 sel->w = sw - 2 * sel->border;
54 sel->h = sh - 2 * sel->border - bh;
57 discard_events(EnterWindowMask);
68 for(c = clients; c; c = next(c->next))
79 sel->tags[arg->i] = tags[arg->i];
90 for(i = 0; i < TLast; i++)
98 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
99 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
108 for(c = clients; c; c = c->next) {
114 if(sel && !sel->tags[tsel]) {
115 if((sel = next(clients))) {
120 discard_events(EnterWindowMask);
131 for(n = 0, c = clients; c; c = c->next)
132 if(c->tags[tsel] && !c->floating)
136 h = (sh - bh) / (n - 1);
140 for(i = 0, c = clients; c; c = c->next) {
150 c->w = sw - 2 * c->border;
151 c->h = sh - 2 * c->border - bh;
156 c->w = mw - 2 * c->border;
157 c->h = sh - 2 * c->border - bh;
161 c->y = sy + (i - 1) * h + bh;
162 c->w = w - 2 * c->border;
163 c->h = h - 2 * c->border;
171 if(sel && !sel->tags[tsel]) {
172 if((sel = next(clients))) {
177 discard_events(EnterWindowMask);
188 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
202 if(!(c = next(sel->next)))
216 if(sel->proto & WM_PROTOCOL_DELWIN)
217 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
219 XKillClient(dpy, sel->win);
223 resize_title(Client *c)
228 for(i = 0; i < TLast; i++)
230 c->tw += textw(c->tags[i]) + dc.font.height;
231 c->tw += textw(c->name) + dc.font.height;
234 c->tx = c->x + c->w - c->tw + 2;
236 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
240 update_name(Client *c)
248 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
250 XGetWMName(dpy, c->win, &name);
253 if(name.encoding == XA_STRING)
254 strncpy(c->name, (char *)name.value, sizeof(c->name));
256 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
259 strncpy(c->name, *list, sizeof(c->name));
260 XFreeStringList(list);
268 update_size(Client *c)
272 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
274 c->flags = size.flags;
275 if(c->flags & PBaseSize) {
276 c->basew = size.base_width;
277 c->baseh = size.base_height;
280 c->basew = c->baseh = 0;
281 if(c->flags & PResizeInc) {
282 c->incw = size.width_inc;
283 c->inch = size.height_inc;
286 c->incw = c->inch = 0;
287 if(c->flags & PMaxSize) {
288 c->maxw = size.max_width;
289 c->maxh = size.max_height;
292 c->maxw = c->maxh = 0;
293 if(c->flags & PMinSize) {
294 c->minw = size.min_width;
295 c->minh = size.min_height;
298 c->minw = c->minh = 0;
299 if(c->flags & PWinGravity)
300 c->grav = size.win_gravity;
302 c->grav = NorthWestGravity;
308 XRaiseWindow(dpy, c->win);
309 XRaiseWindow(dpy, c->title);
315 XLowerWindow(dpy, c->title);
316 XLowerWindow(dpy, c->win);
328 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
330 discard_events(EnterWindowMask);
337 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
339 Bool matched = False;
342 c->tags[tsel] = tags[tsel];
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)))
352 for(j = 0; j < TLast; j++)
353 c->tags[j] = rule[i].tags[j];
354 c->floating = rule[i].floating;
366 c->tags[tsel] = tags[tsel];
370 manage(Window w, XWindowAttributes *wa)
373 XSetWindowAttributes twa;
376 c = emallocz(sizeof(Client));
378 c->tx = c->x = wa->x;
379 c->ty = c->y = wa->y;
382 c->tw = c->w = wa->width;
386 c->proto = win_proto(c->win);
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);
403 for(l = &clients; *l; l = &(*l)->next);
404 c->next = *l; /* *l == nil */
407 XMapRaised(dpy, c->win);
408 XMapRaised(dpy, c->title);
409 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
410 GrabModeAsync, GrabModeSync, None, None);
411 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
412 GrabModeAsync, GrabModeSync, None, None);
413 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
414 GrabModeAsync, GrabModeSync, None, None);
418 || ((c->maxw == c->minw) && (c->maxh == c->minh));
428 gravitate(Client *c, Bool invert)
434 case NorthWestGravity:
436 case NorthEastGravity:
442 dy = -(c->h / 2) + c->border;
444 case SouthEastGravity:
446 case SouthWestGravity:
455 case NorthWestGravity:
457 case SouthWestGravity:
463 dx = -(c->w / 2) + c->border;
465 case NorthEastGravity:
467 case SouthEastGravity:
468 dx = -(c->w + c->border);
484 resize(Client *c, Bool inc)
490 c->w -= (c->w - c->basew) % c->incw;
492 c->h -= (c->h - c->baseh) % c->inch;
494 if(c->minw && c->w < c->minw)
496 if(c->minh && c->h < c->minh)
498 if(c->maxw && c->w > c->maxw)
500 if(c->maxh && c->h > c->maxh)
503 XSetWindowBorderWidth(dpy, c->win, 1);
504 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
505 e.type = ConfigureNotify;
512 e.border_width = c->border;
514 e.override_redirect = False;
515 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
520 dummy_error_handler(Display *dsply, XErrorEvent *err)
531 XSetErrorHandler(dummy_error_handler);
533 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
534 XDestroyWindow(dpy, c->title);
536 for(l = &clients; *l && *l != c; l = &(*l)->next);
538 for(l = &clients; *l; l = &(*l)->next)
539 if((*l)->revert == c)
542 sel = sel->revert ? sel->revert : clients;
547 XSetErrorHandler(error_handler);
558 for(c = clients; c; c = c->next)
568 for(c = clients; c; c = c->next)
575 draw_client(Client *c)
580 XUnmapWindow(dpy, c->title);
581 XSetWindowBorder(dpy, c->win, dc.fg);
585 XSetWindowBorder(dpy, c->win, dc.bg);
586 XMapWindow(dpy, c->title);
591 for(i = 0; i < TLast; i++) {
594 dc.w = textw(c->tags[i]) + dc.font.height;
595 drawtext(c->tags[i], True);
599 dc.w = textw(c->name) + dc.font.height;
600 drawtext(c->name, True);
601 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
602 0, 0, c->tw, c->th, 0, 0);