blob: f0895b32ef6b0d17a716fa50fa21084b04f1de6f [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
23#include <input/Keyboard.h>
Michael Wright872db4f2014-04-22 15:03:51 -070024#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <input/KeyLayoutMap.h>
26#include <input/KeyCharacterMap.h>
27#include <input/InputDevice.h>
28#include <utils/Errors.h>
29#include <utils/Log.h>
30
31namespace android {
32
33// --- KeyMap ---
34
35KeyMap::KeyMap() {
36}
37
38KeyMap::~KeyMap() {
39}
40
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090041status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier,
Jeff Brown5912f952013-07-01 19:10:31 -070042 const PropertyMap* deviceConfiguration) {
43 // Use the configured key layout if available.
44 if (deviceConfiguration) {
45 String8 keyLayoutName;
46 if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"),
47 keyLayoutName)) {
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090048 status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070049 if (status == NAME_NOT_FOUND) {
50 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
51 "it was not found.",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090052 deviceIdentifier.name.c_str(), keyLayoutName.string());
Jeff Brown5912f952013-07-01 19:10:31 -070053 }
54 }
55
56 String8 keyCharacterMapName;
57 if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
58 keyCharacterMapName)) {
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090059 status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070060 if (status == NAME_NOT_FOUND) {
61 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
62 "map '%s' but it was not found.",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090063 deviceIdentifier.name.c_str(), keyCharacterMapName.string());
Jeff Brown5912f952013-07-01 19:10:31 -070064 }
65 }
66
67 if (isComplete()) {
68 return OK;
69 }
70 }
71
72 // Try searching by device identifier.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090073 if (probeKeyMap(deviceIdentifier, "")) {
Jeff Brown5912f952013-07-01 19:10:31 -070074 return OK;
75 }
76
77 // Fall back on the Generic key map.
78 // TODO Apply some additional heuristics here to figure out what kind of
79 // generic key map to use (US English, etc.) for typical external keyboards.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090080 if (probeKeyMap(deviceIdentifier, "Generic")) {
Jeff Brown5912f952013-07-01 19:10:31 -070081 return OK;
82 }
83
84 // Try the Virtual key map as a last resort.
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090085 if (probeKeyMap(deviceIdentifier, "Virtual")) {
Jeff Brown5912f952013-07-01 19:10:31 -070086 return OK;
87 }
88
89 // Give up!
90 ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
Yuichiro Hanadaf92661b2019-06-06 18:50:18 +090091 deviceIdentifier.name.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -070092 return NAME_NOT_FOUND;
93}
94
95bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010096 const std::string& keyMapName) {
Jeff Brown5912f952013-07-01 19:10:31 -070097 if (!haveKeyLayout()) {
98 loadKeyLayout(deviceIdentifier, keyMapName);
99 }
100 if (!haveKeyCharacterMap()) {
101 loadKeyCharacterMap(deviceIdentifier, keyMapName);
102 }
103 return isComplete();
104}
105
106status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100107 const std::string& name) {
Chris Ye1d927aa2020-07-04 18:22:41 -0700108 std::string path(getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_LAYOUT));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100109 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700110 return NAME_NOT_FOUND;
111 }
112
Chris Ye1abffbd2020-08-18 12:50:12 -0700113 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
Bernie Innocenti55396a82020-12-19 18:47:35 +0900114 if (!ret.ok()) {
Chris Ye1abffbd2020-08-18 12:50:12 -0700115 return ret.error().code();
Jeff Brown5912f952013-07-01 19:10:31 -0700116 }
Chris Ye1abffbd2020-08-18 12:50:12 -0700117 keyLayoutMap = *ret;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100118 keyLayoutFile = path;
Jeff Brown5912f952013-07-01 19:10:31 -0700119 return OK;
120}
121
122status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100123 const std::string& name) {
Chris Ye1d927aa2020-07-04 18:22:41 -0700124 std::string path =
125 getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_CHARACTER_MAP);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100126 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700127 return NAME_NOT_FOUND;
128 }
129
Chris Ye3a1e4462020-08-12 10:13:15 -0700130 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
Michael Wright102936e2020-11-04 03:44:27 +0000131 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
Bernie Innocenti55396a82020-12-19 18:47:35 +0900132 if (!ret.ok()) {
Chris Ye3a1e4462020-08-12 10:13:15 -0700133 return ret.error().code();
Jeff Brown5912f952013-07-01 19:10:31 -0700134 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700135 keyCharacterMap = *ret;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100136 keyCharacterMapFile = path;
Jeff Brown5912f952013-07-01 19:10:31 -0700137 return OK;
138}
139
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100140std::string KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
141 const std::string& name, InputDeviceConfigurationFileType type) {
142 return name.empty()
Jeff Brown5912f952013-07-01 19:10:31 -0700143 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
144 : getInputDeviceConfigurationFilePathByName(name, type);
145}
146
147
148// --- Global functions ---
149
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600150bool isKeyboardSpecialFunction(const PropertyMap* config) {
151 if (config == nullptr) {
152 return false;
153 }
154 bool isSpecialFunction = false;
155 config->tryGetProperty(String8("keyboard.specialFunction"), isSpecialFunction);
156 return isSpecialFunction;
157}
158
Jeff Brown5912f952013-07-01 19:10:31 -0700159bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
160 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600161 // TODO: remove the third OR statement (SPECIAL_FUNCTION) in Q
Michael Wright102936e2020-11-04 03:44:27 +0000162 if (!keyMap->haveKeyCharacterMap() || isKeyboardSpecialFunction(deviceConfiguration) ||
163 keyMap->keyCharacterMap->getKeyboardType() ==
164 KeyCharacterMap::KeyboardType::SPECIAL_FUNCTION) {
Jeff Brown5912f952013-07-01 19:10:31 -0700165 return false;
166 }
167
168 if (deviceConfiguration) {
169 bool builtIn = false;
170 if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
171 && builtIn) {
172 return true;
173 }
174 }
175
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100176 return strstr(deviceIdentifier.name.c_str(), "-keypad");
Jeff Brown5912f952013-07-01 19:10:31 -0700177}
178
Jeff Brown5912f952013-07-01 19:10:31 -0700179static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
180 int32_t newMetaState;
181 if (down) {
182 newMetaState = oldMetaState | mask;
183 } else {
184 newMetaState = oldMetaState &
185 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
186 }
187
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700188 return normalizeMetaState(newMetaState);
189}
190
191int32_t normalizeMetaState(int32_t oldMetaState) {
192 int32_t newMetaState = oldMetaState;
Jeff Brown5912f952013-07-01 19:10:31 -0700193 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
194 newMetaState |= AMETA_ALT_ON;
195 }
196
197 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
198 newMetaState |= AMETA_SHIFT_ON;
199 }
200
201 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
202 newMetaState |= AMETA_CTRL_ON;
203 }
204
205 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
206 newMetaState |= AMETA_META_ON;
207 }
208 return newMetaState;
209}
210
211static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
212 if (down) {
213 return oldMetaState;
214 } else {
215 return oldMetaState ^ mask;
216 }
217}
218
219int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
Jeff Brown5912f952013-07-01 19:10:31 -0700220 switch (keyCode) {
221 case AKEYCODE_ALT_LEFT:
222 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
223 case AKEYCODE_ALT_RIGHT:
224 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
225 case AKEYCODE_SHIFT_LEFT:
226 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
227 case AKEYCODE_SHIFT_RIGHT:
228 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
229 case AKEYCODE_SYM:
230 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
231 case AKEYCODE_FUNCTION:
232 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
233 case AKEYCODE_CTRL_LEFT:
234 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
235 case AKEYCODE_CTRL_RIGHT:
236 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
237 case AKEYCODE_META_LEFT:
238 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
239 case AKEYCODE_META_RIGHT:
240 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
241 case AKEYCODE_CAPS_LOCK:
242 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
243 case AKEYCODE_NUM_LOCK:
244 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
245 case AKEYCODE_SCROLL_LOCK:
246 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
247 default:
248 return oldMetaState;
249 }
250}
251
252bool isMetaKey(int32_t keyCode) {
253 switch (keyCode) {
254 case AKEYCODE_ALT_LEFT:
255 case AKEYCODE_ALT_RIGHT:
256 case AKEYCODE_SHIFT_LEFT:
257 case AKEYCODE_SHIFT_RIGHT:
258 case AKEYCODE_SYM:
259 case AKEYCODE_FUNCTION:
260 case AKEYCODE_CTRL_LEFT:
261 case AKEYCODE_CTRL_RIGHT:
262 case AKEYCODE_META_LEFT:
263 case AKEYCODE_META_RIGHT:
264 case AKEYCODE_CAPS_LOCK:
265 case AKEYCODE_NUM_LOCK:
266 case AKEYCODE_SCROLL_LOCK:
267 return true;
268 default:
269 return false;
270 }
271}
272
273
274} // namespace android