blob: c3f5151fd1e9bf6c94a58b59616b5b9665f96f11 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "Keyboard"
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <limits.h>
22
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <input/InputDevice.h>
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070024#include <input/InputEventLabels.h>
25#include <input/KeyCharacterMap.h>
26#include <input/KeyLayoutMap.h>
27#include <input/Keyboard.h>
28#include <log/log.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <utils/Errors.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
31namespace android {
32
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070033static std::string getPath(const InputDeviceIdentifier& deviceIdentifier, const std::string& name,
34 InputDeviceConfigurationFileType type) {
35 return name.empty()
36 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
37 : getInputDeviceConfigurationFilePathByName(name, type);
38}
39
Jeff Brown5912f952013-07-01 19:10:31 -070040// --- KeyMap ---
41
42KeyMap::KeyMap() {
43}
44
45KeyMap::~KeyMap() {
46}
47
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090048status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier,
Jeff Brown5912f952013-07-01 19:10:31 -070049 const PropertyMap* deviceConfiguration) {
50 // Use the configured key layout if available.
51 if (deviceConfiguration) {
52 String8 keyLayoutName;
53 if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"),
54 keyLayoutName)) {
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090055 status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070056 if (status == NAME_NOT_FOUND) {
57 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
58 "it was not found.",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090059 deviceIdentifier.name.c_str(), keyLayoutName.string());
Jeff Brown5912f952013-07-01 19:10:31 -070060 }
61 }
62
63 String8 keyCharacterMapName;
64 if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
65 keyCharacterMapName)) {
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090066 status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070067 if (status == NAME_NOT_FOUND) {
68 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
69 "map '%s' but it was not found.",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090070 deviceIdentifier.name.c_str(), keyCharacterMapName.string());
Jeff Brown5912f952013-07-01 19:10:31 -070071 }
72 }
73
74 if (isComplete()) {
75 return OK;
76 }
77 }
78
79 // Try searching by device identifier.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090080 if (probeKeyMap(deviceIdentifier, "")) {
Jeff Brown5912f952013-07-01 19:10:31 -070081 return OK;
82 }
83
84 // Fall back on the Generic key map.
85 // TODO Apply some additional heuristics here to figure out what kind of
86 // generic key map to use (US English, etc.) for typical external keyboards.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090087 if (probeKeyMap(deviceIdentifier, "Generic")) {
Jeff Brown5912f952013-07-01 19:10:31 -070088 return OK;
89 }
90
91 // Try the Virtual key map as a last resort.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090092 if (probeKeyMap(deviceIdentifier, "Virtual")) {
Jeff Brown5912f952013-07-01 19:10:31 -070093 return OK;
94 }
95
96 // Give up!
97 ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090098 deviceIdentifier.name.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070099 return NAME_NOT_FOUND;
100}
101
102bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100103 const std::string& keyMapName) {
Jeff Brown5912f952013-07-01 19:10:31 -0700104 if (!haveKeyLayout()) {
105 loadKeyLayout(deviceIdentifier, keyMapName);
106 }
107 if (!haveKeyCharacterMap()) {
108 loadKeyCharacterMap(deviceIdentifier, keyMapName);
109 }
110 return isComplete();
111}
112
113status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100114 const std::string& name) {
Chris Ye1d927aa2020-07-04 18:22:41 -0700115 std::string path(getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_LAYOUT));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100116 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700117 return NAME_NOT_FOUND;
118 }
119
Chris Ye1abffbd2020-08-18 12:50:12 -0700120 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700121 if (ret.ok()) {
122 keyLayoutMap = *ret;
123 keyLayoutFile = path;
124 return OK;
125 }
126
127 // Try to load fallback layout if the regular layout could not be loaded due to missing
128 // kernel modules
129 std::string fallbackPath(
130 getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier,
131 InputDeviceConfigurationFileType::
132 KEY_LAYOUT,
133 "_fallback"));
134 ret = KeyLayoutMap::load(fallbackPath);
Bernie Innocenti55396a82020-12-19 18:47:35 +0900135 if (!ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -0700136 return ret.error().code();
Jeff Brown5912f952013-07-01 19:10:31 -0700137 }
Chris Ye1abffbd2020-08-18 12:50:12 -0700138 keyLayoutMap = *ret;
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700139 keyLayoutFile = fallbackPath;
Jeff Brown5912f952013-07-01 19:10:31 -0700140 return OK;
141}
142
143status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100144 const std::string& name) {
Chris Ye1d927aa2020-07-04 18:22:41 -0700145 std::string path =
146 getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_CHARACTER_MAP);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100147 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700148 return NAME_NOT_FOUND;
149 }
150
Chris Ye3a1e4462020-08-12 10:13:15 -0700151 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
Michael Wright102936e2020-11-04 03:44:27 +0000152 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
Bernie Innocenti55396a82020-12-19 18:47:35 +0900153 if (!ret.ok()) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700154 return ret.error().code();
Jeff Brown5912f952013-07-01 19:10:31 -0700155 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700156 keyCharacterMap = *ret;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100157 keyCharacterMapFile = path;
Jeff Brown5912f952013-07-01 19:10:31 -0700158 return OK;
159}
160
Jeff Brown5912f952013-07-01 19:10:31 -0700161// --- Global functions ---
162
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600163bool isKeyboardSpecialFunction(const PropertyMap* config) {
164 if (config == nullptr) {
165 return false;
166 }
167 bool isSpecialFunction = false;
168 config->tryGetProperty(String8("keyboard.specialFunction"), isSpecialFunction);
169 return isSpecialFunction;
170}
171
Jeff Brown5912f952013-07-01 19:10:31 -0700172bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
173 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600174 // TODO: remove the third OR statement (SPECIAL_FUNCTION) in Q
Michael Wright102936e2020-11-04 03:44:27 +0000175 if (!keyMap->haveKeyCharacterMap() || isKeyboardSpecialFunction(deviceConfiguration) ||
176 keyMap->keyCharacterMap->getKeyboardType() ==
177 KeyCharacterMap::KeyboardType::SPECIAL_FUNCTION) {
Jeff Brown5912f952013-07-01 19:10:31 -0700178 return false;
179 }
180
181 if (deviceConfiguration) {
182 bool builtIn = false;
183 if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
184 && builtIn) {
185 return true;
186 }
187 }
188
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100189 return strstr(deviceIdentifier.name.c_str(), "-keypad");
Jeff Brown5912f952013-07-01 19:10:31 -0700190}
191
Jeff Brown5912f952013-07-01 19:10:31 -0700192static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
193 int32_t newMetaState;
194 if (down) {
195 newMetaState = oldMetaState | mask;
196 } else {
197 newMetaState = oldMetaState &
198 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
199 }
200
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700201 return normalizeMetaState(newMetaState);
202}
203
204int32_t normalizeMetaState(int32_t oldMetaState) {
205 int32_t newMetaState = oldMetaState;
Jeff Brown5912f952013-07-01 19:10:31 -0700206 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
207 newMetaState |= AMETA_ALT_ON;
208 }
209
210 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
211 newMetaState |= AMETA_SHIFT_ON;
212 }
213
214 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
215 newMetaState |= AMETA_CTRL_ON;
216 }
217
218 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
219 newMetaState |= AMETA_META_ON;
220 }
221 return newMetaState;
222}
223
224static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
225 if (down) {
226 return oldMetaState;
227 } else {
228 return oldMetaState ^ mask;
229 }
230}
231
232int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
Jeff Brown5912f952013-07-01 19:10:31 -0700233 switch (keyCode) {
234 case AKEYCODE_ALT_LEFT:
235 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
236 case AKEYCODE_ALT_RIGHT:
237 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
238 case AKEYCODE_SHIFT_LEFT:
239 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
240 case AKEYCODE_SHIFT_RIGHT:
241 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
242 case AKEYCODE_SYM:
243 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
244 case AKEYCODE_FUNCTION:
245 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
246 case AKEYCODE_CTRL_LEFT:
247 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
248 case AKEYCODE_CTRL_RIGHT:
249 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
250 case AKEYCODE_META_LEFT:
251 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
252 case AKEYCODE_META_RIGHT:
253 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
254 case AKEYCODE_CAPS_LOCK:
255 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
256 case AKEYCODE_NUM_LOCK:
257 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
258 case AKEYCODE_SCROLL_LOCK:
259 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
260 default:
261 return oldMetaState;
262 }
263}
264
265bool isMetaKey(int32_t keyCode) {
266 switch (keyCode) {
267 case AKEYCODE_ALT_LEFT:
268 case AKEYCODE_ALT_RIGHT:
269 case AKEYCODE_SHIFT_LEFT:
270 case AKEYCODE_SHIFT_RIGHT:
271 case AKEYCODE_SYM:
272 case AKEYCODE_FUNCTION:
273 case AKEYCODE_CTRL_LEFT:
274 case AKEYCODE_CTRL_RIGHT:
275 case AKEYCODE_META_LEFT:
276 case AKEYCODE_META_RIGHT:
277 case AKEYCODE_CAPS_LOCK:
278 case AKEYCODE_NUM_LOCK:
279 case AKEYCODE_SCROLL_LOCK:
280 return true;
281 default:
282 return false;
283 }
284}
285
286
287} // namespace android