Move C linkage binding for c++ to exporting header files instead of includes.
[Rockbox.git] / firmware / events.c
blobf9052958d6c2c5ea1df91a4c464feab820e91234
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Miika Pekkarinen
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include "events.h"
22 #include "panic.h"
24 #define MAX_SYS_EVENTS 10
26 struct sysevent {
27 unsigned short id;
28 bool oneshot;
29 void (*callback)(void *data);
32 static struct sysevent events[MAX_SYS_EVENTS];
34 bool add_event(unsigned short id, bool oneshot, void (*handler))
36 int i;
38 /* Check if the event already exists. */
39 for (i = 0; i < MAX_SYS_EVENTS; i++)
41 if (events[i].callback == handler && events[i].id == id)
42 return false;
45 /* Try to find a free slot. */
46 for (i = 0; i < MAX_SYS_EVENTS; i++)
48 if (events[i].callback == NULL)
50 events[i].id = id;
51 events[i].oneshot = oneshot;
52 events[i].callback = handler;
53 return true;
57 panicf("event line full");
58 return false;
61 void remove_event(unsigned short id, void (*handler))
63 int i;
65 for (i = 0; i < MAX_SYS_EVENTS; i++)
67 if (events[i].id == id && events[i].callback == handler)
69 events[i].callback = NULL;
70 return;
74 panicf("event %d not found", (int)id);
77 void send_event(unsigned short id, void *data)
79 int i;
81 for (i = 0; i < MAX_SYS_EVENTS; i++)
83 if (events[i].id == id && events[i].callback != NULL)
85 events[i].callback(data);
87 if (events[i].oneshot)
88 events[i].callback = NULL;