Hi all,
I wrote a small patch that maybe others can find usefull too.
It adds grid mode to DWM, so that you can easily use tools like
http://sourceforge.net/projects/clusterssh under DWM
This is my first attempt to write any useful C code, since my
daily coding consists mainly of PHP and/or Ruby, before that Perl,
and in the beginning Pascal. I've always wanted to learn C though,
but never found a good excuse to do so. Not until now, when I
really had an itch to scratch I guess :-) but more important
when I saw how clean, elegant and beautiful C can be... Reading
the DWM source code was inspiring, so thank you Anselm for this
pearl of coding.
Sincerely,
Alex
P.S. any feedback is more than welcome :-)
diff -Naur --exclude='.hg*' dwm-tip/config.default.h
dwm-tip-custom/config.default.h
--- dwm-tip/config.default.h 2007-01-31 00:12:46.000000000 +0200
+++ dwm-tip-custom/config.default.h 2007-02-02 13:32:26.000000000 +0200
@@ -9,6 +9,7 @@
#define DEFMODE dotile /* dofloat */
#define FLOATSYMBOL "><>"
#define TILESYMBOL "[]="
+#define GRIDSYMBOL "[-]"
#define FONT "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*"
#define NORMBORDERCOLOR "#dddddd"
diff -Naur --exclude='.hg*' dwm-tip/draw.c dwm-tip-custom/draw.c
--- dwm-tip/draw.c 2007-01-31 00:12:46.000000000 +0200
+++ dwm-tip-custom/draw.c 2007-02-02 13:21:00.000000000 +0200
@@ -111,7 +111,7 @@
dc.x += dc.w;
}
dc.w = bmw;
- drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False,
False);
+ drawtext(arrange == dofloat ? FLOATSYMBOL : (arrange == dogrid ?
GRIDSYMBOL : TILESYMBOL), dc.norm, False, False);
x = dc.x + dc.w;
dc.w = textw(stext);
dc.x = sw - dc.w;
diff -Naur --exclude='.hg*' dwm-tip/dwm.h dwm-tip-custom/dwm.h
--- dwm-tip/dwm.h 2007-02-02 12:47:36.000000000 +0200
+++ dwm-tip-custom/dwm.h 2007-02-02 13:30:19.000000000 +0200
@@ -139,11 +139,13 @@
extern void *emallocz(unsigned int size); /* allocates zero-initialized
memory, exits on error */
extern void eprint(const char *errstr, ...); /* prints errstr and exits with
1 */
extern void spawn(Arg *arg); /* forks a new subprocess with
to arg's cmd */
+extern unsigned int grid_cols(unsigned int n); /* determine the number of
columns in grid mode */
/* view.c */
extern void detach(Client *c); /* detaches c from global
client list */
extern void dofloat(void); /* arranges all windows
floating */
extern void dotile(void); /* arranges all windows tiled */
+extern void dogrid(void); /* arranges all windows in a
grid */
extern void focusnext(Arg *arg); /* focuses next visible client,
arg is ignored */
extern void focusprev(Arg *arg); /* focuses previous visible
client, arg is ignored */
extern void incnmaster(Arg *arg); /* increments nmaster with
arg's index value */
diff -Naur --exclude='.hg*' dwm-tip/util.c dwm-tip-custom/util.c
--- dwm-tip/util.c 2007-01-31 00:12:46.000000000 +0200
+++ dwm-tip-custom/util.c 2007-02-02 13:22:24.000000000 +0200
@@ -52,3 +52,12 @@
}
wait(0);
}
+
+unsigned int grid_cols(unsigned int n) {
+ unsigned int i;
+ for(i = 0; i <= n/2; i++)
+ if(i*i >= n)
+ break;
+ return i;
+}
+
diff -Naur --exclude='.hg*' dwm-tip/view.c dwm-tip-custom/view.c
--- dwm-tip/view.c 2007-02-02 12:49:14.000000000 +0200
+++ dwm-tip-custom/view.c 2007-02-02 13:25:14.000000000 +0200
@@ -118,6 +118,48 @@
}
void
+dogrid(void) {
+ unsigned int i, n, tw, th, cols, rows;
+ Client *c;
+
+ for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
+ n++;
+
+ if (n == 0)
+ return;
+
+ cols = grid_cols(n);
+ rows = cols;
+ if (((cols - 1) * rows) >= n)
+ cols--;
+
+ th = (sh - dc.h) / rows;
+ tw = sw / cols;
+
+ for(i = 0, c = clients; c; c = c->next)
+ if(isvisible(c)) {
+ if(c->isfloat) {
+ resize(c, True);
+ continue;
+ }
+ c->ismax = False;
+ c->x = (i / rows) * tw;
+ c->y = (i % rows) * th + dc.h;
+ c->w = tw - 2 * BORDERPX;
+ c->h = th - 2 * BORDERPX;
+ resize(c, False);
+ i++;
+ }
+ else
+ XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
+ if(!sel || !isvisible(sel)) {
+ for(c = stack; c && !isvisible(c); c = c->snext);
+ focus(c);
+ }
+ restack();
+}
+
+void
focusnext(Arg *arg) {
Client *c;
@@ -215,7 +257,8 @@
void
togglemode(Arg *arg) {
- arrange = (arrange == dofloat) ? dotile : dofloat;
+ arrange = (arrange == dofloat) ?
+ dotile : (arrange == dotile) ? dogrid : dofloat;
if(sel)
arrange();
else