1 /* event.c -- event management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int event_init(t_event *event) {
11 puts("[event] initializing event system ...");
15 FD_ZERO(&(event->rfds));
16 FD_ZERO(&(event->rfds_o));
17 FD_ZERO(&(event->wfds));
18 FD_ZERO(&(event->wfds_o));
23 int event_math(int fd,t_event *event,char rw,char ar) {
26 if(rw==READ) FD_SET(fd,&(event->rfds_o));
27 else FD_SET(fd,&(event->wfds_o));
28 if(fd>event->maxfd) event->maxfd=fd;
30 if(rw==READ) FD_CLR(fd,&(event->rfds_o));
31 else FD_CLR(fd,&(event->wfds_o));
32 if(fd==event->maxfd) event->maxfd-=1;
35 printf("[event] %c fd %d to %c file descriptor set\n",ar==ADD?'+':'-',fd,
41 int event_start(t_event *event,void *ptr,
42 int (*callback0)(t_event *event,void *ptr),
43 int (*callback1)(t_event *event,void *ptr)) {
47 /* switch on event system */
48 event->status=ENABLED;
50 while(event->status&ENABLED) {
52 event->rfds=event->rfds_o;
53 event->wfds=event->wfds_o;
55 if((s_ret=select(event->maxfd+1,&(event->rfds),&(event->wfds),NULL,
56 &(event->timeout)))==-1) {
57 perror("[event] select call");
62 /* 1+ fd(s) available for reading/writing */
63 if(callback0(event,ptr)==CALLBACK_ERROR) {
64 puts("[event] callback0 returned error");
69 /* usual stuff to be executed */
70 if(callback1(event,ptr)==CALLBACK_ERROR) {
71 puts("[event] callback returned error");
80 int event_stop(t_event *event) {
82 puts("[event] shutdown");
83 event->status=DISABLED;