blob: b2bd535cbf71b6d417fc9856df771d78e4f11ac3 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -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
17#ifndef _LIBINPUT_KEY_LAYOUT_MAP_H
18#define _LIBINPUT_KEY_LAYOUT_MAP_H
19
Chris Ye1abffbd2020-08-18 12:50:12 -070020#include <android-base/result.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <stdint.h>
22#include <utils/Errors.h>
23#include <utils/KeyedVector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024#include <utils/RefBase.h>
Chris Ye1abffbd2020-08-18 12:50:12 -070025#include <utils/Tokenizer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <input/InputDevice.h>
28
Jeff Brown5912f952013-07-01 19:10:31 -070029namespace android {
30
31struct AxisInfo {
32 enum Mode {
33 // Axis value is reported directly.
34 MODE_NORMAL = 0,
35 // Axis value should be inverted before reporting.
36 MODE_INVERT = 1,
37 // Axis value should be split into two axes
38 MODE_SPLIT = 2,
39 };
40
41 // Axis mode.
42 Mode mode;
43
44 // Axis id.
45 // When split, this is the axis used for values smaller than the split position.
46 int32_t axis;
47
48 // When split, this is the axis used for values after higher than the split position.
49 int32_t highAxis;
50
51 // The split value, or 0 if not split.
52 int32_t splitValue;
53
54 // The flat value, or -1 if none.
55 int32_t flatOverride;
56
57 AxisInfo() : mode(MODE_NORMAL), axis(-1), highAxis(-1), splitValue(0), flatOverride(-1) {
58 }
59};
60
61/**
62 * Describes a mapping from keyboard scan codes and joystick axes to Android key codes and axes.
63 *
64 * This object is immutable after it has been loaded.
65 */
Chris Ye1abffbd2020-08-18 12:50:12 -070066class KeyLayoutMap {
Jeff Brown5912f952013-07-01 19:10:31 -070067public:
Chris Ye1abffbd2020-08-18 12:50:12 -070068 static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename);
69 static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer);
70 static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename,
71 const char* contents);
Jeff Brown5912f952013-07-01 19:10:31 -070072
73 status_t mapKey(int32_t scanCode, int32_t usageCode,
74 int32_t* outKeyCode, uint32_t* outFlags) const;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080075 status_t findScanCodesForKey(int32_t keyCode, std::vector<int32_t>* outScanCodes) const;
Michael Wright74bdd2e2013-10-17 17:35:53 -070076 status_t findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const;
77 status_t findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const;
Jeff Brown5912f952013-07-01 19:10:31 -070078
79 status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const;
Chris Ye1abffbd2020-08-18 12:50:12 -070080 const std::string getLoadFileName() const;
Chris Yef59a2f42020-10-16 12:55:26 -070081 // Return pair of sensor type and sensor data index, for the input device abs code
82 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode);
Jeff Brown5912f952013-07-01 19:10:31 -070083
Jeff Brown5912f952013-07-01 19:10:31 -070084 virtual ~KeyLayoutMap();
85
86private:
87 struct Key {
88 int32_t keyCode;
89 uint32_t flags;
90 };
91
Michael Wright74bdd2e2013-10-17 17:35:53 -070092 struct Led {
93 int32_t ledCode;
94 };
95
Chris Yef59a2f42020-10-16 12:55:26 -070096 struct Sensor {
97 InputDeviceSensorType sensorType;
98 int32_t sensorDataIndex;
99 };
Michael Wright74bdd2e2013-10-17 17:35:53 -0700100
Jeff Brown5912f952013-07-01 19:10:31 -0700101 KeyedVector<int32_t, Key> mKeysByScanCode;
102 KeyedVector<int32_t, Key> mKeysByUsageCode;
103 KeyedVector<int32_t, AxisInfo> mAxes;
Michael Wright74bdd2e2013-10-17 17:35:53 -0700104 KeyedVector<int32_t, Led> mLedsByScanCode;
105 KeyedVector<int32_t, Led> mLedsByUsageCode;
Chris Yef59a2f42020-10-16 12:55:26 -0700106 std::unordered_map<int32_t, Sensor> mSensorsByAbsCode;
Chris Ye1abffbd2020-08-18 12:50:12 -0700107 std::string mLoadFileName;
Jeff Brown5912f952013-07-01 19:10:31 -0700108
109 KeyLayoutMap();
110
111 const Key* getKey(int32_t scanCode, int32_t usageCode) const;
112
113 class Parser {
114 KeyLayoutMap* mMap;
115 Tokenizer* mTokenizer;
116
117 public:
118 Parser(KeyLayoutMap* map, Tokenizer* tokenizer);
119 ~Parser();
120 status_t parse();
121
122 private:
123 status_t parseKey();
124 status_t parseAxis();
Michael Wright74bdd2e2013-10-17 17:35:53 -0700125 status_t parseLed();
Chris Yef59a2f42020-10-16 12:55:26 -0700126 status_t parseSensor();
Jeff Brown5912f952013-07-01 19:10:31 -0700127 };
128};
129
130} // namespace android
131
132#endif // _LIBINPUT_KEY_LAYOUT_MAP_H