2008-03-16 13:55:16 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Miika Pekkarinen
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2008-03-16 13:55:16 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2009-10-20 21:54:59 +00:00
|
|
|
#define MAX_SYS_EVENTS 28
|
2008-05-20 19:20:28 +00:00
|
|
|
|
2008-03-16 13:55:16 +00:00
|
|
|
struct sysevent {
|
|
|
|
unsigned short id;
|
2008-03-17 05:22:53 +00:00
|
|
|
bool oneshot;
|
2014-03-14 22:15:16 +00:00
|
|
|
bool has_user_data;
|
|
|
|
union {
|
|
|
|
void (*callback)(unsigned short id, void *event_data);
|
|
|
|
struct {
|
|
|
|
void (*callback2)(unsigned short id, void *event_data, void *user_data);
|
|
|
|
void *user_data;
|
|
|
|
};
|
|
|
|
} handler;
|
2008-03-16 13:55:16 +00:00
|
|
|
};
|
|
|
|
|
2008-05-20 19:20:28 +00:00
|
|
|
static struct sysevent events[MAX_SYS_EVENTS];
|
2008-03-16 13:55:16 +00:00
|
|
|
|
2014-03-14 22:15:16 +00:00
|
|
|
static bool do_add_event(unsigned short id, bool oneshot, bool user_data_valid,
|
|
|
|
void *handler, void *user_data)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
2022-12-08 10:10:01 +00:00
|
|
|
int free = -1;
|
|
|
|
/* Check if the event already exists. & lowest free slot available */
|
|
|
|
for (int i = MAX_SYS_EVENTS - 1; i >= 0; i--)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
if (events[i].handler.callback == handler && events[i].id == id
|
2022-12-08 10:10:01 +00:00
|
|
|
&& (!user_data_valid || (user_data == events[i].handler.user_data)))
|
|
|
|
{
|
2008-03-16 13:55:16 +00:00
|
|
|
return false;
|
2022-12-08 10:10:01 +00:00
|
|
|
}
|
|
|
|
else if (events[i].handler.callback == NULL)
|
|
|
|
free = i;
|
2008-03-16 13:55:16 +00:00
|
|
|
}
|
2022-12-08 10:10:01 +00:00
|
|
|
|
|
|
|
/* is there a free slot? */
|
|
|
|
if (free >= 0)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
2022-12-08 10:10:01 +00:00
|
|
|
events[free].id = id;
|
|
|
|
events[free].oneshot = oneshot;
|
|
|
|
if ((events[free].has_user_data = user_data_valid))
|
|
|
|
events[free].handler.user_data = user_data;
|
|
|
|
events[free].handler.callback = handler;
|
|
|
|
return true;
|
2008-03-16 13:55:16 +00:00
|
|
|
}
|
2022-12-08 10:10:01 +00:00
|
|
|
|
2008-03-16 13:55:16 +00:00
|
|
|
panicf("event line full");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-14 22:15:16 +00:00
|
|
|
bool add_event(unsigned short id, void (*handler)(unsigned short id, void *data))
|
|
|
|
{
|
|
|
|
return do_add_event(id, false, false, handler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add_event_ex(unsigned short id, bool oneshot, void (*handler)(unsigned short id, void *event_data, void *user_data), void *user_data)
|
|
|
|
{
|
|
|
|
return do_add_event(id, oneshot, true, handler, user_data);
|
|
|
|
}
|
|
|
|
|
2015-01-05 17:44:36 +00:00
|
|
|
static void do_remove_event(unsigned short id, bool user_data_valid,
|
2014-03-14 22:15:16 +00:00
|
|
|
void *handler, void *user_data)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SYS_EVENTS; i++)
|
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
if (events[i].id == id && events[i].handler.callback == handler
|
2014-03-15 01:23:43 +00:00
|
|
|
&& (!user_data_valid || (user_data == events[i].handler.user_data)))
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
events[i].handler.callback = NULL;
|
2008-03-16 13:55:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 22:15:16 +00:00
|
|
|
void remove_event(unsigned short id, void (*handler)(unsigned short id, void *data))
|
|
|
|
{
|
|
|
|
do_remove_event(id, false, handler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_event_ex(unsigned short id,
|
|
|
|
void (*handler)(unsigned short id, void *event_data, void *user_data),
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
do_remove_event(id, true, handler, user_data);
|
|
|
|
}
|
|
|
|
|
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++)
|
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
if (events[i].id == id && events[i].handler.callback != NULL)
|
2008-03-16 13:55:16 +00:00
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
if (events[i].has_user_data)
|
|
|
|
events[i].handler.callback2(id, data, events[i].handler.user_data);
|
|
|
|
else
|
|
|
|
events[i].handler.callback(id, data);
|
2008-03-16 13:55:16 +00:00
|
|
|
|
2008-03-17 05:22:53 +00:00
|
|
|
if (events[i].oneshot)
|
2014-03-14 22:15:16 +00:00
|
|
|
events[i].handler.callback = NULL;
|
2008-03-16 13:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|