1 /*
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>
24 #include <input/InputEventLabels.h>
25 #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
31 namespace android {
32
33 // --- KeyMap ---
34
KeyMap()35 KeyMap::KeyMap() {
36 }
37
~KeyMap()38 KeyMap::~KeyMap() {
39 }
40
load(const InputDeviceIdentifier & deviceIdentifier,const PropertyMap * deviceConfiguration)41 status_t KeyMap::load(const InputDeviceIdentifier& deviceIdentifier,
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)) {
48 status_t status = loadKeyLayout(deviceIdentifier, keyLayoutName.c_str());
49 if (status == NAME_NOT_FOUND) {
50 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
51 "it was not found.",
52 deviceIdentifier.name.c_str(), keyLayoutName.string());
53 }
54 }
55
56 String8 keyCharacterMapName;
57 if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
58 keyCharacterMapName)) {
59 status_t status = loadKeyCharacterMap(deviceIdentifier, keyCharacterMapName.c_str());
60 if (status == NAME_NOT_FOUND) {
61 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
62 "map '%s' but it was not found.",
63 deviceIdentifier.name.c_str(), keyCharacterMapName.string());
64 }
65 }
66
67 if (isComplete()) {
68 return OK;
69 }
70 }
71
72 // Try searching by device identifier.
73 if (probeKeyMap(deviceIdentifier, "")) {
74 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.
80 if (probeKeyMap(deviceIdentifier, "Generic")) {
81 return OK;
82 }
83
84 // Try the Virtual key map as a last resort.
85 if (probeKeyMap(deviceIdentifier, "Virtual")) {
86 return OK;
87 }
88
89 // Give up!
90 ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
91 deviceIdentifier.name.c_str());
92 return NAME_NOT_FOUND;
93 }
94
probeKeyMap(const InputDeviceIdentifier & deviceIdentifier,const std::string & keyMapName)95 bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
96 const std::string& keyMapName) {
97 if (!haveKeyLayout()) {
98 loadKeyLayout(deviceIdentifier, keyMapName);
99 }
100 if (!haveKeyCharacterMap()) {
101 loadKeyCharacterMap(deviceIdentifier, keyMapName);
102 }
103 return isComplete();
104 }
105
loadKeyLayout(const InputDeviceIdentifier & deviceIdentifier,const std::string & name)106 status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier,
107 const std::string& name) {
108 std::string path(getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_LAYOUT));
109 if (path.empty()) {
110 return NAME_NOT_FOUND;
111 }
112
113 base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path);
114 if (!ret.ok()) {
115 return ret.error().code();
116 }
117 keyLayoutMap = *ret;
118 keyLayoutFile = path;
119 return OK;
120 }
121
loadKeyCharacterMap(const InputDeviceIdentifier & deviceIdentifier,const std::string & name)122 status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
123 const std::string& name) {
124 std::string path =
125 getPath(deviceIdentifier, name, InputDeviceConfigurationFileType::KEY_CHARACTER_MAP);
126 if (path.empty()) {
127 return NAME_NOT_FOUND;
128 }
129
130 base::Result<std::shared_ptr<KeyCharacterMap>> ret =
131 KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE);
132 if (!ret.ok()) {
133 return ret.error().code();
134 }
135 keyCharacterMap = *ret;
136 keyCharacterMapFile = path;
137 return OK;
138 }
139
getPath(const InputDeviceIdentifier & deviceIdentifier,const std::string & name,InputDeviceConfigurationFileType type)140 std::string KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
141 const std::string& name, InputDeviceConfigurationFileType type) {
142 return name.empty()
143 ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
144 : getInputDeviceConfigurationFilePathByName(name, type);
145 }
146
147
148 // --- Global functions ---
149
isKeyboardSpecialFunction(const PropertyMap * config)150 bool 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
isEligibleBuiltInKeyboard(const InputDeviceIdentifier & deviceIdentifier,const PropertyMap * deviceConfiguration,const KeyMap * keyMap)159 bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
160 const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
161 // TODO: remove the third OR statement (SPECIAL_FUNCTION) in Q
162 if (!keyMap->haveKeyCharacterMap() || isKeyboardSpecialFunction(deviceConfiguration) ||
163 keyMap->keyCharacterMap->getKeyboardType() ==
164 KeyCharacterMap::KeyboardType::SPECIAL_FUNCTION) {
165 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
176 return strstr(deviceIdentifier.name.c_str(), "-keypad");
177 }
178
setEphemeralMetaState(int32_t mask,bool down,int32_t oldMetaState)179 static 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
188 return normalizeMetaState(newMetaState);
189 }
190
normalizeMetaState(int32_t oldMetaState)191 int32_t normalizeMetaState(int32_t oldMetaState) {
192 int32_t newMetaState = oldMetaState;
193 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
toggleLockedMetaState(int32_t mask,bool down,int32_t oldMetaState)211 static 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
updateMetaState(int32_t keyCode,bool down,int32_t oldMetaState)219 int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
220 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
isMetaKey(int32_t keyCode)252 bool 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
275