added bar event timer

This commit is contained in:
Anselm R. Garbe 2006-07-11 18:15:11 +02:00
parent 3399650076
commit 586f66331d
9 changed files with 87 additions and 51 deletions

12
bar.c
View File

@ -5,12 +5,22 @@
#include "wm.h" #include "wm.h"
static const char *status[] = {
"sh", "-c", "echo -n `date` `uptime | sed 's/.*://; s/,//g'`"
" `acpi | awk '{print $4}' | sed 's/,//'`", 0 \
};
void void
draw_bar() draw_bar()
{ {
static char buf[1024];
buf[0] = 0;
pipe_spawn(buf, sizeof(buf), dpy, (char **)status);
brush.rect = barrect; brush.rect = barrect;
brush.rect.x = brush.rect.y = 0; brush.rect.x = brush.rect.y = 0;
draw(dpy, &brush, False, 0); draw(dpy, &brush, False, buf);
XCopyArea(dpy, brush.drawable, barwin, brush.gc, 0, 0, barrect.width, XCopyArea(dpy, brush.drawable, barwin, brush.gc, 0, 0, barrect.width,
barrect.height, 0, 0); barrect.height, 0, 0);

View File

@ -122,6 +122,8 @@ unmanage(Client *c)
XSetErrorHandler(error_handler); XSetErrorHandler(error_handler);
XUngrabServer(dpy); XUngrabServer(dpy);
flush_events(EnterWindowMask); flush_events(EnterWindowMask);
if(stack)
focus(stack);
} }
@ -135,3 +137,10 @@ getclient(Window w)
return NULL; return NULL;
} }
void
draw_client(Client *c)
{
}

10
cmd.c
View File

@ -5,22 +5,22 @@
#include "wm.h" #include "wm.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
void void
run(char *arg) run(void *aux)
{ {
spawn(dpy, arg); spawn(dpy, aux);
} }
void void
quit(char *arg) quit(void *aux)
{ {
fputs("quit\n", stderr);
running = False; running = False;
} }
void void
kill(char *arg) kill(void *aux)
{ {
Client *c = stack; Client *c = stack;

View File

@ -3,16 +3,8 @@
* See LICENSE file for license details. * See LICENSE file for license details.
*/ */
#define FONT "-*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-*" #define FONT "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*"
#define BGCOLOR "#000000" #define BGCOLOR "#000000"
#define FGCOLOR "#ffaa00" #define FGCOLOR "#ffaa00"
#define BORDERCOLOR "#000000" #define BORDERCOLOR "#000000"
#define STATUSCMD "echo -n `date` `uptime | sed 's/.*://; s/,//g'`" \ #define STATUSDELAY 1 /* milliseconds */
" `acpi | awk '{print $4}' | sed 's/,//'`"
#define PLCMD "`ls -lL /bin /sbin /usr/bin /usr/local/bin 2>/dev/null | awk 'NF>2 && $1 ~ /^[^d].*x/ {print $NF}' | sort | uniq | gridmenu`"
#define KEYS \
{ Mod1Mask, XK_Return, run, "xterm -u8 -bg black -fg white -fn '-*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-*'" }, \
{ Mod1Mask, XK_p, run, PLCMD }, \
{ Mod1Mask | ShiftMask, XK_q, quit, NULL},

16
key.c
View File

@ -7,8 +7,20 @@
#include <X11/keysym.h> #include <X11/keysym.h>
static const char *term[] = {
"xterm", "-u8", "-bg", "black", "-fg", "white", "-fn",
"-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", 0
};
static const char *proglist[] = {
"sh", "-c", "exec `ls -lL /bin /sbin /usr/bin /usr/local/bin 2>/dev/null | awk 'NF>2 && $1 ~ /^[^d].*x/ {print $NF}' | sort | uniq | gridmenu`", 0
};
static Key key[] = { static Key key[] = {
KEYS { Mod1Mask, XK_Return, run, term },
{ Mod1Mask, XK_p, run, proglist },
{ Mod1Mask | ShiftMask, XK_c, kill, NULL},
{ Mod1Mask | ShiftMask, XK_q, quit, NULL},
}; };
void void
@ -37,7 +49,7 @@ keypress(XEvent *e)
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)
key[i].func(key[i].arg); key[i].func(key[i].aux);
return; return;
} }
} }

25
util.c
View File

@ -14,8 +14,6 @@
#include "util.h" #include "util.h"
static char *shell = NULL;
void void
error(char *errstr, ...) { error(char *errstr, ...) {
va_list ap; va_list ap;
@ -85,21 +83,17 @@ swap(void **p1, void **p2)
} }
void void
spawn(Display *dpy, const char *cmd) spawn(Display *dpy, char *argv[])
{ {
if(!shell && !(shell = getenv("SHELL"))) if(!argv || !argv[0])
shell = "/bin/sh";
if(!cmd)
return; return;
if(fork() == 0) { if(fork() == 0) {
if(fork() == 0) { if(fork() == 0) {
if(dpy) if(dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
setsid(); setsid();
fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd); execvp(argv[0], argv);
execlp(shell, shell, "-c", cmd, NULL); fprintf(stderr, "gridwm: execvp %s", argv[0]);
fprintf(stderr, "gridwm: execlp %s", cmd);
perror(" failed"); perror(" failed");
} }
exit (0); exit (0);
@ -108,15 +102,12 @@ spawn(Display *dpy, const char *cmd)
} }
void void
pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd) pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
{ {
unsigned int l, n; unsigned int l, n;
int pfd[2]; int pfd[2];
if(!shell && !(shell = getenv("SHELL"))) if(!argv || !argv[0])
shell = "/bin/sh";
if(!cmd)
return; return;
if(pipe(pfd) == -1) { if(pipe(pfd) == -1) {
@ -131,8 +122,8 @@ pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
dup2(pfd[1], STDOUT_FILENO); dup2(pfd[1], STDOUT_FILENO);
close(pfd[0]); close(pfd[0]);
close(pfd[1]); close(pfd[1]);
execlp(shell, shell, "-c", cmd, NULL); execvp(argv[0], argv);
fprintf(stderr, "gridwm: execlp %s", cmd); fprintf(stderr, "gridwm: execvp %s", argv[0]);
perror(" failed"); perror(" failed");
} }
else { else {

12
util.h
View File

@ -9,12 +9,16 @@ extern void *emallocz(unsigned int size);
extern void *emalloc(unsigned int size); extern void *emalloc(unsigned int size);
extern void *erealloc(void *ptr, unsigned int size); extern void *erealloc(void *ptr, unsigned int size);
extern char *estrdup(const char *str); extern char *estrdup(const char *str);
#define eassert(a) do { \ #define eassert(a) \
do { \
if(!(a)) \ if(!(a)) \
failed_assert(#a, __FILE__, __LINE__); \ failed_assert(#a, __FILE__, __LINE__); \
} while (0) } while (0)
extern void failed_assert(char *a, char *file, int line); extern void failed_assert(char *a, char *file, int line);
void pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd); extern void pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[]);
extern void spawn(Display *dpy, const char *cmd); extern void spawn(Display *dpy, char *argv[]);
extern void swap(void **p1, void **p2); extern void swap(void **p1, void **p2);
unsigned char *getselection(unsigned long offset, unsigned long *len, unsigned long *remain); extern unsigned char *getselection(unsigned long offset, unsigned long *len,
unsigned long *remain);
extern unsigned int tokenize(char **result, unsigned int reslen,
char *str, char delim);

33
wm.c
View File

@ -3,10 +3,15 @@
* See LICENSE file for license details. * See LICENSE file for license details.
*/ */
#include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <X11/cursorfont.h> #include <X11/cursorfont.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <X11/Xproto.h> #include <X11/Xproto.h>
@ -160,12 +165,9 @@ startup_error_handler(Display *dpy, XErrorEvent *error)
static void static void
cleanup() cleanup()
{ {
/* while(clients)
Client *c; unmanage(clients);
for(c=client; c; c=c->next)
reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
*/
} }
int int
@ -176,6 +178,11 @@ main(int argc, char *argv[])
unsigned int mask; unsigned int mask;
Window w; Window w;
XEvent ev; XEvent ev;
fd_set fds;
struct timeval t, timeout = {
.tv_usec = 0,
.tv_sec = STATUSDELAY,
};
/* command line args */ /* command line args */
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
@ -264,9 +271,19 @@ main(int argc, char *argv[])
scan_wins(); scan_wins();
while(running) { while(running) {
XNextEvent(dpy, &ev); if(XPending(dpy) > 0) {
if(handler[ev.type]) XNextEvent(dpy, &ev);
(handler[ev.type]) (&ev); /* call handler */ if(handler[ev.type])
(handler[ev.type]) (&ev); /* call handler */
continue;
}
FD_ZERO(&fds);
FD_SET(ConnectionNumber(dpy), &fds);
t = timeout;
if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
continue;
else if(errno != EINTR)
draw_bar();
} }
cleanup(); cleanup();

9
wm.h
View File

@ -42,8 +42,8 @@ struct Client {
struct Key { struct Key {
unsigned long mod; unsigned long mod;
KeySym keysym; KeySym keysym;
void (*func)(char *arg); void (*func)(void *aux);
char *arg; void *aux;
}; };
extern Display *dpy; extern Display *dpy;
@ -64,8 +64,9 @@ extern Client *clients, *stack;
extern void draw_bar(); extern void draw_bar();
/* cmd.c */ /* cmd.c */
extern void run(char *arg); extern void run(void *aux);
extern void quit(char *arg); extern void quit(void *aux);
extern void kill(void *aux);
/* client.c */ /* client.c */
extern void manage(Window w, XWindowAttributes *wa); extern void manage(Window w, XWindowAttributes *wa);