Fix include problem
[kugel-rb.git] / firmware / events.c
blob74172e1fa0b0e7322398f5514c9f9e0157c9a65a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include "events.h"
24 #include "panic.h"
26 #define MAX_SYS_EVENTS 28
28 struct sysevent {
29 unsigned short id;
30 bool oneshot;
31 void (*callback)(void *data);
34 static struct sysevent events[MAX_SYS_EVENTS];
36 bool add_event(unsigned short id, bool oneshot, void (*handler)(void *data))
38 int i;
40 /* Check if the event already exists. */
41 for (i = 0; i < MAX_SYS_EVENTS; i++)
43 if (events[i].callback == handler && events[i].id == id)
44 return false;
47 /* Try to find a free slot. */
48 for (i = 0; i < MAX_SYS_EVENTS; i++)
50 if (events[i].callback == NULL)
52 events[i].id = id;
53 events[i].oneshot = oneshot;
54 events[i].callback = handler;
55 return true;
59 panicf("event line full");
60 return false;
63 void remove_event(unsigned short id, void (*handler)(void *data))
65 int i;
67 for (i = 0; i < MAX_SYS_EVENTS; i++)
69 if (events[i].id == id && events[i].callback == handler)
71 events[i].callback = NULL;
72 return;
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;