2008-03-16 13:55:16 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Miika Pekkarinen
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "events.h"
|
|
|
|
#include "panic.h"
|
|
|
|
|
2008-05-20 19:20:28 +00:00
|
|
|
#define MAX_SYS_EVENTS 10
|
|
|
|
|
2008-03-16 13:55:16 +00:00
|
|
|
struct sysevent {
|
|
|
|
unsigned short id;
|
2008-03-17 05:22:53 +00:00
|
|
|
bool oneshot;
|
2008-03-16 13:55:16 +00:00
|
|
|
void (*callback)(void *data);
|
|
|
|
};
|
|
|
|
|
2008-05-20 19:20:28 +00:00
|
|
|
static struct sysevent events[MAX_SYS_EVENTS];
|
2008-03-16 13:55:16 +00:00
|
|
|
|
2008-03-17 05:22:53 +00:00
|
|
|
bool add_event(unsigned short id, bool oneshot, void (*handler))
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-04-27 19:34:41 +00:00
|
|
|
/* Check if the event already exists. */
|
2008-03-16 13:55:16 +00:00
|
|
|
for (i = 0; i < MAX_SYS_EVENTS; i++)
|
|
|
|
{
|
|
|
|
if (events[i].callback == handler && events[i].id == id)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to find a free slot. */
|
|
|
|
for (i = 0; i < MAX_SYS_EVENTS; i++)
|
|
|
|
{
|
|
|
|
if (events[i].callback == NULL)
|
|
|
|
{
|
|
|
|
events[i].id = id;
|
2008-03-17 05:22:53 +00:00
|
|
|
events[i].oneshot = oneshot;
|
2008-03-16 13:55:16 +00:00
|
|
|
events[i].callback = handler;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panicf("event line full");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_event(unsigned short id, void (*handler))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SYS_EVENTS; i++)
|
|
|
|
{
|
|
|
|
if (events[i].id == id && events[i].callback == handler)
|
|
|
|
{
|
|
|
|
events[i].callback = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 19:34:41 +00:00
|
|
|
panicf("event %d not found", (int)id);
|
2008-03-16 13:55:16 +00:00
|
|
|
}
|
|
|
|
|
2008-03-17 05:22:53 +00:00
|
|
|
void send_event(unsigned short id, void *data)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SYS_EVENTS; i++)
|
|
|
|
{
|
|
|
|
if (events[i].id == id && events[i].callback != NULL)
|
|
|
|
{
|
|
|
|
events[i].callback(data);
|
|
|
|
|
2008-03-17 05:22:53 +00:00
|
|
|
if (events[i].oneshot)
|
2008-03-16 13:55:16 +00:00
|
|
|
events[i].callback = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|