X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=dwm.c;h=3699f9955334b9147af73d6eda52419227cbe9ab;hb=5f19423c7bd2aa1ebb3010af15bebffbc3a9cbc3;hp=8933e8e2df6982cb460d9b567c1ab36e9b55730c;hpb=34e7872c89613356293d554970d9d56adc4b0a4e;p=dwm.git diff --git a/dwm.c b/dwm.c index 8933e8e..3699f99 100644 --- a/dwm.c +++ b/dwm.c @@ -17,9 +17,7 @@ * set the override_redirect flag. Clients are organized in a global * doubly-linked client list, the focus history is remembered through a global * stack list. Each client contains an array of Bools of the same size as the - * global tags array to indicate the tags of a client. For each client dwm - * creates a small title window, which is resized whenever the (_NET_)WM_NAME - * properties are updated or the client is moved/resized. + * global tags array to indicate the tags of a client. * * Keys and tagging rules are organized as arrays and defined in config.h. * @@ -46,8 +44,11 @@ /* macros */ #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask) #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) +#define LENGTH(x) (sizeof x / sizeof x[0]) +#define MAXTAGLEN 16 #define MOUSEMASK (BUTTONMASK | PointerMotionMask) + /* enums */ enum { BarTop, BarBot, BarOff }; /* bar position */ enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ @@ -60,12 +61,11 @@ typedef struct Client Client; struct Client { char name[256]; int x, y, w, h; - int rx, ry, rw, rh; /* revert geometry */ int basew, baseh, incw, inch, maxw, maxh, minw, minh; int minax, maxax, minay, maxay; long flags; unsigned int border, oldborder; - Bool isbanned, isfixed, ismax, isfloating, wasfloating; + Bool isbanned, isfixed, isfloating; Bool *tags; Client *next; Client *prev; @@ -136,6 +136,7 @@ void eprint(const char *errstr, ...); void expose(XEvent *e); void floating(void); /* default floating layout */ void focus(Client *c); +void focusin(XEvent *e); void focusnext(const char *arg); void focusprev(const char *arg); Client *getclient(Window w); @@ -143,6 +144,7 @@ unsigned long getcolor(const char *colstr); long getstate(Window w); Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); void grabbuttons(Client *c, Bool focused); +void grabkeys(void); unsigned int idxoftag(const char *tag); void initfont(const char *fontstr); Bool isoccupied(unsigned int t); @@ -154,10 +156,12 @@ void leavenotify(XEvent *e); void manage(Window w, XWindowAttributes *wa); void mappingnotify(XEvent *e); void maprequest(XEvent *e); +void maximize(const char *arg); void movemouse(Client *c); Client *nexttiled(Client *c); void propertynotify(XEvent *e); void quit(const char *arg); +void reapply(const char *arg); void resize(Client *c, int x, int y, int w, int h, Bool sizehints); void resizemouse(Client *c); void restack(void); @@ -174,7 +178,6 @@ unsigned int textw(const char *text); void tile(void); void togglebar(const char *arg); void togglefloating(const char *arg); -void togglemax(const char *arg); void toggletag(const char *arg); void toggleview(const char *arg); void unban(Client *c); @@ -204,9 +207,10 @@ void (*handler[LASTEvent]) (XEvent *) = { [ConfigureNotify] = configurenotify, [DestroyNotify] = destroynotify, [EnterNotify] = enternotify, - [LeaveNotify] = leavenotify, [Expose] = expose, + [FocusIn] = focusin, [KeyPress] = keypress, + [LeaveNotify] = leavenotify, [MappingNotify] = mappingnotify, [MapRequest] = maprequest, [PropertyNotify] = propertynotify, @@ -231,6 +235,8 @@ Regs *regs = NULL; /* configuration, allows nested code to access above variables */ #include "config.h" +Bool prevtags[LENGTH(tags)]; + /* function implementations */ void applyrules(Client *c) { @@ -305,7 +311,7 @@ buttonpress(XEvent *e) { Client *c; XButtonPressedEvent *ev = &e->xbutton; - if(barwin == ev->window) { + if(ev->window == barwin) { x = 0; for(i = 0; i < LENGTH(tags); i++) { x += textw(tags[i]); @@ -333,14 +339,14 @@ buttonpress(XEvent *e) { if(CLEANMASK(ev->state) != MODKEY) return; if(ev->button == Button1) { - if((floating == layout->arrange) || c->isfloating) + if((layout->arrange == floating) || c->isfloating) restack(); else togglefloating(NULL); movemouse(c); } else if(ev->button == Button2) { - if((floating != layout->arrange) && !c->isfixed && c->isfloating) + if((floating != layout->arrange) && c->isfloating) togglefloating(NULL); else zoom(NULL); @@ -459,7 +465,6 @@ configurerequest(XEvent *e) { XWindowChanges wc; if((c = getclient(ev->window))) { - c->ismax = False; if(ev->value_mask & CWBorderWidth) c->border = ev->border_width; if(c->isfixed || c->isfloating || (floating == layout->arrange)) { @@ -556,7 +561,7 @@ drawbar(void) { dc.x = x; if(sel) { drawtext(sel->name, dc.sel); - drawsquare(sel->ismax, sel->isfloating, dc.sel); + drawsquare(False, sel->isfloating, dc.sel); } else drawtext(NULL, dc.norm); @@ -664,8 +669,8 @@ void expose(XEvent *e) { XExposeEvent *ev = &e->xexpose; - if(0 == ev->count) { - if(barwin == ev->window) + if(ev->count == 0) { + if(ev->window == barwin) drawbar(); } } @@ -706,6 +711,14 @@ focus(Client *c) { } void +focusin(XEvent *e) { /* there are some broken focus acquiring clients */ + XFocusChangeEvent *ev = &e->xfocus; + + if(sel && ev->window != sel->win) + XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime); +} + +void focusnext(const char *arg) { Client *c; @@ -779,7 +792,7 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) { int n; XTextProperty name; - if(!text || 0 == size) + if(!text || size == 0) return False; text[0] = '\0'; XGetTextProperty(dpy, w, &name, atom); @@ -789,8 +802,7 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) { strncpy(text, (char *)name.value, size - 1); else { if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success - && n > 0 && *list) - { + && n > 0 && *list) { strncpy(text, *list, size - 1); XFreeStringList(list); } @@ -837,6 +849,25 @@ grabbuttons(Client *c, Bool focused) { GrabModeAsync, GrabModeSync, None, None); } +void +grabkeys(void) { + unsigned int i; + KeyCode code; + + XUngrabKey(dpy, AnyKey, AnyModifier, root); + for(i = 0; i < LENGTH(keys); i++) { + code = XKeysymToKeycode(dpy, keys[i].keysym); + XGrabKey(dpy, code, keys[i].mod, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, keys[i].mod | LockMask, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True, + GrabModeAsync, GrabModeAsync); + } +} + unsigned int idxoftag(const char *tag) { unsigned int i; @@ -924,27 +955,10 @@ isvisible(Client *c) { void keypress(XEvent *e) { - KEYS unsigned int i; - KeyCode code; KeySym keysym; XKeyEvent *ev; - if(!e) { /* grabkeys */ - XUngrabKey(dpy, AnyKey, AnyModifier, root); - for(i = 0; i < LENGTH(keys); i++) { - code = XKeysymToKeycode(dpy, keys[i].keysym); - XGrabKey(dpy, code, keys[i].mod, root, True, - GrabModeAsync, GrabModeAsync); - XGrabKey(dpy, code, keys[i].mod | LockMask, root, True, - GrabModeAsync, GrabModeAsync); - XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True, - GrabModeAsync, GrabModeAsync); - XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True, - GrabModeAsync, GrabModeAsync); - } - return; - } ev = &e->xkey; keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); for(i = 0; i < LENGTH(keys); i++) @@ -1021,8 +1035,7 @@ manage(Window w, XWindowAttributes *wa) { XSetWindowBorder(dpy, w, dc.norm[ColBorder]); configure(c); /* propagates border_width, if size doesn't change */ updatesizehints(c); - XSelectInput(dpy, w, - StructureNotifyMask | PropertyChangeMask | EnterWindowMask); + XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask); grabbuttons(c, False); updatetitle(c); if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success)) @@ -1047,7 +1060,7 @@ mappingnotify(XEvent *e) { XRefreshKeyboardMapping(ev); if(ev->request == MappingKeyboard) - keypress(NULL); + grabkeys(); } void @@ -1064,6 +1077,13 @@ maprequest(XEvent *e) { } void +maximize(const char *arg) { + if(!sel || (!sel->isfloating && layout->arrange != floating)) + return; + resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True); +} + +void movemouse(Client *c) { int x1, y1, ocx, ocy, di, nx, ny; unsigned int dui; @@ -1075,7 +1095,6 @@ movemouse(Client *c) { if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, cursor[CurMove], CurrentTime) != GrabSuccess) return; - c->ismax = False; XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); for(;;) { XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev); @@ -1125,7 +1144,7 @@ propertynotify(XEvent *e) { default: break; case XA_WM_TRANSIENT_FOR: XGetTransientForHint(dpy, c->win, &trans); - if(!c->isfloating && (c->isfloating = (NULL != getclient(trans)))) + if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL))) arrange(); break; case XA_WM_NORMAL_HINTS: @@ -1146,6 +1165,18 @@ quit(const char *arg) { } void +reapply(const char *arg) { + static Bool zerotags[LENGTH(tags)] = { 0 }; + Client *c; + + for(c = clients; c; c = c->next) { + memcpy(c->tags, zerotags, sizeof zerotags); + applyrules(c); + } + arrange(); +} + +void resize(Client *c, int x, int y, int w, int h, Bool sizehints) { XWindowChanges wc; @@ -1221,7 +1252,6 @@ resizemouse(Client *c) { if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, cursor[CurResize], CurrentTime) != GrabSuccess) return; - c->ismax = False; XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1); for(;;) { XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev); @@ -1258,9 +1288,9 @@ restack(void) { drawbar(); if(!sel) return; - if(sel->isfloating || (floating == layout->arrange)) + if(sel->isfloating || (layout->arrange == floating)) XRaiseWindow(dpy, sel->win); - if(floating != layout->arrange) { + if(layout->arrange != floating) { wc.stack_mode = Below; wc.sibling = barwin; if(!sel->isfloating) { @@ -1281,14 +1311,19 @@ restack(void) { void run(void) { char *p; - int r, xfd; + char buf[sizeof stext]; fd_set rd; + int r, xfd; + unsigned int len, offset; XEvent ev; /* main event loop, also reads status text from stdin */ XSync(dpy, False); xfd = ConnectionNumber(dpy); readin = True; + offset = 0; + len = sizeof stext - 1; + buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */ while(running) { FD_ZERO(&rd); if(readin) @@ -1300,10 +1335,9 @@ run(void) { eprint("select failed\n"); } if(FD_ISSET(STDIN_FILENO, &rd)) { - switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) { + switch((r = read(STDIN_FILENO, buf + offset, len - offset))) { case -1: - strncpy(stext, strerror(errno), sizeof stext - 1); - stext[sizeof stext - 1] = '\0'; + strncpy(stext, strerror(errno), len); readin = False; break; case 0: @@ -1311,10 +1345,18 @@ run(void) { readin = False; break; default: - for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0'); - for(; p >= stext && *p != '\n'; --p); - if(p > stext) - strncpy(stext, p + 1, sizeof stext); + for(p = buf + offset; r > 0; p++, r--, offset++) + if(*p == '\n' || *p == '\0') { + *p = '\0'; + strncpy(stext, buf, len); + p += r - 1; /* p is buf + offset + r - 1 */ + for(r = 0; *(p - r) && *(p - r) != '\n'; r++); + offset = r; + if(r) + memmove(buf, p - r + 1, r); + break; + } + break; } drawbar(); } @@ -1390,9 +1432,9 @@ setmwfact(const char *arg) { if(!domwfact) return; /* arg handling, manipulate mwfact */ - if(NULL == arg) + if(arg == NULL) mwfact = MWFACT; - else if(1 == sscanf(arg, "%lf", &delta)) { + else if(sscanf(arg, "%lf", &delta) == 1) { if(arg[0] == '+' || arg[0] == '-') mwfact += delta; else @@ -1451,9 +1493,10 @@ setup(void) { XSelectInput(dpy, root, wa.event_mask); /* grab keys */ - keypress(NULL); + grabkeys(); /* init tags */ + memcpy(prevtags, seltags, sizeof seltags); compileregs(); /* init appearance */ @@ -1495,6 +1538,7 @@ setup(void) { /* multihead support */ selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask); + } void @@ -1507,8 +1551,8 @@ spawn(const char *arg) { return; /* The double-fork construct avoids zombie processes and keeps the code * clean from stupid signal handlers. */ - if(0 == fork()) { - if(0 == fork()) { + if(fork() == 0) { + if(fork() == 0) { if(dpy) close(ConnectionNumber(dpy)); setsid(); @@ -1568,8 +1612,7 @@ tile(void) { ny = way; nw = 0; /* gcc stupidity requires this */ for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) { - c->ismax = False; - if(0 == i) { /* master */ + if(i == 0) { /* master */ nw = mw - 2 * c->border; nh = wah - 2 * c->border; } @@ -1584,8 +1627,8 @@ tile(void) { else nh = th - 2 * c->border; } - resize(c, nx, ny, nw, nh, True); - if((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)) + resize(c, nx, ny, nw, nh, RESIZEHINTS); + if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw))) /* client doesn't accept size constraints */ resize(c, nx, ny, nw, nh, False); if(n > 1 && th != wah) @@ -1614,34 +1657,6 @@ togglefloating(const char *arg) { } void -togglemax(const char *arg) { - XEvent ev; - - if(!sel || sel->isfixed) - return; - if((sel->ismax = !sel->ismax)) { - if((floating == layout->arrange) || sel->isfloating) - sel->wasfloating = True; - else { - togglefloating(NULL); - sel->wasfloating = False; - } - 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); - if(!sel->wasfloating) - togglefloating(NULL); - } - drawbar(); - while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); -} - -void toggletag(const char *arg) { unsigned int i, j; @@ -1836,11 +1851,11 @@ view(const char *arg) { void viewprevtag(const char *arg) { - static Bool tmptags[sizeof tags / sizeof tags[0]]; + static Bool tmp[LENGTH(tags)]; - memcpy(tmptags, seltags, sizeof seltags); + memcpy(tmp, seltags, sizeof seltags); memcpy(seltags, prevtags, sizeof seltags); - memcpy(prevtags, tmptags, sizeof seltags); + memcpy(prevtags, tmp, sizeof seltags); arrange(); } @@ -1862,7 +1877,8 @@ zoom(const char *arg) { 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"); + eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, " + "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n"); else if(argc != 1) eprint("usage: dwm [-v]\n");