2 * ion/libmainloop/mainloop.c
4 * Partly based on a contributed code.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
12 #include <libtu/types.h>
13 #include <libtu/misc.h>
14 #include <libtu/dlist.h>
19 /*{{{ File descriptor management */
22 static WInputFd *input_fds=NULL;
24 static WInputFd *find_input_fd(int fd)
26 WInputFd *tmp=input_fds;
36 bool mainloop_register_input_fd(int fd, void *data,
37 void (*callback)(int fd, void *d))
41 if(find_input_fd(fd)!=NULL)
50 tmp->process_input_fn=callback;
52 LINK_ITEM(input_fds, tmp, next, prev);
57 void mainloop_unregister_input_fd(int fd)
59 WInputFd *tmp=find_input_fd(fd);
62 UNLINK_ITEM(input_fds, tmp, next, prev);
67 static void set_input_fds(fd_set *rfds, int *nfds)
69 WInputFd *tmp=input_fds;
72 FD_SET(tmp->fd, rfds);
79 static void check_input_fds(fd_set *rfds)
81 WInputFd *tmp=input_fds, *next=NULL;
85 if(FD_ISSET(tmp->fd, rfds))
86 tmp->process_input_fn(tmp->fd, tmp->data);
96 void mainloop_select()
103 set_input_fds(&rfds, &nfds);
105 if(select(nfds+1, &rfds, NULL, NULL, NULL)>0)
106 check_input_fds(&rfds);