new_dwm
Anselm R. Garbe 19 years ago
parent efa7e51401
commit 3aad92202d

@ -3,21 +3,19 @@
* See LICENSE file for license details. * See LICENSE file for license details.
*/ */
#include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include "dwm.h" #include "dwm.h"
static void (*arrange)(Arg *) = floating; static void (*arrange)(Arg *) = tiling;
static void static Rule rule[] = {
center(Client *c) { "Firefox-bin", "Gecko", { [Twww] = "www" } },
{ };
XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
}
static Client * static Client *
next(Client *c) next(Client *c)
@ -29,18 +27,18 @@ next(Client *c)
void void
zoom(Arg *arg) zoom(Arg *arg)
{ {
Client **l; Client **l, *old;
if(!sel) if(!(old = sel))
return; return;
for(l = &clients; *l && *l != sel; l = &(*l)->next); for(l = &clients; *l && *l != sel; l = &(*l)->next);
*l = sel->next; *l = sel->next;
sel->next = clients; /* pop */ old->next = clients; /* pop */
clients = sel; clients = old;
sel = old;
arrange(NULL); arrange(NULL);
center(sel);
focus(sel); focus(sel);
} }
@ -119,39 +117,38 @@ void
tiling(Arg *arg) tiling(Arg *arg)
{ {
Client *c; Client *c;
int n, cols, rows, gw, gh, i, j; int n, i, w, h;
float rt, fd;
w = sw - mw;
arrange = tiling; arrange = tiling;
for(n = 0, c = clients; c; c = next(c->next), n++); for(n = 0, c = clients; c; c = c->next)
if(n) { if(c->tags[tsel])
rt = sqrt(n); n++;
if(modff(rt, &fd) < 0.5)
rows = floor(rt);
else
rows = ceil(rt);
if(rows * rows < n)
cols = rows + 1;
else
cols = rows;
gw = (sw - 2) / cols; h = (n > 2) ? sh / (n - 2) : sh;
gh = (sh - 2) / rows;
}
else
cols = rows = gw = gh = 0;
for(i = j = 0, c = clients; c; c = c->next) { for(i = 0, c = clients; c; c = c->next) {
if(c->tags[tsel]) { if(c->tags[tsel]) {
c->x = i * gw; if(n == 1) {
c->y = j * gh; c->x = sx;
c->w = gw; c->y = sy;
c->h = gh; c->w = sw;
resize(c); c->h = sh;
if(++i == cols) {
j++;
i = 0;
} }
else if(i == 1) {
c->x = sx;
c->y = sy;
c->w = mw;
c->h = sh;
}
else {
c->x = sx + mw;
c->y = sy + (i - 2) * h;
c->w = w;
c->h = h;
}
resize(c);
i++;
} }
else else
ban_client(c); ban_client(c);
@ -175,7 +172,6 @@ prevc(Arg *arg)
if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) { if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
craise(c); craise(c);
center(c);
focus(c); focus(c);
} }
} }
@ -192,7 +188,6 @@ nextc(Arg *arg)
c = next(clients); c = next(clients);
if(c) { if(c) {
craise(c); craise(c);
center(c);
c->revert = sel; c->revert = sel;
focus(c); focus(c);
} }
@ -323,6 +318,43 @@ focus(Client *c)
discard_events(EnterWindowMask); discard_events(EnterWindowMask);
} }
static void
init_tags(Client *c)
{
XClassHint ch;
static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
unsigned int i, j;
Bool matched = False;
if(!len) {
c->tags[tsel] = tags[tsel];
return;
}
if(XGetClassHint(dpy, c->win, &ch)) {
if(ch.res_class && ch.res_name) {
fprintf(stderr, "%s:%s\n", ch.res_class, ch.res_name);
for(i = 0; i < len; i++)
if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
&& !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
{
fprintf(stderr, "->>>%s:%s\n", ch.res_class, ch.res_name);
for(j = 0; j < TLast; j++)
c->tags[j] = rule[i].tags[j];
matched = True;
break;
}
}
if(ch.res_class)
XFree(ch.res_class);
if(ch.res_name)
XFree(ch.res_name);
}
if(!matched)
c->tags[tsel] = tags[tsel];
}
void void
manage(Window w, XWindowAttributes *wa) manage(Window w, XWindowAttributes *wa)
{ {
@ -346,13 +378,13 @@ manage(Window w, XWindowAttributes *wa)
twa.background_pixmap = ParentRelative; twa.background_pixmap = ParentRelative;
twa.event_mask = ExposureMask; twa.event_mask = ExposureMask;
c->tags[tsel] = tags[tsel];
c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th, c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
0, DefaultDepth(dpy, screen), CopyFromParent, 0, DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen), DefaultVisual(dpy, screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
update_name(c); update_name(c);
init_tags(c);
for(l = &clients; *l; l = &(*l)->next); for(l = &clients; *l; l = &(*l)->next);
c->next = *l; /* *l == nil */ c->next = *l; /* *l == nil */
@ -368,8 +400,10 @@ 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);
center(c); if(c->tags[tsel])
focus(c); focus(c);
else
ban_client(c);
} }
void void

@ -11,7 +11,7 @@ X11LIB = /usr/X11R6/lib
VERSION = 0.0 VERSION = 0.0
# includes and libs # includes and libs
LIBS = -L${PREFIX}/lib -L/usr/lib -lc -lm -L${X11LIB} -lX11 LIBS = -L${PREFIX}/lib -L/usr/lib -lc -L${X11LIB} -lX11
# Linux/BSD # Linux/BSD
CFLAGS = -g -Wall -O2 -I. -I${PREFIX}/include -I/usr/include -I${X11INC} \ CFLAGS = -g -Wall -O2 -I. -I${PREFIX}/include -I/usr/include -I${X11INC} \

12
dev.c

@ -20,8 +20,7 @@ const char *browse[] = { "firefox", NULL };
const char *xlock[] = { "xlock", NULL }; const char *xlock[] = { "xlock", NULL };
static Key key[] = { static Key key[] = {
{ Mod1Mask, XK_Return, zoom, { 0 } }, { Mod1Mask, XK_Return, spawn, { .argv = term } },
{ 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 } },
@ -33,6 +32,7 @@ static Key key[] = {
{ Mod1Mask, XK_3, view, { .i = Twww } }, { Mod1Mask, XK_3, view, { .i = Twww } },
{ Mod1Mask, XK_4, view, { .i = Twork } }, { Mod1Mask, XK_4, view, { .i = Twork } },
{ Mod1Mask, XK_space, tiling, { 0 } }, { Mod1Mask, XK_space, tiling, { 0 } },
{ Mod1Mask | ShiftMask, XK_Return, zoom, { 0 } },
{ Mod1Mask | ShiftMask, XK_space, floating, { 0 } }, { Mod1Mask | ShiftMask, XK_space, floating, { 0 } },
{ Mod1Mask | ShiftMask, XK_0, tag, { .i = Tscratch } }, { Mod1Mask | ShiftMask, XK_0, tag, { .i = Tscratch } },
{ Mod1Mask | ShiftMask, XK_1, tag, { .i = Tdev } }, { Mod1Mask | ShiftMask, XK_1, tag, { .i = Tdev } },
@ -48,10 +48,10 @@ static Key key[] = {
void void
update_keys(void) update_keys(void)
{ {
unsigned int i, len; static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
unsigned int i;
KeyCode code; KeyCode code;
len = sizeof(key) / sizeof(key[0]);
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
code = XKeysymToKeycode(dpy, key[i].keysym); code = XKeysymToKeycode(dpy, key[i].keysym);
XUngrabKey(dpy, code, key[i].mod, root); XUngrabKey(dpy, code, key[i].mod, root);
@ -63,11 +63,11 @@ void
keypress(XEvent *e) keypress(XEvent *e)
{ {
XKeyEvent *ev = &e->xkey; XKeyEvent *ev = &e->xkey;
unsigned int i, len; static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
unsigned int i;
KeySym keysym; KeySym keysym;
keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
len = sizeof(key) / sizeof(key[0]);
for(i = 0; i < len; i++) for(i = 0; i < len; i++)
if((keysym == key[i].keysym) && (key[i].mod == ev->state)) { if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
if(key[i].func) if(key[i].func)

24
dwm.h

@ -7,22 +7,24 @@
/********** CUSTOMIZE **********/ /********** CUSTOMIZE **********/
#define FONT "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*" #define FONT "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*"
#define BGCOLOR "DarkSlateGrey" #define BGCOLOR "#666699"
#define FGCOLOR "LightSteelBlue" #define FGCOLOR "#ffffff"
#define BORDERCOLOR "SlateGray" #define BORDERCOLOR "#9999CC"
#define WM_PROTOCOL_DELWIN 1 #define MASTERW 52 /* percent */
#define WM_PROTOCOL_DELWIN 1
/* tags */ /* tags */
enum { Tscratch, Tdev, Tirc, Twww, Twork, TLast }; enum { Tscratch, Tdev, Tirc, Twww, Twork, TLast };
/********** CUSTOMIZE **********/ /********** CUSTOMIZE **********/
typedef union Arg Arg;
typedef struct DC DC; typedef struct DC DC;
typedef struct Client Client; typedef struct Client Client;
typedef struct Fnt Fnt; typedef struct Fnt Fnt;
typedef struct Key Key; typedef struct Key Key;
typedef union Arg Arg; typedef struct Rule Rule;
union Arg { union Arg {
const char **argv; const char **argv;
@ -71,6 +73,12 @@ struct Client {
Client *revert; Client *revert;
}; };
struct Rule {
const char *class;
const char *instance;
char *tags[TLast];
};
struct Key { struct Key {
unsigned long mod; unsigned long mod;
KeySym keysym; KeySym keysym;
@ -85,8 +93,8 @@ extern Cursor cursor[CurLast];
extern Bool running, issel; extern Bool running, issel;
extern void (*handler[LASTEvent]) (XEvent *); extern void (*handler[LASTEvent]) (XEvent *);
extern int tsel, screen, sx, sy, sw, sh, th; extern int tsel, screen, sx, sy, sw, sh, mw, th;
extern char stext[1024], *tags[TLast]; extern char *tags[TLast];
extern DC dc; extern DC dc;
extern Client *clients, *sel; extern Client *clients, *sel;

@ -33,9 +33,8 @@ Cursor cursor[CurLast];
Bool running = True; Bool running = True;
Bool issel; Bool issel;
char stext[1024];
int tsel = Tdev; /* default tag */ int tsel = Tdev; /* default tag */
int screen, sx, sy, sw, sh, th; int screen, sx, sy, sw, sh, mw, th;
DC dc = {0}; DC dc = {0};
Client *clients = NULL; Client *clients = NULL;
@ -223,6 +222,7 @@ main(int argc, char *argv[])
sx = sy = 0; sx = sy = 0;
sw = DisplayWidth(dpy, screen); sw = DisplayWidth(dpy, screen);
sh = DisplayHeight(dpy, screen); sh = DisplayHeight(dpy, screen);
mw = (sw * MASTERW) / 100;
issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
XSetErrorHandler(0); XSetErrorHandler(0);

Loading…
Cancel
Save