several other additions/fixes, dwm is quite usable already

This commit is contained in:
Anselm R. Garbe 2006-07-13 18:21:38 +02:00
parent c47da143bd
commit efa7e51401
4 changed files with 165 additions and 111 deletions

235
client.c
View file

@ -13,60 +13,105 @@
static void (*arrange)(Arg *) = floating; static void (*arrange)(Arg *) = floating;
static Client * static void
next(Client *c) center(Client *c)
{ {
for(c = c->next; c && !c->tags[tsel]; c = c->next); XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
return c;
} }
static Client * static Client *
prev(Client *c) next(Client *c)
{ {
for(c = c->prev; c && !c->tags[tsel]; c = c->prev); for(; c && !c->tags[tsel]; c = c->next);
return c; return c;
} }
void
zoom(Arg *arg)
{
Client **l;
if(!sel)
return;
for(l = &clients; *l && *l != sel; l = &(*l)->next);
*l = sel->next;
sel->next = clients; /* pop */
clients = sel;
arrange(NULL);
center(sel);
focus(sel);
}
void void
max(Arg *arg) max(Arg *arg)
{ {
if(!csel) if(!sel)
return; return;
csel->x = sx; sel->x = sx;
csel->y = sy; sel->y = sy;
csel->w = sw - 2 * csel->border; sel->w = sw - 2 * sel->border;
csel->h = sh - 2 * csel->border; sel->h = sh - 2 * sel->border;
craise(csel); craise(sel);
resize(csel); resize(sel);
discard_events(EnterWindowMask); discard_events(EnterWindowMask);
} }
void
view(Arg *arg)
{
tsel = arg->i;
arrange(NULL);
}
void void
tag(Arg *arg) tag(Arg *arg)
{ {
if(!csel) int i, n;
if(!sel)
return; return;
if(arg->i == tsel) if(arg->i == tsel) {
return; for(n = i = 0; i < TLast; i++)
if(sel->tags[i])
n++;
if(n < 2)
return;
}
if(csel->tags[arg->i]) if(sel->tags[arg->i])
csel->tags[arg->i] = NULL; /* toggle tag */ sel->tags[arg->i] = NULL; /* toggle tag */
else else
csel->tags[arg->i] = tags[arg->i]; sel->tags[arg->i] = tags[arg->i];
arrange(NULL); 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 void
floating(Arg *arg) floating(Arg *arg)
{ {
Client *c; Client *c;
arrange = floating; arrange = floating;
if(!csel) for(c = clients; c; c = c->next) {
return; if(c->tags[tsel])
for(c = csel; c; c = next(c)) resize(c);
resize(c); else
ban_client(c);
}
if(sel && !sel->tags[tsel]) {
if((sel = next(clients))) {
craise(sel);
focus(sel);
}
}
discard_events(EnterWindowMask); discard_events(EnterWindowMask);
} }
@ -78,31 +123,43 @@ tiling(Arg *arg)
float rt, fd; float rt, fd;
arrange = tiling; arrange = tiling;
if(!csel) for(n = 0, c = clients; c; c = next(c->next), n++);
return; if(n) {
for(n = 0, c = csel; c; c = next(c), n++); rt = sqrt(n);
rt = sqrt(n); if(modff(rt, &fd) < 0.5)
if(modff(rt, &fd) < 0.5) rows = floor(rt);
rows = floor(rt); else
else rows = ceil(rt);
rows = ceil(rt); if(rows * rows < n)
if(rows * rows < n) cols = rows + 1;
cols = rows + 1; else
else cols = rows;
cols = rows;
gw = (sw - 2) / cols; gw = (sw - 2) / cols;
gh = (sh - 2) / rows; gh = (sh - 2) / rows;
}
else
cols = rows = gw = gh = 0;
for(i = j = 0, c = csel; c; c = next(c)) { for(i = j = 0, c = clients; c; c = c->next) {
c->x = i * gw; if(c->tags[tsel]) {
c->y = j * gh; c->x = i * gw;
c->w = gw; c->y = j * gh;
c->h = gh; c->w = gw;
resize(c); c->h = gh;
if(++i == cols) { resize(c);
j++; if(++i == cols) {
i = 0; j++;
i = 0;
}
}
else
ban_client(c);
}
if(sel && !sel->tags[tsel]) {
if((sel = next(clients))) {
craise(sel);
focus(sel);
} }
} }
discard_events(EnterWindowMask); discard_events(EnterWindowMask);
@ -113,14 +170,12 @@ prevc(Arg *arg)
{ {
Client *c; Client *c;
if(!csel) if(!sel)
return; return;
if(!(c = prev(csel))) if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
c = prev(cend);
if(c) {
craise(c); craise(c);
XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); center(c);
focus(c); focus(c);
} }
} }
@ -130,15 +185,15 @@ nextc(Arg *arg)
{ {
Client *c; Client *c;
if(!csel) if(!sel)
return; return;
if(!(c = next(csel))) if(!(c = next(sel->next)))
c = next(cstart); c = next(clients);
if(c) { if(c) {
craise(c); craise(c);
XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); center(c);
c->revert = sel;
focus(c); focus(c);
} }
} }
@ -146,14 +201,12 @@ nextc(Arg *arg)
void void
ckill(Arg *arg) ckill(Arg *arg)
{ {
Client *c = csel; if(!sel)
if(!c)
return; return;
if(c->proto & WM_PROTOCOL_DELWIN) if(sel->proto & WM_PROTOCOL_DELWIN)
send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]); send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
else else
XKillClient(dpy, c->win); XKillClient(dpy, sel->win);
} }
static void static void
@ -256,12 +309,12 @@ lower(Client *c)
void void
focus(Client *c) focus(Client *c)
{ {
if(csel && csel != c) { if(sel && sel != c) {
XSetWindowBorder(dpy, csel->win, dc.bg); XSetWindowBorder(dpy, sel->win, dc.bg);
XMapWindow(dpy, csel->title); XMapWindow(dpy, sel->title);
draw_client(csel); draw_client(sel);
} }
csel = c; sel = c;
XUnmapWindow(dpy, c->title); XUnmapWindow(dpy, c->title);
XSetWindowBorder(dpy, c->win, dc.fg); XSetWindowBorder(dpy, c->win, dc.fg);
draw_client(c); draw_client(c);
@ -273,7 +326,7 @@ focus(Client *c)
void void
manage(Window w, XWindowAttributes *wa) manage(Window w, XWindowAttributes *wa)
{ {
Client *c; Client *c, **l;
XSetWindowAttributes twa; XSetWindowAttributes twa;
c = emallocz(sizeof(Client)); c = emallocz(sizeof(Client));
@ -284,6 +337,7 @@ manage(Window w, XWindowAttributes *wa)
c->h = wa->height; c->h = wa->height;
c->th = th; c->th = th;
c->border = 1; c->border = 1;
c->proto = win_proto(c->win);
update_size(c); update_size(c);
XSelectInput(dpy, c->win, XSelectInput(dpy, c->win,
StructureNotifyMask | PropertyChangeMask | EnterWindowMask); StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
@ -300,13 +354,9 @@ manage(Window w, XWindowAttributes *wa)
update_name(c); update_name(c);
if(!cstart) for(l = &clients; *l; l = &(*l)->next);
cstart = cend = c; c->next = *l; /* *l == nil */
else { *l = c;
cend->next = c;
c->prev = cend;
cend = c;
}
XSetWindowBorderWidth(dpy, c->win, 1); XSetWindowBorderWidth(dpy, c->win, 1);
XMapRaised(dpy, c->win); XMapRaised(dpy, c->win);
@ -318,7 +368,7 @@ manage(Window w, XWindowAttributes *wa)
XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask, XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
GrabModeAsync, GrabModeSync, None, None); GrabModeAsync, GrabModeSync, None, None);
arrange(NULL); arrange(NULL);
XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2); center(c);
focus(c); focus(c);
} }
@ -420,26 +470,21 @@ dummy_error_handler(Display *dsply, XErrorEvent *err)
void void
unmanage(Client *c) unmanage(Client *c)
{ {
Client **l;
XGrabServer(dpy); XGrabServer(dpy);
XSetErrorHandler(dummy_error_handler); XSetErrorHandler(dummy_error_handler);
XUngrabButton(dpy, AnyButton, AnyModifier, c->win); XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
XDestroyWindow(dpy, c->title); XDestroyWindow(dpy, c->title);
if(c->prev) { for(l = &clients; *l && *l != c; l = &(*l)->next);
c->prev->next = c->next; *l = c->next;
if(csel == c) for(l = &clients; *l; l = &(*l)->next)
csel = c->prev; if((*l)->revert == c)
} (*l)->revert = NULL;
if(c->next) { if(sel == c)
c->next->prev = c->prev; sel = sel->revert ? sel->revert : clients;
if(csel == c)
csel = c->next;
}
if(cstart == c)
cstart = c->next;
if(cend == c)
cend = c->prev;
free(c); free(c);
@ -447,15 +492,15 @@ unmanage(Client *c)
XSetErrorHandler(error_handler); XSetErrorHandler(error_handler);
XUngrabServer(dpy); XUngrabServer(dpy);
arrange(NULL); arrange(NULL);
if(csel) if(sel)
focus(csel); focus(sel);
} }
Client * Client *
gettitle(Window w) gettitle(Window w)
{ {
Client *c; Client *c;
for(c = cstart; c; c = c->next) for(c = clients; c; c = c->next)
if(c->title == w) if(c->title == w)
return c; return c;
return NULL; return NULL;
@ -465,7 +510,7 @@ Client *
getclient(Window w) getclient(Window w)
{ {
Client *c; Client *c;
for(c = cstart; c; c = c->next) for(c = clients; c; c = c->next)
if(c->win == w) if(c->win == w)
return c; return c;
return NULL; return NULL;
@ -475,7 +520,7 @@ void
draw_client(Client *c) draw_client(Client *c)
{ {
int i; int i;
if(c == csel) if(c == sel)
return; return;
dc.x = dc.y = 0; dc.x = dc.y = 0;

22
dev.c
View file

@ -20,19 +20,25 @@ const char *browse[] = { "firefox", NULL };
const char *xlock[] = { "xlock", NULL }; const char *xlock[] = { "xlock", NULL };
static Key key[] = { static Key key[] = {
{ Mod1Mask, XK_Return, spawn, { .argv = term } }, { Mod1Mask, XK_Return, zoom, { 0 } },
{ Mod1Mask, XK_t, spawn, { .argv = term } },
{ Mod1Mask, XK_w, spawn, { .argv = browse } }, { Mod1Mask, XK_w, spawn, { .argv = browse } },
{ Mod1Mask, XK_l, spawn, { .argv = xlock } }, { Mod1Mask, XK_l, spawn, { .argv = xlock } },
{ Mod1Mask, XK_k, prevc, { 0 } }, { Mod1Mask, XK_k, prevc, { 0 } },
{ Mod1Mask, XK_j, nextc, { 0 } }, { Mod1Mask, XK_j, nextc, { 0 } },
{ Mod1Mask, XK_t, tiling, { 0 } },
{ Mod1Mask, XK_f, floating, { 0 } },
{ Mod1Mask, XK_m, max, { 0 } }, { Mod1Mask, XK_m, max, { 0 } },
{ Mod1Mask, XK_0, tag, { .i = Tscratch } }, { Mod1Mask, XK_0, view, { .i = Tscratch } },
{ Mod1Mask, XK_1, tag, { .i = Tdev } }, { Mod1Mask, XK_1, view, { .i = Tdev } },
{ Mod1Mask, XK_2, tag, { .i = Tirc } }, { Mod1Mask, XK_2, view, { .i = Tirc } },
{ Mod1Mask, XK_3, tag, { .i = Twww } }, { Mod1Mask, XK_3, view, { .i = Twww } },
{ Mod1Mask, XK_4, tag, { .i = Twork } }, { Mod1Mask, XK_4, view, { .i = Twork } },
{ Mod1Mask, XK_space, tiling, { 0 } },
{ Mod1Mask | ShiftMask, XK_space, floating, { 0 } },
{ Mod1Mask | ShiftMask, XK_0, tag, { .i = Tscratch } },
{ Mod1Mask | ShiftMask, XK_1, tag, { .i = Tdev } },
{ Mod1Mask | ShiftMask, XK_2, tag, { .i = Tirc } },
{ Mod1Mask | ShiftMask, XK_3, tag, { .i = Twww } },
{ Mod1Mask | ShiftMask, XK_4, tag, { .i = Twork } },
{ Mod1Mask | ShiftMask, XK_c, ckill, { 0 } }, { Mod1Mask | ShiftMask, XK_c, ckill, { 0 } },
{ Mod1Mask | ShiftMask, XK_q, quit, { 0 } }, { Mod1Mask | ShiftMask, XK_q, quit, { 0 } },
}; };

8
dwm.h
View file

@ -68,7 +68,7 @@ struct Client {
Window trans; Window trans;
Window title; Window title;
Client *next; Client *next;
Client *prev; Client *revert;
}; };
struct Key { struct Key {
@ -89,7 +89,7 @@ extern int tsel, screen, sx, sy, sw, sh, th;
extern char stext[1024], *tags[TLast]; extern char stext[1024], *tags[TLast];
extern DC dc; extern DC dc;
extern Client *cstart, *cend, *csel; extern Client *clients, *sel;
/* client.c */ /* client.c */
extern void manage(Window w, XWindowAttributes *wa); extern void manage(Window w, XWindowAttributes *wa);
@ -109,7 +109,9 @@ extern void prevc(Arg *arg);
extern void max(Arg *arg); extern void max(Arg *arg);
extern void floating(Arg *arg); extern void floating(Arg *arg);
extern void tiling(Arg *arg); extern void tiling(Arg *arg);
void tag(Arg *arg); extern void tag(Arg *arg);
extern void view(Arg *arg);
extern void zoom(Arg *arg);
extern void gravitate(Client *c, Bool invert); extern void gravitate(Client *c, Bool invert);
/* draw.c */ /* draw.c */

11
main.c
View file

@ -38,9 +38,8 @@ int tsel = Tdev; /* default tag */
int screen, sx, sy, sw, sh, th; int screen, sx, sy, sw, sh, th;
DC dc = {0}; DC dc = {0};
Client *cstart = NULL; Client *clients = NULL;
Client *cend = NULL; Client *sel = NULL;
Client *csel = NULL;
static Bool other_wm_running; static Bool other_wm_running;
static const char version[] = static const char version[] =
@ -169,8 +168,10 @@ startup_error_handler(Display *dpy, XErrorEvent *error)
static void static void
cleanup() cleanup()
{ {
while(csel) while(sel) {
unmanage(csel); resize(sel);
unmanage(sel);
}
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
} }