blob: e4611f71cbd1708df27a626cbd7cdc409431c0a5 [file] [log] [blame]
Jeff Browna3477c82010-11-10 16:03:06 -08001/*
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 <ui/Keyboard.h>
24#include <ui/KeycodeLabels.h>
Jeff Browndb360642010-12-02 13:50:46 -080025#include <ui/KeyLayoutMap.h>
26#include <ui/KeyCharacterMap.h>
Jeff Browna3477c82010-11-10 16:03:06 -080027#include <utils/Errors.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30
31namespace android {
32
Jeff Browndb360642010-12-02 13:50:46 -080033// --- KeyMap ---
Jeff Browna3477c82010-11-10 16:03:06 -080034
Jeff Browndb360642010-12-02 13:50:46 -080035KeyMap::KeyMap() :
36 keyLayoutMap(NULL), keyCharacterMap(NULL) {
Jeff Browna3477c82010-11-10 16:03:06 -080037}
38
Jeff Browndb360642010-12-02 13:50:46 -080039KeyMap::~KeyMap() {
40 delete keyLayoutMap;
41 delete keyCharacterMap;
42}
43
44status_t KeyMap::load(const InputDeviceIdentifier& deviceIdenfifier,
45 const PropertyMap* deviceConfiguration) {
Jeff Brown66888372010-11-29 17:37:49 -080046 // Use the configured key layout if available.
47 if (deviceConfiguration) {
48 String8 keyLayoutName;
49 if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"),
50 keyLayoutName)) {
Jeff Browndb360642010-12-02 13:50:46 -080051 status_t status = loadKeyLayout(deviceIdenfifier, keyLayoutName);
52 if (status == NAME_NOT_FOUND) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000053 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
Jeff Brown66888372010-11-29 17:37:49 -080054 "it was not found.",
Jeff Browndb360642010-12-02 13:50:46 -080055 deviceIdenfifier.name.string(), keyLayoutName.string());
Jeff Brown66888372010-11-29 17:37:49 -080056 }
57 }
58
59 String8 keyCharacterMapName;
60 if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
61 keyCharacterMapName)) {
Jeff Browndb360642010-12-02 13:50:46 -080062 status_t status = loadKeyCharacterMap(deviceIdenfifier, keyCharacterMapName);
63 if (status == NAME_NOT_FOUND) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000064 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
Jeff Brown66888372010-11-29 17:37:49 -080065 "map '%s' but it was not found.",
Jeff Browndb360642010-12-02 13:50:46 -080066 deviceIdenfifier.name.string(), keyLayoutName.string());
Jeff Brown66888372010-11-29 17:37:49 -080067 }
68 }
69
Jeff Browndb360642010-12-02 13:50:46 -080070 if (isComplete()) {
Jeff Brown66888372010-11-29 17:37:49 -080071 return OK;
72 }
Jeff Browna3477c82010-11-10 16:03:06 -080073 }
Jeff Browna3477c82010-11-10 16:03:06 -080074
Jeff Browndb360642010-12-02 13:50:46 -080075 // Try searching by device identifier.
76 if (probeKeyMap(deviceIdenfifier, String8::empty())) {
Jeff Brown66888372010-11-29 17:37:49 -080077 return OK;
78 }
Jeff Browna3477c82010-11-10 16:03:06 -080079
Jeff Brown66888372010-11-29 17:37:49 -080080 // Fall back on the Generic key map.
Jeff Browna3477c82010-11-10 16:03:06 -080081 // TODO Apply some additional heuristics here to figure out what kind of
Jeff Browndb360642010-12-02 13:50:46 -080082 // generic key map to use (US English, etc.) for typical external keyboards.
83 if (probeKeyMap(deviceIdenfifier, String8("Generic"))) {
84 return OK;
85 }
86
87 // Try the Virtual key map as a last resort.
88 if (probeKeyMap(deviceIdenfifier, String8("Virtual"))) {
Jeff Brown66888372010-11-29 17:37:49 -080089 return OK;
90 }
Jeff Browna3477c82010-11-10 16:03:06 -080091
92 // Give up!
Steve Blocke6f43dd2012-01-06 19:20:56 +000093 ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
Jeff Browndb360642010-12-02 13:50:46 -080094 deviceIdenfifier.name.string());
Jeff Browna3477c82010-11-10 16:03:06 -080095 return NAME_NOT_FOUND;
96}
97
Jeff Browndb360642010-12-02 13:50:46 -080098bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
99 const String8& keyMapName) {
100 if (!haveKeyLayout()) {
101 loadKeyLayout(deviceIdentifier, keyMapName);
102 }
103 if (!haveKeyCharacterMap()) {
104 loadKeyCharacterMap(deviceIdentifier, keyMapName);
105 }
106 return isComplete();
107}
108
109status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier,
110 const String8& name) {
111 String8 path(getPath(deviceIdentifier, name,
112 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT));
113 if (path.isEmpty()) {
114 return NAME_NOT_FOUND;
115 }
116
117 KeyLayoutMap* map;
118 status_t status = KeyLayoutMap::load(path, &map);
119 if (status) {
120 return status;
121 }
122
123 keyLayoutFile.setTo(path);
124 keyLayoutMap = map;
125 return OK;
126}
127
128status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
129 const String8& name) {
130 String8 path(getPath(deviceIdentifier, name,
131 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP));
132 if (path.isEmpty()) {
133 return NAME_NOT_FOUND;
134 }
135
136 KeyCharacterMap* map;
137 status_t status = KeyCharacterMap::load(path, &map);
138 if (status) {
139 return status;
140 }
141
142 keyCharacterMapFile.setTo(path);
143 keyCharacterMap = map;
144 return OK;
145}
146
147String8 KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
148 const String8& name, InputDeviceConfigurationFileType type) {
149 return name.isEmpty()
150 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
151 : getInputDeviceConfigurationFilePathByName(name, type);
152}
153
154
155// --- Global functions ---
156
157bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
158 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
159 if (!keyMap->haveKeyCharacterMap()
160 || keyMap->keyCharacterMap->getKeyboardType()
161 == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) {
162 return false;
163 }
164
165 if (deviceConfiguration) {
166 bool builtIn = false;
167 if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
168 && builtIn) {
169 return true;
170 }
171 }
172
173 return strstr(deviceIdentifier.name.string(), "-keypad");
174}
175
Jeff Brown3ea4de82011-02-19 01:08:02 -0800176static int lookupValueByLabel(const char* literal, const KeycodeLabel *list) {
Jeff Browna3477c82010-11-10 16:03:06 -0800177 while (list->literal) {
178 if (strcmp(literal, list->literal) == 0) {
179 return list->value;
180 }
181 list++;
182 }
183 return list->value;
184}
185
Jeff Brown3ea4de82011-02-19 01:08:02 -0800186static const char* lookupLabelByValue(int value, const KeycodeLabel *list) {
187 while (list->literal) {
188 if (list->value == value) {
189 return list->literal;
190 }
191 list++;
192 }
193 return NULL;
194}
195
Jeff Browna3477c82010-11-10 16:03:06 -0800196int32_t getKeyCodeByLabel(const char* label) {
Jeff Brown3ea4de82011-02-19 01:08:02 -0800197 return int32_t(lookupValueByLabel(label, KEYCODES));
Jeff Browna3477c82010-11-10 16:03:06 -0800198}
199
200uint32_t getKeyFlagByLabel(const char* label) {
Jeff Brown3ea4de82011-02-19 01:08:02 -0800201 return uint32_t(lookupValueByLabel(label, FLAGS));
202}
203
204int32_t getAxisByLabel(const char* label) {
205 return int32_t(lookupValueByLabel(label, AXES));
206}
207
208const char* getAxisLabel(int32_t axisId) {
209 return lookupLabelByValue(axisId, AXES);
Jeff Browna3477c82010-11-10 16:03:06 -0800210}
211
212static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
213 int32_t newMetaState;
214 if (down) {
215 newMetaState = oldMetaState | mask;
216 } else {
217 newMetaState = oldMetaState &
218 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
219 }
220
221 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
222 newMetaState |= AMETA_ALT_ON;
223 }
224
225 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
226 newMetaState |= AMETA_SHIFT_ON;
227 }
228
229 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
230 newMetaState |= AMETA_CTRL_ON;
231 }
232
233 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
234 newMetaState |= AMETA_META_ON;
235 }
236 return newMetaState;
237}
238
239static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
240 if (down) {
241 return oldMetaState;
242 } else {
243 return oldMetaState ^ mask;
244 }
245}
246
247int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
248 int32_t mask;
249 switch (keyCode) {
250 case AKEYCODE_ALT_LEFT:
251 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
252 case AKEYCODE_ALT_RIGHT:
253 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
254 case AKEYCODE_SHIFT_LEFT:
255 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
256 case AKEYCODE_SHIFT_RIGHT:
257 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
258 case AKEYCODE_SYM:
259 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
260 case AKEYCODE_FUNCTION:
261 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
262 case AKEYCODE_CTRL_LEFT:
263 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
264 case AKEYCODE_CTRL_RIGHT:
265 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
266 case AKEYCODE_META_LEFT:
267 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
268 case AKEYCODE_META_RIGHT:
269 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
270 case AKEYCODE_CAPS_LOCK:
271 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
272 case AKEYCODE_NUM_LOCK:
273 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
274 case AKEYCODE_SCROLL_LOCK:
275 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
276 default:
277 return oldMetaState;
278 }
279}
280
Jeff Brown80f3e7c2011-03-02 14:41:58 -0800281bool isMetaKey(int32_t keyCode) {
282 switch (keyCode) {
283 case AKEYCODE_ALT_LEFT:
284 case AKEYCODE_ALT_RIGHT:
285 case AKEYCODE_SHIFT_LEFT:
286 case AKEYCODE_SHIFT_RIGHT:
287 case AKEYCODE_SYM:
288 case AKEYCODE_FUNCTION:
289 case AKEYCODE_CTRL_LEFT:
290 case AKEYCODE_CTRL_RIGHT:
291 case AKEYCODE_META_LEFT:
292 case AKEYCODE_META_RIGHT:
293 case AKEYCODE_CAPS_LOCK:
294 case AKEYCODE_NUM_LOCK:
295 case AKEYCODE_SCROLL_LOCK:
296 return true;
297 default:
298 return false;
299 }
300}
301
Jeff Browna3477c82010-11-10 16:03:06 -0800302
303} // namespace android