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)) {
48 /* switch on event system */
49 event->status=ENABLED;
51 while(event->status&ENABLED) {
53 event->rfds=event->rfds_o;
54 event->wfds=event->wfds_o;
57 if((s_ret=select(event->maxfd+1,&(event->rfds),&(event->wfds),NULL,
59 perror("[event] select call");
64 /* 1+ fd(s) available for reading/writing */
65 if(callback0(event,ptr)==CALLBACK_ERROR) {
66 puts("[event] callback0 returned error");
71 /* usual stuff to be executed */
72 if(callback1(event,ptr)==CALLBACK_ERROR) {
73 puts("[event] callback returned error");
82 int event_stop(t_event *event) {
84 puts("[event] shutdown");
85 event->status=DISABLED;