4 * Partly based on a contributed code.
6 * See the included file LICENSE for details.
10 #include <sys/select.h>
12 #include <libtu/types.h>
13 #include <libtu/misc.h>
14 #include <libtu/dlist.h>
20 /*{{{ File descriptor management */
23 static WInputFd *input_fds=NULL;
25 static WInputFd *find_input_fd(int fd)
27 WInputFd *tmp=input_fds;
37 bool mainloop_register_input_fd(int fd, void *data,
38 void (*callback)(int fd, void *d))
42 if(find_input_fd(fd)!=NULL)
51 tmp->process_input_fn=callback;
53 LINK_ITEM(input_fds, tmp, next, prev);
58 void mainloop_unregister_input_fd(int fd)
60 WInputFd *tmp=find_input_fd(fd);
63 UNLINK_ITEM(input_fds, tmp, next, prev);
68 static void set_input_fds(fd_set *rfds, int *nfds)
70 WInputFd *tmp=input_fds;
73 FD_SET(tmp->fd, rfds);
80 static void check_input_fds(fd_set *rfds)
82 WInputFd *tmp=input_fds, *next=NULL;
86 if(FD_ISSET(tmp->fd, rfds))
87 tmp->process_input_fn(tmp->fd, tmp->data);
97 void mainloop_select()
105 set_input_fds(&rfds, &nfds);
111 mainloop_block_signals(&oldmask);
113 if(!mainloop_unhandled_signals())
114 ret=pselect(nfds+1, &rfds, NULL, NULL, NULL, &oldmask);
116 sigprocmask(SIG_SETMASK, &oldmask, NULL);
119 #warning "pselect() unavailable -- using dirty hacks"
121 struct timeval tv_={0, 0}, *tv=&tv_;
123 /* If there are timers, make sure we return from select with
124 * some delay, if the timer signal happens right before
125 * entering select(). Race conditions with other signals
126 * we'll just have to ignore without pselect().
128 if(!libmainloop_get_timeout(tv))
131 if(!mainloop_unhandled_signals())
132 ret=select(nfds+1, &rfds, NULL, NULL, tv);
136 check_input_fds(&rfds);