don't read anymore on stdin, read from fifo file

This commit is contained in:
Julien Danjou 2007-10-12 13:10:43 +02:00
parent daee37199b
commit 33b0e65b56
1 changed files with 35 additions and 14 deletions

View File

@ -24,6 +24,9 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <X11/cursorfont.h> #include <X11/cursorfont.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
@ -40,8 +43,10 @@
#include "util.h" #include "util.h"
#include "statusbar.h" #include "statusbar.h"
#define CONTROL_FIFO_PATH ".awesome_ctl"
static int (*xerrorxlib) (Display *, XErrorEvent *); static int (*xerrorxlib) (Display *, XErrorEvent *);
static Bool readin = True, running = True; static Bool running = True;
/** Cleanup everything on quit /** Cleanup everything on quit
* \param awesomeconf awesome config * \param awesomeconf awesome config
@ -217,7 +222,7 @@ void
uicb_quit(awesome_config *awesomeconf __attribute__((unused)), uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
const char *arg __attribute__ ((unused))) const char *arg __attribute__ ((unused)))
{ {
readin = running = False; running = False;
} }
/* There's no way to check accesses to destroyed windows, thus those cases are /* There's no way to check accesses to destroyed windows, thus those cases are
@ -254,8 +259,9 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p; char *p;
const char *confpath = NULL; const char *confpath = NULL, *homedir;
int r, xfd, e_dummy; char *fifopath;
int r, cfd, xfd, e_dummy;
fd_set rd; fd_set rd;
XEvent ev; XEvent ev;
Display * dpy; Display * dpy;
@ -266,6 +272,8 @@ main(int argc, char *argv[])
Atom netatom[NetLast]; Atom netatom[NetLast];
event_handler **handler; event_handler **handler;
Client **clients, **sel; Client **clients, **sel;
struct stat fifost;
ssize_t fifopath_len;
if(argc >= 2) if(argc >= 2)
{ {
@ -367,33 +375,47 @@ main(int argc, char *argv[])
XSync(dpy, False); XSync(dpy, False);
/* construct fifo path */
homedir = getenv("HOME");
fifopath_len = a_strlen(homedir) + a_strlen(CONTROL_FIFO_PATH) + 2;
fifopath = p_new(char, fifopath_len);
a_strcpy(fifopath, fifopath_len, homedir);
a_strcat(fifopath, fifopath_len, "/");
a_strcat(fifopath, fifopath_len, CONTROL_FIFO_PATH);
if(lstat(fifopath, &fifost) == -1)
if(mkfifo(fifopath, 0600) == -1)
perror("error creating control fifo");
cfd = open(fifopath, O_RDONLY | O_NDELAY);
p_delete(&fifopath);
/* main event loop, also reads status text from stdin */ /* main event loop, also reads status text from stdin */
while(running) while(running)
{ {
FD_ZERO(&rd); FD_ZERO(&rd);
if(readin) if(cfd > 0)
FD_SET(STDIN_FILENO, &rd); FD_SET(cfd, &rd);
FD_SET(xfd, &rd); FD_SET(xfd, &rd);
if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) if(select(MAX(xfd, cfd) + 1, &rd, NULL, NULL, NULL) == -1)
{ {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
eprint("select failed\n"); eprint("select failed\n");
} }
if(FD_ISSET(STDIN_FILENO, &rd)) if(cfd >= 0 && FD_ISSET(cfd, &rd))
{ {
switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1)) switch (r = read(cfd, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
{ {
case -1: case -1:
perror("awesome: error reading fifo");
a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext), a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
strerror(errno), sizeof(awesomeconf[0].statustext) - 1); strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0'; awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
readin = False; cfd = -1;
break; break;
case 0: case 0:
a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
"EOF", 4);
readin = False;
break; break;
default: default:
for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1; for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
@ -413,7 +435,6 @@ main(int argc, char *argv[])
handler[ev.type](&ev, awesomeconf); /* call handler */ handler[ev.type](&ev, awesomeconf); /* call handler */
} }
} }
close(STDIN_FILENO);
cleanup(awesomeconf); cleanup(awesomeconf);
XCloseDisplay(dpy); XCloseDisplay(dpy);