1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* $Id: event.c 1040 2009-09-23 04:14:17Z michael $
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/event.c $
*
* generic timer handling
*
* Copyright (C) 2009 Ed Martin <edman007@edman007.com>
* Copyright (C) 2004, 2009 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* exported functions:
*
* int event_add(void (*callback) (void *data), void *data, const int fd, const int read, const int write);
* Adds a file description to watch
*
* int event_del(const int fd);
* Remove an event
*
* int event_modify(const int fd, const int read, const int write, const int active);
* Modify an event
*
* int named_event_add(char *event, void (*callback) (void *data), void *data);
* Add an event identified by a string
*
* int named_event_del(char *event, void (*callback) (void *data), void *data);
* delete the event identified by this string/callback/data
*
* int named_event_trigger(char *event); //call all calbacks for this event
* call the callbacks of all events that have identified as this string
*
* int event_process(const struct timespec *delay);
* process the event list
*
* void event_exit();
* releases all events
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include "debug.h"
#include "cfg.h"
#include "event.h"
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif
typedef struct {
void (*callback) (event_flags_t flags, void *data);
void *data;
int fd;
int read;
int write;
int active;
int fds_id;
} event_t;
//our set of FDs
static event_t *events = NULL;
static int event_count = 0;
static void free_events(void);
int event_add(void (*callback) (event_flags_t flags, void *data), void *data, const int fd, const int read,
const int write, const int active)
{
event_count++;
events = realloc(events, sizeof(event_t) * event_count);
int i = event_count - 1;
events[i].callback = callback;
events[i].data = data;
events[i].fd = fd;
events[i].read = read;
events[i].write = write;
events[i].active = active;
events[i].fds_id = -1;
return 0;
}
int event_process(const struct timespec *timeout)
{
int i, j;
struct pollfd *fds = malloc(sizeof(struct pollfd) * event_count);
for (i = 0, j = 0; i < event_count; i++) {
events[i].fds_id = -1;
if (events[i].active) {
events[i].fds_id = j;
fds[j].events = 0;
fds[j].fd = events[i].fd;
if (events[i].read) {
fds[j].events |= POLLIN;
}
if (events[i].write) {
fds[j].events |= POLLOUT;
}
j++;
}
}
#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4)
int ready = ppoll(fds, j, timeout, NULL);
#else
int ready = poll(fds, j, timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000);
#endif
if (ready > 0) {
//search the file descriptors, call all relavant callbacks
for (i = 0, j = 0; i < event_count; i++) {
if (events[i].fds_id != j) {
continue;
}
if (fds[j].revents) {
int flags = 0;
if (fds[j].revents & POLLIN) {
flags |= EVENT_READ;
}
if (fds[j].revents & POLLOUT) {
flags |= EVENT_WRITE;
}
if (fds[j].revents & POLLHUP) {
flags |= EVENT_HUP;
}
if (fds[j].revents & POLLERR) {
flags |= EVENT_ERR;
}
events[i].callback(flags, events[i].data);
}
j++;
}
}
free(fds);
return 0;
}
int event_del(const int fd)
{
int i;
for (i = 0; i < event_count; i++) {
if (events[i].fd == fd) {
events[i] = events[event_count - 1];
break;
}
}
event_count--;
events = realloc(events, sizeof(event_t) * event_count);
return 0;
}
int event_modify(const int fd, const int read, const int write, const int active)
{
int i;
for (i = 0; i < event_count; i++) {
if (events[i].fd == fd) {
events[i].read = read;
events[i].write = write;
events[i].active = active;
break;
}
}
event_count--;
events = realloc(events, sizeof(event_t) * event_count);
return 0;
}
static void free_events(void)
{
if (events != NULL) {
free(events);
}
event_count = 0;
events = NULL;
}
void event_exit(void)
{
free_events();
}
/*
* Named events are the user facing side of the event subsystem
*
*/
//we rely on "=" working for copying these structs (no pointers except the callers)
typedef struct {
void (*callback) (void *data);
void *data;
} event_callback_t;
typedef struct {
char *name;
event_callback_t *c;
int callback_count;
} named_event_list_t;
static named_event_list_t *ev_names = NULL;
static int ev_count = 0;
int named_event_add(char *event, void (*callback) (void *data), void *data)
{
if (event == NULL || strlen(event) == 0) {
return 1;
}
if (callback == NULL) {
return 2;
}
int i;
for (i = 0; i < ev_count; i++) {
if (0 == strcmp(event, ev_names[i].name)) {
break;
}
}
if (i >= ev_count) {
//create the entry
ev_count++;
ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count);
ev_names[i].name = strdup(event);
ev_names[i].callback_count = 0;
ev_names[i].c = NULL;
}
int j = ev_names[i].callback_count;
ev_names[i].callback_count++;
ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count);
ev_names[i].c[j].callback = callback;
ev_names[i].c[j].data = data;
return 0;
}
int named_event_del(char *event, void (*callback) (void *data), void *data)
{
int i, j;
for (i = 0; i < ev_count; i++) {
if (0 == strcmp(event, ev_names[i].name)) {
break;
}
}
if (i >= ev_count) {
return 1; //nothing removed
}
for (j = 0; j < ev_names[i].callback_count; j++) {
if (ev_names[i].c[j].callback == callback && ev_names[i].c[j].data == data) {
ev_names[i].callback_count--;
ev_names[i].c[j] = ev_names[i].c[ev_names[i].callback_count];
ev_names[i].c = realloc(ev_names[i].c, sizeof(event_callback_t) * ev_names[i].callback_count);
if (ev_names[i].callback_count == 0) {
//drop this event
free(ev_names[i].name);
ev_count--;
ev_names[i] = ev_names[ev_count];
ev_names = realloc(ev_names, sizeof(named_event_list_t) * ev_count);
}
return 0;
}
}
return 2;
}
int named_event_trigger(char *event)
{
int i, j;
for (i = 0; i < ev_count; i++) {
if (0 == strcmp(event, ev_names[i].name)) {
for (j = 0; j < ev_names[i].callback_count; j++) {
ev_names[i].c[j].callback(ev_names[i].c[j].data);
}
return 0;
}
}
return 1;
}
|