blob: e203d190a65cb10a1b30d89469013c4285fdf280 [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
Prabir Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Jeff Brown5912f952013-07-01 19:10:31 -070018
Chris Ye1abffbd2020-08-18 12:50:12 -070019#include <android-base/result.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <stdint.h>
21#include <utils/Errors.h>
Chris Ye1abffbd2020-08-18 12:50:12 -070022#include <utils/Tokenizer.h>
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070023#include <set>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Chris Yef59a2f42020-10-16 12:55:26 -070025#include <input/InputDevice.h>
26
Jeff Brown5912f952013-07-01 19:10:31 -070027namespace android {
28
29struct 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
55 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 */
Chris Ye1abffbd2020-08-18 12:50:12 -070064class KeyLayoutMap {
Jeff Brown5912f952013-07-01 19:10:31 -070065public:
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070066 static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename,
67 const char* contents = nullptr);
Chris Ye1abffbd2020-08-18 12:50:12 -070068 static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename,
69 const char* contents);
Jeff Brown5912f952013-07-01 19:10:31 -070070
71 status_t mapKey(int32_t scanCode, int32_t usageCode,
72 int32_t* outKeyCode, uint32_t* outFlags) const;
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -070073 std::vector<int32_t> findScanCodesForKey(int32_t keyCode) const;
74 std::optional<int32_t> findScanCodeForLed(int32_t ledCode) const;
75 std::optional<int32_t> findUsageCodeForLed(int32_t ledCode) const;
Jeff Brown5912f952013-07-01 19:10:31 -070076
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -070077 std::optional<AxisInfo> mapAxis(int32_t scanCode) const;
Chris Ye1abffbd2020-08-18 12:50:12 -070078 const std::string getLoadFileName() const;
Chris Yef59a2f42020-10-16 12:55:26 -070079 // Return pair of sensor type and sensor data index, for the input device abs code
Prabir Pradhanae4ff282022-08-23 16:21:39 +000080 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) const;
Jeff Brown5912f952013-07-01 19:10:31 -070081
Jeff Brown5912f952013-07-01 19:10:31 -070082 virtual ~KeyLayoutMap();
83
84private:
Siarhei Vishniakou5ed8eaa2022-05-18 12:30:16 -070085 static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer);
86
Jeff Brown5912f952013-07-01 19:10:31 -070087 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
Siarhei Vishniakouef1564c2022-05-18 09:45:54 -0700101 std::unordered_map<int32_t, Key> mKeysByScanCode;
102 std::unordered_map<int32_t, Key> mKeysByUsageCode;
103 std::unordered_map<int32_t, AxisInfo> mAxes;
104 std::unordered_map<int32_t, Led> mLedsByScanCode;
105 std::unordered_map<int32_t, Led> mLedsByUsageCode;
Chris Yef59a2f42020-10-16 12:55:26 -0700106 std::unordered_map<int32_t, Sensor> mSensorsByAbsCode;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700107 std::set<std::string> mRequiredKernelConfigs;
Chris Ye1abffbd2020-08-18 12:50:12 -0700108 std::string mLoadFileName;
Jeff Brown5912f952013-07-01 19:10:31 -0700109
110 KeyLayoutMap();
111
112 const Key* getKey(int32_t scanCode, int32_t usageCode) const;
113
114 class Parser {
115 KeyLayoutMap* mMap;
116 Tokenizer* mTokenizer;
117
118 public:
119 Parser(KeyLayoutMap* map, Tokenizer* tokenizer);
120 ~Parser();
121 status_t parse();
122
123 private:
124 status_t parseKey();
125 status_t parseAxis();
Michael Wright74bdd2e2013-10-17 17:35:53 -0700126 status_t parseLed();
Chris Yef59a2f42020-10-16 12:55:26 -0700127 status_t parseSensor();
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700128 status_t parseRequiredKernelConfig();
Jeff Brown5912f952013-07-01 19:10:31 -0700129 };
130};
131
132} // namespace android