blob: 0c22bfefeddc5e1291e1624315a159dcf56c894b [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
41status_t KeyMap::load(const InputDeviceIdentifier& deviceIdenfifier,
42 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)) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010048 status_t status = loadKeyLayout(deviceIdenfifier, 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.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010052 deviceIdenfifier.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)) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010059 status_t status = loadKeyCharacterMap(deviceIdenfifier, 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.",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010063 deviceIdenfifier.name.c_str(), keyLayoutName.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.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010073 if (probeKeyMap(deviceIdenfifier, "")) {
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.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010080 if (probeKeyMap(deviceIdenfifier, "Generic")) {
Jeff Brown5912f952013-07-01 19:10:31 -070081 return OK;
82 }
83
84 // Try the Virtual key map as a last resort.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010085 if (probeKeyMap(deviceIdenfifier, "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!",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010091 deviceIdenfifier.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) {
108 std::string path(getPath(deviceIdentifier, name,
Jeff Brown5912f952013-07-01 19:10:31 -0700109 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT));
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100110 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700111 return NAME_NOT_FOUND;
112 }
113
114 status_t status = KeyLayoutMap::load(path, &keyLayoutMap);
115 if (status) {
116 return status;
117 }
118
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100119 keyLayoutFile = path;
Jeff Brown5912f952013-07-01 19:10:31 -0700120 return OK;
121}
122
123status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100124 const std::string& name) {
125 std::string path = getPath(deviceIdentifier, name,
126 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP);
127 if (path.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -0700128 return NAME_NOT_FOUND;
129 }
130
131 status_t status = KeyCharacterMap::load(path,
132 KeyCharacterMap::FORMAT_BASE, &keyCharacterMap);
133 if (status) {
134 return status;
135 }
136
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100137 keyCharacterMapFile = path;
Jeff Brown5912f952013-07-01 19:10:31 -0700138 return OK;
139}
140
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100141std::string KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
142 const std::string& name, InputDeviceConfigurationFileType type) {
143 return name.empty()
Jeff Brown5912f952013-07-01 19:10:31 -0700144 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
145 : getInputDeviceConfigurationFilePathByName(name, type);
146}
147
148
149// --- Global functions ---
150
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600151bool isKeyboardSpecialFunction(const PropertyMap* config) {
152 if (config == nullptr) {
153 return false;
154 }
155 bool isSpecialFunction = false;
156 config->tryGetProperty(String8("keyboard.specialFunction"), isSpecialFunction);
157 return isSpecialFunction;
158}
159
Jeff Brown5912f952013-07-01 19:10:31 -0700160bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
161 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
Siarhei Vishniakou61da25a2018-02-15 21:04:49 -0600162 // TODO: remove the third OR statement (SPECIAL_FUNCTION) in Q
163 if (!keyMap->haveKeyCharacterMap() || isKeyboardSpecialFunction(deviceConfiguration)
Jeff Brown5912f952013-07-01 19:10:31 -0700164 || keyMap->keyCharacterMap->getKeyboardType()
165 == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) {
166 return false;
167 }
168
169 if (deviceConfiguration) {
170 bool builtIn = false;
171 if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
172 && builtIn) {
173 return true;
174 }
175 }
176
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100177 return strstr(deviceIdentifier.name.c_str(), "-keypad");
Jeff Brown5912f952013-07-01 19:10:31 -0700178}
179
Jeff Brown5912f952013-07-01 19:10:31 -0700180static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
181 int32_t newMetaState;
182 if (down) {
183 newMetaState = oldMetaState | mask;
184 } else {
185 newMetaState = oldMetaState &
186 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
187 }
188
Dmitry Torokhov115f93e2015-09-17 18:04:50 -0700189 return normalizeMetaState(newMetaState);
190}
191
192int32_t normalizeMetaState(int32_t oldMetaState) {
193 int32_t newMetaState = oldMetaState;
Jeff Brown5912f952013-07-01 19:10:31 -0700194 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
195 newMetaState |= AMETA_ALT_ON;
196 }
197
198 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
199 newMetaState |= AMETA_SHIFT_ON;
200 }
201
202 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
203 newMetaState |= AMETA_CTRL_ON;
204 }
205
206 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
207 newMetaState |= AMETA_META_ON;
208 }
209 return newMetaState;
210}
211
212static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
213 if (down) {
214 return oldMetaState;
215 } else {
216 return oldMetaState ^ mask;
217 }
218}
219
220int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
Jeff Brown5912f952013-07-01 19:10:31 -0700221 switch (keyCode) {
222 case AKEYCODE_ALT_LEFT:
223 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
224 case AKEYCODE_ALT_RIGHT:
225 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
226 case AKEYCODE_SHIFT_LEFT:
227 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
228 case AKEYCODE_SHIFT_RIGHT:
229 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
230 case AKEYCODE_SYM:
231 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
232 case AKEYCODE_FUNCTION:
233 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
234 case AKEYCODE_CTRL_LEFT:
235 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
236 case AKEYCODE_CTRL_RIGHT:
237 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
238 case AKEYCODE_META_LEFT:
239 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
240 case AKEYCODE_META_RIGHT:
241 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
242 case AKEYCODE_CAPS_LOCK:
243 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
244 case AKEYCODE_NUM_LOCK:
245 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
246 case AKEYCODE_SCROLL_LOCK:
247 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
248 default:
249 return oldMetaState;
250 }
251}
252
253bool isMetaKey(int32_t keyCode) {
254 switch (keyCode) {
255 case AKEYCODE_ALT_LEFT:
256 case AKEYCODE_ALT_RIGHT:
257 case AKEYCODE_SHIFT_LEFT:
258 case AKEYCODE_SHIFT_RIGHT:
259 case AKEYCODE_SYM:
260 case AKEYCODE_FUNCTION:
261 case AKEYCODE_CTRL_LEFT:
262 case AKEYCODE_CTRL_RIGHT:
263 case AKEYCODE_META_LEFT:
264 case AKEYCODE_META_RIGHT:
265 case AKEYCODE_CAPS_LOCK:
266 case AKEYCODE_NUM_LOCK:
267 case AKEYCODE_SCROLL_LOCK:
268 return true;
269 default:
270 return false;
271 }
272}
273
274
275} // namespace android