summaryrefslogtreecommitdiff
path: root/firmware/events.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/events.c')
-rw-r--r--firmware/events.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/firmware/events.c b/firmware/events.c
new file mode 100644
index 0000000000..eaf2e5c352
--- /dev/null
+++ b/firmware/events.c
@@ -0,0 +1,88 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 by Miika Pekkarinen
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <stdio.h>
21#include "events.h"
22#include "panic.h"
23
24struct sysevent {
25 unsigned short id;
26 void (*callback)(void *data);
27};
28
29struct sysevent events[MAX_SYS_EVENTS];
30
31bool add_event(unsigned short id, void (*handler))
32{
33 int i;
34
35 /* Chcek if the event already exists. */
36 for (i = 0; i < MAX_SYS_EVENTS; i++)
37 {
38 if (events[i].callback == handler && events[i].id == id)
39 return false;
40 }
41
42 /* Try to find a free slot. */
43 for (i = 0; i < MAX_SYS_EVENTS; i++)
44 {
45 if (events[i].callback == NULL)
46 {
47 events[i].id = id;
48 events[i].callback = handler;
49 return true;
50 }
51 }
52
53 panicf("event line full");
54 return false;
55}
56
57void remove_event(unsigned short id, void (*handler))
58{
59 int i;
60
61 for (i = 0; i < MAX_SYS_EVENTS; i++)
62 {
63 if (events[i].id == id && events[i].callback == handler)
64 {
65 events[i].callback = NULL;
66 return;
67 }
68 }
69
70 panicf("event not found");
71}
72
73void send_event(unsigned short id, bool oneshot, void *data)
74{
75 int i;
76
77 for (i = 0; i < MAX_SYS_EVENTS; i++)
78 {
79 if (events[i].id == id && events[i].callback != NULL)
80 {
81 events[i].callback(data);
82
83 if (oneshot)
84 events[i].callback = NULL;
85 }
86 }
87}
88