| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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_KEY_LAYOUT_MAP_H | 
|  | 18 | #define _LIBINPUT_KEY_LAYOUT_MAP_H | 
|  | 19 |  | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 20 | #include <android-base/result.h> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | #include <stdint.h> | 
|  | 22 | #include <utils/Errors.h> | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 23 | #include <utils/Tokenizer.h> | 
| Siarhei Vishniakou | d945d3e | 2022-05-18 09:42:52 -0700 | [diff] [blame] | 24 | #include <set> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 26 | #include <input/InputDevice.h> | 
|  | 27 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | struct AxisInfo { | 
|  | 31 | enum Mode { | 
|  | 32 | // Axis value is reported directly. | 
|  | 33 | MODE_NORMAL = 0, | 
|  | 34 | // Axis value should be inverted before reporting. | 
|  | 35 | MODE_INVERT = 1, | 
|  | 36 | // Axis value should be split into two axes | 
|  | 37 | MODE_SPLIT = 2, | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | // Axis mode. | 
|  | 41 | Mode mode; | 
|  | 42 |  | 
|  | 43 | // Axis id. | 
|  | 44 | // When split, this is the axis used for values smaller than the split position. | 
|  | 45 | int32_t axis; | 
|  | 46 |  | 
|  | 47 | // When split, this is the axis used for values after higher than the split position. | 
|  | 48 | int32_t highAxis; | 
|  | 49 |  | 
|  | 50 | // The split value, or 0 if not split. | 
|  | 51 | int32_t splitValue; | 
|  | 52 |  | 
|  | 53 | // The flat value, or -1 if none. | 
|  | 54 | int32_t flatOverride; | 
|  | 55 |  | 
|  | 56 | AxisInfo() : mode(MODE_NORMAL), axis(-1), highAxis(-1), splitValue(0), flatOverride(-1) { | 
|  | 57 | } | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | /** | 
|  | 61 | * Describes a mapping from keyboard scan codes and joystick axes to Android key codes and axes. | 
|  | 62 | * | 
|  | 63 | * This object is immutable after it has been loaded. | 
|  | 64 | */ | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 65 | class KeyLayoutMap { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 66 | public: | 
| Siarhei Vishniakou | d945d3e | 2022-05-18 09:42:52 -0700 | [diff] [blame] | 67 | static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename, | 
|  | 68 | const char* contents = nullptr); | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 69 | static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename, | 
|  | 70 | const char* contents); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | status_t mapKey(int32_t scanCode, int32_t usageCode, | 
|  | 73 | int32_t* outKeyCode, uint32_t* outFlags) const; | 
| Siarhei Vishniakou | 53f0a5e | 2022-05-18 09:45:54 -0700 | [diff] [blame] | 74 | std::vector<int32_t> findScanCodesForKey(int32_t keyCode) const; | 
|  | 75 | std::optional<int32_t> findScanCodeForLed(int32_t ledCode) const; | 
|  | 76 | std::optional<int32_t> findUsageCodeForLed(int32_t ledCode) const; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 |  | 
| Siarhei Vishniakou | 53f0a5e | 2022-05-18 09:45:54 -0700 | [diff] [blame] | 78 | std::optional<AxisInfo> mapAxis(int32_t scanCode) const; | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 79 | const std::string getLoadFileName() const; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 80 | // Return pair of sensor type and sensor data index, for the input device abs code | 
|  | 81 | base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 82 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 83 | virtual ~KeyLayoutMap(); | 
|  | 84 |  | 
|  | 85 | private: | 
| Siarhei Vishniakou | ac7f2e7 | 2022-05-18 12:30:16 -0700 | [diff] [blame] | 86 | static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer); | 
|  | 87 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 88 | struct Key { | 
|  | 89 | int32_t keyCode; | 
|  | 90 | uint32_t flags; | 
|  | 91 | }; | 
|  | 92 |  | 
| Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 93 | struct Led { | 
|  | 94 | int32_t ledCode; | 
|  | 95 | }; | 
|  | 96 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 97 | struct Sensor { | 
|  | 98 | InputDeviceSensorType sensorType; | 
|  | 99 | int32_t sensorDataIndex; | 
|  | 100 | }; | 
| Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 101 |  | 
| Siarhei Vishniakou | 53f0a5e | 2022-05-18 09:45:54 -0700 | [diff] [blame] | 102 | std::unordered_map<int32_t, Key> mKeysByScanCode; | 
|  | 103 | std::unordered_map<int32_t, Key> mKeysByUsageCode; | 
|  | 104 | std::unordered_map<int32_t, AxisInfo> mAxes; | 
|  | 105 | std::unordered_map<int32_t, Led> mLedsByScanCode; | 
|  | 106 | std::unordered_map<int32_t, Led> mLedsByUsageCode; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 107 | std::unordered_map<int32_t, Sensor> mSensorsByAbsCode; | 
| Siarhei Vishniakou | d945d3e | 2022-05-18 09:42:52 -0700 | [diff] [blame] | 108 | std::set<std::string> mRequiredKernelConfigs; | 
| Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame] | 109 | std::string mLoadFileName; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | KeyLayoutMap(); | 
|  | 112 |  | 
|  | 113 | const Key* getKey(int32_t scanCode, int32_t usageCode) const; | 
|  | 114 |  | 
|  | 115 | class Parser { | 
|  | 116 | KeyLayoutMap* mMap; | 
|  | 117 | Tokenizer* mTokenizer; | 
|  | 118 |  | 
|  | 119 | public: | 
|  | 120 | Parser(KeyLayoutMap* map, Tokenizer* tokenizer); | 
|  | 121 | ~Parser(); | 
|  | 122 | status_t parse(); | 
|  | 123 |  | 
|  | 124 | private: | 
|  | 125 | status_t parseKey(); | 
|  | 126 | status_t parseAxis(); | 
| Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 127 | status_t parseLed(); | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 128 | status_t parseSensor(); | 
| Siarhei Vishniakou | d945d3e | 2022-05-18 09:42:52 -0700 | [diff] [blame] | 129 | status_t parseRequiredKernelConfig(); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 130 | }; | 
|  | 131 | }; | 
|  | 132 |  | 
|  | 133 | } // namespace android | 
|  | 134 |  | 
|  | 135 | #endif // _LIBINPUT_KEY_LAYOUT_MAP_H |