From 88d414ce800b0faaf549fe91543de04ae9461a4d Mon Sep 17 00:00:00 2001 From: David Delassus Date: Mon, 17 Oct 2011 20:39:48 +0200 Subject: [PATCH] Add FIFO, FIFO located at : ${TMPDIR}/wmfs-${DISPLAY}.fifo --- Makefile.in | 1 + src/fifo.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fifo.h | 19 +++++++++++++++ src/wmfs.c | 41 ++++++++++++++++++++++++++++++--- src/wmfs.h | 7 ++++++ 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/fifo.c create mode 100644 src/fifo.h diff --git a/Makefile.in b/Makefile.in index 2a28d55..6303370 100644 --- a/Makefile.in +++ b/Makefile.in @@ -16,6 +16,7 @@ SRCS= \ src/screen.c \ src/tag.c \ src/util.c \ + src/fifo.c \ src/wmfs.c diff --git a/src/fifo.c b/src/fifo.c new file mode 100644 index 0000000..3e9ba73 --- /dev/null +++ b/src/fifo.c @@ -0,0 +1,66 @@ +/* + * wmfs2 by Martin Duquesnoy { for(i = 2011; i < 2111; ++i) ©(i); } + * File created by David Delassus. + * For license, see COPYING. + */ + +#include "wmfs.h" +#include "util.h" +#include "config.h" +#include "fifo.h" + +void +fifo_init(void) +{ + xasprintf(&(W->fifo.path), "%s/wmfs-%s.fifo", P_tmpdir, DisplayString(W->dpy)); + + if(mkfifo(W->fifo.path, 0644) < 0 || !(W->fifo.fd = open(W->fifo.path, O_NONBLOCK, 0))) + warnx("Can't create FIFO: %s\n", strerror(errno)); +} + +static void +fifo_parse(char *cmd) +{ + void (*func)(Uicb); + char *uicb = NULL; + char *arg = NULL; + char *p = NULL; + + /* remove trailing newline */ + if((p = strchr(cmd, '\n'))) + *p = 0; + + /* Check if an argument is present */ + if((p = strchr(cmd, ' '))) + { + *p = 0; + arg = xstrdup(p + 1); + } + uicb = xstrdup(cmd); + + /* call the UICB function */ + func = uicb_name_func(uicb); + if(func) + func(arg); + + if(arg) + free(arg); + free(uicb); + + XSync(W->dpy, false); +} + +void +fifo_read(void) +{ + char buf[256] = {0}; + + /* Don't read it if not open */ + if(!(W->fifo.fd)) + return; + + read(W->fifo.fd, buf, sizeof(buf) - 1); + + if(buf[0]) + fifo_parse(buf); +} diff --git a/src/fifo.h b/src/fifo.h new file mode 100644 index 0000000..81a6a57 --- /dev/null +++ b/src/fifo.h @@ -0,0 +1,19 @@ +/* + * wmfs2 by Martin Duquesnoy { for(i = 2011; i < 2111; ++i) ©(i); } + * File created by David Delassus. + * For license, see COPYING. + */ + +#ifndef __FIFO_H +#define __FIFO_H + +#include +#include +#include +#include +#include + +void fifo_init(void); +void fifo_read(void); + +#endif /* __FIFO_H */ diff --git a/src/wmfs.c b/src/wmfs.c index d146b06..4c96c33 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -15,6 +15,7 @@ #include "util.h" #include "config.h" #include "client.h" +#include "fifo.h" int wmfs_error_handler(Display *d, XErrorEvent *event) @@ -241,10 +242,35 @@ static void wmfs_loop(void) { XEvent ev; + int fd = ConnectionNumber(W->dpy); + int maxfd = fd + 1; + fd_set iset; - while(XPending(W->dpy)) - while(W->running && !XNextEvent(W->dpy, &ev)) - EVENT_HANDLE(&ev); + if(W->fifo.fd > 0) + maxfd += W->fifo.fd; + + while(W->running) + { + FD_ZERO(&iset); + FD_SET(fd, &iset); + + if(W->fifo.fd > 0) + FD_SET(W->fifo.fd, &iset); + + if(select(maxfd, &iset, NULL, NULL, NULL) > 0) + { + if(FD_ISSET(fd, &iset)) + { + while(XPending(W->dpy)) + { + XNextEvent(W->dpy, &ev); + EVENT_HANDLE(&ev); + } + } + else if(W->fifo.fd > 0 && FD_ISSET(W->fifo.fd, &iset)) + fifo_read(); + } + } } static inline void @@ -255,6 +281,7 @@ wmfs_init(void) screen_init(); event_init(); config_init(); + fifo_init(); } void @@ -290,6 +317,14 @@ wmfs_quit(void) free(t); } + /* FIFO stuffs */ + if(W->fifo.fd > 0) + { + close(W->fifo.fd); + unlink(W->fifo.path); + } + free(W->fifo.path); + free(W->net_atom); free(W); diff --git a/src/wmfs.h b/src/wmfs.h index 39aaab9..776ab2f 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -223,6 +223,13 @@ struct wmfs Atom *net_atom; bool running; + /* FIFO stuffs */ + struct + { + char *path; + int fd; + } fifo; + /* Lists heads */ struct {