Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 by Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include "events.h" |
| 24 | #include "panic.h" |
| 25 | |
Thomas Martitz | d85c3ec | 2009-10-20 21:54:59 +0000 | [diff] [blame] | 26 | #define MAX_SYS_EVENTS 28 |
Bertrik Sikken | 869fbfc | 2008-05-20 19:20:28 +0000 | [diff] [blame] | 27 | |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 28 | struct sysevent { |
| 29 | unsigned short id; |
Jonathan Gordon | a67e5d8 | 2008-03-17 05:22:53 +0000 | [diff] [blame] | 30 | bool oneshot; |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 31 | bool has_user_data; |
| 32 | union { |
| 33 | void (*callback)(unsigned short id, void *event_data); |
| 34 | struct { |
| 35 | void (*callback2)(unsigned short id, void *event_data, void *user_data); |
| 36 | void *user_data; |
| 37 | }; |
| 38 | } handler; |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Bertrik Sikken | 869fbfc | 2008-05-20 19:20:28 +0000 | [diff] [blame] | 41 | static struct sysevent events[MAX_SYS_EVENTS]; |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 42 | |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 43 | static bool do_add_event(unsigned short id, bool oneshot, bool user_data_valid, |
| 44 | void *handler, void *user_data) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | |
Bertrik Sikken | f4eb0ee | 2008-04-27 19:34:41 +0000 | [diff] [blame] | 48 | /* Check if the event already exists. */ |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 49 | for (i = 0; i < MAX_SYS_EVENTS; i++) |
| 50 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 51 | if (events[i].handler.callback == handler && events[i].id == id |
Thomas Martitz | 915bc09 | 2014-03-15 02:23:43 +0100 | [diff] [blame] | 52 | && (!user_data_valid || (user_data == events[i].handler.user_data))) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /* Try to find a free slot. */ |
| 57 | for (i = 0; i < MAX_SYS_EVENTS; i++) |
| 58 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 59 | if (events[i].handler.callback == NULL) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 60 | { |
| 61 | events[i].id = id; |
Jonathan Gordon | a67e5d8 | 2008-03-17 05:22:53 +0000 | [diff] [blame] | 62 | events[i].oneshot = oneshot; |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 63 | if ((events[i].has_user_data = user_data_valid)) |
| 64 | events[i].handler.user_data = user_data; |
| 65 | events[i].handler.callback = handler; |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | panicf("event line full"); |
| 71 | return false; |
| 72 | } |
| 73 | |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 74 | bool add_event(unsigned short id, void (*handler)(unsigned short id, void *data)) |
| 75 | { |
| 76 | return do_add_event(id, false, false, handler, NULL); |
| 77 | } |
| 78 | |
| 79 | bool add_event_ex(unsigned short id, bool oneshot, void (*handler)(unsigned short id, void *event_data, void *user_data), void *user_data) |
| 80 | { |
| 81 | return do_add_event(id, oneshot, true, handler, user_data); |
| 82 | } |
| 83 | |
Thomas Jarosch | fdd4aef | 2015-01-05 18:44:36 +0100 | [diff] [blame] | 84 | static void do_remove_event(unsigned short id, bool user_data_valid, |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 85 | void *handler, void *user_data) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 86 | { |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < MAX_SYS_EVENTS; i++) |
| 90 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 91 | if (events[i].id == id && events[i].handler.callback == handler |
Thomas Martitz | 915bc09 | 2014-03-15 02:23:43 +0100 | [diff] [blame] | 92 | && (!user_data_valid || (user_data == events[i].handler.user_data))) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 93 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 94 | events[i].handler.callback = NULL; |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | } |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 100 | void remove_event(unsigned short id, void (*handler)(unsigned short id, void *data)) |
| 101 | { |
| 102 | do_remove_event(id, false, handler, NULL); |
| 103 | } |
| 104 | |
| 105 | void remove_event_ex(unsigned short id, |
| 106 | void (*handler)(unsigned short id, void *event_data, void *user_data), |
| 107 | void *user_data) |
| 108 | { |
| 109 | do_remove_event(id, true, handler, user_data); |
| 110 | } |
| 111 | |
Jonathan Gordon | a67e5d8 | 2008-03-17 05:22:53 +0000 | [diff] [blame] | 112 | void send_event(unsigned short id, void *data) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 113 | { |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < MAX_SYS_EVENTS; i++) |
| 117 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 118 | if (events[i].id == id && events[i].handler.callback != NULL) |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 119 | { |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 120 | if (events[i].has_user_data) |
| 121 | events[i].handler.callback2(id, data, events[i].handler.user_data); |
| 122 | else |
| 123 | events[i].handler.callback(id, data); |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 124 | |
Jonathan Gordon | a67e5d8 | 2008-03-17 05:22:53 +0000 | [diff] [blame] | 125 | if (events[i].oneshot) |
Thomas Martitz | 470989b | 2014-03-14 23:15:16 +0100 | [diff] [blame] | 126 | events[i].handler.callback = NULL; |
Miika Pekkarinen | 19c6e66 | 2008-03-16 13:55:16 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |