X-Git-Url: https://jasonwoof.com/gitweb/?p=dwm.git;a=blobdiff_plain;f=client.c;h=6d7bd92f4106f3109eadcbf7cb87540d94b86128;hp=2403d749652f866cd942448910b0a9ece7352d1a;hb=7fe717c29f9dafc4fc38313adbbb7c85619dec69;hpb=c47da143bdf5b4e3924a411f42648d4b3e86ff00 diff --git a/client.c b/client.c index 2403d74..6d7bd92 100644 --- a/client.c +++ b/client.c @@ -3,70 +3,113 @@ * See LICENSE file for license details. */ -#include #include +#include #include #include #include #include "dwm.h" -static void (*arrange)(Arg *) = floating; +static void (*arrange)(Arg *) = tiling; + +static Rule rule[] = { + { "Firefox-bin", "Gecko", { [Twww] = "www" } }, +}; static Client * next(Client *c) { - for(c = c->next; c && !c->tags[tsel]; c = c->next); + for(; c && !c->tags[tsel]; c = c->next); return c; } -static Client * -prev(Client *c) +void +zoom(Arg *arg) { - for(c = c->prev; c && !c->tags[tsel]; c = c->prev); - return c; + Client **l, *old; + + if(!(old = sel)) + return; + + for(l = &clients; *l && *l != sel; l = &(*l)->next); + *l = sel->next; + + old->next = clients; /* pop */ + clients = old; + sel = old; + arrange(NULL); + focus(sel); } void max(Arg *arg) { - if(!csel) + if(!sel) return; - csel->x = sx; - csel->y = sy; - csel->w = sw - 2 * csel->border; - csel->h = sh - 2 * csel->border; - craise(csel); - resize(csel); + sel->x = sx; + sel->y = sy; + sel->w = sw - 2 * sel->border; + sel->h = sh - 2 * sel->border; + craise(sel); + resize(sel, False); discard_events(EnterWindowMask); } void +view(Arg *arg) +{ + tsel = arg->i; + arrange(NULL); +} + +void tag(Arg *arg) { - if(!csel) + int i, n; + if(!sel) return; - if(arg->i == tsel) - return; + if(arg->i == tsel) { + for(n = i = 0; i < TLast; i++) + if(sel->tags[i]) + n++; + if(n < 2) + return; + } - if(csel->tags[arg->i]) - csel->tags[arg->i] = NULL; /* toggle tag */ + if(sel->tags[arg->i]) + sel->tags[arg->i] = NULL; /* toggle tag */ else - csel->tags[arg->i] = tags[arg->i]; + sel->tags[arg->i] = tags[arg->i]; arrange(NULL); } +static void +ban_client(Client *c) +{ + XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); + XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty); +} + void floating(Arg *arg) { Client *c; arrange = floating; - if(!csel) - return; - for(c = csel; c; c = next(c)) - resize(c); + for(c = clients; c; c = c->next) { + if(c->tags[tsel]) + resize(c, True); + else + ban_client(c); + } + if(sel && !sel->tags[tsel]) { + if((sel = next(clients))) { + craise(sel); + focus(sel); + } + } discard_events(EnterWindowMask); } @@ -74,35 +117,46 @@ void tiling(Arg *arg) { Client *c; - int n, cols, rows, gw, gh, i, j; - float rt, fd; + int n, i, w, h; + w = sw - mw; arrange = tiling; - if(!csel) - return; - for(n = 0, c = csel; c; c = next(c), n++); - rt = sqrt(n); - if(modff(rt, &fd) < 0.5) - rows = floor(rt); - else - rows = ceil(rt); - if(rows * rows < n) - cols = rows + 1; - else - cols = rows; - - gw = (sw - 2) / cols; - gh = (sh - 2) / rows; - - for(i = j = 0, c = csel; c; c = next(c)) { - c->x = i * gw; - c->y = j * gh; - c->w = gw; - c->h = gh; - resize(c); - if(++i == cols) { - j++; - i = 0; + for(n = 0, c = clients; c; c = c->next) + if(c->tags[tsel]) + n++; + + h = (n > 1) ? sh / (n - 1) : sh; + + for(i = 0, c = clients; c; c = c->next) { + if(c->tags[tsel]) { + if(n == 1) { + c->x = sx; + c->y = sy; + c->w = sw - 2 * c->border; + c->h = sh - 2 * c->border; + } + else if(i == 0) { + c->x = sx; + c->y = sy; + c->w = mw - 2 * c->border; + c->h = sh - 2 * c->border; + } + else { + c->x = sx + mw; + c->y = sy + (i - 1) * h; + c->w = w - 2 * c->border; + c->h = h - 2 * c->border; + } + resize(c, False); + i++; + } + else + ban_client(c); + } + if(sel && !sel->tags[tsel]) { + if((sel = next(clients))) { + craise(sel); + focus(sel); } } discard_events(EnterWindowMask); @@ -113,14 +167,11 @@ prevc(Arg *arg) { Client *c; - if(!csel) + if(!sel) return; - if(!(c = prev(csel))) - c = prev(cend); - if(c) { + if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) { craise(c); - XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); focus(c); } } @@ -130,15 +181,14 @@ nextc(Arg *arg) { Client *c; - if(!csel) + if(!sel) return; - if(!(c = next(csel))) - c = next(cstart); - + if(!(c = next(sel->next))) + c = next(clients); if(c) { craise(c); - XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); + c->revert = sel; focus(c); } } @@ -146,14 +196,12 @@ nextc(Arg *arg) void ckill(Arg *arg) { - Client *c = csel; - - if(!c) + if(!sel) return; - if(c->proto & WM_PROTOCOL_DELWIN) - send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]); + if(sel->proto & WM_PROTOCOL_DELWIN) + send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]); else - XKillClient(dpy, c->win); + XKillClient(dpy, sel->win); } static void @@ -256,24 +304,58 @@ lower(Client *c) void focus(Client *c) { - if(csel && csel != c) { - XSetWindowBorder(dpy, csel->win, dc.bg); - XMapWindow(dpy, csel->title); - draw_client(csel); - } - csel = c; - XUnmapWindow(dpy, c->title); - XSetWindowBorder(dpy, c->win, dc.fg); + Client *old = sel; + + sel = c; + if(old && old != c) + draw_client(old); draw_client(c); XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); XFlush(dpy); discard_events(EnterWindowMask); } +static void +init_tags(Client *c) +{ + XClassHint ch; + static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0; + unsigned int i, j; + Bool matched = False; + + if(!len) { + c->tags[tsel] = tags[tsel]; + return; + } + + if(XGetClassHint(dpy, c->win, &ch)) { + if(ch.res_class && ch.res_name) { + fprintf(stderr, "%s:%s\n", ch.res_class, ch.res_name); + for(i = 0; i < len; i++) + if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class)) + && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance))) + { + fprintf(stderr, "->>>%s:%s\n", ch.res_class, ch.res_name); + for(j = 0; j < TLast; j++) + c->tags[j] = rule[i].tags[j]; + matched = True; + break; + } + } + if(ch.res_class) + XFree(ch.res_class); + if(ch.res_name) + XFree(ch.res_name); + } + + if(!matched) + c->tags[tsel] = tags[tsel]; +} + void manage(Window w, XWindowAttributes *wa) { - Client *c; + Client *c, **l; XSetWindowAttributes twa; c = emallocz(sizeof(Client)); @@ -284,6 +366,7 @@ manage(Window w, XWindowAttributes *wa) c->h = wa->height; c->th = th; c->border = 1; + c->proto = win_proto(c->win); update_size(c); XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | EnterWindowMask); @@ -292,21 +375,17 @@ manage(Window w, XWindowAttributes *wa) twa.background_pixmap = ParentRelative; twa.event_mask = ExposureMask; - c->tags[tsel] = tags[tsel]; c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th, 0, DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); update_name(c); + init_tags(c); - if(!cstart) - cstart = cend = c; - else { - cend->next = c; - c->prev = cend; - cend = c; - } + for(l = &clients; *l; l = &(*l)->next); + c->next = *l; /* *l == nil */ + *l = c; XSetWindowBorderWidth(dpy, c->win, 1); XMapRaised(dpy, c->win); @@ -318,8 +397,10 @@ manage(Window w, XWindowAttributes *wa) XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask, GrabModeAsync, GrabModeSync, None, None); arrange(NULL); - XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); - focus(c); + if(c->tags[tsel]) + focus(c); + else + ban_client(c); } void @@ -379,14 +460,16 @@ gravitate(Client *c, Bool invert) void -resize(Client *c) +resize(Client *c, Bool inc) { XConfigureEvent e; - if(c->incw) - c->w -= (c->w - c->basew) % c->incw; - if(c->inch) - c->h -= (c->h - c->baseh) % c->inch; + if(inc) { + if(c->incw) + c->w -= (c->w - c->basew) % c->incw; + if(c->inch) + c->h -= (c->h - c->baseh) % c->inch; + } if(c->minw && c->w < c->minw) c->w = c->minw; if(c->minh && c->h < c->minh) @@ -420,26 +503,21 @@ dummy_error_handler(Display *dsply, XErrorEvent *err) void unmanage(Client *c) { + Client **l; + XGrabServer(dpy); XSetErrorHandler(dummy_error_handler); XUngrabButton(dpy, AnyButton, AnyModifier, c->win); XDestroyWindow(dpy, c->title); - if(c->prev) { - c->prev->next = c->next; - if(csel == c) - csel = c->prev; - } - if(c->next) { - c->next->prev = c->prev; - if(csel == c) - csel = c->next; - } - if(cstart == c) - cstart = c->next; - if(cend == c) - cend = c->prev; + for(l = &clients; *l && *l != c; l = &(*l)->next); + *l = c->next; + for(l = &clients; *l; l = &(*l)->next) + if((*l)->revert == c) + (*l)->revert = NULL; + if(sel == c) + sel = sel->revert ? sel->revert : clients; free(c); @@ -447,15 +525,15 @@ unmanage(Client *c) XSetErrorHandler(error_handler); XUngrabServer(dpy); arrange(NULL); - if(csel) - focus(csel); + if(sel) + focus(sel); } Client * gettitle(Window w) { Client *c; - for(c = cstart; c; c = c->next) + for(c = clients; c; c = c->next) if(c->title == w) return c; return NULL; @@ -465,7 +543,7 @@ Client * getclient(Window w) { Client *c; - for(c = cstart; c; c = c->next) + for(c = clients; c; c = c->next) if(c->win == w) return c; return NULL; @@ -475,8 +553,14 @@ void draw_client(Client *c) { int i; - if(c == csel) + if(c == sel) { + XUnmapWindow(dpy, c->title); + XSetWindowBorder(dpy, c->win, dc.fg); return; + } + + XSetWindowBorder(dpy, c->win, dc.bg); + XMapWindow(dpy, c->title); dc.x = dc.y = 0; dc.h = c->th;