• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include <input/InputEventLabels.h>
18 
19 #include <linux/input-event-codes.h>
20 #include <linux/input.h>
21 #include <strings.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 // clang-format off
31 
32 // NOTE: If you add a new keycode here you must also add it to several other files.
33 //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
34 #define KEYCODES_SEQUENCE \
35     DEFINE_KEYCODE(UNKNOWN), \
36     DEFINE_KEYCODE(SOFT_LEFT), \
37     DEFINE_KEYCODE(SOFT_RIGHT), \
38     DEFINE_KEYCODE(HOME), \
39     DEFINE_KEYCODE(BACK), \
40     DEFINE_KEYCODE(CALL), \
41     DEFINE_KEYCODE(ENDCALL), \
42     DEFINE_KEYCODE(0), \
43     DEFINE_KEYCODE(1), \
44     DEFINE_KEYCODE(2), \
45     DEFINE_KEYCODE(3), \
46     DEFINE_KEYCODE(4), \
47     DEFINE_KEYCODE(5), \
48     DEFINE_KEYCODE(6), \
49     DEFINE_KEYCODE(7), \
50     DEFINE_KEYCODE(8), \
51     DEFINE_KEYCODE(9), \
52     DEFINE_KEYCODE(STAR), \
53     DEFINE_KEYCODE(POUND), \
54     DEFINE_KEYCODE(DPAD_UP), \
55     DEFINE_KEYCODE(DPAD_DOWN), \
56     DEFINE_KEYCODE(DPAD_LEFT), \
57     DEFINE_KEYCODE(DPAD_RIGHT), \
58     DEFINE_KEYCODE(DPAD_CENTER), \
59     DEFINE_KEYCODE(VOLUME_UP), \
60     DEFINE_KEYCODE(VOLUME_DOWN), \
61     DEFINE_KEYCODE(POWER), \
62     DEFINE_KEYCODE(CAMERA), \
63     DEFINE_KEYCODE(CLEAR), \
64     DEFINE_KEYCODE(A), \
65     DEFINE_KEYCODE(B), \
66     DEFINE_KEYCODE(C), \
67     DEFINE_KEYCODE(D), \
68     DEFINE_KEYCODE(E), \
69     DEFINE_KEYCODE(F), \
70     DEFINE_KEYCODE(G), \
71     DEFINE_KEYCODE(H), \
72     DEFINE_KEYCODE(I), \
73     DEFINE_KEYCODE(J), \
74     DEFINE_KEYCODE(K), \
75     DEFINE_KEYCODE(L), \
76     DEFINE_KEYCODE(M), \
77     DEFINE_KEYCODE(N), \
78     DEFINE_KEYCODE(O), \
79     DEFINE_KEYCODE(P), \
80     DEFINE_KEYCODE(Q), \
81     DEFINE_KEYCODE(R), \
82     DEFINE_KEYCODE(S), \
83     DEFINE_KEYCODE(T), \
84     DEFINE_KEYCODE(U), \
85     DEFINE_KEYCODE(V), \
86     DEFINE_KEYCODE(W), \
87     DEFINE_KEYCODE(X), \
88     DEFINE_KEYCODE(Y), \
89     DEFINE_KEYCODE(Z), \
90     DEFINE_KEYCODE(COMMA), \
91     DEFINE_KEYCODE(PERIOD), \
92     DEFINE_KEYCODE(ALT_LEFT), \
93     DEFINE_KEYCODE(ALT_RIGHT), \
94     DEFINE_KEYCODE(SHIFT_LEFT), \
95     DEFINE_KEYCODE(SHIFT_RIGHT), \
96     DEFINE_KEYCODE(TAB), \
97     DEFINE_KEYCODE(SPACE), \
98     DEFINE_KEYCODE(SYM), \
99     DEFINE_KEYCODE(EXPLORER), \
100     DEFINE_KEYCODE(ENVELOPE), \
101     DEFINE_KEYCODE(ENTER), \
102     DEFINE_KEYCODE(DEL), \
103     DEFINE_KEYCODE(GRAVE), \
104     DEFINE_KEYCODE(MINUS), \
105     DEFINE_KEYCODE(EQUALS), \
106     DEFINE_KEYCODE(LEFT_BRACKET), \
107     DEFINE_KEYCODE(RIGHT_BRACKET), \
108     DEFINE_KEYCODE(BACKSLASH), \
109     DEFINE_KEYCODE(SEMICOLON), \
110     DEFINE_KEYCODE(APOSTROPHE), \
111     DEFINE_KEYCODE(SLASH), \
112     DEFINE_KEYCODE(AT), \
113     DEFINE_KEYCODE(NUM), \
114     DEFINE_KEYCODE(HEADSETHOOK), \
115     DEFINE_KEYCODE(FOCUS), \
116     DEFINE_KEYCODE(PLUS), \
117     DEFINE_KEYCODE(MENU), \
118     DEFINE_KEYCODE(NOTIFICATION), \
119     DEFINE_KEYCODE(SEARCH), \
120     DEFINE_KEYCODE(MEDIA_PLAY_PAUSE), \
121     DEFINE_KEYCODE(MEDIA_STOP), \
122     DEFINE_KEYCODE(MEDIA_NEXT), \
123     DEFINE_KEYCODE(MEDIA_PREVIOUS), \
124     DEFINE_KEYCODE(MEDIA_REWIND), \
125     DEFINE_KEYCODE(MEDIA_FAST_FORWARD), \
126     DEFINE_KEYCODE(MUTE), \
127     DEFINE_KEYCODE(PAGE_UP), \
128     DEFINE_KEYCODE(PAGE_DOWN), \
129     DEFINE_KEYCODE(PICTSYMBOLS), \
130     DEFINE_KEYCODE(SWITCH_CHARSET), \
131     DEFINE_KEYCODE(BUTTON_A), \
132     DEFINE_KEYCODE(BUTTON_B), \
133     DEFINE_KEYCODE(BUTTON_C), \
134     DEFINE_KEYCODE(BUTTON_X), \
135     DEFINE_KEYCODE(BUTTON_Y), \
136     DEFINE_KEYCODE(BUTTON_Z), \
137     DEFINE_KEYCODE(BUTTON_L1), \
138     DEFINE_KEYCODE(BUTTON_R1), \
139     DEFINE_KEYCODE(BUTTON_L2), \
140     DEFINE_KEYCODE(BUTTON_R2), \
141     DEFINE_KEYCODE(BUTTON_THUMBL), \
142     DEFINE_KEYCODE(BUTTON_THUMBR), \
143     DEFINE_KEYCODE(BUTTON_START), \
144     DEFINE_KEYCODE(BUTTON_SELECT), \
145     DEFINE_KEYCODE(BUTTON_MODE), \
146     DEFINE_KEYCODE(ESCAPE), \
147     DEFINE_KEYCODE(FORWARD_DEL), \
148     DEFINE_KEYCODE(CTRL_LEFT), \
149     DEFINE_KEYCODE(CTRL_RIGHT), \
150     DEFINE_KEYCODE(CAPS_LOCK), \
151     DEFINE_KEYCODE(SCROLL_LOCK), \
152     DEFINE_KEYCODE(META_LEFT), \
153     DEFINE_KEYCODE(META_RIGHT), \
154     DEFINE_KEYCODE(FUNCTION), \
155     DEFINE_KEYCODE(SYSRQ), \
156     DEFINE_KEYCODE(BREAK), \
157     DEFINE_KEYCODE(MOVE_HOME), \
158     DEFINE_KEYCODE(MOVE_END), \
159     DEFINE_KEYCODE(INSERT), \
160     DEFINE_KEYCODE(FORWARD), \
161     DEFINE_KEYCODE(MEDIA_PLAY), \
162     DEFINE_KEYCODE(MEDIA_PAUSE), \
163     DEFINE_KEYCODE(MEDIA_CLOSE), \
164     DEFINE_KEYCODE(MEDIA_EJECT), \
165     DEFINE_KEYCODE(MEDIA_RECORD), \
166     DEFINE_KEYCODE(F1), \
167     DEFINE_KEYCODE(F2), \
168     DEFINE_KEYCODE(F3), \
169     DEFINE_KEYCODE(F4), \
170     DEFINE_KEYCODE(F5), \
171     DEFINE_KEYCODE(F6), \
172     DEFINE_KEYCODE(F7), \
173     DEFINE_KEYCODE(F8), \
174     DEFINE_KEYCODE(F9), \
175     DEFINE_KEYCODE(F10), \
176     DEFINE_KEYCODE(F11), \
177     DEFINE_KEYCODE(F12), \
178     DEFINE_KEYCODE(NUM_LOCK), \
179     DEFINE_KEYCODE(NUMPAD_0), \
180     DEFINE_KEYCODE(NUMPAD_1), \
181     DEFINE_KEYCODE(NUMPAD_2), \
182     DEFINE_KEYCODE(NUMPAD_3), \
183     DEFINE_KEYCODE(NUMPAD_4), \
184     DEFINE_KEYCODE(NUMPAD_5), \
185     DEFINE_KEYCODE(NUMPAD_6), \
186     DEFINE_KEYCODE(NUMPAD_7), \
187     DEFINE_KEYCODE(NUMPAD_8), \
188     DEFINE_KEYCODE(NUMPAD_9), \
189     DEFINE_KEYCODE(NUMPAD_DIVIDE), \
190     DEFINE_KEYCODE(NUMPAD_MULTIPLY), \
191     DEFINE_KEYCODE(NUMPAD_SUBTRACT), \
192     DEFINE_KEYCODE(NUMPAD_ADD), \
193     DEFINE_KEYCODE(NUMPAD_DOT), \
194     DEFINE_KEYCODE(NUMPAD_COMMA), \
195     DEFINE_KEYCODE(NUMPAD_ENTER), \
196     DEFINE_KEYCODE(NUMPAD_EQUALS), \
197     DEFINE_KEYCODE(NUMPAD_LEFT_PAREN), \
198     DEFINE_KEYCODE(NUMPAD_RIGHT_PAREN), \
199     DEFINE_KEYCODE(VOLUME_MUTE), \
200     DEFINE_KEYCODE(INFO), \
201     DEFINE_KEYCODE(CHANNEL_UP), \
202     DEFINE_KEYCODE(CHANNEL_DOWN), \
203     DEFINE_KEYCODE(ZOOM_IN), \
204     DEFINE_KEYCODE(ZOOM_OUT), \
205     DEFINE_KEYCODE(TV), \
206     DEFINE_KEYCODE(WINDOW), \
207     DEFINE_KEYCODE(GUIDE), \
208     DEFINE_KEYCODE(DVR), \
209     DEFINE_KEYCODE(BOOKMARK), \
210     DEFINE_KEYCODE(CAPTIONS), \
211     DEFINE_KEYCODE(SETTINGS), \
212     DEFINE_KEYCODE(TV_POWER), \
213     DEFINE_KEYCODE(TV_INPUT), \
214     DEFINE_KEYCODE(STB_POWER), \
215     DEFINE_KEYCODE(STB_INPUT), \
216     DEFINE_KEYCODE(AVR_POWER), \
217     DEFINE_KEYCODE(AVR_INPUT), \
218     DEFINE_KEYCODE(PROG_RED), \
219     DEFINE_KEYCODE(PROG_GREEN), \
220     DEFINE_KEYCODE(PROG_YELLOW), \
221     DEFINE_KEYCODE(PROG_BLUE), \
222     DEFINE_KEYCODE(APP_SWITCH), \
223     DEFINE_KEYCODE(BUTTON_1), \
224     DEFINE_KEYCODE(BUTTON_2), \
225     DEFINE_KEYCODE(BUTTON_3), \
226     DEFINE_KEYCODE(BUTTON_4), \
227     DEFINE_KEYCODE(BUTTON_5), \
228     DEFINE_KEYCODE(BUTTON_6), \
229     DEFINE_KEYCODE(BUTTON_7), \
230     DEFINE_KEYCODE(BUTTON_8), \
231     DEFINE_KEYCODE(BUTTON_9), \
232     DEFINE_KEYCODE(BUTTON_10), \
233     DEFINE_KEYCODE(BUTTON_11), \
234     DEFINE_KEYCODE(BUTTON_12), \
235     DEFINE_KEYCODE(BUTTON_13), \
236     DEFINE_KEYCODE(BUTTON_14), \
237     DEFINE_KEYCODE(BUTTON_15), \
238     DEFINE_KEYCODE(BUTTON_16), \
239     DEFINE_KEYCODE(LANGUAGE_SWITCH), \
240     DEFINE_KEYCODE(MANNER_MODE), \
241     DEFINE_KEYCODE(3D_MODE), \
242     DEFINE_KEYCODE(CONTACTS), \
243     DEFINE_KEYCODE(CALENDAR), \
244     DEFINE_KEYCODE(MUSIC), \
245     DEFINE_KEYCODE(CALCULATOR), \
246     DEFINE_KEYCODE(ZENKAKU_HANKAKU), \
247     DEFINE_KEYCODE(EISU), \
248     DEFINE_KEYCODE(MUHENKAN), \
249     DEFINE_KEYCODE(HENKAN), \
250     DEFINE_KEYCODE(KATAKANA_HIRAGANA), \
251     DEFINE_KEYCODE(YEN), \
252     DEFINE_KEYCODE(RO), \
253     DEFINE_KEYCODE(KANA), \
254     DEFINE_KEYCODE(ASSIST), \
255     DEFINE_KEYCODE(BRIGHTNESS_DOWN), \
256     DEFINE_KEYCODE(BRIGHTNESS_UP), \
257     DEFINE_KEYCODE(MEDIA_AUDIO_TRACK), \
258     DEFINE_KEYCODE(SLEEP), \
259     DEFINE_KEYCODE(WAKEUP), \
260     DEFINE_KEYCODE(PAIRING), \
261     DEFINE_KEYCODE(MEDIA_TOP_MENU), \
262     DEFINE_KEYCODE(11), \
263     DEFINE_KEYCODE(12), \
264     DEFINE_KEYCODE(LAST_CHANNEL), \
265     DEFINE_KEYCODE(TV_DATA_SERVICE), \
266     DEFINE_KEYCODE(VOICE_ASSIST), \
267     DEFINE_KEYCODE(TV_RADIO_SERVICE), \
268     DEFINE_KEYCODE(TV_TELETEXT), \
269     DEFINE_KEYCODE(TV_NUMBER_ENTRY), \
270     DEFINE_KEYCODE(TV_TERRESTRIAL_ANALOG), \
271     DEFINE_KEYCODE(TV_TERRESTRIAL_DIGITAL), \
272     DEFINE_KEYCODE(TV_SATELLITE), \
273     DEFINE_KEYCODE(TV_SATELLITE_BS), \
274     DEFINE_KEYCODE(TV_SATELLITE_CS), \
275     DEFINE_KEYCODE(TV_SATELLITE_SERVICE), \
276     DEFINE_KEYCODE(TV_NETWORK), \
277     DEFINE_KEYCODE(TV_ANTENNA_CABLE), \
278     DEFINE_KEYCODE(TV_INPUT_HDMI_1), \
279     DEFINE_KEYCODE(TV_INPUT_HDMI_2), \
280     DEFINE_KEYCODE(TV_INPUT_HDMI_3), \
281     DEFINE_KEYCODE(TV_INPUT_HDMI_4), \
282     DEFINE_KEYCODE(TV_INPUT_COMPOSITE_1), \
283     DEFINE_KEYCODE(TV_INPUT_COMPOSITE_2), \
284     DEFINE_KEYCODE(TV_INPUT_COMPONENT_1), \
285     DEFINE_KEYCODE(TV_INPUT_COMPONENT_2), \
286     DEFINE_KEYCODE(TV_INPUT_VGA_1), \
287     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION), \
288     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION_MIX_UP), \
289     DEFINE_KEYCODE(TV_AUDIO_DESCRIPTION_MIX_DOWN), \
290     DEFINE_KEYCODE(TV_ZOOM_MODE), \
291     DEFINE_KEYCODE(TV_CONTENTS_MENU), \
292     DEFINE_KEYCODE(TV_MEDIA_CONTEXT_MENU), \
293     DEFINE_KEYCODE(TV_TIMER_PROGRAMMING), \
294     DEFINE_KEYCODE(HELP), \
295     DEFINE_KEYCODE(NAVIGATE_PREVIOUS), \
296     DEFINE_KEYCODE(NAVIGATE_NEXT), \
297     DEFINE_KEYCODE(NAVIGATE_IN), \
298     DEFINE_KEYCODE(NAVIGATE_OUT), \
299     DEFINE_KEYCODE(STEM_PRIMARY), \
300     DEFINE_KEYCODE(STEM_1), \
301     DEFINE_KEYCODE(STEM_2), \
302     DEFINE_KEYCODE(STEM_3), \
303     DEFINE_KEYCODE(DPAD_UP_LEFT), \
304     DEFINE_KEYCODE(DPAD_DOWN_LEFT), \
305     DEFINE_KEYCODE(DPAD_UP_RIGHT), \
306     DEFINE_KEYCODE(DPAD_DOWN_RIGHT), \
307     DEFINE_KEYCODE(MEDIA_SKIP_FORWARD), \
308     DEFINE_KEYCODE(MEDIA_SKIP_BACKWARD), \
309     DEFINE_KEYCODE(MEDIA_STEP_FORWARD), \
310     DEFINE_KEYCODE(MEDIA_STEP_BACKWARD), \
311     DEFINE_KEYCODE(SOFT_SLEEP), \
312     DEFINE_KEYCODE(CUT), \
313     DEFINE_KEYCODE(COPY), \
314     DEFINE_KEYCODE(PASTE), \
315     DEFINE_KEYCODE(SYSTEM_NAVIGATION_UP), \
316     DEFINE_KEYCODE(SYSTEM_NAVIGATION_DOWN), \
317     DEFINE_KEYCODE(SYSTEM_NAVIGATION_LEFT), \
318     DEFINE_KEYCODE(SYSTEM_NAVIGATION_RIGHT), \
319     DEFINE_KEYCODE(ALL_APPS), \
320     DEFINE_KEYCODE(REFRESH), \
321     DEFINE_KEYCODE(THUMBS_UP), \
322     DEFINE_KEYCODE(THUMBS_DOWN), \
323     DEFINE_KEYCODE(PROFILE_SWITCH), \
324     DEFINE_KEYCODE(VIDEO_APP_1), \
325     DEFINE_KEYCODE(VIDEO_APP_2), \
326     DEFINE_KEYCODE(VIDEO_APP_3), \
327     DEFINE_KEYCODE(VIDEO_APP_4), \
328     DEFINE_KEYCODE(VIDEO_APP_5), \
329     DEFINE_KEYCODE(VIDEO_APP_6), \
330     DEFINE_KEYCODE(VIDEO_APP_7), \
331     DEFINE_KEYCODE(VIDEO_APP_8), \
332     DEFINE_KEYCODE(FEATURED_APP_1), \
333     DEFINE_KEYCODE(FEATURED_APP_2), \
334     DEFINE_KEYCODE(FEATURED_APP_3), \
335     DEFINE_KEYCODE(FEATURED_APP_4), \
336     DEFINE_KEYCODE(DEMO_APP_1), \
337     DEFINE_KEYCODE(DEMO_APP_2), \
338     DEFINE_KEYCODE(DEMO_APP_3), \
339     DEFINE_KEYCODE(DEMO_APP_4), \
340     DEFINE_KEYCODE(KEYBOARD_BACKLIGHT_DOWN), \
341     DEFINE_KEYCODE(KEYBOARD_BACKLIGHT_UP), \
342     DEFINE_KEYCODE(KEYBOARD_BACKLIGHT_TOGGLE), \
343     DEFINE_KEYCODE(STYLUS_BUTTON_PRIMARY), \
344     DEFINE_KEYCODE(STYLUS_BUTTON_SECONDARY), \
345     DEFINE_KEYCODE(STYLUS_BUTTON_TERTIARY), \
346     DEFINE_KEYCODE(STYLUS_BUTTON_TAIL), \
347     DEFINE_KEYCODE(RECENT_APPS), \
348     DEFINE_KEYCODE(MACRO_1), \
349     DEFINE_KEYCODE(MACRO_2), \
350     DEFINE_KEYCODE(MACRO_3), \
351     DEFINE_KEYCODE(MACRO_4), \
352     DEFINE_KEYCODE(EMOJI_PICKER), \
353     DEFINE_KEYCODE(SCREENSHOT)
354 
355 // NOTE: If you add a new axis here you must also add it to several other files.
356 //       Refer to frameworks/base/core/java/android/view/MotionEvent.java for the full list.
357 #define AXES_SEQUENCE \
358     DEFINE_AXIS(X), \
359     DEFINE_AXIS(Y), \
360     DEFINE_AXIS(PRESSURE), \
361     DEFINE_AXIS(SIZE), \
362     DEFINE_AXIS(TOUCH_MAJOR), \
363     DEFINE_AXIS(TOUCH_MINOR), \
364     DEFINE_AXIS(TOOL_MAJOR), \
365     DEFINE_AXIS(TOOL_MINOR), \
366     DEFINE_AXIS(ORIENTATION), \
367     DEFINE_AXIS(VSCROLL), \
368     DEFINE_AXIS(HSCROLL), \
369     DEFINE_AXIS(Z), \
370     DEFINE_AXIS(RX), \
371     DEFINE_AXIS(RY), \
372     DEFINE_AXIS(RZ), \
373     DEFINE_AXIS(HAT_X), \
374     DEFINE_AXIS(HAT_Y), \
375     DEFINE_AXIS(LTRIGGER), \
376     DEFINE_AXIS(RTRIGGER), \
377     DEFINE_AXIS(THROTTLE), \
378     DEFINE_AXIS(RUDDER), \
379     DEFINE_AXIS(WHEEL), \
380     DEFINE_AXIS(GAS), \
381     DEFINE_AXIS(BRAKE), \
382     DEFINE_AXIS(DISTANCE), \
383     DEFINE_AXIS(TILT), \
384     DEFINE_AXIS(SCROLL), \
385     DEFINE_AXIS(RELATIVE_X), \
386     DEFINE_AXIS(RELATIVE_Y), \
387     {"RESERVED_29", 29}, \
388     {"RESERVED_30", 30}, \
389     {"RESERVED_31", 31}, \
390     DEFINE_AXIS(GENERIC_1), \
391     DEFINE_AXIS(GENERIC_2), \
392     DEFINE_AXIS(GENERIC_3), \
393     DEFINE_AXIS(GENERIC_4), \
394     DEFINE_AXIS(GENERIC_5), \
395     DEFINE_AXIS(GENERIC_6), \
396     DEFINE_AXIS(GENERIC_7), \
397     DEFINE_AXIS(GENERIC_8), \
398     DEFINE_AXIS(GENERIC_9), \
399     DEFINE_AXIS(GENERIC_10), \
400     DEFINE_AXIS(GENERIC_11), \
401     DEFINE_AXIS(GENERIC_12), \
402     DEFINE_AXIS(GENERIC_13), \
403     DEFINE_AXIS(GENERIC_14), \
404     DEFINE_AXIS(GENERIC_15), \
405     DEFINE_AXIS(GENERIC_16), \
406     DEFINE_AXIS(GESTURE_X_OFFSET), \
407     DEFINE_AXIS(GESTURE_Y_OFFSET), \
408     DEFINE_AXIS(GESTURE_SCROLL_X_DISTANCE), \
409     DEFINE_AXIS(GESTURE_SCROLL_Y_DISTANCE), \
410     DEFINE_AXIS(GESTURE_PINCH_SCALE_FACTOR), \
411     DEFINE_AXIS(GESTURE_SWIPE_FINGER_COUNT)
412 
413 // NOTE: If you add new LEDs here, you must also add them to Input.h
414 #define LEDS_SEQUENCE \
415     DEFINE_LED(NUM_LOCK), \
416     DEFINE_LED(CAPS_LOCK), \
417     DEFINE_LED(SCROLL_LOCK), \
418     DEFINE_LED(COMPOSE), \
419     DEFINE_LED(KANA), \
420     DEFINE_LED(SLEEP), \
421     DEFINE_LED(SUSPEND), \
422     DEFINE_LED(MUTE), \
423     DEFINE_LED(MISC), \
424     DEFINE_LED(MAIL), \
425     DEFINE_LED(CHARGING), \
426     DEFINE_LED(CONTROLLER_1), \
427     DEFINE_LED(CONTROLLER_2), \
428     DEFINE_LED(CONTROLLER_3), \
429     DEFINE_LED(CONTROLLER_4)
430 
431 #define FLAGS_SEQUENCE \
432     DEFINE_FLAG(VIRTUAL), \
433     DEFINE_FLAG(FUNCTION), \
434     DEFINE_FLAG(GESTURE), \
435     DEFINE_FLAG(WAKE), \
436     DEFINE_FLAG(FALLBACK_USAGE_MAPPING)
437 
438 // clang-format on
439 
440 // --- InputEventLookup ---
441 
InputEventLookup()442 InputEventLookup::InputEventLookup()
443       : KEYCODES({KEYCODES_SEQUENCE}),
444         KEY_NAMES({KEYCODES_SEQUENCE}),
445         AXES({AXES_SEQUENCE}),
446         AXES_NAMES({AXES_SEQUENCE}),
447         LEDS({LEDS_SEQUENCE}),
448         FLAGS({FLAGS_SEQUENCE}) {}
449 
lookupValueByLabel(const std::unordered_map<std::string,int> & map,const char * literal)450 std::optional<int> InputEventLookup::lookupValueByLabel(
451         const std::unordered_map<std::string, int>& map, const char* literal) {
452     std::string str(literal);
453     auto it = map.find(str);
454     return it != map.end() ? std::make_optional(it->second) : std::nullopt;
455 }
456 
lookupLabelByValue(const std::vector<InputEventLabel> & vec,int value)457 const char* InputEventLookup::lookupLabelByValue(const std::vector<InputEventLabel>& vec,
458                                                  int value) {
459     if (static_cast<size_t>(value) < vec.size()) {
460         return vec[value].literal;
461     }
462     return nullptr;
463 }
464 
getKeyCodeByLabel(const char * label)465 std::optional<int> InputEventLookup::getKeyCodeByLabel(const char* label) {
466     const auto& self = get();
467     return self.lookupValueByLabel(self.KEYCODES, label);
468 }
469 
getLabelByKeyCode(int32_t keyCode)470 const char* InputEventLookup::getLabelByKeyCode(int32_t keyCode) {
471     const auto& self = get();
472     if (keyCode >= 0 && static_cast<size_t>(keyCode) < self.KEYCODES.size()) {
473         return get().lookupLabelByValue(self.KEY_NAMES, keyCode);
474     }
475     return nullptr;
476 }
477 
getKeyFlagByLabel(const char * label)478 std::optional<int> InputEventLookup::getKeyFlagByLabel(const char* label) {
479     const auto& self = get();
480     return lookupValueByLabel(self.FLAGS, label);
481 }
482 
getAxisByLabel(const char * label)483 std::optional<int> InputEventLookup::getAxisByLabel(const char* label) {
484     const auto& self = get();
485     return lookupValueByLabel(self.AXES, label);
486 }
487 
getAxisLabel(int32_t axisId)488 const char* InputEventLookup::getAxisLabel(int32_t axisId) {
489     const auto& self = get();
490     return lookupLabelByValue(self.AXES_NAMES, axisId);
491 }
492 
getLedByLabel(const char * label)493 std::optional<int> InputEventLookup::getLedByLabel(const char* label) {
494     const auto& self = get();
495     return lookupValueByLabel(self.LEDS, label);
496 }
497 
498 namespace {
499 
500 struct label {
501     const char* name;
502     int value;
503 };
504 
505 #define LABEL(constant) \
506     { #constant, constant }
507 #define LABEL_END \
508     { nullptr, -1 }
509 
510 static struct label ev_key_value_labels[] = {
511         {"UP", 0},
512         {"DOWN", 1},
513         {"REPEAT", 2},
514         LABEL_END,
515 };
516 
517 #include "input.h-labels.h"
518 
519 #undef LABEL
520 #undef LABEL_END
521 
getLabel(const label * labels,int value)522 std::string getLabel(const label* labels, int value) {
523     if (labels == nullptr) return std::to_string(value);
524     while (labels->name != nullptr && value != labels->value) {
525         labels++;
526     }
527     return labels->name != nullptr ? labels->name : std::to_string(value);
528 }
529 
getValue(const label * labels,const char * searchLabel)530 std::optional<int> getValue(const label* labels, const char* searchLabel) {
531     if (labels == nullptr) return {};
532     while (labels->name != nullptr && ::strcasecmp(labels->name, searchLabel) != 0) {
533         labels++;
534     }
535     return labels->name != nullptr ? std::make_optional(labels->value) : std::nullopt;
536 }
537 
getCodeLabelsForType(int32_t type)538 const label* getCodeLabelsForType(int32_t type) {
539     switch (type) {
540         case EV_SYN:
541             return syn_labels;
542         case EV_KEY:
543             return key_labels;
544         case EV_REL:
545             return rel_labels;
546         case EV_ABS:
547             return abs_labels;
548         case EV_SW:
549             return sw_labels;
550         case EV_MSC:
551             return msc_labels;
552         case EV_LED:
553             return led_labels;
554         case EV_REP:
555             return rep_labels;
556         case EV_SND:
557             return snd_labels;
558         case EV_FF:
559             return ff_labels;
560         case EV_FF_STATUS:
561             return ff_status_labels;
562         default:
563             return nullptr;
564     }
565 }
566 
getValueLabelsForTypeAndCode(int32_t type,int32_t code)567 const label* getValueLabelsForTypeAndCode(int32_t type, int32_t code) {
568     if (type == EV_KEY) {
569         return ev_key_value_labels;
570     }
571     if (type == EV_ABS && code == ABS_MT_TOOL_TYPE) {
572         return mt_tool_labels;
573     }
574     return nullptr;
575 }
576 
577 } // namespace
578 
getLinuxEvdevLabel(int32_t type,int32_t code,int32_t value)579 EvdevEventLabel InputEventLookup::getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value) {
580     return {
581             .type = getLabel(ev_labels, type),
582             .code = getLabel(getCodeLabelsForType(type), code),
583             .value = getLabel(getValueLabelsForTypeAndCode(type, code), value),
584     };
585 }
586 
getLinuxEvdevEventTypeByLabel(const char * label)587 std::optional<int> InputEventLookup::getLinuxEvdevEventTypeByLabel(const char* label) {
588     return getValue(ev_labels, label);
589 }
590 
getLinuxEvdevEventCodeByLabel(int32_t type,const char * label)591 std::optional<int> InputEventLookup::getLinuxEvdevEventCodeByLabel(int32_t type,
592                                                                    const char* label) {
593     return getValue(getCodeLabelsForType(type), label);
594 }
595 
getLinuxEvdevInputPropByLabel(const char * label)596 std::optional<int> InputEventLookup::getLinuxEvdevInputPropByLabel(const char* label) {
597     return getValue(input_prop_labels, label);
598 }
599 
600 } // namespace android
601