2 * ion/libmainloop/mainloop.c
4 * Partly based on a contributed code.
6 * See the included file LICENSE for details.
9 #include <libtu/types.h>
10 #include <libtu/misc.h>
11 #include <libtu/dlist.h>
16 /*{{{ File descriptor management */
19 static WInputFd *input_fds=NULL;
21 static WInputFd *find_input_fd(int fd)
23 WInputFd *tmp=input_fds;
33 bool mainloop_register_input_fd(int fd, void *data,
34 void (*callback)(int fd, void *d))
38 if(find_input_fd(fd)!=NULL)
47 tmp->process_input_fn=callback;
49 LINK_ITEM(input_fds, tmp, next, prev);
54 void mainloop_unregister_input_fd(int fd)
56 WInputFd *tmp=find_input_fd(fd);
59 UNLINK_ITEM(input_fds, tmp, next, prev);
64 static void set_input_fds(fd_set *rfds, int *nfds)
66 WInputFd *tmp=input_fds;
69 FD_SET(tmp->fd, rfds);
76 static void check_input_fds(fd_set *rfds)
78 WInputFd *tmp=input_fds, *next=NULL;
82 if(FD_ISSET(tmp->fd, rfds))
83 tmp->process_input_fn(tmp->fd, tmp->data);
93 void mainloop_select()
100 set_input_fds(&rfds, &nfds);
102 if(select(nfds+1, &rfds, NULL, NULL, NULL)>0)
103 check_input_fds(&rfds);