1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _LIBINPUT_INPUT_EVENT_LABELS_H
18 #define _LIBINPUT_INPUT_EVENT_LABELS_H
19 
20 #include <input/Input.h>
21 #include <android/keycodes.h>
22 
23 #define DEFINE_KEYCODE(key) { #key, AKEYCODE_##key }
24 #define DEFINE_AXIS(axis) { #axis, AMOTION_EVENT_AXIS_##axis }
25 #define DEFINE_LED(led) { #led, ALED_##led }
26 #define DEFINE_FLAG(flag) { #flag, POLICY_FLAG_##flag }
27 
28 namespace android {
29 
30 template<typename T, size_t N>
size(T (&)[N])31 size_t size(T (&)[N]) { return N; }
32 
33 struct InputEventLabel {
34     const char *literal;
35     int value;
36 };
37 
38 
39 static const InputEventLabel KEYCODES[] = {
40     // NOTE: If you add a new keycode here you must also add it to several other files.
41     //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
42     DEFINE_KEYCODE(UNKNOWN),
43     DEFINE_KEYCODE(SOFT_LEFT),
44     DEFINE_KEYCODE(SOFT_RIGHT),
45     DEFINE_KEYCODE(HOME),
46     DEFINE_KEYCODE(BACK),
47     DEFINE_KEYCODE(CALL),
48     DEFINE_KEYCODE(ENDCALL),
49     DEFINE_KEYCODE(0),
50     DEFINE_KEYCODE(1),
51     DEFINE_KEYCODE(2),
52     DEFINE_KEYCODE(3),
53     DEFINE_KEYCODE(4),
54     DEFINE_KEYCODE(5),
55     DEFINE_KEYCODE(6),
56     DEFINE_KEYCODE(7),
57     DEFINE_KEYCODE(8),
58     DEFINE_KEYCODE(9),
59     DEFINE_KEYCODE(STAR),
60     DEFINE_KEYCODE(POUND),
61     DEFINE_KEYCODE(DPAD_UP),
62     DEFINE_KEYCODE(DPAD_DOWN),
63     DEFINE_KEYCODE(DPAD_LEFT),
64     DEFINE_KEYCODE(DPAD_RIGHT),
65     DEFINE_KEYCODE(DPAD_CENTER),
66     DEFINE_KEYCODE(VOLUME_UP),
67     DEFINE_KEYCODE(VOLUME_DOWN),
68     DEFINE_KEYCODE(POWER),
69     DEFINE_KEYCODE(CAMERA),
70     DEFINE_KEYCODE(CLEAR),
71     DEFINE_KEYCODE(A),
72     DEFINE_KEYCODE(B),
73     DEFINE_KEYCODE(C),
74     DEFINE_KEYCODE(D),
75     DEFINE_KEYCODE(E),
76     DEFINE_KEYCODE(F),
77     DEFINE_KEYCODE(G),
78     DEFINE_KEYCODE(H),
79     DEFINE_KEYCODE(I),
80     DEFINE_KEYCODE(J),
81     DEFINE_KEYCODE(K),
82     DEFINE_KEYCODE(L),
83     DEFINE_KEYCODE(M),
84     DEFINE_KEYCODE(N),
85     DEFINE_KEYCODE(O),
86     DEFINE_KEYCODE(P),
87     DEFINE_KEYCODE(Q),
88     DEFINE_KEYCODE(R),
89     DEFINE_KEYCODE(S),
90     DEFINE_KEYCODE(T),
91     DEFINE_KEYCODE(U),
92     DEFINE_KEYCODE(V),
93     DEFINE_KEYCODE(W),
94     DEFINE_KEYCODE(X),
95     DEFINE_KEYCODE(Y),
96     DEFINE_KEYCODE(Z),
97     DEFINE_KEYCODE(COMMA),
98     DEFINE_KEYCODE(PERIOD),
99     DEFINE_KEYCODE(ALT_LEFT),
100     DEFINE_KEYCODE(ALT_RIGHT),
101     DEFINE_KEYCODE(SHIFT_LEFT),
102     DEFINE_KEYCODE(SHIFT_RIGHT),
103     DEFINE_KEYCODE(TAB),
104     DEFINE_KEYCODE(SPACE),
105     DEFINE_KEYCODE(SYM),
106     DEFINE_KEYCODE(EXPLORER),
107     DEFINE_KEYCODE(ENVELOPE),
108     DEFINE_KEYCODE(ENTER),
109     DEFINE_KEYCODE(DEL),
110     DEFINE_KEYCODE(GRAVE),
111     DEFINE_KEYCODE(MINUS),
112     DEFINE_KEYCODE(EQUALS),
113     DEFINE_KEYCODE(LEFT_BRACKET),
114     DEFINE_KEYCODE(RIGHT_BRACKET),
115     DEFINE_KEYCODE(BACKSLASH),
116     DEFINE_KEYCODE(SEMICOLON),
117     DEFINE_KEYCODE(APOSTROPHE),
118     DEFINE_KEYCODE(SLASH),
119     DEFINE_KEYCODE(AT),
120     DEFINE_KEYCODE(NUM),
121     DEFINE_KEYCODE(HEADSETHOOK),
122     DEFINE_KEYCODE(FOCUS),   // *Camera* focus
123     DEFINE_KEYCODE(PLUS),
124     DEFINE_KEYCODE(MENU),
125     DEFINE_KEYCODE(NOTIFICATION),
126     DEFINE_KEYCODE(SEARCH),
127     DEFINE_KEYCODE(MEDIA_PLAY_PAUSE),
128     DEFINE_KEYCODE(MEDIA_STOP),
129     DEFINE_KEYCODE(MEDIA_NEXT),
130     DEFINE_KEYCODE(MEDIA_PREVIOUS),
131     DEFINE_KEYCODE(MEDIA_REWIND),
132     DEFINE_KEYCODE(MEDIA_FAST_FORWARD),
133     DEFINE_KEYCODE(MUTE),
134     DEFINE_KEYCODE(PAGE_UP),
135     DEFINE_KEYCODE(PAGE_DOWN),
136     DEFINE_KEYCODE(PICTSYMBOLS),
137     DEFINE_KEYCODE(SWITCH_CHARSET),
138     DEFINE_KEYCODE(BUTTON_A),
139     DEFINE_KEYCODE(BUTTON_B),
140     DEFINE_KEYCODE(BUTTON_C),
141     DEFINE_KEYCODE(BUTTON_X),
142     DEFINE_KEYCODE(BUTTON_Y),
143     DEFINE_KEYCODE(BUTTON_Z),
144     DEFINE_KEYCODE(BUTTON_L1),
145     DEFINE_KEYCODE(BUTTON_R1),
146     DEFINE_KEYCODE(BUTTON_L2),
147     DEFINE_KEYCODE(BUTTON_R2),
148     DEFINE_KEYCODE(BUTTON_THUMBL),
149     DEFINE_KEYCODE(BUTTON_THUMBR),
150     DEFINE_KEYCODE(BUTTON_START),
151     DEFINE_KEYCODE(BUTTON_SELECT),
152     DEFINE_KEYCODE(BUTTON_MODE),
153     DEFINE_KEYCODE(ESCAPE),
154     DEFINE_KEYCODE(FORWARD_DEL),
155     DEFINE_KEYCODE(CTRL_LEFT),
156     DEFINE_KEYCODE(CTRL_RIGHT),
157     DEFINE_KEYCODE(CAPS_LOCK),
158     DEFINE_KEYCODE(SCROLL_LOCK),
159     DEFINE_KEYCODE(META_LEFT),
160     DEFINE_KEYCODE(META_RIGHT),
161     DEFINE_KEYCODE(FUNCTION),
162     DEFINE_KEYCODE(SYSRQ),
163     DEFINE_KEYCODE(BREAK),
164     DEFINE_KEYCODE(MOVE_HOME),
165     DEFINE_KEYCODE(MOVE_END),
166     DEFINE_KEYCODE(INSERT),
167     DEFINE_KEYCODE(FORWARD),
168     DEFINE_KEYCODE(MEDIA_PLAY),
169     DEFINE_KEYCODE(MEDIA_PAUSE),
170     DEFINE_KEYCODE(MEDIA_CLOSE),
171     DEFINE_KEYCODE(MEDIA_EJECT),
172     DEFINE_KEYCODE(MEDIA_RECORD),
173     DEFINE_KEYCODE(F1),
174     DEFINE_KEYCODE(F2),
175     DEFINE_KEYCODE(F3),
176     DEFINE_KEYCODE(F4),
177     DEFINE_KEYCODE(F5),
178     DEFINE_KEYCODE(F6),
179     DEFINE_KEYCODE(F7),
180     DEFINE_KEYCODE(F8),
181     DEFINE_KEYCODE(F9),
182     DEFINE_KEYCODE(F10),
183     DEFINE_KEYCODE(F11),
184     DEFINE_KEYCODE(F12),
185     DEFINE_KEYCODE(NUM_LOCK),
186     DEFINE_KEYCODE(NUMPAD_0),
187     DEFINE_KEYCODE(NUMPAD_1),
188     DEFINE_KEYCODE(NUMPAD_2),
189     DEFINE_KEYCODE(NUMPAD_3),
190     DEFINE_KEYCODE(NUMPAD_4),
191     DEFINE_KEYCODE(NUMPAD_5),
192     DEFINE_KEYCODE(NUMPAD_6),
193     DEFINE_KEYCODE(NUMPAD_7),
194     DEFINE_KEYCODE(NUMPAD_8),
195     DEFINE_KEYCODE(NUMPAD_9),
196     DEFINE_KEYCODE(NUMPAD_DIVIDE),
197     DEFINE_KEYCODE(NUMPAD_MULTIPLY),
198     DEFINE_KEYCODE(NUMPAD_SUBTRACT),
199     DEFINE_KEYCODE(NUMPAD_ADD),
200     DEFINE_KEYCODE(NUMPAD_DOT),
201     DEFINE_KEYCODE(NUMPAD_COMMA),
202     DEFINE_KEYCODE(NUMPAD_ENTER),
203     DEFINE_KEYCODE(NUMPAD_EQUALS),
204     DEFINE_KEYCODE(NUMPAD_LEFT_PAREN),
205     DEFINE_KEYCODE(NUMPAD_RIGHT_PAREN),
206     DEFINE_KEYCODE(VOLUME_MUTE),
207     DEFINE_KEYCODE(INFO),
208     DEFINE_KEYCODE(CHANNEL_UP),
209     DEFINE_KEYCODE(CHANNEL_DOWN),
210     DEFINE_KEYCODE(ZOOM_IN),
211     DEFINE_KEYCODE(ZOOM_OUT),
212     DEFINE_KEYCODE(TV),
213     DEFINE_KEYCODE(WINDOW),
214     DEFINE_KEYCODE(GUIDE),
215     DEFINE_KEYCODE(DVR),
216     DEFINE_KEYCODE(BOOKMARK),
217     DEFINE_KEYCODE(CAPTIONS),
218     DEFINE_KEYCODE(SETTINGS),
219     DEFINE_KEYCODE(TV_POWER),
220     DEFINE_KEYCODE(TV_INPUT),
221     DEFINE_KEYCODE(STB_POWER),
222     DEFINE_KEYCODE(STB_INPUT),
223     DEFINE_KEYCODE(AVR_POWER),
224     DEFINE_KEYCODE(AVR_INPUT),
225     DEFINE_KEYCODE(PROG_RED),
226     DEFINE_KEYCODE(PROG_GREEN),
227     DEFINE_KEYCODE(PROG_YELLOW),
228     DEFINE_KEYCODE(PROG_BLUE),
229     DEFINE_KEYCODE(APP_SWITCH),
230     DEFINE_KEYCODE(BUTTON_1),
231     DEFINE_KEYCODE(BUTTON_2),
232     DEFINE_KEYCODE(BUTTON_3),
233     DEFINE_KEYCODE(BUTTON_4),
234     DEFINE_KEYCODE(BUTTON_5),
235     DEFINE_KEYCODE(BUTTON_6),
236     DEFINE_KEYCODE(BUTTON_7),
237     DEFINE_KEYCODE(BUTTON_8),
238     DEFINE_KEYCODE(BUTTON_9),
239     DEFINE_KEYCODE(BUTTON_10),
240     DEFINE_KEYCODE(BUTTON_11),
241     DEFINE_KEYCODE(BUTTON_12),
242     DEFINE_KEYCODE(BUTTON_13),
243     DEFINE_KEYCODE(BUTTON_14),
244     DEFINE_KEYCODE(BUTTON_15),
245     DEFINE_KEYCODE(BUTTON_16),
246     DEFINE_KEYCODE(LANGUAGE_SWITCH),
247     DEFINE_KEYCODE(MANNER_MODE),
248     DEFINE_KEYCODE(3D_MODE),
249     DEFINE_KEYCODE(CONTACTS),
250     DEFINE_KEYCODE(CALENDAR),
251     DEFINE_KEYCODE(MUSIC),
252     DEFINE_KEYCODE(CALCULATOR),
253     DEFINE_KEYCODE(ZENKAKU_HANKAKU),
254     DEFINE_KEYCODE(EISU),
255     DEFINE_KEYCODE(MUHENKAN),
256     DEFINE_KEYCODE(HENKAN),
257     DEFINE_KEYCODE(KATAKANA_HIRAGANA),
258     DEFINE_KEYCODE(YEN),
259     DEFINE_KEYCODE(RO),
260     DEFINE_KEYCODE(KANA),
261     DEFINE_KEYCODE(ASSIST),
262     DEFINE_KEYCODE(BRIGHTNESS_DOWN),
263     DEFINE_KEYCODE(BRIGHTNESS_UP),
264     DEFINE_KEYCODE(MEDIA_AUDIO_TRACK),
265     DEFINE_KEYCODE(SLEEP),
266     DEFINE_KEYCODE(WAKEUP),
267     DEFINE_KEYCODE(PAIRING),
268     DEFINE_KEYCODE(MEDIA_TOP_MENU),
269     DEFINE_KEYCODE(11),
270     DEFINE_KEYCODE(12),
271     DEFINE_KEYCODE(LAST_CHANNEL),
272     DEFINE_KEYCODE(TV_DATA_SERVICE),
273     DEFINE_KEYCODE(VOICE_ASSIST),
274     DEFINE_KEYCODE(TV_RADIO_SERVICE),
275     DEFINE_KEYCODE(TV_TELETEXT),
276     DEFINE_KEYCODE(TV_NUMBER_ENTRY),
277     DEFINE_KEYCODE(TV_TERRESTRIAL_ANALOG),
278     DEFINE_KEYCODE(TV_TERRESTRIAL_DIGITAL),
279     DEFINE_KEYCODE(TV_SATELLITE),
280     DEFINE_KEYCODE(TV_SATELLITE_BS),
281     DEFINE_KEYCODE(TV_SATELLITE_CS),
282     DEFINE_KEYCODE(TV_SATELLITE_SERVICE),
283     DEFINE_KEYCODE(TV_NETWORK),
284     DEFINE_KEYCODE(TV_ANTENNA_CABLE),
285     DEFINE_KEYCODE(TV_INPUT_HDMI_1),
286     DEFINE_KEYCODE(TV_INPUT_HDMI_2),
287     DEFINE_KEYCODE(TV_INPUT_HDMI_3),
288     DEFINE_KEYCODE(TV_INPUT_HDMI_4),
289     DEFINE_KEYCODE(TV_INPUT_COMPOSITE_1),
290     DEFINE_KEYCODE(TV_INPUT_COMPOSITE_2),
291     DEFINE_KEYCODE(TV_INPUT_COMPONENT_1),
292     DEFINE_KEYCODE(TV_INPUT_COMPONENT_2),
293     DEFINE_KEYCODE(TV_INPUT_VGA_1),
294     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION),
295     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION_MIX_UP),
296     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION_MIX_DOWN),
297     DEFINE_KEYCODE(TV_ZOOM_MODE),
298     DEFINE_KEYCODE(TV_CONTENTS_MENU),
299     DEFINE_KEYCODE(TV_MEDIA_CONTEXT_MENU),
300     DEFINE_KEYCODE(TV_TIMER_PROGRAMMING),
301     DEFINE_KEYCODE(HELP),
302     DEFINE_KEYCODE(NAVIGATE_PREVIOUS),
303     DEFINE_KEYCODE(NAVIGATE_NEXT),
304     DEFINE_KEYCODE(NAVIGATE_IN),
305     DEFINE_KEYCODE(NAVIGATE_OUT),
306     DEFINE_KEYCODE(MEDIA_SKIP_FORWARD),
307     DEFINE_KEYCODE(MEDIA_SKIP_BACKWARD),
308     DEFINE_KEYCODE(MEDIA_STEP_FORWARD),
309     DEFINE_KEYCODE(MEDIA_STEP_BACKWARD),
310 
311     { NULL, 0 }
312 };
313 
314 static const InputEventLabel AXES[] = {
315     DEFINE_AXIS(X),
316     DEFINE_AXIS(Y),
317     DEFINE_AXIS(PRESSURE),
318     DEFINE_AXIS(SIZE),
319     DEFINE_AXIS(TOUCH_MAJOR),
320     DEFINE_AXIS(TOUCH_MINOR),
321     DEFINE_AXIS(TOOL_MAJOR),
322     DEFINE_AXIS(TOOL_MINOR),
323     DEFINE_AXIS(ORIENTATION),
324     DEFINE_AXIS(VSCROLL),
325     DEFINE_AXIS(HSCROLL),
326     DEFINE_AXIS(Z),
327     DEFINE_AXIS(RX),
328     DEFINE_AXIS(RY),
329     DEFINE_AXIS(RZ),
330     DEFINE_AXIS(HAT_X),
331     DEFINE_AXIS(HAT_Y),
332     DEFINE_AXIS(LTRIGGER),
333     DEFINE_AXIS(RTRIGGER),
334     DEFINE_AXIS(THROTTLE),
335     DEFINE_AXIS(RUDDER),
336     DEFINE_AXIS(WHEEL),
337     DEFINE_AXIS(GAS),
338     DEFINE_AXIS(BRAKE),
339     DEFINE_AXIS(DISTANCE),
340     DEFINE_AXIS(TILT),
341     DEFINE_AXIS(GENERIC_1),
342     DEFINE_AXIS(GENERIC_2),
343     DEFINE_AXIS(GENERIC_3),
344     DEFINE_AXIS(GENERIC_4),
345     DEFINE_AXIS(GENERIC_5),
346     DEFINE_AXIS(GENERIC_6),
347     DEFINE_AXIS(GENERIC_7),
348     DEFINE_AXIS(GENERIC_8),
349     DEFINE_AXIS(GENERIC_9),
350     DEFINE_AXIS(GENERIC_10),
351     DEFINE_AXIS(GENERIC_11),
352     DEFINE_AXIS(GENERIC_12),
353     DEFINE_AXIS(GENERIC_13),
354     DEFINE_AXIS(GENERIC_14),
355     DEFINE_AXIS(GENERIC_15),
356     DEFINE_AXIS(GENERIC_16),
357 
358     // NOTE: If you add a new axis here you must also add it to several other files.
359     //       Refer to frameworks/base/core/java/android/view/MotionEvent.java for the full list.
360     { NULL, 0 }
361 };
362 
363 static const InputEventLabel LEDS[] = {
364     DEFINE_LED(NUM_LOCK),
365     DEFINE_LED(CAPS_LOCK),
366     DEFINE_LED(SCROLL_LOCK),
367     DEFINE_LED(COMPOSE),
368     DEFINE_LED(KANA),
369     DEFINE_LED(SLEEP),
370     DEFINE_LED(SUSPEND),
371     DEFINE_LED(MUTE),
372     DEFINE_LED(MISC),
373     DEFINE_LED(MAIL),
374     DEFINE_LED(CHARGING),
375     DEFINE_LED(CONTROLLER_1),
376     DEFINE_LED(CONTROLLER_2),
377     DEFINE_LED(CONTROLLER_3),
378     DEFINE_LED(CONTROLLER_4),
379 
380     // NOTE: If you add new LEDs here, you must also add them to Input.h
381     { NULL, 0 }
382 };
383 
384 static const InputEventLabel FLAGS[] = {
385     DEFINE_FLAG(VIRTUAL),
386     DEFINE_FLAG(FUNCTION),
387     DEFINE_FLAG(GESTURE),
388 
389     { NULL, 0 }
390 };
391 
lookupValueByLabel(const char * literal,const InputEventLabel * list)392 static int lookupValueByLabel(const char* literal, const InputEventLabel *list) {
393     while (list->literal) {
394         if (strcmp(literal, list->literal) == 0) {
395             return list->value;
396         }
397         list++;
398     }
399     return list->value;
400 }
401 
lookupLabelByValue(int value,const InputEventLabel * list)402 static const char* lookupLabelByValue(int value, const InputEventLabel* list) {
403     while (list->literal) {
404         if (list->value == value) {
405             return list->literal;
406         }
407         list++;
408     }
409     return NULL;
410 }
411 
getKeyCodeByLabel(const char * label)412 static int32_t getKeyCodeByLabel(const char* label) {
413     return int32_t(lookupValueByLabel(label, KEYCODES));
414 }
415 
getLabelByKeyCode(int32_t keyCode)416 static const char* getLabelByKeyCode(int32_t keyCode) {
417     if (keyCode >= 0 && keyCode < size(KEYCODES)) {
418         return KEYCODES[keyCode].literal;
419     }
420     return NULL;
421 }
422 
getKeyFlagByLabel(const char * label)423 static uint32_t getKeyFlagByLabel(const char* label) {
424     return uint32_t(lookupValueByLabel(label, FLAGS));
425 }
426 
getAxisByLabel(const char * label)427 static int32_t getAxisByLabel(const char* label) {
428     return int32_t(lookupValueByLabel(label, AXES));
429 }
430 
getAxisLabel(int32_t axisId)431 static const char* getAxisLabel(int32_t axisId) {
432     return lookupLabelByValue(axisId, AXES);
433 }
434 
getLedByLabel(const char * label)435 static int32_t getLedByLabel(const char* label) {
436     return int32_t(lookupValueByLabel(label, LEDS));
437 }
438 
439 
440 } // namespace android
441 #endif // _LIBINPUT_INPUT_EVENT_LABELS_H
442