From b2aff93c0cf1e33b37c48181c3f2ddc2ad6ce16b Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Wed, 31 Aug 2011 22:03:42 +0200 Subject: [PATCH] Multiplexage for fifo/x event --- wmfs2/src/fifo.c | 17 ++++++++++------- wmfs2/src/fifo.h | 7 ++++--- wmfs2/src/wmfs.c | 24 +++++++++++++++++++----- wmfs2/src/wmfs.h | 2 +- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/wmfs2/src/fifo.c b/wmfs2/src/fifo.c index b2b4a4f..30cfb8f 100644 --- a/wmfs2/src/fifo.c +++ b/wmfs2/src/fifo.c @@ -1,5 +1,6 @@ /* * wmfs2 by Martin Duquesnoy { for(i = 2011; i < 2111; ++i) ©(i); } + * File created by David Delassus. * For license, see COPYING. */ @@ -14,13 +15,12 @@ fifo_init(void) { W->fifo.path = FIFO_DEFAULT_PATH; - if(mkfifo(W->fifo.path, 0644) < 0 || !(W->fifo.fp = fopen(W->fifo.path, "w+"))) + 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)); return; } - - fcntl(fileno(W->fifo.fp), F_SETFL, O_NONBLOCK); } static void @@ -30,11 +30,13 @@ fifo_parse(char *cmd) char *uicb = strtok(cmd, " "); char *arg = strtok(NULL, "\n"); - printf ("%s %s\n", uicb, arg); + printf("%s %s\n", uicb, arg); func = uicb_name_func(uicb); if(func) func(arg); + + XSync(W->dpy, false); } void @@ -43,11 +45,12 @@ fifo_read(void) char buf[256] = {0}; /* Don't read it if not open */ - if(!(W->fifo.fp)) return; + if(!(W->fifo.fd)) + return; - fread(buf, sizeof(buf) - 1, 1, W->fifo.fp); + read(W->fifo.fd, buf, sizeof(buf) - 1); - if (buf[0]) + if(buf[0]) fifo_parse(buf); } diff --git a/wmfs2/src/fifo.h b/wmfs2/src/fifo.h index e63457a..3c1395e 100644 --- a/wmfs2/src/fifo.h +++ b/wmfs2/src/fifo.h @@ -1,7 +1,8 @@ /* -* wmfs2 by Martin Duquesnoy { for(i = 2011; i < 2111; ++i) ©(i); } -* For license, see COPYING. -*/ + * 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 diff --git a/wmfs2/src/wmfs.c b/wmfs2/src/wmfs.c index 18e62bb..600f33e 100644 --- a/wmfs2/src/wmfs.c +++ b/wmfs2/src/wmfs.c @@ -212,13 +212,27 @@ static void wmfs_loop(void) { XEvent ev; + int fd = ConnectionNumber(W->dpy); + fd_set iset; - while(XPending(W->dpy)) + while(W->running) { - while(W->running && !XNextEvent(W->dpy, &ev)) + FD_ZERO(&iset); + FD_SET(fd, &iset); + FD_SET(W->fifo.fd, &iset); + + if(select(fd + W->fifo.fd + 1, &iset, NULL, NULL, NULL) > 0) { - HANDLE_EVENT(&ev); - fifo_read(); + if(FD_ISSET(fd, &iset)) + { + while(XPending(W->dpy)) + { + XNextEvent(W->dpy, &ev); + HANDLE_EVENT(&ev); + } + } + else if(FD_ISSET(W->fifo.fd, &iset)) + fifo_read(); } } } @@ -252,7 +266,7 @@ wmfs_quit(void) FREE_LIST(Theme, W->h.theme); /* FIFO stuffs */ - fclose(W->fifo.fp); + close(W->fifo.fd); free(W->fifo.path); W->running = false; diff --git a/wmfs2/src/wmfs.h b/wmfs2/src/wmfs.h index d38658e..26a031e 100644 --- a/wmfs2/src/wmfs.h +++ b/wmfs2/src/wmfs.h @@ -216,7 +216,7 @@ struct Wmfs /* FIFO */ struct { - FILE *fp; + int fd; char *path; } fifo;