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 <android-base/result.h> 20 #include <input/InputDevice.h> 21 22 #include <stdint.h> 23 #include <utils/Errors.h> 24 #include <utils/Tokenizer.h> 25 #include <set> 26 27 namespace android { 28 29 struct AxisInfo { 30 enum Mode { 31 // Axis value is reported directly. 32 MODE_NORMAL = 0, 33 // Axis value should be inverted before reporting. 34 MODE_INVERT = 1, 35 // Axis value should be split into two axes 36 MODE_SPLIT = 2, 37 }; 38 39 // Axis mode. 40 Mode mode; 41 42 // Axis id. 43 // When split, this is the axis used for values smaller than the split position. 44 int32_t axis; 45 46 // When split, this is the axis used for values after higher than the split position. 47 int32_t highAxis; 48 49 // The split value, or 0 if not split. 50 int32_t splitValue; 51 52 // The flat value, or -1 if none. 53 int32_t flatOverride; 54 AxisInfoAxisInfo55 AxisInfo() : mode(MODE_NORMAL), axis(-1), highAxis(-1), splitValue(0), flatOverride(-1) { 56 } 57 }; 58 59 /** 60 * Describes a mapping from keyboard scan codes and joystick axes to Android key codes and axes. 61 * 62 * This object is immutable after it has been loaded. 63 */ 64 class KeyLayoutMap { 65 public: 66 static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename, 67 const char* contents = nullptr); 68 static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename, 69 const char* contents); 70 71 status_t mapKey(int32_t scanCode, int32_t usageCode, 72 int32_t* outKeyCode, uint32_t* outFlags) const; 73 std::vector<int32_t> findScanCodesForKey(int32_t keyCode) const; 74 std::optional<int32_t> findScanCodeForLed(int32_t ledCode) const; 75 std::vector<int32_t> findUsageCodesForKey(int32_t keyCode) const; 76 std::optional<int32_t> findUsageCodeForLed(int32_t ledCode) const; 77 78 std::optional<AxisInfo> mapAxis(int32_t scanCode) const; 79 const std::string getLoadFileName() const; 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) const; 82 83 virtual ~KeyLayoutMap(); 84 85 private: 86 static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer); 87 88 struct Key { 89 int32_t keyCode; 90 uint32_t flags; 91 }; 92 93 struct Led { 94 int32_t ledCode; 95 }; 96 97 struct Sensor { 98 InputDeviceSensorType sensorType; 99 int32_t sensorDataIndex; 100 }; 101 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; 107 std::unordered_map<int32_t, Sensor> mSensorsByAbsCode; 108 std::set<std::string> mRequiredKernelConfigs; 109 std::string mLoadFileName; 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(); 127 status_t parseLed(); 128 status_t parseSensor(); 129 status_t parseRequiredKernelConfig(); 130 }; 131 }; 132 133 } // namespace android 134