blob: 44247c1249a56d30f26144436fe517987f108987 [file] [log] [blame]
Michael Wright872db4f2014-04-22 15:03:51 -07001/*
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
Prabir Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Michael Wright872db4f2014-04-22 15:03:51 -070018
19#include <input/Input.h>
20#include <android/keycodes.h>
Chris Yefff9edb2020-08-13 16:12:54 -070021#include <unordered_map>
Michael Wright872db4f2014-04-22 15:03:51 -070022
Michael Wright872db4f2014-04-22 15:03:51 -070023namespace android {
24
25template<typename T, size_t N>
26size_t size(T (&)[N]) { return N; }
27
28struct InputEventLabel {
29 const char *literal;
30 int value;
31};
32
Prabir Pradhan1e63fc22023-02-23 19:03:03 +000033struct EvdevEventLabel {
34 std::string type;
35 std::string code;
36 std::string value;
37};
38
Chris Ye4958d062020-08-20 13:21:10 -070039// 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.
Michael Wright872db4f2014-04-22 15:03:51 -070041
Chris Ye4958d062020-08-20 13:21:10 -070042class InputEventLookup {
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070043 /**
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 */
Chris Ye4958d062020-08-20 13:21:10 -070048public:
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070049 InputEventLookup(InputEventLookup& other) = delete;
50
51 void operator=(const InputEventLookup&) = delete;
52
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080053 static std::optional<int> lookupValueByLabel(const std::unordered_map<std::string, int>& map,
54 const char* literal);
Chris Ye4958d062020-08-20 13:21:10 -070055
56 static const char* lookupLabelByValue(const std::vector<InputEventLabel>& vec, int value);
57
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080058 static std::optional<int> getKeyCodeByLabel(const char* label);
Chris Ye4958d062020-08-20 13:21:10 -070059
60 static const char* getLabelByKeyCode(int32_t keyCode);
61
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080062 static std::optional<int> getKeyFlagByLabel(const char* label);
Chris Ye4958d062020-08-20 13:21:10 -070063
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080064 static std::optional<int> getAxisByLabel(const char* label);
Chris Ye4958d062020-08-20 13:21:10 -070065
66 static const char* getAxisLabel(int32_t axisId);
67
Siarhei Vishniakou5df34932023-01-23 12:41:01 -080068 static std::optional<int> getLedByLabel(const char* label);
Chris Ye4958d062020-08-20 13:21:10 -070069
Prabir Pradhan1e63fc22023-02-23 19:03:03 +000070 static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value);
71
Prabir Pradhan2f13e022023-07-21 18:31:00 +000072 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
Chris Ye4958d062020-08-20 13:21:10 -070078private:
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070079 InputEventLookup();
Chris Ye4958d062020-08-20 13:21:10 -070080
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070081 static const InputEventLookup& get() {
82 static InputEventLookup sLookup;
83 return sLookup;
84 }
Chris Ye4958d062020-08-20 13:21:10 -070085
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070086 const std::unordered_map<std::string, int> KEYCODES;
Chris Ye4958d062020-08-20 13:21:10 -070087
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070088 const std::vector<InputEventLabel> KEY_NAMES;
Chris Ye4958d062020-08-20 13:21:10 -070089
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070090 const std::unordered_map<std::string, int> AXES;
Chris Ye4958d062020-08-20 13:21:10 -070091
Siarhei Vishniakouffe62932023-06-15 19:46:30 -070092 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;
Michael Wright872db4f2014-04-22 15:03:51 -070097};
98
Michael Wright872db4f2014-04-22 15:03:51 -070099} // namespace android