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))) {
134 for(n = 0, c = clients; c; c = c->next)
135 if(c->tags[tsel] && !c->floating)
139 h = (sh - bh) / (n - 1);
143 for(i = 0, c = clients; c; c = c->next) {
153 c->w = sw - 2 * c->border;
154 c->h = sh - 2 * c->border - bh;
159 c->w = mw - 2 * c->border;
160 c->h = sh - 2 * c->border - bh;
164 c->y = sy + (i - 1) * h + bh;
165 c->w = w - 2 * c->border;
166 c->h = h - 2 * c->border;
174 if(!sel || (sel && !sel->tags[tsel])) {
175 if((sel = next(clients))) {
191 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
205 if(!(c = next(sel->next)))
219 if(sel->proto & WM_PROTOCOL_DELWIN)
220 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
222 XKillClient(dpy, sel->win);
226 resize_title(Client *c)
231 for(i = 0; i < TLast; i++)
233 c->tw += textw(c->tags[i]) + dc.font.height;
234 c->tw += textw(c->name) + dc.font.height;
237 c->tx = c->x + c->w - c->tw + 2;
239 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
243 update_name(Client *c)
251 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
253 XGetWMName(dpy, c->win, &name);
256 if(name.encoding == XA_STRING)
257 strncpy(c->name, (char *)name.value, sizeof(c->name));
259 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
262 strncpy(c->name, *list, sizeof(c->name));
263 XFreeStringList(list);
271 update_size(Client *c)
275 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
277 c->flags = size.flags;
278 if(c->flags & PBaseSize) {
279 c->basew = size.base_width;
280 c->baseh = size.base_height;
283 c->basew = c->baseh = 0;
284 if(c->flags & PResizeInc) {
285 c->incw = size.width_inc;
286 c->inch = size.height_inc;
289 c->incw = c->inch = 0;
290 if(c->flags & PMaxSize) {
291 c->maxw = size.max_width;
292 c->maxh = size.max_height;
295 c->maxw = c->maxh = 0;
296 if(c->flags & PMinSize) {
297 c->minw = size.min_width;
298 c->minh = size.min_height;
301 c->minw = c->minh = 0;
302 if(c->flags & PWinGravity)
303 c->grav = size.win_gravity;
305 c->grav = NorthWestGravity;
311 XRaiseWindow(dpy, c->win);
312 XRaiseWindow(dpy, c->title);
318 XLowerWindow(dpy, c->title);
319 XLowerWindow(dpy, c->win);
333 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
335 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
342 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
344 Bool matched = False;
347 c->tags[tsel] = tags[tsel];
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)))
357 for(j = 0; j < TLast; j++)
358 c->tags[j] = rule[i].tags[j];
359 c->floating = rule[i].floating;
371 c->tags[tsel] = tags[tsel];
375 manage(Window w, XWindowAttributes *wa)
378 XSetWindowAttributes twa;
381 c = emallocz(sizeof(Client));
383 c->tx = c->x = wa->x;
384 c->ty = c->y = wa->y;
387 c->tw = c->w = wa->width;
391 c->proto = win_proto(c->win);
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);
408 for(l = &clients; *l; l = &(*l)->next);
409 c->next = *l; /* *l == nil */
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);
421 || ((c->maxw == c->minw) && (c->maxh == c->minh));
424 /* mapping the window now prevents flicker */
426 XMapRaised(dpy, c->win);
427 XMapRaised(dpy, c->title);
432 XMapRaised(dpy, c->win);
433 XMapRaised(dpy, c->title);
438 gravitate(Client *c, Bool invert)
444 case NorthWestGravity:
446 case NorthEastGravity:
452 dy = -(c->h / 2) + c->border;
454 case SouthEastGravity:
456 case SouthWestGravity:
465 case NorthWestGravity:
467 case SouthWestGravity:
473 dx = -(c->w / 2) + c->border;
475 case NorthEastGravity:
477 case SouthEastGravity:
478 dx = -(c->w + c->border);
494 resize(Client *c, Bool inc)
500 c->w -= (c->w - c->basew) % c->incw;
502 c->h -= (c->h - c->baseh) % c->inch;
504 if(c->x > sw) /* might happen on restart */
507 c->ty = c->y = sh - c->h;
508 if(c->minw && c->w < c->minw)
510 if(c->minh && c->h < c->minh)
512 if(c->maxw && c->w > c->maxw)
514 if(c->maxh && c->h > c->maxh)
517 XSetWindowBorderWidth(dpy, c->win, 1);
518 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
519 e.type = ConfigureNotify;
526 e.border_width = c->border;
528 e.override_redirect = False;
529 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
534 dummy_error_handler(Display *dsply, XErrorEvent *err)
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);
552 for(l = &clients; *l; l = &(*l)->next)
553 if((*l)->revert == c)
556 sel = sel->revert ? sel->revert : clients;
561 XSetErrorHandler(error_handler);
572 for(c = clients; c; c = c->next)
582 for(c = clients; c; c = c->next)
589 draw_client(Client *c)
594 XUnmapWindow(dpy, c->title);
595 XSetWindowBorder(dpy, c->win, dc.fg);
599 XSetWindowBorder(dpy, c->win, dc.bg);
600 XMapWindow(dpy, c->title);
605 for(i = 0; i < TLast; i++) {
608 dc.w = textw(c->tags[i]) + dc.font.height;
609 drawtext(c->tags[i], False, True);
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);