some more changes towards a better dwm

This commit is contained in:
Anselm R Garbe 2008-02-28 17:07:30 +00:00
parent 00ca643bd7
commit 2f70a14ee1
2 changed files with 49 additions and 61 deletions

View File

@ -11,7 +11,7 @@
#define SELBGCOLOR "#0066ff" #define SELBGCOLOR "#0066ff"
#define SELFGCOLOR "#ffffff" #define SELFGCOLOR "#ffffff"
/* tagging */ /* old */
const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "nil", "6", "7", "8", "www" }; const char tags[][MAXTAGLEN] = { "1", "2", "3", "4", "nil", "6", "7", "8", "www" };
unsigned int vtags[LENGTH(tags)] = { 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 }; unsigned int vtags[LENGTH(tags)] = { 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 };
Bool initags[LENGTH(tags)] = { [0] = True, [5] = True }; Bool initags[LENGTH(tags)] = { [0] = True, [5] = True };

108
dwm.c
View File

@ -71,6 +71,7 @@ struct Client {
unsigned int border, oldborder; unsigned int border, oldborder;
Bool isbanned, isfixed, isfloating, isurgent; Bool isbanned, isfixed, isfloating, isurgent;
Bool *tags; Bool *tags;
View *view;
Client *next; Client *next;
Client *prev; Client *prev;
Client *snext; Client *snext;
@ -110,12 +111,8 @@ typedef struct {
Bool isfloating; Bool isfloating;
} Rule; } Rule;
typedef struct {
const char name[MAXTAGLEN];
unsigned int view;
} Tag;
struct View { struct View {
unsigned int id;
int x, y, w, h, wax, way, wah, waw; int x, y, w, h, wax, way, wah, waw;
double mwfact; double mwfact;
Layout *layout; Layout *layout;
@ -139,8 +136,8 @@ void destroynotify(XEvent *e);
void detach(Client *c); void detach(Client *c);
void detachstack(Client *c); void detachstack(Client *c);
void drawbar(View *v); void drawbar(View *v);
void drawsquare(View *v, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]); void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
void drawtext(View *v, const char *text, unsigned long col[ColLast], Bool invert); void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
void *emallocz(unsigned int size); void *emallocz(unsigned int size);
void enternotify(XEvent *e); void enternotify(XEvent *e);
void eprint(const char *errstr, ...); void eprint(const char *errstr, ...);
@ -153,7 +150,6 @@ void focusprev(const char *arg);
Client *getclient(Window w); Client *getclient(Window w);
unsigned long getcolor(const char *colstr); unsigned long getcolor(const char *colstr);
View *getviewbar(Window barwin); View *getviewbar(Window barwin);
View *getview(Client *c);
long getstate(Window w); long getstate(Window w);
Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
void grabbuttons(Client *c, Bool focused); void grabbuttons(Client *c, Bool focused);
@ -211,7 +207,6 @@ void selectview(const char *arg);
/* variables */ /* variables */
char stext[256], buf[256]; char stext[256], buf[256];
int nviews = 1; int nviews = 1;
View *selview;
int screen; int screen;
int (*xerrorxlib)(Display *, XErrorEvent *); int (*xerrorxlib)(Display *, XErrorEvent *);
unsigned int bh, bpos; unsigned int bh, bpos;
@ -246,6 +241,7 @@ Cursor cursor[CurLast];
Display *dpy; Display *dpy;
DC dc = {0}; DC dc = {0};
View *views; View *views;
View *selview;
Window root; Window root;
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
@ -260,6 +256,7 @@ addtag(Client *c, const char *t) {
if(c->tags[i] && vtags[i] != vtags[tidx]) if(c->tags[i] && vtags[i] != vtags[tidx])
return; /* conflict */ return; /* conflict */
c->tags[tidx] = True; c->tags[tidx] = True;
c->view = &views[vtags[tidx]];
} }
void void
@ -288,8 +285,10 @@ applyrules(Client *c) {
XFree(ch.res_class); XFree(ch.res_class);
if(ch.res_name) if(ch.res_name)
XFree(ch.res_name); XFree(ch.res_name);
if(!matched) if(!matched) {
memcpy(c->tags, seltags, sizeof initags); memcpy(c->tags, seltags, sizeof initags);
c->view = selview;
}
} }
@ -330,7 +329,7 @@ void
ban(Client *c) { ban(Client *c) {
if(c->isbanned) if(c->isbanned)
return; return;
XMoveWindow(dpy, c->win, c->x + 3 * getview(c)->w, c->y); XMoveWindow(dpy, c->win, c->x + 3 * c->view->w, c->y);
c->isbanned = True; c->isbanned = True;
} }
@ -340,9 +339,7 @@ buttonpress(XEvent *e) {
Client *c; Client *c;
XButtonPressedEvent *ev = &e->xbutton; XButtonPressedEvent *ev = &e->xbutton;
View *v = selview; if(ev->window == selview->barwin) {
if(ev->window == v->barwin) {
x = 0; x = 0;
for(i = 0; i < LENGTH(tags); i++) { for(i = 0; i < LENGTH(tags); i++) {
x += textw(tags[i]); x += textw(tags[i]);
@ -370,17 +367,17 @@ buttonpress(XEvent *e) {
if(CLEANMASK(ev->state) != MODKEY) if(CLEANMASK(ev->state) != MODKEY)
return; return;
if(ev->button == Button1) { if(ev->button == Button1) {
restack(getview(c)); restack(c->view);
movemouse(c); movemouse(c);
} }
else if(ev->button == Button2) { else if(ev->button == Button2) {
if((floating != v->layout->arrange) && c->isfloating) if((floating != c->view->layout->arrange) && c->isfloating)
togglefloating(NULL); togglefloating(NULL);
else else
zoom(NULL); zoom(NULL);
} }
else if(ev->button == Button3 && !c->isfixed) { else if(ev->button == Button3 && !c->isfixed) {
restack(getview(c)); restack(c->view);
resizemouse(c); resizemouse(c);
} }
} }
@ -457,7 +454,7 @@ configurenotify(XEvent *e) {
XFreePixmap(dpy, dc.drawable); XFreePixmap(dpy, dc.drawable);
dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen)); dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
XResizeWindow(dpy, v->barwin, v->w, bh); XResizeWindow(dpy, v->barwin, v->w, bh);
updatebarpos(v); updatebarpos(selview);
arrange(); arrange();
} }
} }
@ -469,7 +466,7 @@ configurerequest(XEvent *e) {
XWindowChanges wc; XWindowChanges wc;
if((c = getclient(ev->window))) { if((c = getclient(ev->window))) {
View *v = getview(c); View *v = c->view;
if(ev->value_mask & CWBorderWidth) if(ev->value_mask & CWBorderWidth)
c->border = ev->border_width; c->border = ev->border_width;
if(c->isfixed || c->isfloating || (floating == v->layout->arrange)) { if(c->isfixed || c->isfloating || (floating == v->layout->arrange)) {
@ -541,21 +538,23 @@ drawbar(View *v) {
Client *c; Client *c;
dc.x = 0; dc.x = 0;
for(c = stack; c && (!isvisible(c) || getview(c) != v); c = c->snext); for(c = stack; c && (!isvisible(c) || c->view != v); c = c->snext);
for(i = 0; i < LENGTH(tags); i++) { for(i = 0; i < LENGTH(tags); i++) {
if(&views[vtags[i]] != v)
continue;
dc.w = textw(tags[i]); dc.w = textw(tags[i]);
if(seltags[i]) { if(seltags[i]) {
drawtext(v, tags[i], dc.sel, isurgent(i)); drawtext(tags[i], dc.sel, isurgent(i));
drawsquare(v, c && c->tags[i], isoccupied(i), isurgent(i), dc.sel); drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.sel);
} }
else { else {
drawtext(v, tags[i], dc.norm, isurgent(i)); drawtext(tags[i], dc.norm, isurgent(i));
drawsquare(v, c && c->tags[i], isoccupied(i), isurgent(i), dc.norm); drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.norm);
} }
dc.x += dc.w; dc.x += dc.w;
} }
dc.w = blw; dc.w = blw;
drawtext(v, v->layout->symbol, dc.norm, False); drawtext(v->layout->symbol, dc.norm, False);
x = dc.x + dc.w; x = dc.x + dc.w;
if(v == selview) { if(v == selview) {
dc.w = textw(stext); dc.w = textw(stext);
@ -564,25 +563,25 @@ drawbar(View *v) {
dc.x = x; dc.x = x;
dc.w = v->w - x; dc.w = v->w - x;
} }
drawtext(v, stext, dc.norm, False); drawtext(stext, dc.norm, False);
} }
else else
dc.x = v->w; dc.x = v->w;
if((dc.w = dc.x - x) > bh) { if((dc.w = dc.x - x) > bh) {
dc.x = x; dc.x = x;
if(c) { if(c) {
drawtext(v, c->name, dc.sel, False); drawtext(c->name, dc.sel, False);
drawsquare(v, False, c->isfloating, False, dc.sel); drawsquare(False, c->isfloating, False, dc.sel);
} }
else else
drawtext(v, NULL, dc.norm, False); drawtext(NULL, dc.norm, False);
} }
XCopyArea(dpy, dc.drawable, v->barwin, dc.gc, 0, 0, v->w, bh, 0, 0); XCopyArea(dpy, dc.drawable, v->barwin, dc.gc, 0, 0, v->w, bh, 0, 0);
XSync(dpy, False); XSync(dpy, False);
} }
void void
drawsquare(View *v, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) { drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
int x; int x;
XGCValues gcv; XGCValues gcv;
XRectangle r = { dc.x, dc.y, dc.w, dc.h }; XRectangle r = { dc.x, dc.y, dc.w, dc.h };
@ -603,7 +602,7 @@ drawsquare(View *v, Bool filled, Bool empty, Bool invert, unsigned long col[ColL
} }
void void
drawtext(View *v, const char *text, unsigned long col[ColLast], Bool invert) { drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
int x, y, w, h; int x, y, w, h;
unsigned int len, olen; unsigned int len, olen;
XRectangle r = { dc.x, dc.y, dc.w, dc.h }; XRectangle r = { dc.x, dc.y, dc.w, dc.h };
@ -680,7 +679,7 @@ expose(XEvent *e) {
View *v; View *v;
XExposeEvent *ev = &e->xexpose; XExposeEvent *ev = &e->xexpose;
if(ev->count == 0 && (v = getviewbar(ev->window))) if(ev->count == 0 && ((v = getviewbar(ev->window))))
drawbar(v); drawbar(v);
} }
@ -698,13 +697,13 @@ void
focus(Client *c) { focus(Client *c) {
View *v = selview; View *v = selview;
if(c) if(c)
selview = getview(c); selview = c->view;
else else
selview = viewat(); selview = viewat();
if(selview != v) if(selview != v)
drawbar(v); drawbar(v);
if(!c || (c && !isvisible(c))) if(!c || (c && !isvisible(c)))
for(c = stack; c && (!isvisible(c) || getview(c) != selview); c = c->snext); for(c = stack; c && (!isvisible(c) || c->view != selview); c = c->snext);
if(sel && sel != c) { if(sel && sel != c) {
grabbuttons(sel, False); grabbuttons(sel, False);
XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]); XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
@ -718,7 +717,7 @@ focus(Client *c) {
if(c) { if(c) {
XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]); XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
selview = getview(c); selview = c->view;
} }
else else
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
@ -744,7 +743,7 @@ focusnext(const char *arg) {
for(c = clients; c && !isvisible(c); c = c->next); for(c = clients; c && !isvisible(c); c = c->next);
if(c) { if(c) {
focus(c); focus(c);
restack(getview(c)); restack(c->view);
} }
} }
@ -761,7 +760,7 @@ focusprev(const char *arg) {
} }
if(c) { if(c) {
focus(c); focus(c);
restack(getview(c)); restack(c->view);
} }
} }
@ -793,16 +792,6 @@ getviewbar(Window barwin) {
return NULL; return NULL;
} }
View *
getview(Client *c) {
unsigned int i;
for(i = 0; i < LENGTH(tags); i++)
if(c->tags[i])
return &views[c->tags[i] - 1];
return &views[0]; /* fallback */
}
long long
getstate(Window w) { getstate(Window w) {
int format, status; int format, status;
@ -1058,7 +1047,7 @@ manage(Window w, XWindowAttributes *wa) {
applyrules(c); applyrules(c);
v = getview(c); v = c->view;
c->x = wa->x + v->x; c->x = wa->x + v->x;
c->y = wa->y + v->y; c->y = wa->y + v->y;
@ -1137,7 +1126,7 @@ movemouse(Client *c) {
ocx = nx = c->x; ocx = nx = c->x;
ocy = ny = c->y; ocy = ny = c->y;
v = getview(c); v = c->view;
if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor[CurMove], CurrentTime) != GrabSuccess) None, cursor[CurMove], CurrentTime) != GrabSuccess)
return; return;
@ -1176,7 +1165,7 @@ movemouse(Client *c) {
Client * Client *
nexttiled(Client *c, View *v) { nexttiled(Client *c, View *v) {
for(; c && (c->isfloating || getview(c) != v || !isvisible(c)); c = c->next); for(; c && (c->isfloating || c->view != v || !isvisible(c)); c = c->next);
return c; return c;
} }
@ -1201,7 +1190,7 @@ propertynotify(XEvent *e) {
break; break;
case XA_WM_HINTS: case XA_WM_HINTS:
updatewmhints(c); updatewmhints(c);
drawbar(getview(c)); drawbar(c->view);
break; break;
} }
if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
@ -1234,7 +1223,7 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
View *v; View *v;
XWindowChanges wc; XWindowChanges wc;
v = getview(c); v = c->view;
if(sizehints) { if(sizehints) {
/* set minimum possible */ /* set minimum possible */
if (w < 1) if (w < 1)
@ -1305,7 +1294,7 @@ resizemouse(Client *c) {
ocx = c->x; ocx = c->x;
ocy = c->y; ocy = c->y;
v = getview(c); v = c->view;
if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor[CurResize], CurrentTime) != GrabSuccess) None, cursor[CurResize], CurrentTime) != GrabSuccess)
return; return;
@ -1484,13 +1473,12 @@ setlayout(const char *arg) {
if(sel) if(sel)
arrange(); arrange();
else else
drawbar(v); drawbar(selview);
} }
void void
setmwfact(const char *arg) { setmwfact(const char *arg) {
double delta; double delta;
View *v = selview; View *v = selview;
if(!domwfact) if(!domwfact)
@ -1537,7 +1525,7 @@ setup(void) {
isxinerama = True; isxinerama = True;
nviews = 2; /* aim Xinerama */ nviews = 2; /* aim Xinerama */
#endif #endif
selview = views = emallocz(nviews * sizeof(View)); views = emallocz(nviews * sizeof(View));
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
@ -1627,10 +1615,10 @@ v->h = DisplayHeight(dpy, screen);
if(info) if(info)
XFree(info); XFree(info);
selview = viewat();
/* grab keys */ /* grab keys */
grabkeys(); grabkeys();
selview = &views[0];
} }
void void
@ -1990,8 +1978,8 @@ zoom(const char *arg) {
if(!sel || !dozoom || sel->isfloating) if(!sel || !dozoom || sel->isfloating)
return; return;
if(c == nexttiled(clients, getview(c))) if(c == nexttiled(clients, c->view))
if(!(c = nexttiled(c->next, getview(c)))) if(!(c = nexttiled(c->next, c->view)))
return; return;
detach(c); detach(c);
attach(c); attach(c);