1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2008 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android/keycodes.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
22*38e8c45fSAndroid Build Coastguard Worker
23*38e8c45fSAndroid Build Coastguard Worker namespace android {
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker template<typename T, size_t N>
size(T (&)[N])26*38e8c45fSAndroid Build Coastguard Worker size_t size(T (&)[N]) { return N; }
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker struct InputEventLabel {
29*38e8c45fSAndroid Build Coastguard Worker const char *literal;
30*38e8c45fSAndroid Build Coastguard Worker int value;
31*38e8c45fSAndroid Build Coastguard Worker };
32*38e8c45fSAndroid Build Coastguard Worker
33*38e8c45fSAndroid Build Coastguard Worker struct EvdevEventLabel {
34*38e8c45fSAndroid Build Coastguard Worker std::string type;
35*38e8c45fSAndroid Build Coastguard Worker std::string code;
36*38e8c45fSAndroid Build Coastguard Worker std::string value;
37*38e8c45fSAndroid Build Coastguard Worker };
38*38e8c45fSAndroid Build Coastguard Worker
39*38e8c45fSAndroid Build Coastguard Worker // NOTE: If you want a new key code, axis code, led code or flag code in keylayout file,
40*38e8c45fSAndroid Build Coastguard Worker // then you must add it to InputEventLabels.cpp.
41*38e8c45fSAndroid Build Coastguard Worker
42*38e8c45fSAndroid Build Coastguard Worker class InputEventLookup {
43*38e8c45fSAndroid Build Coastguard Worker /**
44*38e8c45fSAndroid Build Coastguard Worker * This class is not purely static, but uses a singleton pattern in order to delay the
45*38e8c45fSAndroid Build Coastguard Worker * initialization of the maps that it contains. If it were purely static, the maps could be
46*38e8c45fSAndroid Build Coastguard Worker * created early, and would cause sanitizers to report memory leaks.
47*38e8c45fSAndroid Build Coastguard Worker */
48*38e8c45fSAndroid Build Coastguard Worker public:
49*38e8c45fSAndroid Build Coastguard Worker InputEventLookup(InputEventLookup& other) = delete;
50*38e8c45fSAndroid Build Coastguard Worker
51*38e8c45fSAndroid Build Coastguard Worker void operator=(const InputEventLookup&) = delete;
52*38e8c45fSAndroid Build Coastguard Worker
53*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> lookupValueByLabel(const std::unordered_map<std::string, int>& map,
54*38e8c45fSAndroid Build Coastguard Worker const char* literal);
55*38e8c45fSAndroid Build Coastguard Worker
56*38e8c45fSAndroid Build Coastguard Worker static const char* lookupLabelByValue(const std::vector<InputEventLabel>& vec, int value);
57*38e8c45fSAndroid Build Coastguard Worker
58*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getKeyCodeByLabel(const char* label);
59*38e8c45fSAndroid Build Coastguard Worker
60*38e8c45fSAndroid Build Coastguard Worker static const char* getLabelByKeyCode(int32_t keyCode);
61*38e8c45fSAndroid Build Coastguard Worker
62*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getKeyFlagByLabel(const char* label);
63*38e8c45fSAndroid Build Coastguard Worker
64*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getAxisByLabel(const char* label);
65*38e8c45fSAndroid Build Coastguard Worker
66*38e8c45fSAndroid Build Coastguard Worker static const char* getAxisLabel(int32_t axisId);
67*38e8c45fSAndroid Build Coastguard Worker
68*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getLedByLabel(const char* label);
69*38e8c45fSAndroid Build Coastguard Worker
70*38e8c45fSAndroid Build Coastguard Worker static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value);
71*38e8c45fSAndroid Build Coastguard Worker
72*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getLinuxEvdevEventTypeByLabel(const char* label);
73*38e8c45fSAndroid Build Coastguard Worker
74*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getLinuxEvdevEventCodeByLabel(int32_t type, const char* label);
75*38e8c45fSAndroid Build Coastguard Worker
76*38e8c45fSAndroid Build Coastguard Worker static std::optional<int> getLinuxEvdevInputPropByLabel(const char* label);
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker private:
79*38e8c45fSAndroid Build Coastguard Worker InputEventLookup();
80*38e8c45fSAndroid Build Coastguard Worker
get()81*38e8c45fSAndroid Build Coastguard Worker static const InputEventLookup& get() {
82*38e8c45fSAndroid Build Coastguard Worker static InputEventLookup sLookup;
83*38e8c45fSAndroid Build Coastguard Worker return sLookup;
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker
86*38e8c45fSAndroid Build Coastguard Worker const std::unordered_map<std::string, int> KEYCODES;
87*38e8c45fSAndroid Build Coastguard Worker
88*38e8c45fSAndroid Build Coastguard Worker const std::vector<InputEventLabel> KEY_NAMES;
89*38e8c45fSAndroid Build Coastguard Worker
90*38e8c45fSAndroid Build Coastguard Worker const std::unordered_map<std::string, int> AXES;
91*38e8c45fSAndroid Build Coastguard Worker
92*38e8c45fSAndroid Build Coastguard Worker const std::vector<InputEventLabel> AXES_NAMES;
93*38e8c45fSAndroid Build Coastguard Worker
94*38e8c45fSAndroid Build Coastguard Worker const std::unordered_map<std::string, int> LEDS;
95*38e8c45fSAndroid Build Coastguard Worker
96*38e8c45fSAndroid Build Coastguard Worker const std::unordered_map<std::string, int> FLAGS;
97*38e8c45fSAndroid Build Coastguard Worker };
98*38e8c45fSAndroid Build Coastguard Worker
99*38e8c45fSAndroid Build Coastguard Worker } // namespace android
100