The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2007-2008 The Android Open Source Project |
| 2 | ** |
| 3 | ** This software is licensed under the terms of the GNU General Public |
| 4 | ** License version 2, as published by the Free Software Foundation, and |
| 5 | ** may be copied, distributed, and modified under those terms. |
| 6 | ** |
| 7 | ** This program is distributed in the hope that it will be useful, |
| 8 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | ** GNU General Public License for more details. |
| 11 | */ |
| 12 | #include "android/hw-events.h" |
| 13 | #include "android/utils/bufprint.h" |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | typedef struct { |
| 18 | const char* name; |
| 19 | int value; |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 20 | } EventInfo; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 21 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 22 | #define EV_TYPE(n,v) { "EV_" STRINGIFY(n), (v) }, |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 23 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 24 | #define BTN_CODE(n,v) { "BTN_" STRINGIFY(n), (v) }, |
| 25 | #define KEY_CODE(n,v) { "KEY_" STRINGIFY(n), (v) }, |
| 26 | #define REL_CODE(n,v) { "REL_" STRINGIFY(n), (v) }, |
| 27 | #define ABS_CODE(n,v) { "ABS_" STRINGIFY(n), (v) }, |
| 28 | #define END_CODE { NULL, 0 } |
| 29 | |
| 30 | static const EventInfo _ev_types_tab[] = |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 31 | { |
| 32 | EVENT_TYPE_LIST |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 33 | END_CODE |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 36 | static const EventInfo _key_codes_list[] = |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 37 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 38 | EVENT_KEY_LIST |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 39 | EVENT_BTN_LIST |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 40 | END_CODE |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 43 | static const EventInfo _rel_codes_list[] = |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 44 | { |
| 45 | EVENT_REL_LIST |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 46 | END_CODE |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 47 | }; |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 48 | static const EventInfo _abs_codes_list[] = |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 49 | { |
| 50 | EVENT_ABS_LIST |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 51 | END_CODE |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #undef EV_TYPE |
| 55 | #undef BTN_CODE |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 56 | #undef KEY_CODE |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 57 | #undef REL_CODE |
| 58 | #undef ABS_CODE |
| 59 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 60 | typedef const EventInfo* EventList; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 61 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 62 | typedef struct { |
| 63 | int type; |
| 64 | const EventInfo* table; |
| 65 | } EventCodeList; |
| 66 | |
| 67 | |
| 68 | static const EventCodeList _codes[] = { |
| 69 | { EV_KEY, _key_codes_list }, |
| 70 | { EV_REL, _rel_codes_list }, |
| 71 | { EV_ABS, _abs_codes_list }, |
| 72 | { -1, NULL } |
| 73 | }; |
| 74 | |
| 75 | static EventList |
| 76 | eventList_findByType( int type ) |
| 77 | { |
| 78 | int nn; |
| 79 | |
| 80 | for (nn = 0; _codes[nn].type >= 0; nn++) { |
| 81 | if (_codes[nn].type == type) |
| 82 | return _codes[nn].table; |
| 83 | } |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | static int |
| 88 | eventList_getCount( EventList list ) |
| 89 | { |
| 90 | int nn; |
| 91 | |
| 92 | if (list == NULL) |
| 93 | return 0; |
| 94 | |
| 95 | for (nn = 0; list[nn].name != NULL; nn++) { |
| 96 | /* nothing */ |
| 97 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 98 | return nn; |
| 99 | } |
| 100 | |
| 101 | static int |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 102 | eventList_findCodeByName( EventList list, |
| 103 | const char* name, |
| 104 | int namelen ) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 105 | { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 106 | if (namelen <= 0) |
| 107 | return -1; |
| 108 | |
David 'Digit' Turner | 88935f7 | 2011-05-09 10:24:18 +0200 | [diff] [blame] | 109 | for ( ; list->name != NULL; list += 1 ) { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 110 | if ( !memcmp(name, list->name, namelen) && |
| 111 | list->name[namelen] == 0 ) |
| 112 | { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 113 | return list->value; |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 114 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 115 | } |
| 116 | return -1; |
| 117 | } |
| 118 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 119 | static char* |
| 120 | eventList_bufprintCode( EventList list, |
| 121 | int index, |
| 122 | char* buf, |
| 123 | char* bufend ) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 124 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 125 | if (list == NULL) |
| 126 | return buf; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 127 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 128 | return bufprint(buf, bufend, "%s", list[index].name); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | int |
| 133 | android_event_from_str( const char* name, |
| 134 | int *ptype, |
| 135 | int *pcode, |
| 136 | int *pvalue ) |
| 137 | { |
| 138 | const char* p; |
| 139 | const char* pend; |
| 140 | const char* q; |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 141 | EventList list; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 142 | char* end; |
| 143 | |
| 144 | *ptype = 0; |
| 145 | *pcode = 0; |
| 146 | *pvalue = 0; |
| 147 | |
| 148 | p = name; |
| 149 | pend = p + strcspn(p, " \t"); |
| 150 | q = strchr(p, ':'); |
| 151 | if (q == NULL || q > pend) |
| 152 | q = pend; |
| 153 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 154 | *ptype = eventList_findCodeByName( _ev_types_tab, p, q-p ); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 155 | if (*ptype < 0) { |
| 156 | *ptype = (int) strtol( p, &end, 0 ); |
| 157 | if (end != q) |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | if (*q != ':') |
| 162 | return 0; |
| 163 | |
| 164 | p = q + 1; |
| 165 | q = strchr(p, ':'); |
| 166 | if (q == NULL || q > pend) |
| 167 | q = pend; |
| 168 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 169 | list = eventList_findByType( *ptype ); |
David 'Digit' Turner | 88935f7 | 2011-05-09 10:24:18 +0200 | [diff] [blame] | 170 | if (list == NULL) { |
| 171 | *pcode = -1; |
| 172 | } else { |
| 173 | *pcode = eventList_findCodeByName( list, p, q-p ); |
| 174 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 175 | if (*pcode < 0) { |
| 176 | *pcode = (int) strtol( p, &end, 0 ); |
| 177 | if (end != q) |
| 178 | return -2; |
| 179 | } |
| 180 | |
| 181 | if (*q != ':') |
| 182 | return 0; |
| 183 | |
| 184 | p = q + 1; |
| 185 | q = strchr(p, ':'); |
| 186 | if (q == NULL || q > pend) |
| 187 | q = pend; |
| 188 | |
| 189 | *pvalue = (int)strtol( p, &end, 0 ); |
| 190 | if (end != q) |
| 191 | return -3; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int |
| 197 | android_event_get_type_count( void ) |
| 198 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 199 | return eventList_getCount( _ev_types_tab ); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | char* |
| 203 | android_event_bufprint_type_str( char* buff, char* end, int type_index ) |
| 204 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 205 | return eventList_bufprintCode( _ev_types_tab, type_index, buff, end ); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* returns the list of valid event code string aliases for a given event type */ |
| 209 | int |
| 210 | android_event_get_code_count( int type ) |
| 211 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 212 | EventList list = eventList_findByType(type); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 213 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 214 | return eventList_getCount(list); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | char* |
| 218 | android_event_bufprint_code_str( char* buff, char* end, int type, int code_index ) |
| 219 | { |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 220 | EventList list = eventList_findByType(type); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 221 | |
David 'Digit' Turner | a69c35e | 2009-07-30 15:20:54 +0200 | [diff] [blame] | 222 | return eventList_bufprintCode(list, code_index, buff, end); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 223 | } |
| 224 | |