• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <input/Input.h>
20 #include <android/keycodes.h>
21 #include <unordered_map>
22 
23 namespace android {
24 
25 template<typename T, size_t N>
size(T (&)[N])26 size_t size(T (&)[N]) { return N; }
27 
28 struct InputEventLabel {
29     const char *literal;
30     int value;
31 };
32 
33 struct EvdevEventLabel {
34     std::string type;
35     std::string code;
36     std::string value;
37 };
38 
39 //   NOTE: If you want a new key code, axis code, led code or flag code in keylayout file,
40 //   then you must add it to InputEventLabels.cpp.
41 
42 class InputEventLookup {
43     /**
44      * This class is not purely static, but uses a singleton pattern in order to delay the
45      * initialization of the maps that it contains. If it were purely static, the maps could be
46      * created early, and would cause sanitizers to report memory leaks.
47      */
48 public:
49     InputEventLookup(InputEventLookup& other) = delete;
50 
51     void operator=(const InputEventLookup&) = delete;
52 
53     static std::optional<int> lookupValueByLabel(const std::unordered_map<std::string, int>& map,
54                                                  const char* literal);
55 
56     static const char* lookupLabelByValue(const std::vector<InputEventLabel>& vec, int value);
57 
58     static std::optional<int> getKeyCodeByLabel(const char* label);
59 
60     static const char* getLabelByKeyCode(int32_t keyCode);
61 
62     static std::optional<int> getKeyFlagByLabel(const char* label);
63 
64     static std::optional<int> getAxisByLabel(const char* label);
65 
66     static const char* getAxisLabel(int32_t axisId);
67 
68     static std::optional<int> getLedByLabel(const char* label);
69 
70     static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value);
71 
72     static std::optional<int> getLinuxEvdevEventTypeByLabel(const char* label);
73 
74     static std::optional<int> getLinuxEvdevEventCodeByLabel(int32_t type, const char* label);
75 
76     static std::optional<int> getLinuxEvdevInputPropByLabel(const char* label);
77 
78 private:
79     InputEventLookup();
80 
get()81     static const InputEventLookup& get() {
82         static InputEventLookup sLookup;
83         return sLookup;
84     }
85 
86     const std::unordered_map<std::string, int> KEYCODES;
87 
88     const std::vector<InputEventLabel> KEY_NAMES;
89 
90     const std::unordered_map<std::string, int> AXES;
91 
92     const std::vector<InputEventLabel> AXES_NAMES;
93 
94     const std::unordered_map<std::string, int> LEDS;
95 
96     const std::unordered_map<std::string, int> FLAGS;
97 };
98 
99 } // namespace android
100