[hackers] [dwm] some more rearrangements

From: Anselm R. Garbe <garbeam_AT_gmail.com>
Date: Sun Sep 16 12:37:33 2007

changeset: 997:8e721021e636
tag: tip
user: Anselm R. Garbe <garbeam_AT_gmail.com>
date: Sun Sep 16 12:34:08 2007 +0200
summary: some more rearrangements

diff -r b4d47b6a8ba8 -r 8e721021e636 dwm.c
--- a/dwm.c Sun Sep 16 11:53:14 2007 +0200
+++ b/dwm.c Sun Sep 16 12:34:08 2007 +0200
@@ -112,14 +112,14 @@ typedef struct {
         regex_t *tagregex;
 } Regs;
 
-/* functions */
-
+/* forward declarations */
 static void applyrules(Client *c);
 static void arrange(void);
 static void attach(Client *c);
 static void attachstack(Client *c);
 static void ban(Client *c);
 static void buttonpress(XEvent *e);
+static void checkotherwm(void);
 static void cleanup(void);
 static void compileregs(void);
 static void configure(Client *c);
@@ -140,17 +140,13 @@ static void focusnext(const char *arg);
 static void focusnext(const char *arg);
 static void focusprev(const char *arg);
 static Client *getclient(Window w);
+static unsigned long getcolor(const char *colstr);
 static long getstate(Window w);
 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, Bool focused);
 static unsigned int idxoftag(const char *tag);
-static void initbar(void);
-static unsigned long initcolor(const char *colstr);
 static void initfont(const char *fontstr);
-static void initlayouts(void);
-static void initstyle(void);
 static Bool isarrange(void (*func)());
-static Bool isfloating(void);
 static Bool isoccupied(unsigned int t);
 static Bool isprotodel(Client *c);
 static Bool isvisible(Client *c);
@@ -167,6 +163,7 @@ static void resize(Client *c, int x, int
 static void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
 static void resizemouse(Client *c);
 static void restack(void);
+static void run(void);
 static void scan(void);
 static void setclientstate(Client *c, long state);
 static void setlayout(const char *arg);
@@ -236,7 +233,7 @@ static Regs *regs = NULL;
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
-/* implementation */
+/* functions*/
 static void
 applyrules(Client *c) {
         static char buf[512];
@@ -338,19 +335,35 @@ buttonpress(XEvent *e) {
                 focus(c);
                 if(CLEANMASK(ev->state) != MODKEY)
                         return;
- if(ev->button == Button1 && (isfloating() || c->isfloating)) {
+ if(ev->button == Button1 && (isarrange(floating) || c->isfloating)) {
                         restack();
                         movemouse(c);
                 }
                 else if(ev->button == Button2)
                         zoom(NULL);
                 else if(ev->button == Button3
- && (isfloating() || c->isfloating) && !c->isfixed)
+ && (isarrange(floating) || c->isfloating) && !c->isfixed)
                 {
                         restack();
                         resizemouse(c);
                 }
         }
+}
+
+static void
+checkotherwm(void) {
+ otherwm = False;
+ XSetErrorHandler(xerrorstart);
+
+ /* this causes an error if some other window manager is running */
+ XSelectInput(dpy, root, SubstructureRedirectMask);
+ XSync(dpy, False);
+ if(otherwm)
+ eprint("dwm: another window manager is already running\n");
+ XSync(dpy, False);
+ XSetErrorHandler(NULL);
+ xerrorxlib = XSetErrorHandler(xerror);
+ XSync(dpy, False);
 }
 
 static void
@@ -446,7 +459,7 @@ configurerequest(XEvent *e) {
                 c->ismax = False;
                 if(ev->value_mask & CWBorderWidth)
                         c->border = ev->border_width;
- if(c->isfixed || c->isfloating || isfloating()) {
+ if(c->isfixed || c->isfloating || isarrange(floating)) {
                         if(ev->value_mask & CWX)
                                 c->x = ev->x;
                         if(ev->value_mask & CWY)
@@ -728,6 +741,16 @@ getclient(Window w) {
         return c;
 }
 
+static unsigned long
+getcolor(const char *colstr) {
+ Colormap cmap = DefaultColormap(dpy, screen);
+ XColor color;
+
+ if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
+ eprint("error, cannot allocate color '%s'\n", colstr);
+ return color.pixel;
+}
+
 static long
 getstate(Window w) {
         int format, status;
@@ -821,37 +844,6 @@ idxoftag(const char *tag) {
 }
 
 static void
-initbar(void) {
- XSetWindowAttributes wa;
-
- wa.override_redirect = 1;
- wa.background_pixmap = ParentRelative;
- wa.event_mask = ButtonPressMask | ExposureMask;
- barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
- DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
- CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
- XDefineCursor(dpy, barwin, cursor[CurNormal]);
- updatebarpos();
- XMapRaised(dpy, barwin);
- strcpy(stext, "dwm-"VERSION);
- dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
- dc.gc = XCreateGC(dpy, root, 0, 0);
- XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
- if(!dc.font.set)
- XSetFont(dpy, dc.gc, dc.font.xfont->fid);
-}
-
-static unsigned long
-initcolor(const char *colstr) {
- Colormap cmap = DefaultColormap(dpy, screen);
- XColor color;
-
- if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
- eprint("error, cannot allocate color '%s'\n", colstr);
- return color.pixel;
-}
-
-static void
 initfont(const char *fontstr) {
         char *def, **missing;
         int i, n;
@@ -893,39 +885,10 @@ initfont(const char *fontstr) {
         dc.font.height = dc.font.ascent + dc.font.descent;
 }
 
-static void
-initlayouts(void) {
- unsigned int i, w;
-
- nlayouts = sizeof layouts / sizeof layouts[0];
- for(blw = i = 0; i < nlayouts; i++) {
- w = textw(layouts[i].symbol);
- if(w > blw)
- blw = w;
- }
-}
-
-static void
-initstyle(void) {
- dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
- dc.norm[ColBG] = initcolor(NORMBGCOLOR);
- dc.norm[ColFG] = initcolor(NORMFGCOLOR);
- dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
- dc.sel[ColBG] = initcolor(SELBGCOLOR);
- dc.sel[ColFG] = initcolor(SELFGCOLOR);
- initfont(FONT);
- dc.h = bh = dc.font.height + 2;
-}
-
 static Bool
 isarrange(void (*func)())
 {
         return func == layouts[ltidx].arrange;
-}
-
-static Bool
-isfloating(void) {
- return layouts[ltidx].arrange == floating;
 }
 
 static Bool
@@ -1300,9 +1263,9 @@ restack(void) {
         drawbar();
         if(!sel)
                 return;
- if(sel->isfloating || isfloating())
+ if(sel->isfloating || isarrange(floating))
                 XRaiseWindow(dpy, sel->win);
- if(!isfloating()) {
+ if(!isarrange(floating)) {
                 wc.stack_mode = Below;
                 wc.sibling = barwin;
                 if(!sel->isfloating) {
@@ -1321,517 +1284,15 @@ restack(void) {
 }
 
 static void
-scan(void) {
- unsigned int i, num;
- Window *wins, d1, d2;
- XWindowAttributes wa;
-
- wins = NULL;
- if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
- for(i = 0; i < num; i++) {
- if(!XGetWindowAttributes(dpy, wins[i], &wa)
- || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
- continue;
- if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
- manage(wins[i], &wa);
- }
- for(i = 0; i < num; i++) { /* now the transients */
- if(!XGetWindowAttributes(dpy, wins[i], &wa))
- continue;
- if(XGetTransientForHint(dpy, wins[i], &d1)
- && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
- manage(wins[i], &wa);
- }
- }
- if(wins)
- XFree(wins);
-}
-
-static void
-setclientstate(Client *c, long state) {
- long data[] = {state, None};
-
- XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
- PropModeReplace, (unsigned char *)data, 2);
-}
-
-static void
-setlayout(const char *arg) {
- unsigned int i;
-
- if(!arg) {
- if(++ltidx == nlayouts)
- ltidx = 0;;
- }
- else {
- for(i = 0; i < nlayouts; i++)
- if(!strcmp(arg, layouts[i].symbol))
- break;
- if(i == nlayouts)
- return;
- ltidx = i;
- }
- if(sel)
- arrange();
- else
- drawbar();
-}
-
-static void
-setmwfact(const char *arg) {
- double delta;
-
- if(!isarrange(tile))
- return;
- /* arg handling, manipulate mwfact */
- if(arg == NULL)
- mwfact = MWFACT;
- else if(1 == sscanf(arg, "%lf", &delta)) {
- if(arg[0] != '+' && arg[0] != '-')
- mwfact = delta;
- else
- mwfact += delta;
- if(mwfact < 0.1)
- mwfact = 0.1;
- else if(mwfact > 0.9)
- mwfact = 0.9;
- }
- arrange();
-}
-
-static void
-setup(void) {
- int i, j;
- unsigned int mask;
- Window w;
- XModifierKeymap *modmap;
- XSetWindowAttributes wa;
-
- /* init atoms */
- wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
- wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
- wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
- wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
- netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
- netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
- XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
- PropModeReplace, (unsigned char *) netatom, NetLast);
- /* init cursors */
- cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
- cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
- cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
- /* init modifier map */
- modmap = XGetModifierMapping(dpy);
- for (i = 0; i < 8; i++)
- for (j = 0; j < modmap->max_keypermod; j++) {
- if(modmap->modifiermap[i * modmap->max_keypermod + j]
- == XKeysymToKeycode(dpy, XK_Num_Lock))
- numlockmask = (1 << i);
- }
- XFreeModifiermap(modmap);
- /* select for events */
- wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
- | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
- wa.cursor = cursor[CurNormal];
- XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
- XSelectInput(dpy, root, wa.event_mask);
- keypress(NULL); /* grabkeys */
- compileregs();
- for(ntags = 0; tags[ntags]; ntags++);
- seltags = emallocz(sizeof(Bool) * ntags);
- seltags[0] = True;
- /* geometry */
- sx = sy = 0;
- sw = DisplayWidth(dpy, screen);
- sh = DisplayHeight(dpy, screen);
- initstyle();
- initlayouts();
- initbar();
- /* multihead support */
- selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
-}
-
-static void
-spawn(const char *arg) {
- static char *shell = NULL;
-
- if(!shell && !(shell = getenv("SHELL")))
- shell = "/bin/sh";
- if(!arg)
- return;
- /* The double-fork construct avoids zombie processes and keeps the code
- * clean from stupid signal handlers. */
- if(fork() == 0) {
- if(fork() == 0) {
- if(dpy)
- close(ConnectionNumber(dpy));
- setsid();
- execl(shell, shell, "-c", arg, (char *)NULL);
- fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
- perror(" failed");
- }
- exit(0);
- }
- wait(0);
-}
-
-static void
-tag(const char *arg) {
- unsigned int i;
-
- if(!sel)
- return;
- for(i = 0; i < ntags; i++)
- sel->tags[i] = arg == NULL;
- i = idxoftag(arg);
- if(i >= 0 && i < ntags)
- sel->tags[i] = True;
- arrange();
-}
-
-static unsigned int
-textnw(const char *text, unsigned int len) {
- XRectangle r;
-
- if(dc.font.set) {
- XmbTextExtents(dc.font.set, text, len, NULL, &r);
- return r.width;
- }
- return XTextWidth(dc.font.xfont, text, len);
-}
-
-static unsigned int
-textw(const char *text) {
- return textnw(text, strlen(text)) + dc.font.height;
-}
-
-static void
-tile(void) {
- unsigned int i, n, nx, ny, nw, nh, mw, th;
- Client *c;
-
- for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
- n++;
-
- /* window geoms */
- mw = (n == 1) ? waw : mwfact * waw;
- th = (n > 1) ? wah / (n - 1) : 0;
- if(n > 1 && th < bh)
- th = wah;
-
- nx = wax;
- ny = way;
- for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) {
- c->ismax = False;
- if(i == 0) { /* master */
- nw = mw - 2 * c->border;
- nh = wah - 2 * c->border;
- }
- else { /* tile window */
- if(i == 1) {
- ny = way;
- nx += mw;
- }
- nw = waw - mw - 2 * c->border;
- if(i + 1 == n) /* remainder */
- nh = (way + wah) - ny - 2 * c->border;
- else
- nh = th - 2 * c->border;
- }
- resize(c, nx, ny, nw, nh, RESIZEHINTS);
- if(n > 1 && th != wah)
- ny += nh + 2 * c->border;
- }
-}
-
-static void
-togglebar(const char *arg) {
- if(bpos == BarOff)
- bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
- else
- bpos = BarOff;
- updatebarpos();
- arrange();
-}
-
-static void
-togglefloating(const char *arg) {
- if(!sel)
- return;
- sel->isfloating = !sel->isfloating;
- if(sel->isfloating)
- resize(sel, sel->x, sel->y, sel->w, sel->h, True);
- arrange();
-}
-
-static void
-togglemax(const char *arg) {
- XEvent ev;
-
- if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
- return;
- if((sel->ismax = !sel->ismax)) {
- sel->rx = sel->x;
- sel->ry = sel->y;
- sel->rw = sel->w;
- sel->rh = sel->h;
- resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
- }
- else
- resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
- drawbar();
- while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
-}
-
-static void
-toggletag(const char *arg) {
- unsigned int i, j;
-
- if(!sel)
- return;
- i = idxoftag(arg);
- sel->tags[i] = !sel->tags[i];
- for(j = 0; j < ntags && !sel->tags[j]; j++);
- if(j == ntags)
- sel->tags[i] = True;
- arrange();
-}
-
-static void
-toggleview(const char *arg) {
- unsigned int i, j;
-
- i = idxoftag(arg);
- seltags[i] = !seltags[i];
- for(j = 0; j < ntags && !seltags[j]; j++);
- if(j == ntags)
- seltags[i] = True; /* cannot toggle last view */
- arrange();
-}
-
-static void
-unban(Client *c) {
- if(!c->isbanned)
- return;
- XMoveWindow(dpy, c->win, c->x, c->y);
- c->isbanned = False;
-}
-
-static void
-unmanage(Client *c) {
- XWindowChanges wc;
-
- wc.border_width = c->oldborder;
- /* The server grab construct avoids race conditions. */
- XGrabServer(dpy);
- XSetErrorHandler(xerrordummy);
- XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
- detach(c);
- detachstack(c);
- if(sel == c)
- focus(NULL);
- XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
- setclientstate(c, WithdrawnState);
- free(c->tags);
- free(c);
- XSync(dpy, False);
- XSetErrorHandler(xerror);
- XUngrabServer(dpy);
- arrange();
-}
-
-static void
-unmapnotify(XEvent *e) {
- Client *c;
- XUnmapEvent *ev = &e->xunmap;
-
- if((c = getclient(ev->window)))
- unmanage(c);
-}
-
-static void
-updatebarpos(void) {
- XEvent ev;
-
- wax = sx;
- way = sy;
- wah = sh;
- waw = sw;
- switch(bpos) {
- default:
- wah -= bh;
- way += bh;
- XMoveWindow(dpy, barwin, sx, sy);
- break;
- case BarBot:
- wah -= bh;
- XMoveWindow(dpy, barwin, sx, sy + wah);
- break;
- case BarOff:
- XMoveWindow(dpy, barwin, sx, sy - bh);
- break;
- }
- XSync(dpy, False);
- while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
-}
-
-static void
-updatesizehints(Client *c) {
- long msize;
- XSizeHints size;
-
- if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
- size.flags = PSize;
- c->flags = size.flags;
- if(c->flags & PBaseSize) {
- c->basew = size.base_width;
- c->baseh = size.base_height;
- }
- else if(c->flags & PMinSize) {
- c->basew = size.min_width;
- c->baseh = size.min_height;
- }
- else
- c->basew = c->baseh = 0;
- if(c->flags & PResizeInc) {
- c->incw = size.width_inc;
- c->inch = size.height_inc;
- }
- else
- c->incw = c->inch = 0;
- if(c->flags & PMaxSize) {
- c->maxw = size.max_width;
- c->maxh = size.max_height;
- }
- else
- c->maxw = c->maxh = 0;
- if(c->flags & PMinSize) {
- c->minw = size.min_width;
- c->minh = size.min_height;
- }
- else if(c->flags & PBaseSize) {
- c->minw = size.base_width;
- c->minh = size.base_height;
- }
- else
- c->minw = c->minh = 0;
- if(c->flags & PAspect) {
- c->minax = size.min_aspect.x;
- c->maxax = size.max_aspect.x;
- c->minay = size.min_aspect.y;
- c->maxay = size.max_aspect.y;
- }
- else
- c->minax = c->maxax = c->minay = c->maxay = 0;
- c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
- && c->maxw == c->minw && c->maxh == c->minh);
-}
-
-static void
-updatetitle(Client *c) {
- if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
- gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
-}
-
-/* There's no way to check accesses to destroyed windows, thus those cases are
- * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
- * default error handler, which may call exit. */
-static int
-xerror(Display *dpy, XErrorEvent *ee) {
- if(ee->error_code == BadWindow
- || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
- || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
- || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
- || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
- || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
- || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
- || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
- return 0;
- fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
- ee->request_code, ee->error_code);
- return xerrorxlib(dpy, ee); /* may call exit */
-}
-
-static int
-xerrordummy(Display *dsply, XErrorEvent *ee) {
- return 0;
-}
-
-/* Startup Error handler to check if another window manager
- * is already running. */
-static int
-xerrorstart(Display *dsply, XErrorEvent *ee) {
- otherwm = True;
- return -1;
-}
-
-static void
-view(const char *arg) {
- unsigned int i;
-
- for(i = 0; i < ntags; i++)
- seltags[i] = arg == NULL;
- i = idxoftag(arg);
- if(i >= 0 && i < ntags)
- seltags[i] = True;
- arrange();
-}
-
-static void
-zoom(const char *arg) {
- Client *c;
-
- if(!sel || !isarrange(tile) || sel->isfloating)
- return;
- if((c = sel) == nexttiled(clients))
- if(!(c = nexttiled(c->next)))
- return;
- detach(c);
- attach(c);
- focus(c);
- arrange();
-}
-
-int
-main(int argc, char *argv[]) {
+run(void) {
         char *p;
         int r, xfd;
         fd_set rd;
         XEvent ev;
 
- if(argc == 2 && !strcmp("-v", argv[1]))
- eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
- else if(argc != 1)
- eprint("usage: dwm [-v]\n");
-
- /* macros from config.h can be used at function level only */
- mwfact = MWFACT;
- bpos = BARPOS;
-
- setlocale(LC_CTYPE, "");
- if(!(dpy = XOpenDisplay(0)))
- eprint("dwm: cannot open display\n");
- xfd = ConnectionNumber(dpy);
- screen = DefaultScreen(dpy);
- root = RootWindow(dpy, screen);
- otherwm = False;
- XSetErrorHandler(xerrorstart);
- /* this causes an error if some other window manager is running */
- XSelectInput(dpy, root, SubstructureRedirectMask);
- XSync(dpy, False);
- if(otherwm)
- eprint("dwm: another window manager is already running\n");
-
- XSync(dpy, False);
- XSetErrorHandler(NULL);
- xerrorxlib = XSetErrorHandler(xerror);
- XSync(dpy, False);
- setup();
- drawbar();
- scan();
-
         /* main event loop, also reads status text from stdin */
         XSync(dpy, False);
+ xfd = ConnectionNumber(dpy);
         readin = True;
         while(running) {
                 FD_ZERO(&rd);
@@ -1868,7 +1329,542 @@ main(int argc, char *argv[]) {
                                 (handler[ev.type])(&ev); /* call handler */
                 }
         }
+}
+
+static void
+scan(void) {
+ unsigned int i, num;
+ Window *wins, d1, d2;
+ XWindowAttributes wa;
+
+ wins = NULL;
+ if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
+ for(i = 0; i < num; i++) {
+ if(!XGetWindowAttributes(dpy, wins[i], &wa)
+ || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
+ continue;
+ if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
+ manage(wins[i], &wa);
+ }
+ for(i = 0; i < num; i++) { /* now the transients */
+ if(!XGetWindowAttributes(dpy, wins[i], &wa))
+ continue;
+ if(XGetTransientForHint(dpy, wins[i], &d1)
+ && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
+ manage(wins[i], &wa);
+ }
+ }
+ if(wins)
+ XFree(wins);
+}
+
+static void
+setclientstate(Client *c, long state) {
+ long data[] = {state, None};
+
+ XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
+ PropModeReplace, (unsigned char *)data, 2);
+}
+
+static void
+setlayout(const char *arg) {
+ unsigned int i;
+
+ if(!arg) {
+ if(++ltidx == nlayouts)
+ ltidx = 0;;
+ }
+ else {
+ for(i = 0; i < nlayouts; i++)
+ if(!strcmp(arg, layouts[i].symbol))
+ break;
+ if(i == nlayouts)
+ return;
+ ltidx = i;
+ }
+ if(sel)
+ arrange();
+ else
+ drawbar();
+}
+
+static void
+setmwfact(const char *arg) {
+ double delta;
+
+ if(!isarrange(tile))
+ return;
+ /* arg handling, manipulate mwfact */
+ if(arg == NULL)
+ mwfact = MWFACT;
+ else if(1 == sscanf(arg, "%lf", &delta)) {
+ if(arg[0] != '+' && arg[0] != '-')
+ mwfact = delta;
+ else
+ mwfact += delta;
+ if(mwfact < 0.1)
+ mwfact = 0.1;
+ else if(mwfact > 0.9)
+ mwfact = 0.9;
+ }
+ arrange();
+}
+
+static void
+setup(void) {
+ unsigned int i, j, mask;
+ Window w;
+ XModifierKeymap *modmap;
+ XSetWindowAttributes wa;
+
+ /* init atoms */
+ wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
+ wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
+ wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
+ wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
+ netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
+ netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
+ XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
+ PropModeReplace, (unsigned char *) netatom, NetLast);
+
+ /* init cursors */
+ cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
+ cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
+ cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
+
+ /* init geometry */
+ sx = sy = 0;
+ sw = DisplayWidth(dpy, screen);
+ sh = DisplayHeight(dpy, screen);
+
+ /* init modifier map */
+ modmap = XGetModifierMapping(dpy);
+ for(i = 0; i < 8; i++)
+ for(j = 0; j < modmap->max_keypermod; j++) {
+ if(modmap->modifiermap[i * modmap->max_keypermod + j]
+ == XKeysymToKeycode(dpy, XK_Num_Lock))
+ numlockmask = (1 << i);
+ }
+ XFreeModifiermap(modmap);
+
+ /* select for events */
+ wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
+ | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
+ wa.cursor = cursor[CurNormal];
+ XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
+ XSelectInput(dpy, root, wa.event_mask);
+
+ /* grab keys */
+ keypress(NULL);
+
+ /* init tags */
+ compileregs();
+ for(ntags = 0; tags[ntags]; ntags++);
+ seltags = emallocz(sizeof(Bool) * ntags);
+ seltags[0] = True;
+
+ /* init appearance */
+ dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
+ dc.norm[ColBG] = getcolor(NORMBGCOLOR);
+ dc.norm[ColFG] = getcolor(NORMFGCOLOR);
+ dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
+ dc.sel[ColBG] = getcolor(SELBGCOLOR);
+ dc.sel[ColFG] = getcolor(SELFGCOLOR);
+ initfont(FONT);
+ dc.h = bh = dc.font.height + 2;
+
+ /* init layouts */
+ mwfact = MWFACT;
+ nlayouts = sizeof layouts / sizeof layouts[0];
+ for(blw = i = 0; i < nlayouts; i++) {
+ j = textw(layouts[i].symbol);
+ if(j > blw)
+ blw = j;
+ }
+
+ /* init bar */
+ bpos = BARPOS;
+ wa.override_redirect = 1;
+ wa.background_pixmap = ParentRelative;
+ wa.event_mask = ButtonPressMask | ExposureMask;
+ barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
+ DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
+ CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
+ XDefineCursor(dpy, barwin, cursor[CurNormal]);
+ updatebarpos();
+ XMapRaised(dpy, barwin);
+ strcpy(stext, "dwm-"VERSION);
+ dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
+ dc.gc = XCreateGC(dpy, root, 0, 0);
+ XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
+ if(!dc.font.set)
+ XSetFont(dpy, dc.gc, dc.font.xfont->fid);
+
+ /* multihead support */
+ selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
+}
+
+static void
+spawn(const char *arg) {
+ static char *shell = NULL;
+
+ if(!shell && !(shell = getenv("SHELL")))
+ shell = "/bin/sh";
+ if(!arg)
+ return;
+ /* The double-fork construct avoids zombie processes and keeps the code
+ * clean from stupid signal handlers. */
+ if(fork() == 0) {
+ if(fork() == 0) {
+ if(dpy)
+ close(ConnectionNumber(dpy));
+ setsid();
+ execl(shell, shell, "-c", arg, (char *)NULL);
+ fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
+ perror(" failed");
+ }
+ exit(0);
+ }
+ wait(0);
+}
+
+static void
+tag(const char *arg) {
+ unsigned int i;
+
+ if(!sel)
+ return;
+ for(i = 0; i < ntags; i++)
+ sel->tags[i] = arg == NULL;
+ i = idxoftag(arg);
+ if(i >= 0 && i < ntags)
+ sel->tags[i] = True;
+ arrange();
+}
+
+static unsigned int
+textnw(const char *text, unsigned int len) {
+ XRectangle r;
+
+ if(dc.font.set) {
+ XmbTextExtents(dc.font.set, text, len, NULL, &r);
+ return r.width;
+ }
+ return XTextWidth(dc.font.xfont, text, len);
+}
+
+static unsigned int
+textw(const char *text) {
+ return textnw(text, strlen(text)) + dc.font.height;
+}
+
+static void
+tile(void) {
+ unsigned int i, n, nx, ny, nw, nh, mw, th;
+ Client *c;
+
+ for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
+ n++;
+
+ /* window geoms */
+ mw = (n == 1) ? waw : mwfact * waw;
+ th = (n > 1) ? wah / (n - 1) : 0;
+ if(n > 1 && th < bh)
+ th = wah;
+
+ nx = wax;
+ ny = way;
+ for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) {
+ c->ismax = False;
+ if(i == 0) { /* master */
+ nw = mw - 2 * c->border;
+ nh = wah - 2 * c->border;
+ }
+ else { /* tile window */
+ if(i == 1) {
+ ny = way;
+ nx += mw;
+ }
+ nw = waw - mw - 2 * c->border;
+ if(i + 1 == n) /* remainder */
+ nh = (way + wah) - ny - 2 * c->border;
+ else
+ nh = th - 2 * c->border;
+ }
+ resize(c, nx, ny, nw, nh, RESIZEHINTS);
+ if(n > 1 && th != wah)
+ ny += nh + 2 * c->border;
+ }
+}
+
+static void
+togglebar(const char *arg) {
+ if(bpos == BarOff)
+ bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
+ else
+ bpos = BarOff;
+ updatebarpos();
+ arrange();
+}
+
+static void
+togglefloating(const char *arg) {
+ if(!sel)
+ return;
+ sel->isfloating = !sel->isfloating;
+ if(sel->isfloating)
+ resize(sel, sel->x, sel->y, sel->w, sel->h, True);
+ arrange();
+}
+
+static void
+togglemax(const char *arg) {
+ XEvent ev;
+
+ if(!sel || (!isarrange(floating) && !sel->isfloating) || sel->isfixed)
+ return;
+ if((sel->ismax = !sel->ismax)) {
+ sel->rx = sel->x;
+ sel->ry = sel->y;
+ sel->rw = sel->w;
+ sel->rh = sel->h;
+ resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
+ }
+ else
+ resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
+ drawbar();
+ while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
+}
+
+static void
+toggletag(const char *arg) {
+ unsigned int i, j;
+
+ if(!sel)
+ return;
+ i = idxoftag(arg);
+ sel->tags[i] = !sel->tags[i];
+ for(j = 0; j < ntags && !sel->tags[j]; j++);
+ if(j == ntags)
+ sel->tags[i] = True;
+ arrange();
+}
+
+static void
+toggleview(const char *arg) {
+ unsigned int i, j;
+
+ i = idxoftag(arg);
+ seltags[i] = !seltags[i];
+ for(j = 0; j < ntags && !seltags[j]; j++);
+ if(j == ntags)
+ seltags[i] = True; /* cannot toggle last view */
+ arrange();
+}
+
+static void
+unban(Client *c) {
+ if(!c->isbanned)
+ return;
+ XMoveWindow(dpy, c->win, c->x, c->y);
+ c->isbanned = False;
+}
+
+static void
+unmanage(Client *c) {
+ XWindowChanges wc;
+
+ wc.border_width = c->oldborder;
+ /* The server grab construct avoids race conditions. */
+ XGrabServer(dpy);
+ XSetErrorHandler(xerrordummy);
+ XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
+ detach(c);
+ detachstack(c);
+ if(sel == c)
+ focus(NULL);
+ XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
+ setclientstate(c, WithdrawnState);
+ free(c->tags);
+ free(c);
+ XSync(dpy, False);
+ XSetErrorHandler(xerror);
+ XUngrabServer(dpy);
+ arrange();
+}
+
+static void
+unmapnotify(XEvent *e) {
+ Client *c;
+ XUnmapEvent *ev = &e->xunmap;
+
+ if((c = getclient(ev->window)))
+ unmanage(c);
+}
+
+static void
+updatebarpos(void) {
+ XEvent ev;
+
+ wax = sx;
+ way = sy;
+ wah = sh;
+ waw = sw;
+ switch(bpos) {
+ default:
+ wah -= bh;
+ way += bh;
+ XMoveWindow(dpy, barwin, sx, sy);
+ break;
+ case BarBot:
+ wah -= bh;
+ XMoveWindow(dpy, barwin, sx, sy + wah);
+ break;
+ case BarOff:
+ XMoveWindow(dpy, barwin, sx, sy - bh);
+ break;
+ }
+ XSync(dpy, False);
+ while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
+}
+
+static void
+updatesizehints(Client *c) {
+ long msize;
+ XSizeHints size;
+
+ if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
+ size.flags = PSize;
+ c->flags = size.flags;
+ if(c->flags & PBaseSize) {
+ c->basew = size.base_width;
+ c->baseh = size.base_height;
+ }
+ else if(c->flags & PMinSize) {
+ c->basew = size.min_width;
+ c->baseh = size.min_height;
+ }
+ else
+ c->basew = c->baseh = 0;
+ if(c->flags & PResizeInc) {
+ c->incw = size.width_inc;
+ c->inch = size.height_inc;
+ }
+ else
+ c->incw = c->inch = 0;
+ if(c->flags & PMaxSize) {
+ c->maxw = size.max_width;
+ c->maxh = size.max_height;
+ }
+ else
+ c->maxw = c->maxh = 0;
+ if(c->flags & PMinSize) {
+ c->minw = size.min_width;
+ c->minh = size.min_height;
+ }
+ else if(c->flags & PBaseSize) {
+ c->minw = size.base_width;
+ c->minh = size.base_height;
+ }
+ else
+ c->minw = c->minh = 0;
+ if(c->flags & PAspect) {
+ c->minax = size.min_aspect.x;
+ c->maxax = size.max_aspect.x;
+ c->minay = size.min_aspect.y;
+ c->maxay = size.max_aspect.y;
+ }
+ else
+ c->minax = c->maxax = c->minay = c->maxay = 0;
+ c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
+ && c->maxw == c->minw && c->maxh == c->minh);
+}
+
+static void
+updatetitle(Client *c) {
+ if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
+ gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
+}
+
+/* There's no way to check accesses to destroyed windows, thus those cases are
+ * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
+ * default error handler, which may call exit. */
+static int
+xerror(Display *dpy, XErrorEvent *ee) {
+ if(ee->error_code == BadWindow
+ || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
+ || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
+ || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
+ || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
+ || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
+ || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
+ || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
+ return 0;
+ fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
+ ee->request_code, ee->error_code);
+ return xerrorxlib(dpy, ee); /* may call exit */
+}
+
+static int
+xerrordummy(Display *dsply, XErrorEvent *ee) {
+ return 0;
+}
+
+/* Startup Error handler to check if another window manager
+ * is already running. */
+static int
+xerrorstart(Display *dsply, XErrorEvent *ee) {
+ otherwm = True;
+ return -1;
+}
+
+static void
+view(const char *arg) {
+ unsigned int i;
+
+ for(i = 0; i < ntags; i++)
+ seltags[i] = arg == NULL;
+ i = idxoftag(arg);
+ if(i >= 0 && i < ntags)
+ seltags[i] = True;
+ arrange();
+}
+
+static void
+zoom(const char *arg) {
+ Client *c;
+
+ if(!sel || !isarrange(tile) || sel->isfloating)
+ return;
+ if((c = sel) == nexttiled(clients))
+ if(!(c = nexttiled(c->next)))
+ return;
+ detach(c);
+ attach(c);
+ focus(c);
+ arrange();
+}
+
+int
+main(int argc, char *argv[]) {
+ if(argc == 2 && !strcmp("-v", argv[1]))
+ eprint("dwm-"VERSION", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
+ else if(argc != 1)
+ eprint("usage: dwm [-v]\n");
+
+ setlocale(LC_CTYPE, "");
+ if(!(dpy = XOpenDisplay(0)))
+ eprint("dwm: cannot open display\n");
+ screen = DefaultScreen(dpy);
+ root = RootWindow(dpy, screen);
+
+ checkotherwm();
+ setup();
+ drawbar();
+ scan();
+ run();
         cleanup();
+
         XCloseDisplay(dpy);
         return 0;
 }
Received on Sun Sep 16 2007 - 12:37:33 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:58:08 UTC