Add FIFO, FIFO located at : ${TMPDIR}/wmfs-${DISPLAY}.fifo

This commit is contained in:
David Delassus 2011-10-17 20:39:48 +02:00
parent 81f07444e9
commit 88d414ce80
5 changed files with 131 additions and 3 deletions

View File

@ -16,6 +16,7 @@ SRCS= \
src/screen.c \
src/tag.c \
src/util.c \
src/fifo.c \
src/wmfs.c

66
src/fifo.c Normal file
View File

@ -0,0 +1,66 @@
/*
* wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { 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);
}

19
src/fifo.h Normal file
View File

@ -0,0 +1,19 @@
/*
* wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
* File created by David Delassus.
* For license, see COPYING.
*/
#ifndef __FIFO_H
#define __FIFO_H
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
void fifo_init(void);
void fifo_read(void);
#endif /* __FIFO_H */

View File

@ -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);

View File

@ -223,6 +223,13 @@ struct wmfs
Atom *net_atom;
bool running;
/* FIFO stuffs */
struct
{
char *path;
int fd;
} fifo;
/* Lists heads */
struct
{