added generic client support (thanks Stephen)

This commit is contained in:
Connor Lane Smith 2011-05-06 21:40:53 +01:00
parent d1716bda6b
commit 214992a9ba
2 changed files with 37 additions and 19 deletions

View File

@ -12,7 +12,7 @@ static const Bool foreground = False;
static Key keys[] = { \ static Key keys[] = { \
/* modifier key function argument */ /* modifier key function argument */
{ MODKEY|ShiftMask, XK_Return, focusonce, { 0 } }, { MODKEY|ShiftMask, XK_Return, focusonce, { 0 } },
{ MODKEY|ShiftMask, XK_Return, spawn, { .v = (char*[]){ "surf", "-e", winid, NULL} } }, { MODKEY|ShiftMask, XK_Return, spawn, { 0 } },
{ MODKEY|ShiftMask, XK_l, rotate, { .i = +1 } }, { MODKEY|ShiftMask, XK_l, rotate, { .i = +1 } },
{ MODKEY|ShiftMask, XK_h, rotate, { .i = -1 } }, { MODKEY|ShiftMask, XK_h, rotate, { .i = -1 } },
{ MODKEY, XK_Tab, rotate, { .i = 0 } }, { MODKEY, XK_Tab, rotate, { .i = 0 } },

View File

@ -39,7 +39,7 @@
/* Macros */ /* Macros */
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define LENGTH(x) (sizeof x / sizeof x[0]) #define LENGTH(x) (sizeof (x) / sizeof *(x))
#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask)) #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
#define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height) #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
@ -116,6 +116,7 @@ static void rotate(const Arg *arg);
static void run(void); static void run(void);
static void sendxembed(Client *c, long msg, long detail, long d1, long d2); static void sendxembed(Client *c, long msg, long detail, long d1, long d2);
static void setup(void); static void setup(void);
static void setcmd(int argc, char *argv[]);
static void sigchld(int unused); static void sigchld(int unused);
static void spawn(const Arg *arg); static void spawn(const Arg *arg);
static int textnw(const char *text, unsigned int len); static int textnw(const char *text, unsigned int len);
@ -150,6 +151,7 @@ static Window root, win;
static Client *clients = NULL, *sel = NULL, *lastsel = NULL; static Client *clients = NULL, *sel = NULL, *lastsel = NULL;
static int (*xerrorxlib)(Display *, XErrorEvent *); static int (*xerrorxlib)(Display *, XErrorEvent *);
static char winid[64]; static char winid[64];
static char **cmd = NULL;
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
#include "config.h" #include "config.h"
@ -189,8 +191,8 @@ cleanup(void) {
Client *c, *n; Client *c, *n;
for(c = clients; c; c = n) { for(c = clients; c; c = n) {
killclient(NULL);
focus(c); focus(c);
killclient(NULL);
XReparentWindow(dpy, c->win, root, 0, 0); XReparentWindow(dpy, c->win, root, 0, 0);
n = c->next; n = c->next;
unmanage(c); unmanage(c);
@ -203,6 +205,7 @@ cleanup(void) {
XFreeGC(dpy, dc.gc); XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win); XDestroyWindow(dpy, win);
XSync(dpy, False); XSync(dpy, False);
free(cmd);
} }
void void
@ -360,7 +363,7 @@ emallocz(size_t size) {
void *p; void *p;
if(!(p = calloc(1, size))) if(!(p = calloc(1, size)))
die(0, "tabbed: cannot malloc"); die("tabbed: cannot malloc\n");
return p; return p;
} }
@ -423,7 +426,7 @@ getcolor(const char *colstr) {
XColor color; XColor color;
if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
die("error, cannot allocate color '%s'\n", colstr); die("tabbed: cannot allocate color '%s'\n", colstr);
return color.pixel; return color.pixel;
} }
@ -502,7 +505,7 @@ initfont(const char *fontstr) {
dc.font.xfont = NULL; dc.font.xfont = NULL;
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)) if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed"))) && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
die("error, cannot load font: '%s'\n", fontstr); die("tabbed: cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent; dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent; dc.font.descent = dc.font.xfont->descent;
} }
@ -578,7 +581,7 @@ manage(Window w) {
XGrabKey(dpy, code, keys[i].mod | modifiers[j], w, XGrabKey(dpy, code, keys[i].mod | modifiers[j], w,
True, GrabModeAsync, GrabModeAsync); True, GrabModeAsync, GrabModeAsync);
} }
c = emallocz(sizeof(Client)); c = emallocz(sizeof *c);
c->next = clients; c->next = clients;
c->win = w; c->win = w;
clients = c; clients = c;
@ -702,6 +705,17 @@ sendxembed(Client *c, long msg, long detail, long d1, long d2) {
XSendEvent(dpy, c->win, False, NoEventMask, &e); XSendEvent(dpy, c->win, False, NoEventMask, &e);
} }
void
setcmd(int argc, char *argv[]) {
int i;
cmd = emallocz((argc+2) * sizeof *cmd);
for(i = 0; i < argc; i++)
cmd[i] = argv[i];
cmd[argc] = winid;
cmd[argc+1] = NULL;
}
void void
setup(void) { setup(void) {
/* clean up any zombies immediately */ /* clean up any zombies immediately */
@ -740,7 +754,7 @@ setup(void) {
class_hint.res_class = "Tabbed"; class_hint.res_class = "Tabbed";
XSetClassHint(dpy, win, &class_hint); XSetClassHint(dpy, win, &class_hint);
XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1); XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1);
snprintf(winid, LENGTH(winid), "%u", (int)win); snprintf(winid, sizeof winid, "%lu", win);
nextfocus = foreground; nextfocus = foreground;
focus(clients); focus(clients);
} }
@ -748,7 +762,7 @@ setup(void) {
void void
sigchld(int unused) { sigchld(int unused) {
if(signal(SIGCHLD, sigchld) == SIG_ERR) if(signal(SIGCHLD, sigchld) == SIG_ERR)
die("Can't install SIGCHLD handler"); die("tabbed: cannot install SIGCHLD handler");
while(0 < waitpid(-1, NULL, WNOHANG)); while(0 < waitpid(-1, NULL, WNOHANG));
} }
@ -758,8 +772,8 @@ spawn(const Arg *arg) {
if(dpy) if(dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
setsid(); setsid();
execvp(((char **)arg->v)[0], (char **)arg->v); execvp(cmd[0], cmd);
fprintf(stderr, "tabbed: execvp %s", ((char **)arg->v)[0]); fprintf(stderr, "tabbed: execvp %s", cmd[0]);
perror(" failed"); perror(" failed");
exit(0); exit(0);
} }
@ -844,16 +858,20 @@ xerror(Display *dpy, XErrorEvent *ee) {
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
int detach = 0; int i, detach = 0;
if(argc == 2 && !strcmp("-v", argv[1])) for(i = 1; i < argc && !cmd; i++) {
die("tabbed-"VERSION", © 2009-2010 tabbed engineers, see LICENSE for details\n"); if(!strcmp("-v", argv[i]))
else if(argc == 2 && strcmp("-d", argv[1]) == 0) die("tabbed-"VERSION", © 2009-2011 tabbed engineers, see LICENSE for details\n");
detach = 1; else if(!strcmp("-d", argv[i]))
else if(argc != 1) detach = 1;
die("usage: tabbed [-d] [-v]\n"); else
setcmd(argc-i, argv+i);
}
if(!cmd)
die("usage: tabbed [-d] [-v] command\n");
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fprintf(stderr, "warning: no locale support\n"); fprintf(stderr, "tabbed: no locale support\n");
if(!(dpy = XOpenDisplay(NULL))) if(!(dpy = XOpenDisplay(NULL)))
die("tabbed: cannot open display\n"); die("tabbed: cannot open display\n");
setup(); setup();