blob: 4318f65fbe14394e057b4b0a7131c073e361eac1 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/* 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
17typedef struct {
18 const char* name;
19 int value;
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020020} EventInfo;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080021
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020022#define EV_TYPE(n,v) { "EV_" STRINGIFY(n), (v) },
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080023
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020024#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
30static const EventInfo _ev_types_tab[] =
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080031{
32 EVENT_TYPE_LIST
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020033 END_CODE
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080034};
35
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020036static const EventInfo _key_codes_list[] =
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080037{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020038 EVENT_KEY_LIST
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080039 EVENT_BTN_LIST
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020040 END_CODE
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080041};
42
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020043static const EventInfo _rel_codes_list[] =
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080044{
45 EVENT_REL_LIST
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020046 END_CODE
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080047};
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020048static const EventInfo _abs_codes_list[] =
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080049{
50 EVENT_ABS_LIST
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020051 END_CODE
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080052};
53
54#undef EV_TYPE
55#undef BTN_CODE
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020056#undef KEY_CODE
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080057#undef REL_CODE
58#undef ABS_CODE
59
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020060typedef const EventInfo* EventList;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080061
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +020062typedef struct {
63 int type;
64 const EventInfo* table;
65} EventCodeList;
66
67
68static 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
75static EventList
76eventList_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
87static int
88eventList_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 Project8b23a6c2009-03-03 19:30:32 -080098 return nn;
99}
100
101static int
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200102eventList_findCodeByName( EventList list,
103 const char* name,
104 int namelen )
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800105{
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800106 if (namelen <= 0)
107 return -1;
108
David 'Digit' Turner88935f72011-05-09 10:24:18 +0200109 for ( ; list->name != NULL; list += 1 ) {
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200110 if ( !memcmp(name, list->name, namelen) &&
111 list->name[namelen] == 0 )
112 {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800113 return list->value;
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200114 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800115 }
116 return -1;
117}
118
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200119static char*
120eventList_bufprintCode( EventList list,
121 int index,
122 char* buf,
123 char* bufend )
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800124{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200125 if (list == NULL)
126 return buf;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800127
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200128 return bufprint(buf, bufend, "%s", list[index].name);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800129}
130
131
132int
133android_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' Turnera69c35e2009-07-30 15:20:54 +0200141 EventList list;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800142 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' Turnera69c35e2009-07-30 15:20:54 +0200154 *ptype = eventList_findCodeByName( _ev_types_tab, p, q-p );
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800155 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' Turnera69c35e2009-07-30 15:20:54 +0200169 list = eventList_findByType( *ptype );
David 'Digit' Turner88935f72011-05-09 10:24:18 +0200170 if (list == NULL) {
171 *pcode = -1;
172 } else {
173 *pcode = eventList_findCodeByName( list, p, q-p );
174 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800175 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
196int
197android_event_get_type_count( void )
198{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200199 return eventList_getCount( _ev_types_tab );
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800200}
201
202char*
203android_event_bufprint_type_str( char* buff, char* end, int type_index )
204{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200205 return eventList_bufprintCode( _ev_types_tab, type_index, buff, end );
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800206}
207
208/* returns the list of valid event code string aliases for a given event type */
209int
210android_event_get_code_count( int type )
211{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200212 EventList list = eventList_findByType(type);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800213
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200214 return eventList_getCount(list);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800215}
216
217char*
218android_event_bufprint_code_str( char* buff, char* end, int type, int code_index )
219{
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200220 EventList list = eventList_findByType(type);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800221
David 'Digit' Turnera69c35e2009-07-30 15:20:54 +0200222 return eventList_bufprintCode(list, code_index, buff, end);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800223}
224