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;
57 discard_events(EnterWindowMask);
73 sel->tags[arg->i] = tags[arg->i];
84 for(i = 0; i < TLast; i++)
92 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
93 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
102 for(c = clients; c; c = c->next) {
108 if(sel && !sel->tags[tsel]) {
109 if((sel = next(clients))) {
114 discard_events(EnterWindowMask);
125 for(n = 0, c = clients; c; c = c->next)
126 if(c->tags[tsel] && !c->floating)
129 h = (n > 1) ? sh / (n - 1) : sh;
131 for(i = 0, c = clients; c; c = c->next) {
141 c->w = sw - 2 * c->border;
142 c->h = sh - 2 * c->border;
147 c->w = mw - 2 * c->border;
148 c->h = sh - 2 * c->border;
152 c->y = sy + (i - 1) * h;
153 c->w = w - 2 * c->border;
154 c->h = h - 2 * c->border;
162 if(sel && !sel->tags[tsel]) {
163 if((sel = next(clients))) {
168 discard_events(EnterWindowMask);
179 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
193 if(!(c = next(sel->next)))
207 if(sel->proto & WM_PROTOCOL_DELWIN)
208 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
210 XKillClient(dpy, sel->win);
214 resize_title(Client *c)
219 for(i = 0; i < TLast; i++)
221 c->tw += textw(c->tags[i]) + dc.font.height;
222 c->tw += textw(c->name) + dc.font.height;
225 c->tx = c->x + c->w - c->tw + 2;
227 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
231 update_name(Client *c)
239 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
241 XGetWMName(dpy, c->win, &name);
244 if(name.encoding == XA_STRING)
245 strncpy(c->name, (char *)name.value, sizeof(c->name));
247 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
250 strncpy(c->name, *list, sizeof(c->name));
251 XFreeStringList(list);
259 update_size(Client *c)
263 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
265 c->flags = size.flags;
266 if(c->flags & PBaseSize) {
267 c->basew = size.base_width;
268 c->baseh = size.base_height;
271 c->basew = c->baseh = 0;
272 if(c->flags & PResizeInc) {
273 c->incw = size.width_inc;
274 c->inch = size.height_inc;
277 c->incw = c->inch = 0;
278 if(c->flags & PMaxSize) {
279 c->maxw = size.max_width;
280 c->maxh = size.max_height;
283 c->maxw = c->maxh = 0;
284 if(c->flags & PMinSize) {
285 c->minw = size.min_width;
286 c->minh = size.min_height;
289 c->minw = c->minh = 0;
290 if(c->flags & PWinGravity)
291 c->grav = size.win_gravity;
293 c->grav = NorthWestGravity;
299 XRaiseWindow(dpy, c->win);
300 XRaiseWindow(dpy, c->title);
306 XLowerWindow(dpy, c->title);
307 XLowerWindow(dpy, c->win);
319 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
321 discard_events(EnterWindowMask);
328 static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
330 Bool matched = False;
333 c->tags[tsel] = tags[tsel];
337 if(XGetClassHint(dpy, c->win, &ch)) {
338 if(ch.res_class && ch.res_name) {
339 for(i = 0; i < len; i++)
340 if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
341 && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
343 for(j = 0; j < TLast; j++)
344 c->tags[j] = rule[i].tags[j];
345 c->floating = rule[i].floating;
357 c->tags[tsel] = tags[tsel];
361 manage(Window w, XWindowAttributes *wa)
364 XSetWindowAttributes twa;
367 c = emallocz(sizeof(Client));
369 c->tx = c->x = wa->x;
370 c->ty = c->y = wa->y;
371 c->tw = c->w = wa->width;
375 c->proto = win_proto(c->win);
377 XSelectInput(dpy, c->win,
378 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
379 XGetTransientForHint(dpy, c->win, &trans);
380 twa.override_redirect = 1;
381 twa.background_pixmap = ParentRelative;
382 twa.event_mask = ExposureMask;
384 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
385 0, DefaultDepth(dpy, screen), CopyFromParent,
386 DefaultVisual(dpy, screen),
387 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
392 for(l = &clients; *l; l = &(*l)->next);
393 c->next = *l; /* *l == nil */
396 XSetWindowBorderWidth(dpy, c->win, 1);
397 XMapRaised(dpy, c->win);
398 XMapRaised(dpy, c->title);
399 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
400 GrabModeAsync, GrabModeSync, None, None);
401 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
402 GrabModeAsync, GrabModeSync, None, None);
403 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
404 GrabModeAsync, GrabModeSync, None, None);
408 || ((c->maxw == c->minw) && (c->maxh == c->minh));
418 gravitate(Client *c, Bool invert)
424 case NorthWestGravity:
426 case NorthEastGravity:
432 dy = -(c->h / 2) + c->border;
434 case SouthEastGravity:
436 case SouthWestGravity:
445 case NorthWestGravity:
447 case SouthWestGravity:
453 dx = -(c->w / 2) + c->border;
455 case NorthEastGravity:
457 case SouthEastGravity:
458 dx = -(c->w + c->border);
474 resize(Client *c, Bool inc)
480 c->w -= (c->w - c->basew) % c->incw;
482 c->h -= (c->h - c->baseh) % c->inch;
484 if(c->minw && c->w < c->minw)
486 if(c->minh && c->h < c->minh)
488 if(c->maxw && c->w > c->maxw)
490 if(c->maxh && c->h > c->maxh)
493 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
494 e.type = ConfigureNotify;
501 e.border_width = c->border;
503 e.override_redirect = False;
504 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
509 dummy_error_handler(Display *dsply, XErrorEvent *err)
520 XSetErrorHandler(dummy_error_handler);
522 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
523 XDestroyWindow(dpy, c->title);
525 for(l = &clients; *l && *l != c; l = &(*l)->next);
527 for(l = &clients; *l; l = &(*l)->next)
528 if((*l)->revert == c)
531 sel = sel->revert ? sel->revert : clients;
536 XSetErrorHandler(error_handler);
547 for(c = clients; c; c = c->next)
557 for(c = clients; c; c = c->next)
564 draw_client(Client *c)
568 XUnmapWindow(dpy, c->title);
569 XSetWindowBorder(dpy, c->win, dc.fg);
573 XSetWindowBorder(dpy, c->win, dc.bg);
574 XMapWindow(dpy, c->title);
580 for(i = 0; i < TLast; i++) {
583 dc.w = textw(c->tags[i]) + dc.font.height;
584 draw(True, c->tags[i]);
588 dc.w = textw(c->name) + dc.font.height;
590 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
591 0, 0, c->tw, c->th, 0, 0);