blob: af9ce3a38fe99f633d9e14a3881ce5183a93996f [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
Biswarup Pale9fe2df2023-04-05 18:20:54 +000047bool VirtualInputDevice::writeInputEvent(uint16_t type, uint16_t code, int32_t value,
48 std::chrono::nanoseconds eventTime) {
49 std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(eventTime);
50 std::chrono::microseconds microseconds =
51 std::chrono::duration_cast<std::chrono::microseconds>(eventTime - seconds);
52 struct input_event ev = {.type = type,
53 .code = code,
54 .value = value,
55 .input_event_sec = static_cast<time_t>(seconds.count()),
56 .input_event_usec = static_cast<suseconds_t>(microseconds.count())};
Zixuan Qudd0635d2023-02-06 04:52:38 +000057 return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
58}
59
60/** Utility method to write keyboard key events or mouse button events. */
61bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
62 const std::map<int, int>& evKeyCodeMapping,
Biswarup Pale9fe2df2023-04-05 18:20:54 +000063 const std::map<int, UinputAction>& actionMapping,
64 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +000065 auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode);
66 if (evKeyCodeIterator == evKeyCodeMapping.end()) {
67 ALOGE("Unsupported native EV keycode for android code %d", androidCode);
68 return false;
69 }
70 auto actionIterator = actionMapping.find(androidAction);
71 if (actionIterator == actionMapping.end()) {
72 return false;
73 }
74 if (!writeInputEvent(EV_KEY, static_cast<uint16_t>(evKeyCodeIterator->second),
Biswarup Pale9fe2df2023-04-05 18:20:54 +000075 static_cast<int32_t>(actionIterator->second), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +000076 return false;
77 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +000078 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +000079 return false;
80 }
81 return true;
82}
83
84// --- VirtualKeyboard ---
85const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
86 {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
87 {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
88};
89// Keycode mapping from https://source.android.com/devices/input/keyboard-devices
90const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = {
91 {AKEYCODE_0, KEY_0},
92 {AKEYCODE_1, KEY_1},
93 {AKEYCODE_2, KEY_2},
94 {AKEYCODE_3, KEY_3},
95 {AKEYCODE_4, KEY_4},
96 {AKEYCODE_5, KEY_5},
97 {AKEYCODE_6, KEY_6},
98 {AKEYCODE_7, KEY_7},
99 {AKEYCODE_8, KEY_8},
100 {AKEYCODE_9, KEY_9},
101 {AKEYCODE_A, KEY_A},
102 {AKEYCODE_B, KEY_B},
103 {AKEYCODE_C, KEY_C},
104 {AKEYCODE_D, KEY_D},
105 {AKEYCODE_E, KEY_E},
106 {AKEYCODE_F, KEY_F},
107 {AKEYCODE_G, KEY_G},
108 {AKEYCODE_H, KEY_H},
109 {AKEYCODE_I, KEY_I},
110 {AKEYCODE_J, KEY_J},
111 {AKEYCODE_K, KEY_K},
112 {AKEYCODE_L, KEY_L},
113 {AKEYCODE_M, KEY_M},
114 {AKEYCODE_N, KEY_N},
115 {AKEYCODE_O, KEY_O},
116 {AKEYCODE_P, KEY_P},
117 {AKEYCODE_Q, KEY_Q},
118 {AKEYCODE_R, KEY_R},
119 {AKEYCODE_S, KEY_S},
120 {AKEYCODE_T, KEY_T},
121 {AKEYCODE_U, KEY_U},
122 {AKEYCODE_V, KEY_V},
123 {AKEYCODE_W, KEY_W},
124 {AKEYCODE_X, KEY_X},
125 {AKEYCODE_Y, KEY_Y},
126 {AKEYCODE_Z, KEY_Z},
127 {AKEYCODE_GRAVE, KEY_GRAVE},
128 {AKEYCODE_MINUS, KEY_MINUS},
129 {AKEYCODE_EQUALS, KEY_EQUAL},
130 {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE},
131 {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE},
132 {AKEYCODE_BACKSLASH, KEY_BACKSLASH},
133 {AKEYCODE_SEMICOLON, KEY_SEMICOLON},
134 {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE},
135 {AKEYCODE_COMMA, KEY_COMMA},
136 {AKEYCODE_PERIOD, KEY_DOT},
137 {AKEYCODE_SLASH, KEY_SLASH},
138 {AKEYCODE_ALT_LEFT, KEY_LEFTALT},
139 {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT},
140 {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL},
141 {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL},
142 {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT},
143 {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT},
144 {AKEYCODE_META_LEFT, KEY_LEFTMETA},
145 {AKEYCODE_META_RIGHT, KEY_RIGHTMETA},
146 {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK},
147 {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK},
148 {AKEYCODE_NUM_LOCK, KEY_NUMLOCK},
149 {AKEYCODE_ENTER, KEY_ENTER},
150 {AKEYCODE_TAB, KEY_TAB},
151 {AKEYCODE_SPACE, KEY_SPACE},
152 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
153 {AKEYCODE_DPAD_UP, KEY_UP},
154 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
155 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
156 {AKEYCODE_MOVE_END, KEY_END},
157 {AKEYCODE_MOVE_HOME, KEY_HOME},
158 {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN},
159 {AKEYCODE_PAGE_UP, KEY_PAGEUP},
160 {AKEYCODE_DEL, KEY_BACKSPACE},
161 {AKEYCODE_FORWARD_DEL, KEY_DELETE},
162 {AKEYCODE_INSERT, KEY_INSERT},
163 {AKEYCODE_ESCAPE, KEY_ESC},
164 {AKEYCODE_BREAK, KEY_PAUSE},
165 {AKEYCODE_F1, KEY_F1},
166 {AKEYCODE_F2, KEY_F2},
167 {AKEYCODE_F3, KEY_F3},
168 {AKEYCODE_F4, KEY_F4},
169 {AKEYCODE_F5, KEY_F5},
170 {AKEYCODE_F6, KEY_F6},
171 {AKEYCODE_F7, KEY_F7},
172 {AKEYCODE_F8, KEY_F8},
173 {AKEYCODE_F9, KEY_F9},
174 {AKEYCODE_F10, KEY_F10},
175 {AKEYCODE_F11, KEY_F11},
176 {AKEYCODE_F12, KEY_F12},
177 {AKEYCODE_BACK, KEY_BACK},
178 {AKEYCODE_FORWARD, KEY_FORWARD},
179 {AKEYCODE_NUMPAD_1, KEY_KP1},
180 {AKEYCODE_NUMPAD_2, KEY_KP2},
181 {AKEYCODE_NUMPAD_3, KEY_KP3},
182 {AKEYCODE_NUMPAD_4, KEY_KP4},
183 {AKEYCODE_NUMPAD_5, KEY_KP5},
184 {AKEYCODE_NUMPAD_6, KEY_KP6},
185 {AKEYCODE_NUMPAD_7, KEY_KP7},
186 {AKEYCODE_NUMPAD_8, KEY_KP8},
187 {AKEYCODE_NUMPAD_9, KEY_KP9},
188 {AKEYCODE_NUMPAD_0, KEY_KP0},
189 {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS},
190 {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS},
191 {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK},
192 {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH},
193 {AKEYCODE_NUMPAD_DOT, KEY_KPDOT},
194 {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER},
195 {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL},
196 {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA},
197};
198VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
199VirtualKeyboard::~VirtualKeyboard() {}
200
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000201bool VirtualKeyboard::writeKeyEvent(int32_t androidKeyCode, int32_t androidAction,
202 std::chrono::nanoseconds eventTime) {
203 return writeEvKeyEvent(androidKeyCode, androidAction, KEY_CODE_MAPPING, KEY_ACTION_MAPPING,
204 eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000205}
206
207// --- VirtualDpad ---
208// Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
209const std::map<int, int> VirtualDpad::DPAD_KEY_CODE_MAPPING = {
210 // clang-format off
211 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
212 {AKEYCODE_DPAD_UP, KEY_UP},
213 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
214 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
215 {AKEYCODE_DPAD_CENTER, KEY_SELECT},
216 {AKEYCODE_BACK, KEY_BACK},
217 // clang-format on
218};
219
220VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
221
222VirtualDpad::~VirtualDpad() {}
223
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000224bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
225 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000226 return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000227 VirtualKeyboard::KEY_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000228}
229
230// --- VirtualMouse ---
231const std::map<int, UinputAction> VirtualMouse::BUTTON_ACTION_MAPPING = {
232 {AMOTION_EVENT_ACTION_BUTTON_PRESS, UinputAction::PRESS},
233 {AMOTION_EVENT_ACTION_BUTTON_RELEASE, UinputAction::RELEASE},
234};
235
236// Button code mapping from https://source.android.com/devices/input/touch-devices
237const std::map<int, int> VirtualMouse::BUTTON_CODE_MAPPING = {
238 // clang-format off
239 {AMOTION_EVENT_BUTTON_PRIMARY, BTN_LEFT},
240 {AMOTION_EVENT_BUTTON_SECONDARY, BTN_RIGHT},
241 {AMOTION_EVENT_BUTTON_TERTIARY, BTN_MIDDLE},
242 {AMOTION_EVENT_BUTTON_BACK, BTN_BACK},
243 {AMOTION_EVENT_BUTTON_FORWARD, BTN_FORWARD},
244 // clang-format on
245};
246
247VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
248
249VirtualMouse::~VirtualMouse() {}
250
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000251bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
252 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000253 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000254 BUTTON_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000255}
256
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000257bool VirtualMouse::writeRelativeEvent(float relativeX, float relativeY,
258 std::chrono::nanoseconds eventTime) {
259 return writeInputEvent(EV_REL, REL_X, relativeX, eventTime) &&
260 writeInputEvent(EV_REL, REL_Y, relativeY, eventTime) &&
261 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000262}
263
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000264bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement,
265 std::chrono::nanoseconds eventTime) {
266 return writeInputEvent(EV_REL, REL_HWHEEL, xAxisMovement, eventTime) &&
267 writeInputEvent(EV_REL, REL_WHEEL, yAxisMovement, eventTime) &&
268 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000269}
270
271// --- VirtualTouchscreen ---
272const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = {
273 {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS},
274 {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE},
275 {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE},
276 {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL},
277};
278// Tool type mapping from https://source.android.com/devices/input/touch-devices
279const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = {
280 {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER},
281 {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM},
282};
283
284VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
285
286VirtualTouchscreen::~VirtualTouchscreen() {}
287
288bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) {
289 if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) {
290 ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu",
291 pointerId, MAX_POINTERS - 0);
292 return false;
293 }
294
295 if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) {
296 ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.",
297 pointerId);
298 return false;
299 }
300 if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) {
301 ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.",
302 pointerId, mFd.get());
303 return false;
304 }
305 return true;
306}
307
308bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
309 float locationX, float locationY, float pressure,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000310 float majorAxisSize, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000311 auto actionIterator = TOUCH_ACTION_MAPPING.find(action);
312 if (actionIterator == TOUCH_ACTION_MAPPING.end()) {
313 return false;
314 }
315 UinputAction uinputAction = actionIterator->second;
316 if (!isValidPointerId(pointerId, uinputAction)) {
317 return false;
318 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000319 if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000320 return false;
321 }
322 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
323 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
324 return false;
325 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000326 if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second),
327 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000328 return false;
329 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000330 if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000331 return false;
332 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000333 if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000334 return false;
335 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000336 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000337 return false;
338 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000339 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000340 return false;
341 }
342 if (!isnan(pressure)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000343 if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000344 return false;
345 }
346 }
347 if (!isnan(majorAxisSize)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000348 if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000349 return false;
350 }
351 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000352 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000353}
354
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000355bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) {
356 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000357 return false;
358 }
359 // When a pointer is no longer in touch, remove the pointer id from the corresponding
360 // entry in the unreleased touches map.
361 mActivePointers.reset(pointerId);
362 ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get());
363
364 // Only sends the BTN UP event when there's no pointers on the touchscreen.
365 if (mActivePointers.none()) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000366 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
367 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000368 return false;
369 }
370 ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
371 }
372 return true;
373}
374
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000375bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000376 // When a new pointer is down on the touchscreen, add the pointer id in the corresponding
377 // entry in the unreleased touches map.
378 if (mActivePointers.none()) {
379 // Only sends the BTN Down event when the first pointer on the touchscreen is down.
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000380 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS),
381 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000382 return false;
383 }
384 ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent",
385 pointerId, mFd.get());
386 }
387
388 mActivePointers.set(pointerId);
389 ALOGD_IF(isDebug(), "Added pointer %d under touchscreen %d in the map", pointerId, mFd.get());
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000390 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000391 return false;
392 }
393 return true;
394}
395
396} // namespace android