1 /*
2 * ============================================================================
3 * Title: Event Subsystem Tables
4 * Author: J. Zbiciak
5 * ============================================================================
6 * This file contains lookup tables used by the event subsystem, and
7 * lookup functions on those tables.
8 * ============================================================================
9 * Private tables:
10 *
11 * NAME_TO_NUM -- Maps event names to event numbers.
12 * NUM_TO_NAME -- Maps event numbers to event names.
13 *
14 * External APIs:
15 *
16 * EVENT_NAME_TO_NUM Convert an event name to an event number.
17 * EVENT_NUM_TO_NAME Convert an event number to an event name.
18 * ============================================================================
19 */
20
21 #include "config.h"
22 #include "periph/periph.h"
23 #include "event/event.h"
24 #include "event/event_tbl.h"
25
26 /* ======================================================================== */
27 /* Sanity check that the following events go in exactly this order, and */
28 /* are contiguous: */
29 /* */
30 /* LMETA, RMETA, LSUPER, RSUPER, LGUI, RGUI */
31 /* */
32 /* This ensures that files that include event_tbl.h see a consistent set */
33 /* of enum values regardless of whether we build for SDL1 or SDL2, so the */
34 /* same .o files could be used for both links. */
35 /* ======================================================================== */
36 STATIC_ASSERT(EVENT_RMETA == EVENT_LMETA + 1 &&
37 EVENT_LSUPER == EVENT_LMETA + 2 &&
38 EVENT_RSUPER == EVENT_LMETA + 3 &&
39 EVENT_LGUI == EVENT_LMETA + 4 &&
40 EVENT_RGUI == EVENT_LMETA + 5,
41 "Check declaration order for events "
42 "LMETA/RMETA/LSUPER/RSUPER/LGUI/RGUI "
43 "in event/event_tbl.inc");
44
45 /* ======================================================================== */
46 /* EVENT_NAME_T -- Structure used to map event names to numbers. */
47 /* ======================================================================== */
48 typedef struct event_name_to_num
49 {
50 const char *name; /* Printable name for event. */
51 event_num_t num; /* Event index number into mask_tbl */
52 } event_name_to_num_t;
53
54 /* ======================================================================== */
55 /* NAME_TO_NUM -- Maps event names to event numbers. */
56 /* ======================================================================== */
57 #define EVT_DECL(name, num) { name, num },
58 #define EVT_DECL_A(name, num) EVT_DECL(name, num)
59 static const event_name_to_num_t name_to_num[] =
60 {
61 #include "event_tbl.inc"
62 };
63 #undef EVT_DECL
64 #undef EVT_DECL_A
65
66 static const int name_count = sizeof(name_to_num) / sizeof(name_to_num[0]);
67
68 /* ======================================================================== */
69 /* NUM_TO_NAME -- Maps event numbers to event names. */
70 /* ======================================================================== */
71 #define EVT_DECL(name, num) [num] = name,
72 #define EVT_DECL_A(name, num) /* drop aliases */
73 static const char *const num_to_name[] =
74 {
75 #include "event_tbl.inc"
76 };
77 #undef EVT_DECL
78 #undef EVT_DECL_A
79
80 /* ======================================================================== */
81 /* EVENT_NAME_TO_NUM -- Convert an event name to an event_num_t. */
82 /* */
83 /* For now, a slow linear search. Do not place this in a critical path. */
84 /* Returns EVENT_BAD if not found. EVENT_BAD is the only negative event. */
85 /* ======================================================================== */
event_name_to_num(const char * const event_name)86 event_num_t event_name_to_num(const char *const event_name)
87 {
88 /* "!BAD!" is an internal name. Don't let it leak out. */
89 if (!stricmp("!BAD!", event_name))
90 return EVENT_BAD;
91
92 for (int i = 0; i < name_count; ++i)
93 if (!stricmp(event_name, name_to_num[i].name))
94 return name_to_num[i].num;
95
96 return EVENT_BAD;
97 }
98
99 /* ======================================================================== */
100 /* EVENT_NUM_TO_NAME -- Convert an event number to a name, or NULL. */
101 /* ======================================================================== */
event_num_to_name(const event_num_t event_num)102 const char *event_num_to_name(const event_num_t event_num)
103 {
104 if (event_num < 0 || event_num >= EVENT_COUNT)
105 return NULL;
106
107 /* "!BAD!" is an internal name. Don't let it leak out. */
108 if (!stricmp("!BAD!", num_to_name[(int)event_num]))
109 return NULL;
110
111 return num_to_name[(int)event_num];
112 }
113
114 /* ======================================================================== */
115 /* This program is free software; you can redistribute it and/or modify */
116 /* it under the terms of the GNU General Public License as published by */
117 /* the Free Software Foundation; either version 2 of the License, or */
118 /* (at your option) any later version. */
119 /* */
120 /* This program is distributed in the hope that it will be useful, */
121 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
122 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
123 /* General Public License for more details. */
124 /* */
125 /* You should have received a copy of the GNU General Public License along */
126 /* with this program; if not, write to the Free Software Foundation, Inc., */
127 /* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
128 /* ======================================================================== */
129 /* Copyright (c) 1998-2020, Joseph Zbiciak */
130 /* ======================================================================== */
131