blob: 3c1f2b6b5610059e0763850da1f4a014b44d489f [file] [log] [blame]
Zixuan Qudd0635d2023-02-06 04:52:38 +00001/*
2 * Copyright (C) 2023 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 "VirtualInputDevice"
18
19#include <android/input.h>
20#include <android/keycodes.h>
21#include <fcntl.h>
22#include <input/Input.h>
23#include <input/VirtualInputDevice.h>
24#include <linux/uinput.h>
25#include <math.h>
26#include <utils/Log.h>
27
28#include <map>
29#include <string>
30
31using android::base::unique_fd;
32
33/**
34 * Log debug messages about native virtual input devices.
35 * Enable this via "adb shell setprop log.tag.VirtualInputDevice DEBUG"
36 */
37static bool isDebug() {
38 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
39}
40
41namespace android {
42VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {}
43VirtualInputDevice::~VirtualInputDevice() {
44 ioctl(mFd, UI_DEV_DESTROY);
45}
46
47bool VirtualInputDevice::writeInputEvent(uint16_t type, uint16_t code, int32_t value) {
48 struct input_event ev = {.type = type, .code = code, .value = value};
49 return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
50}
51
52/** Utility method to write keyboard key events or mouse button events. */
53bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
54 const std::map<int, int>& evKeyCodeMapping,
55 const std::map<int, UinputAction>& actionMapping) {
56 auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode);
57 if (evKeyCodeIterator == evKeyCodeMapping.end()) {
58 ALOGE("Unsupported native EV keycode for android code %d", androidCode);
59 return false;
60 }
61 auto actionIterator = actionMapping.find(androidAction);
62 if (actionIterator == actionMapping.end()) {
63 return false;
64 }
65 if (!writeInputEvent(EV_KEY, static_cast<uint16_t>(evKeyCodeIterator->second),
66 static_cast<int32_t>(actionIterator->second))) {
67 return false;
68 }
69 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0)) {
70 return false;
71 }
72 return true;
73}
74
75// --- VirtualKeyboard ---
76const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
77 {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
78 {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
79};
80// Keycode mapping from https://source.android.com/devices/input/keyboard-devices
81const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = {
82 {AKEYCODE_0, KEY_0},
83 {AKEYCODE_1, KEY_1},
84 {AKEYCODE_2, KEY_2},
85 {AKEYCODE_3, KEY_3},
86 {AKEYCODE_4, KEY_4},
87 {AKEYCODE_5, KEY_5},
88 {AKEYCODE_6, KEY_6},
89 {AKEYCODE_7, KEY_7},
90 {AKEYCODE_8, KEY_8},
91 {AKEYCODE_9, KEY_9},
92 {AKEYCODE_A, KEY_A},
93 {AKEYCODE_B, KEY_B},
94 {AKEYCODE_C, KEY_C},
95 {AKEYCODE_D, KEY_D},
96 {AKEYCODE_E, KEY_E},
97 {AKEYCODE_F, KEY_F},
98 {AKEYCODE_G, KEY_G},
99 {AKEYCODE_H, KEY_H},
100 {AKEYCODE_I, KEY_I},
101 {AKEYCODE_J, KEY_J},
102 {AKEYCODE_K, KEY_K},
103 {AKEYCODE_L, KEY_L},
104 {AKEYCODE_M, KEY_M},
105 {AKEYCODE_N, KEY_N},
106 {AKEYCODE_O, KEY_O},
107 {AKEYCODE_P, KEY_P},
108 {AKEYCODE_Q, KEY_Q},
109 {AKEYCODE_R, KEY_R},
110 {AKEYCODE_S, KEY_S},
111 {AKEYCODE_T, KEY_T},
112 {AKEYCODE_U, KEY_U},
113 {AKEYCODE_V, KEY_V},
114 {AKEYCODE_W, KEY_W},
115 {AKEYCODE_X, KEY_X},
116 {AKEYCODE_Y, KEY_Y},
117 {AKEYCODE_Z, KEY_Z},
118 {AKEYCODE_GRAVE, KEY_GRAVE},
119 {AKEYCODE_MINUS, KEY_MINUS},
120 {AKEYCODE_EQUALS, KEY_EQUAL},
121 {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE},
122 {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE},
123 {AKEYCODE_BACKSLASH, KEY_BACKSLASH},
124 {AKEYCODE_SEMICOLON, KEY_SEMICOLON},
125 {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE},
126 {AKEYCODE_COMMA, KEY_COMMA},
127 {AKEYCODE_PERIOD, KEY_DOT},
128 {AKEYCODE_SLASH, KEY_SLASH},
129 {AKEYCODE_ALT_LEFT, KEY_LEFTALT},
130 {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT},
131 {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL},
132 {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL},
133 {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT},
134 {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT},
135 {AKEYCODE_META_LEFT, KEY_LEFTMETA},
136 {AKEYCODE_META_RIGHT, KEY_RIGHTMETA},
137 {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK},
138 {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK},
139 {AKEYCODE_NUM_LOCK, KEY_NUMLOCK},
140 {AKEYCODE_ENTER, KEY_ENTER},
141 {AKEYCODE_TAB, KEY_TAB},
142 {AKEYCODE_SPACE, KEY_SPACE},
143 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
144 {AKEYCODE_DPAD_UP, KEY_UP},
145 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
146 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
147 {AKEYCODE_MOVE_END, KEY_END},
148 {AKEYCODE_MOVE_HOME, KEY_HOME},
149 {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN},
150 {AKEYCODE_PAGE_UP, KEY_PAGEUP},
151 {AKEYCODE_DEL, KEY_BACKSPACE},
152 {AKEYCODE_FORWARD_DEL, KEY_DELETE},
153 {AKEYCODE_INSERT, KEY_INSERT},
154 {AKEYCODE_ESCAPE, KEY_ESC},
155 {AKEYCODE_BREAK, KEY_PAUSE},
156 {AKEYCODE_F1, KEY_F1},
157 {AKEYCODE_F2, KEY_F2},
158 {AKEYCODE_F3, KEY_F3},
159 {AKEYCODE_F4, KEY_F4},
160 {AKEYCODE_F5, KEY_F5},
161 {AKEYCODE_F6, KEY_F6},
162 {AKEYCODE_F7, KEY_F7},
163 {AKEYCODE_F8, KEY_F8},
164 {AKEYCODE_F9, KEY_F9},
165 {AKEYCODE_F10, KEY_F10},
166 {AKEYCODE_F11, KEY_F11},
167 {AKEYCODE_F12, KEY_F12},
168 {AKEYCODE_BACK, KEY_BACK},
169 {AKEYCODE_FORWARD, KEY_FORWARD},
170 {AKEYCODE_NUMPAD_1, KEY_KP1},
171 {AKEYCODE_NUMPAD_2, KEY_KP2},
172 {AKEYCODE_NUMPAD_3, KEY_KP3},
173 {AKEYCODE_NUMPAD_4, KEY_KP4},
174 {AKEYCODE_NUMPAD_5, KEY_KP5},
175 {AKEYCODE_NUMPAD_6, KEY_KP6},
176 {AKEYCODE_NUMPAD_7, KEY_KP7},
177 {AKEYCODE_NUMPAD_8, KEY_KP8},
178 {AKEYCODE_NUMPAD_9, KEY_KP9},
179 {AKEYCODE_NUMPAD_0, KEY_KP0},
180 {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS},
181 {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS},
182 {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK},
183 {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH},
184 {AKEYCODE_NUMPAD_DOT, KEY_KPDOT},
185 {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER},
186 {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL},
187 {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA},
188};
189VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
190VirtualKeyboard::~VirtualKeyboard() {}
191
192bool VirtualKeyboard::writeKeyEvent(int32_t androidKeyCode, int32_t androidAction) {
193 return writeEvKeyEvent(androidKeyCode, androidAction, KEY_CODE_MAPPING, KEY_ACTION_MAPPING);
194}
195
196// --- VirtualDpad ---
197// Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
198const std::map<int, int> VirtualDpad::DPAD_KEY_CODE_MAPPING = {
199 // clang-format off
200 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
201 {AKEYCODE_DPAD_UP, KEY_UP},
202 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
203 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
204 {AKEYCODE_DPAD_CENTER, KEY_SELECT},
205 {AKEYCODE_BACK, KEY_BACK},
206 // clang-format on
207};
208
209VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
210
211VirtualDpad::~VirtualDpad() {}
212
213bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction) {
214 return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
215 VirtualKeyboard::KEY_ACTION_MAPPING);
216}
217
218// --- VirtualMouse ---
219const std::map<int, UinputAction> VirtualMouse::BUTTON_ACTION_MAPPING = {
220 {AMOTION_EVENT_ACTION_BUTTON_PRESS, UinputAction::PRESS},
221 {AMOTION_EVENT_ACTION_BUTTON_RELEASE, UinputAction::RELEASE},
222};
223
224// Button code mapping from https://source.android.com/devices/input/touch-devices
225const std::map<int, int> VirtualMouse::BUTTON_CODE_MAPPING = {
226 // clang-format off
227 {AMOTION_EVENT_BUTTON_PRIMARY, BTN_LEFT},
228 {AMOTION_EVENT_BUTTON_SECONDARY, BTN_RIGHT},
229 {AMOTION_EVENT_BUTTON_TERTIARY, BTN_MIDDLE},
230 {AMOTION_EVENT_BUTTON_BACK, BTN_BACK},
231 {AMOTION_EVENT_BUTTON_FORWARD, BTN_FORWARD},
232 // clang-format on
233};
234
235VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
236
237VirtualMouse::~VirtualMouse() {}
238
239bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction) {
240 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
241 BUTTON_ACTION_MAPPING);
242}
243
244bool VirtualMouse::writeRelativeEvent(float relativeX, float relativeY) {
245 return writeInputEvent(EV_REL, REL_X, relativeX) && writeInputEvent(EV_REL, REL_Y, relativeY) &&
246 writeInputEvent(EV_SYN, SYN_REPORT, 0);
247}
248
249bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement) {
250 return writeInputEvent(EV_REL, REL_HWHEEL, xAxisMovement) &&
251 writeInputEvent(EV_REL, REL_WHEEL, yAxisMovement) &&
252 writeInputEvent(EV_SYN, SYN_REPORT, 0);
253}
254
255// --- VirtualTouchscreen ---
256const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = {
257 {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS},
258 {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE},
259 {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE},
260 {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL},
261};
262// Tool type mapping from https://source.android.com/devices/input/touch-devices
263const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = {
264 {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER},
265 {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM},
266};
267
268VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
269
270VirtualTouchscreen::~VirtualTouchscreen() {}
271
272bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) {
273 if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) {
274 ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu",
275 pointerId, MAX_POINTERS - 0);
276 return false;
277 }
278
279 if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) {
280 ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.",
281 pointerId);
282 return false;
283 }
284 if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) {
285 ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.",
286 pointerId, mFd.get());
287 return false;
288 }
289 return true;
290}
291
292bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
293 float locationX, float locationY, float pressure,
294 float majorAxisSize) {
295 auto actionIterator = TOUCH_ACTION_MAPPING.find(action);
296 if (actionIterator == TOUCH_ACTION_MAPPING.end()) {
297 return false;
298 }
299 UinputAction uinputAction = actionIterator->second;
300 if (!isValidPointerId(pointerId, uinputAction)) {
301 return false;
302 }
303 if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId)) {
304 return false;
305 }
306 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
307 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
308 return false;
309 }
310 if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE,
311 static_cast<int32_t>(toolTypeIterator->second))) {
312 return false;
313 }
314 if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId)) {
315 return false;
316 }
317 if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId)) {
318 return false;
319 }
320 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX)) {
321 return false;
322 }
323 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY)) {
324 return false;
325 }
326 if (!isnan(pressure)) {
327 if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure)) {
328 return false;
329 }
330 }
331 if (!isnan(majorAxisSize)) {
332 if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize)) {
333 return false;
334 }
335 }
336 return writeInputEvent(EV_SYN, SYN_REPORT, 0);
337}
338
339bool VirtualTouchscreen::handleTouchUp(int32_t pointerId) {
340 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1))) {
341 return false;
342 }
343 // When a pointer is no longer in touch, remove the pointer id from the corresponding
344 // entry in the unreleased touches map.
345 mActivePointers.reset(pointerId);
346 ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get());
347
348 // Only sends the BTN UP event when there's no pointers on the touchscreen.
349 if (mActivePointers.none()) {
350 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE))) {
351 return false;
352 }
353 ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
354 }
355 return true;
356}
357
358bool VirtualTouchscreen::handleTouchDown(int32_t pointerId) {
359 // When a new pointer is down on the touchscreen, add the pointer id in the corresponding
360 // entry in the unreleased touches map.
361 if (mActivePointers.none()) {
362 // Only sends the BTN Down event when the first pointer on the touchscreen is down.
363 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS))) {
364 return false;
365 }
366 ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent",
367 pointerId, mFd.get());
368 }
369
370 mActivePointers.set(pointerId);
371 ALOGD_IF(isDebug(), "Added pointer %d under touchscreen %d in the map", pointerId, mFd.get());
372 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId))) {
373 return false;
374 }
375 return true;
376}
377
378} // namespace android