Fix FIFO (close it and reopen it after reading)

This commit is contained in:
David Delassus 2011-10-30 18:18:03 +01:00
parent 984145243d
commit 7b9d9d1e53
3 changed files with 22 additions and 10 deletions

View File

@ -17,7 +17,7 @@ fifo_init(void)
if(mkfifo(W->fifo.path, 0644) < 0)
warnx("Can't create FIFO: %s\n", strerror(errno));
if(!(W->fifo.fd = open(W->fifo.path, O_NONBLOCK, 0)))
if(!(W->fifo.fd = open(W->fifo.path, O_RDONLY | O_NDELAY, 0)))
warnx("Can't open FIFO: %s\n", strerror(errno));
}
@ -42,11 +42,14 @@ fifo_parse(char *cmd)
XSync(W->dpy, false);
}
void
int
fifo_read(void)
{
static char buf[256] = { 0 };
char buf[256] = { 0 };
int ret = 0;
if(read(W->fifo.fd, buf, sizeof(buf) - 1) > 0)
if((ret = read(W->fifo.fd, buf, sizeof(buf) - 1)) > 0)
fifo_parse(buf);
return ret;
}

View File

@ -14,6 +14,6 @@
#include <string.h>
void fifo_init(void);
void fifo_read(void);
int fifo_read(void);
#endif /* __FIFO_H */

View File

@ -243,19 +243,20 @@ wmfs_loop(void)
{
XEvent ev;
int fd = ConnectionNumber(W->dpy);
int maxfd = fd + 1;
fd_set iset;
if(W->fifo.fd > 0)
maxfd += W->fifo.fd;
while(W->running)
{
int maxfd = fd + 1;
FD_ZERO(&iset);
FD_SET(fd, &iset);
if(W->fifo.fd > 0)
{
maxfd += W->fifo.fd;
FD_SET(W->fifo.fd, &iset);
}
if(select(maxfd, &iset, NULL, NULL, NULL) > 0)
{
@ -268,7 +269,15 @@ wmfs_loop(void)
}
}
else if(W->fifo.fd > 0 && FD_ISSET(W->fifo.fd, &iset))
fifo_read();
{
if (fifo_read() == 0)
{
close (W->fifo.fd);
if (!(W->fifo.fd = open (W->fifo.path, O_RDONLY | O_NDELAY, 0)))
warnx ("Can't reopen FIFO: %s\n", strerror (errno));
}
}
}
}
}