-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.c
42 lines (34 loc) · 876 Bytes
/
event.c
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
/* event.c */
#include "event.h"
/* receive_event */
int receive_event(Queue *events, const Event *event) {
Event *new_event;
/* Allocate space for the new_event. */
if ((new_event = (Event *)malloc(sizeof(Event))) == NULL) {
return -1;
}
/* Make a copy of new_event and enqueue it. */
memcpy(new_event, event, sizeof(Event));
if (queue_enqueue(events, new_event) != 0) {
return -1;
}
return 0;
}
/* process_event */
int process_event(Queue *events, int (*dispatch)(Event *event)) {
Event *event;
if (queue_size(events) == 0) {
/* Return that there are not events to dispatch. */
return -1;
} else {
if (queue_dequeue(events, (void **)&event) != 0) {
/* Return that an event couldn't be retrieved. */
return -1;
} else {
/* Call a user-defined function to dispatch the event. */
dispatch(event);
free(event);
}
}
return 0;
}