blob: 26a4e6a412a22e615531c761771a6d0a03df9f73 [file] [log] [blame]
Jeff Brown6b53e8d2010-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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080023#include <androidfw/Keyboard.h>
24#include <androidfw/KeycodeLabels.h>
25#include <androidfw/KeyLayoutMap.h>
26#include <androidfw/KeyCharacterMap.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070027#include <androidfw/InputDevice.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080028#include <utils/Errors.h>
29#include <utils/Log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080030
31namespace android {
32
Jeff Brown90655042010-12-02 13:50:46 -080033// --- KeyMap ---
Jeff Brown6b53e8d2010-11-10 16:03:06 -080034
Jeff Brown9f25b7f2012-04-10 14:30:49 -070035KeyMap::KeyMap() {
Jeff Brown6b53e8d2010-11-10 16:03:06 -080036}
37
Jeff Brown90655042010-12-02 13:50:46 -080038KeyMap::~KeyMap() {
Jeff Brown90655042010-12-02 13:50:46 -080039}
40
41status_t KeyMap::load(const InputDeviceIdentifier& deviceIdenfifier,
42 const PropertyMap* deviceConfiguration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -080043 // Use the configured key layout if available.
44 if (deviceConfiguration) {
45 String8 keyLayoutName;
46 if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"),
47 keyLayoutName)) {
Jeff Brown90655042010-12-02 13:50:46 -080048 status_t status = loadKeyLayout(deviceIdenfifier, keyLayoutName);
49 if (status == NAME_NOT_FOUND) {
Steve Block3762c312012-01-06 19:20:56 +000050 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
Jeff Brown47e6b1b2010-11-29 17:37:49 -080051 "it was not found.",
Jeff Brown90655042010-12-02 13:50:46 -080052 deviceIdenfifier.name.string(), keyLayoutName.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -080053 }
54 }
55
56 String8 keyCharacterMapName;
57 if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
58 keyCharacterMapName)) {
Jeff Brown90655042010-12-02 13:50:46 -080059 status_t status = loadKeyCharacterMap(deviceIdenfifier, keyCharacterMapName);
60 if (status == NAME_NOT_FOUND) {
Steve Block3762c312012-01-06 19:20:56 +000061 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
Jeff Brown47e6b1b2010-11-29 17:37:49 -080062 "map '%s' but it was not found.",
Jeff Brown90655042010-12-02 13:50:46 -080063 deviceIdenfifier.name.string(), keyLayoutName.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -080064 }
65 }
66
Jeff Brown90655042010-12-02 13:50:46 -080067 if (isComplete()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -080068 return OK;
69 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080070 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080071
Jeff Brown90655042010-12-02 13:50:46 -080072 // Try searching by device identifier.
73 if (probeKeyMap(deviceIdenfifier, String8::empty())) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -080074 return OK;
75 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080076
Jeff Brown47e6b1b2010-11-29 17:37:49 -080077 // Fall back on the Generic key map.
Jeff Brown6b53e8d2010-11-10 16:03:06 -080078 // TODO Apply some additional heuristics here to figure out what kind of
Jeff Brown90655042010-12-02 13:50:46 -080079 // generic key map to use (US English, etc.) for typical external keyboards.
80 if (probeKeyMap(deviceIdenfifier, String8("Generic"))) {
81 return OK;
82 }
83
84 // Try the Virtual key map as a last resort.
85 if (probeKeyMap(deviceIdenfifier, String8("Virtual"))) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -080086 return OK;
87 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080088
89 // Give up!
Steve Block3762c312012-01-06 19:20:56 +000090 ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
Jeff Brown90655042010-12-02 13:50:46 -080091 deviceIdenfifier.name.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -080092 return NAME_NOT_FOUND;
93}
94
Jeff Brown90655042010-12-02 13:50:46 -080095bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
96 const String8& keyMapName) {
97 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,
107 const String8& name) {
108 String8 path(getPath(deviceIdentifier, name,
109 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT));
110 if (path.isEmpty()) {
111 return NAME_NOT_FOUND;
112 }
113
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700114 status_t status = KeyLayoutMap::load(path, &keyLayoutMap);
Jeff Brown90655042010-12-02 13:50:46 -0800115 if (status) {
116 return status;
117 }
118
119 keyLayoutFile.setTo(path);
Jeff Brown90655042010-12-02 13:50:46 -0800120 return OK;
121}
122
123status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
124 const String8& name) {
125 String8 path(getPath(deviceIdentifier, name,
126 INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP));
127 if (path.isEmpty()) {
128 return NAME_NOT_FOUND;
129 }
130
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 status_t status = KeyCharacterMap::load(path,
132 KeyCharacterMap::FORMAT_BASE, &keyCharacterMap);
Jeff Brown90655042010-12-02 13:50:46 -0800133 if (status) {
134 return status;
135 }
136
137 keyCharacterMapFile.setTo(path);
Jeff Brown90655042010-12-02 13:50:46 -0800138 return OK;
139}
140
141String8 KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
142 const String8& name, InputDeviceConfigurationFileType type) {
143 return name.isEmpty()
144 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
145 : getInputDeviceConfigurationFilePathByName(name, type);
146}
147
148
149// --- Global functions ---
150
151bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
152 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
153 if (!keyMap->haveKeyCharacterMap()
154 || keyMap->keyCharacterMap->getKeyboardType()
155 == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) {
156 return false;
157 }
158
159 if (deviceConfiguration) {
160 bool builtIn = false;
161 if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
162 && builtIn) {
163 return true;
164 }
165 }
166
167 return strstr(deviceIdentifier.name.string(), "-keypad");
168}
169
Jeff Brown6f2fba42011-02-19 01:08:02 -0800170static int lookupValueByLabel(const char* literal, const KeycodeLabel *list) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800171 while (list->literal) {
172 if (strcmp(literal, list->literal) == 0) {
173 return list->value;
174 }
175 list++;
176 }
177 return list->value;
178}
179
Jeff Brown6f2fba42011-02-19 01:08:02 -0800180static const char* lookupLabelByValue(int value, const KeycodeLabel *list) {
181 while (list->literal) {
182 if (list->value == value) {
183 return list->literal;
184 }
185 list++;
186 }
187 return NULL;
188}
189
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800190int32_t getKeyCodeByLabel(const char* label) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800191 return int32_t(lookupValueByLabel(label, KEYCODES));
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800192}
193
194uint32_t getKeyFlagByLabel(const char* label) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800195 return uint32_t(lookupValueByLabel(label, FLAGS));
196}
197
198int32_t getAxisByLabel(const char* label) {
199 return int32_t(lookupValueByLabel(label, AXES));
200}
201
202const char* getAxisLabel(int32_t axisId) {
203 return lookupLabelByValue(axisId, AXES);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800204}
205
206static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
207 int32_t newMetaState;
208 if (down) {
209 newMetaState = oldMetaState | mask;
210 } else {
211 newMetaState = oldMetaState &
212 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
213 }
214
215 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
216 newMetaState |= AMETA_ALT_ON;
217 }
218
219 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
220 newMetaState |= AMETA_SHIFT_ON;
221 }
222
223 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
224 newMetaState |= AMETA_CTRL_ON;
225 }
226
227 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
228 newMetaState |= AMETA_META_ON;
229 }
230 return newMetaState;
231}
232
233static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
234 if (down) {
235 return oldMetaState;
236 } else {
237 return oldMetaState ^ mask;
238 }
239}
240
241int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
242 int32_t mask;
243 switch (keyCode) {
244 case AKEYCODE_ALT_LEFT:
245 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
246 case AKEYCODE_ALT_RIGHT:
247 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
248 case AKEYCODE_SHIFT_LEFT:
249 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
250 case AKEYCODE_SHIFT_RIGHT:
251 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
252 case AKEYCODE_SYM:
253 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
254 case AKEYCODE_FUNCTION:
255 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
256 case AKEYCODE_CTRL_LEFT:
257 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
258 case AKEYCODE_CTRL_RIGHT:
259 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
260 case AKEYCODE_META_LEFT:
261 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
262 case AKEYCODE_META_RIGHT:
263 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
264 case AKEYCODE_CAPS_LOCK:
265 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
266 case AKEYCODE_NUM_LOCK:
267 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
268 case AKEYCODE_SCROLL_LOCK:
269 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
270 default:
271 return oldMetaState;
272 }
273}
274
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275bool isMetaKey(int32_t keyCode) {
276 switch (keyCode) {
277 case AKEYCODE_ALT_LEFT:
278 case AKEYCODE_ALT_RIGHT:
279 case AKEYCODE_SHIFT_LEFT:
280 case AKEYCODE_SHIFT_RIGHT:
281 case AKEYCODE_SYM:
282 case AKEYCODE_FUNCTION:
283 case AKEYCODE_CTRL_LEFT:
284 case AKEYCODE_CTRL_RIGHT:
285 case AKEYCODE_META_LEFT:
286 case AKEYCODE_META_RIGHT:
287 case AKEYCODE_CAPS_LOCK:
288 case AKEYCODE_NUM_LOCK:
289 case AKEYCODE_SCROLL_LOCK:
290 return true;
291 default:
292 return false;
293 }
294}
295
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800296
297} // namespace android