blob: fad5b9539c2d78d6fb84ec72c47e6b328741aa0b
1 | /*********************************************************************** |
2 | * |
3 | * event.h |
4 | * |
5 | * Abstraction of select call into "event-handling" to make programming |
6 | * easier. |
7 | * |
8 | * Copyright (C) 2001 Roaring Penguin Software Inc. |
9 | * |
10 | * This program may be distributed according to the terms of the GNU |
11 | * General Public License, version 2 or (at your option) any later version. |
12 | * |
13 | * $Id$ |
14 | * |
15 | * LIC: GPL |
16 | * |
17 | ***********************************************************************/ |
18 | |
19 | #define DEBUG_EVENT |
20 | |
21 | #ifndef INCLUDE_EVENT_H |
22 | #define INCLUDE_EVENT_H 1 |
23 | |
24 | /* Solaris moans if we don't do this... */ |
25 | #ifdef __sun |
26 | #define __EXTENSIONS__ 1 |
27 | #endif |
28 | |
29 | struct EventSelector_t; |
30 | |
31 | /* Callback function */ |
32 | typedef void (*EventCallbackFunc)(struct EventSelector_t *es, |
33 | int fd, unsigned int flags, |
34 | void *data); |
35 | |
36 | #include "eventpriv.h" |
37 | |
38 | /* Create an event selector */ |
39 | extern EventSelector *Event_CreateSelector(void); |
40 | |
41 | /* Destroy the event selector */ |
42 | extern void Event_DestroySelector(EventSelector *es); |
43 | |
44 | /* Handle one event */ |
45 | extern int Event_HandleEvent(EventSelector *es); |
46 | |
47 | /* Add a handler for a ready file descriptor */ |
48 | extern EventHandler *Event_AddHandler(EventSelector *es, |
49 | int fd, |
50 | unsigned int flags, |
51 | EventCallbackFunc fn, void *data); |
52 | |
53 | /* Add a handler for a ready file descriptor with associated timeout*/ |
54 | extern EventHandler *Event_AddHandlerWithTimeout(EventSelector *es, |
55 | int fd, |
56 | unsigned int flags, |
57 | struct timeval t, |
58 | EventCallbackFunc fn, |
59 | void *data); |
60 | |
61 | |
62 | /* Add a timer handler */ |
63 | extern EventHandler *Event_AddTimerHandler(EventSelector *es, |
64 | struct timeval t, |
65 | EventCallbackFunc fn, |
66 | void *data); |
67 | |
68 | /* Change the timeout of a timer handler */ |
69 | void Event_ChangeTimeout(EventHandler *handler, struct timeval t); |
70 | |
71 | /* Delete a handler */ |
72 | extern int Event_DelHandler(EventSelector *es, |
73 | EventHandler *eh); |
74 | |
75 | /* Retrieve callback function from a handler */ |
76 | extern EventCallbackFunc Event_GetCallback(EventHandler *eh); |
77 | |
78 | /* Retrieve data field from a handler */ |
79 | extern void *Event_GetData(EventHandler *eh); |
80 | |
81 | /* Set callback and data to new values */ |
82 | extern void Event_SetCallbackAndData(EventHandler *eh, |
83 | EventCallbackFunc fn, |
84 | void *data); |
85 | |
86 | /* Handle a signal synchronously in event loop */ |
87 | int Event_HandleSignal(EventSelector *es, int sig, void (*handler)(int sig)); |
88 | |
89 | /* Reap children synchronously in event loop */ |
90 | int Event_HandleChildExit(EventSelector *es, pid_t pid, |
91 | void (*handler)(pid_t, int, void *), void *data); |
92 | |
93 | extern int Event_EnableDebugging(char const *fname); |
94 | |
95 | #ifdef DEBUG_EVENT |
96 | extern void Event_DebugMsg(char const *fmt, ...); |
97 | #define EVENT_DEBUG(x) Event_DebugMsg x |
98 | #else |
99 | #define EVENT_DEBUG(x) ((void) 0) |
100 | #endif |
101 | |
102 | /* Flags */ |
103 | #define EVENT_FLAG_READABLE 1 |
104 | #define EVENT_FLAG_WRITEABLE 2 |
105 | #define EVENT_FLAG_WRITABLE EVENT_FLAG_WRITEABLE |
106 | |
107 | /* This is strictly a timer event */ |
108 | #define EVENT_FLAG_TIMER 4 |
109 | |
110 | /* This is a read or write event with an associated timeout */ |
111 | #define EVENT_FLAG_TIMEOUT 8 |
112 | |
113 | #define EVENT_TIMER_BITS (EVENT_FLAG_TIMER | EVENT_FLAG_TIMEOUT) |
114 | #endif |
115 |