blob: 0579967698c1607114c1595fe4f18c2f105feb26 [file] [log] [blame]
Zixuan Qudd0635d2023-02-06 04:52:38 +00001/*
Biswarup Pal590eb732023-11-30 17:59:57 +00002 * Copyright 2023 The Android Open Source Project
Zixuan Qudd0635d2023-02-06 04:52:38 +00003 *
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>
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +000021#include <android_companion_virtualdevice_flags.h>
Zixuan Qudd0635d2023-02-06 04:52:38 +000022#include <fcntl.h>
23#include <input/Input.h>
24#include <input/VirtualInputDevice.h>
25#include <linux/uinput.h>
26#include <math.h>
27#include <utils/Log.h>
28
29#include <map>
30#include <string>
31
32using android::base::unique_fd;
33
34/**
35 * Log debug messages about native virtual input devices.
36 * Enable this via "adb shell setprop log.tag.VirtualInputDevice DEBUG"
37 */
38static bool isDebug() {
39 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
40}
41
42namespace android {
Biswarup Pal590eb732023-11-30 17:59:57 +000043
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +000044namespace vd_flags = android::companion::virtualdevice::flags;
45
Zixuan Qudd0635d2023-02-06 04:52:38 +000046VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {}
Biswarup Pal590eb732023-11-30 17:59:57 +000047
Zixuan Qudd0635d2023-02-06 04:52:38 +000048VirtualInputDevice::~VirtualInputDevice() {
49 ioctl(mFd, UI_DEV_DESTROY);
50}
51
Biswarup Pale9fe2df2023-04-05 18:20:54 +000052bool VirtualInputDevice::writeInputEvent(uint16_t type, uint16_t code, int32_t value,
53 std::chrono::nanoseconds eventTime) {
54 std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(eventTime);
55 std::chrono::microseconds microseconds =
56 std::chrono::duration_cast<std::chrono::microseconds>(eventTime - seconds);
Colin Cross983cae02023-05-09 22:45:35 -070057 struct input_event ev = {.type = type, .code = code, .value = value};
58 ev.input_event_sec = static_cast<decltype(ev.input_event_sec)>(seconds.count());
59 ev.input_event_usec = static_cast<decltype(ev.input_event_usec)>(microseconds.count());
60
Zixuan Qudd0635d2023-02-06 04:52:38 +000061 return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
62}
63
Biswarup Pal590eb732023-11-30 17:59:57 +000064/** Utility method to write keyboard key events or mouse/stylus button events. */
Zixuan Qudd0635d2023-02-06 04:52:38 +000065bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
66 const std::map<int, int>& evKeyCodeMapping,
Biswarup Pale9fe2df2023-04-05 18:20:54 +000067 const std::map<int, UinputAction>& actionMapping,
68 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +000069 auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode);
70 if (evKeyCodeIterator == evKeyCodeMapping.end()) {
71 ALOGE("Unsupported native EV keycode for android code %d", androidCode);
72 return false;
73 }
74 auto actionIterator = actionMapping.find(androidAction);
75 if (actionIterator == actionMapping.end()) {
Biswarup Pal590eb732023-11-30 17:59:57 +000076 ALOGE("Unsupported native action for android action %d", androidAction);
Zixuan Qudd0635d2023-02-06 04:52:38 +000077 return false;
78 }
Biswarup Pal590eb732023-11-30 17:59:57 +000079 int32_t action = static_cast<int32_t>(actionIterator->second);
80 uint16_t evKeyCode = static_cast<uint16_t>(evKeyCodeIterator->second);
81 if (!writeInputEvent(EV_KEY, evKeyCode, action, eventTime)) {
82 ALOGE("Failed to write native action %d and EV keycode %u.", action, evKeyCode);
Zixuan Qudd0635d2023-02-06 04:52:38 +000083 return false;
84 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +000085 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
Biswarup Pal590eb732023-11-30 17:59:57 +000086 ALOGE("Failed to write SYN_REPORT for EV_KEY event.");
Zixuan Qudd0635d2023-02-06 04:52:38 +000087 return false;
88 }
89 return true;
90}
91
92// --- VirtualKeyboard ---
93const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
94 {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
95 {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
96};
Biswarup Pal590eb732023-11-30 17:59:57 +000097
Zixuan Qudd0635d2023-02-06 04:52:38 +000098// Keycode mapping from https://source.android.com/devices/input/keyboard-devices
99const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = {
100 {AKEYCODE_0, KEY_0},
101 {AKEYCODE_1, KEY_1},
102 {AKEYCODE_2, KEY_2},
103 {AKEYCODE_3, KEY_3},
104 {AKEYCODE_4, KEY_4},
105 {AKEYCODE_5, KEY_5},
106 {AKEYCODE_6, KEY_6},
107 {AKEYCODE_7, KEY_7},
108 {AKEYCODE_8, KEY_8},
109 {AKEYCODE_9, KEY_9},
110 {AKEYCODE_A, KEY_A},
111 {AKEYCODE_B, KEY_B},
112 {AKEYCODE_C, KEY_C},
113 {AKEYCODE_D, KEY_D},
114 {AKEYCODE_E, KEY_E},
115 {AKEYCODE_F, KEY_F},
116 {AKEYCODE_G, KEY_G},
117 {AKEYCODE_H, KEY_H},
118 {AKEYCODE_I, KEY_I},
119 {AKEYCODE_J, KEY_J},
120 {AKEYCODE_K, KEY_K},
121 {AKEYCODE_L, KEY_L},
122 {AKEYCODE_M, KEY_M},
123 {AKEYCODE_N, KEY_N},
124 {AKEYCODE_O, KEY_O},
125 {AKEYCODE_P, KEY_P},
126 {AKEYCODE_Q, KEY_Q},
127 {AKEYCODE_R, KEY_R},
128 {AKEYCODE_S, KEY_S},
129 {AKEYCODE_T, KEY_T},
130 {AKEYCODE_U, KEY_U},
131 {AKEYCODE_V, KEY_V},
132 {AKEYCODE_W, KEY_W},
133 {AKEYCODE_X, KEY_X},
134 {AKEYCODE_Y, KEY_Y},
135 {AKEYCODE_Z, KEY_Z},
136 {AKEYCODE_GRAVE, KEY_GRAVE},
137 {AKEYCODE_MINUS, KEY_MINUS},
138 {AKEYCODE_EQUALS, KEY_EQUAL},
139 {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE},
140 {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE},
141 {AKEYCODE_BACKSLASH, KEY_BACKSLASH},
142 {AKEYCODE_SEMICOLON, KEY_SEMICOLON},
143 {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE},
144 {AKEYCODE_COMMA, KEY_COMMA},
145 {AKEYCODE_PERIOD, KEY_DOT},
146 {AKEYCODE_SLASH, KEY_SLASH},
147 {AKEYCODE_ALT_LEFT, KEY_LEFTALT},
148 {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT},
149 {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL},
150 {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL},
151 {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT},
152 {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT},
153 {AKEYCODE_META_LEFT, KEY_LEFTMETA},
154 {AKEYCODE_META_RIGHT, KEY_RIGHTMETA},
155 {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK},
156 {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK},
157 {AKEYCODE_NUM_LOCK, KEY_NUMLOCK},
158 {AKEYCODE_ENTER, KEY_ENTER},
159 {AKEYCODE_TAB, KEY_TAB},
160 {AKEYCODE_SPACE, KEY_SPACE},
161 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
162 {AKEYCODE_DPAD_UP, KEY_UP},
163 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
164 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
165 {AKEYCODE_MOVE_END, KEY_END},
166 {AKEYCODE_MOVE_HOME, KEY_HOME},
167 {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN},
168 {AKEYCODE_PAGE_UP, KEY_PAGEUP},
169 {AKEYCODE_DEL, KEY_BACKSPACE},
170 {AKEYCODE_FORWARD_DEL, KEY_DELETE},
171 {AKEYCODE_INSERT, KEY_INSERT},
172 {AKEYCODE_ESCAPE, KEY_ESC},
173 {AKEYCODE_BREAK, KEY_PAUSE},
174 {AKEYCODE_F1, KEY_F1},
175 {AKEYCODE_F2, KEY_F2},
176 {AKEYCODE_F3, KEY_F3},
177 {AKEYCODE_F4, KEY_F4},
178 {AKEYCODE_F5, KEY_F5},
179 {AKEYCODE_F6, KEY_F6},
180 {AKEYCODE_F7, KEY_F7},
181 {AKEYCODE_F8, KEY_F8},
182 {AKEYCODE_F9, KEY_F9},
183 {AKEYCODE_F10, KEY_F10},
184 {AKEYCODE_F11, KEY_F11},
185 {AKEYCODE_F12, KEY_F12},
186 {AKEYCODE_BACK, KEY_BACK},
187 {AKEYCODE_FORWARD, KEY_FORWARD},
188 {AKEYCODE_NUMPAD_1, KEY_KP1},
189 {AKEYCODE_NUMPAD_2, KEY_KP2},
190 {AKEYCODE_NUMPAD_3, KEY_KP3},
191 {AKEYCODE_NUMPAD_4, KEY_KP4},
192 {AKEYCODE_NUMPAD_5, KEY_KP5},
193 {AKEYCODE_NUMPAD_6, KEY_KP6},
194 {AKEYCODE_NUMPAD_7, KEY_KP7},
195 {AKEYCODE_NUMPAD_8, KEY_KP8},
196 {AKEYCODE_NUMPAD_9, KEY_KP9},
197 {AKEYCODE_NUMPAD_0, KEY_KP0},
198 {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS},
199 {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS},
200 {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK},
201 {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH},
202 {AKEYCODE_NUMPAD_DOT, KEY_KPDOT},
203 {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER},
204 {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL},
205 {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA},
Vladimir Komsiyski65efbaa2023-11-14 09:30:12 +0100206 {AKEYCODE_LANGUAGE_SWITCH, KEY_LANGUAGE},
Zixuan Qudd0635d2023-02-06 04:52:38 +0000207};
Biswarup Pal590eb732023-11-30 17:59:57 +0000208
Zixuan Qudd0635d2023-02-06 04:52:38 +0000209VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
Biswarup Pal590eb732023-11-30 17:59:57 +0000210
Zixuan Qudd0635d2023-02-06 04:52:38 +0000211VirtualKeyboard::~VirtualKeyboard() {}
212
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000213bool VirtualKeyboard::writeKeyEvent(int32_t androidKeyCode, int32_t androidAction,
214 std::chrono::nanoseconds eventTime) {
215 return writeEvKeyEvent(androidKeyCode, androidAction, KEY_CODE_MAPPING, KEY_ACTION_MAPPING,
216 eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000217}
218
219// --- VirtualDpad ---
220// Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
221const std::map<int, int> VirtualDpad::DPAD_KEY_CODE_MAPPING = {
222 // clang-format off
223 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
224 {AKEYCODE_DPAD_UP, KEY_UP},
225 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
226 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
227 {AKEYCODE_DPAD_CENTER, KEY_SELECT},
228 {AKEYCODE_BACK, KEY_BACK},
229 // clang-format on
230};
231
232VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
233
234VirtualDpad::~VirtualDpad() {}
235
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000236bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
237 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000238 return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000239 VirtualKeyboard::KEY_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000240}
241
242// --- VirtualMouse ---
243const std::map<int, UinputAction> VirtualMouse::BUTTON_ACTION_MAPPING = {
244 {AMOTION_EVENT_ACTION_BUTTON_PRESS, UinputAction::PRESS},
245 {AMOTION_EVENT_ACTION_BUTTON_RELEASE, UinputAction::RELEASE},
246};
247
248// Button code mapping from https://source.android.com/devices/input/touch-devices
249const std::map<int, int> VirtualMouse::BUTTON_CODE_MAPPING = {
250 // clang-format off
251 {AMOTION_EVENT_BUTTON_PRIMARY, BTN_LEFT},
252 {AMOTION_EVENT_BUTTON_SECONDARY, BTN_RIGHT},
253 {AMOTION_EVENT_BUTTON_TERTIARY, BTN_MIDDLE},
254 {AMOTION_EVENT_BUTTON_BACK, BTN_BACK},
255 {AMOTION_EVENT_BUTTON_FORWARD, BTN_FORWARD},
256 // clang-format on
257};
258
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000259VirtualMouse::VirtualMouse(unique_fd fd)
260 : VirtualInputDevice(std::move(fd)),
261 mAccumulatedHighResScrollX(0),
262 mAccumulatedHighResScrollY(0) {}
Zixuan Qudd0635d2023-02-06 04:52:38 +0000263
264VirtualMouse::~VirtualMouse() {}
265
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000266bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
267 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000268 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000269 BUTTON_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000270}
271
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000272bool VirtualMouse::writeRelativeEvent(float relativeX, float relativeY,
273 std::chrono::nanoseconds eventTime) {
274 return writeInputEvent(EV_REL, REL_X, relativeX, eventTime) &&
275 writeInputEvent(EV_REL, REL_Y, relativeY, eventTime) &&
276 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000277}
278
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000279bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement,
280 std::chrono::nanoseconds eventTime) {
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000281 if (!vd_flags::high_resolution_scroll()) {
Biswarup Palba27d1d2024-07-09 19:57:33 +0000282 return writeInputEvent(EV_REL, REL_HWHEEL, static_cast<int32_t>(xAxisMovement),
283 eventTime) &&
284 writeInputEvent(EV_REL, REL_WHEEL, static_cast<int32_t>(yAxisMovement),
285 eventTime) &&
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000286 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
287 }
288
Biswarup Palba27d1d2024-07-09 19:57:33 +0000289 const auto highResScrollX =
290 static_cast<int32_t>(xAxisMovement * kEvdevHighResScrollUnitsPerDetent);
291 const auto highResScrollY =
292 static_cast<int32_t>(yAxisMovement * kEvdevHighResScrollUnitsPerDetent);
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000293 bool highResScrollResult =
294 writeInputEvent(EV_REL, REL_HWHEEL_HI_RES, highResScrollX, eventTime) &&
295 writeInputEvent(EV_REL, REL_WHEEL_HI_RES, highResScrollY, eventTime);
296 if (!highResScrollResult) {
297 return false;
298 }
299
300 // According to evdev spec, a high-resolution mouse needs to emit REL_WHEEL / REL_HWHEEL events
301 // in addition to high-res scroll events. Regular scroll events can approximate high-res scroll
302 // events, so we send a regular scroll event when the accumulated scroll motion reaches a detent
303 // (single mouse wheel click).
304 mAccumulatedHighResScrollX += highResScrollX;
305 mAccumulatedHighResScrollY += highResScrollY;
Biswarup Palba27d1d2024-07-09 19:57:33 +0000306 const int32_t scrollX = mAccumulatedHighResScrollX / kEvdevHighResScrollUnitsPerDetent;
307 const int32_t scrollY = mAccumulatedHighResScrollY / kEvdevHighResScrollUnitsPerDetent;
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000308 if (scrollX != 0) {
309 if (!writeInputEvent(EV_REL, REL_HWHEEL, scrollX, eventTime)) {
310 return false;
311 }
Biswarup Palba27d1d2024-07-09 19:57:33 +0000312 mAccumulatedHighResScrollX %= kEvdevHighResScrollUnitsPerDetent;
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000313 }
314 if (scrollY != 0) {
315 if (!writeInputEvent(EV_REL, REL_WHEEL, scrollY, eventTime)) {
316 return false;
317 }
Biswarup Palba27d1d2024-07-09 19:57:33 +0000318 mAccumulatedHighResScrollY %= kEvdevHighResScrollUnitsPerDetent;
Biswarup Pal8ff5e5e2024-06-15 12:58:20 +0000319 }
320
321 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000322}
323
324// --- VirtualTouchscreen ---
325const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = {
326 {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS},
327 {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE},
328 {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE},
329 {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL},
330};
Biswarup Pal590eb732023-11-30 17:59:57 +0000331
Zixuan Qudd0635d2023-02-06 04:52:38 +0000332// Tool type mapping from https://source.android.com/devices/input/touch-devices
333const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = {
334 {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER},
335 {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM},
336};
337
338VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
339
340VirtualTouchscreen::~VirtualTouchscreen() {}
341
342bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) {
343 if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) {
344 ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu",
345 pointerId, MAX_POINTERS - 0);
346 return false;
347 }
348
349 if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) {
350 ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.",
351 pointerId);
352 return false;
353 }
354 if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) {
355 ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.",
356 pointerId, mFd.get());
357 return false;
358 }
359 return true;
360}
361
362bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
363 float locationX, float locationY, float pressure,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000364 float majorAxisSize, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000365 auto actionIterator = TOUCH_ACTION_MAPPING.find(action);
366 if (actionIterator == TOUCH_ACTION_MAPPING.end()) {
367 return false;
368 }
369 UinputAction uinputAction = actionIterator->second;
370 if (!isValidPointerId(pointerId, uinputAction)) {
371 return false;
372 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000373 if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000374 return false;
375 }
376 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
377 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
378 return false;
379 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000380 if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second),
381 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000382 return false;
383 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000384 if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000385 return false;
386 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000387 if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000388 return false;
389 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000390 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000391 return false;
392 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000393 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000394 return false;
395 }
396 if (!isnan(pressure)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000397 if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000398 return false;
399 }
400 }
401 if (!isnan(majorAxisSize)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000402 if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000403 return false;
404 }
405 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000406 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000407}
408
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000409bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) {
410 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000411 return false;
412 }
413 // When a pointer is no longer in touch, remove the pointer id from the corresponding
414 // entry in the unreleased touches map.
415 mActivePointers.reset(pointerId);
416 ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get());
417
418 // Only sends the BTN UP event when there's no pointers on the touchscreen.
419 if (mActivePointers.none()) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000420 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
421 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000422 return false;
423 }
424 ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
425 }
426 return true;
427}
428
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000429bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000430 // When a new pointer is down on the touchscreen, add the pointer id in the corresponding
431 // entry in the unreleased touches map.
432 if (mActivePointers.none()) {
433 // Only sends the BTN Down event when the first pointer on the touchscreen is down.
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000434 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS),
435 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000436 return false;
437 }
438 ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent",
439 pointerId, mFd.get());
440 }
441
442 mActivePointers.set(pointerId);
443 ALOGD_IF(isDebug(), "Added pointer %d under touchscreen %d in the map", pointerId, mFd.get());
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000444 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000445 return false;
446 }
447 return true;
448}
449
Biswarup Pal590eb732023-11-30 17:59:57 +0000450// --- VirtualStylus ---
451const std::map<int, int> VirtualStylus::TOOL_TYPE_MAPPING = {
452 {AMOTION_EVENT_TOOL_TYPE_STYLUS, BTN_TOOL_PEN},
453 {AMOTION_EVENT_TOOL_TYPE_ERASER, BTN_TOOL_RUBBER},
454};
455
456// Button code mapping from https://source.android.com/devices/input/touch-devices
457const std::map<int, int> VirtualStylus::BUTTON_CODE_MAPPING = {
458 {AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, BTN_STYLUS},
459 {AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, BTN_STYLUS2},
460};
461
462VirtualStylus::VirtualStylus(unique_fd fd)
463 : VirtualInputDevice(std::move(fd)), mIsStylusDown(false) {}
464
465VirtualStylus::~VirtualStylus() {}
466
467bool VirtualStylus::writeMotionEvent(int32_t toolType, int32_t action, int32_t locationX,
468 int32_t locationY, int32_t pressure, int32_t tiltX,
469 int32_t tiltY, std::chrono::nanoseconds eventTime) {
470 auto actionIterator = VirtualTouchscreen::TOUCH_ACTION_MAPPING.find(action);
471 if (actionIterator == VirtualTouchscreen::TOUCH_ACTION_MAPPING.end()) {
472 ALOGE("Unsupported action passed for stylus: %d.", action);
473 return false;
474 }
475 UinputAction uinputAction = actionIterator->second;
476 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
477 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
478 ALOGE("Unsupported tool type passed for stylus: %d.", toolType);
479 return false;
480 }
481 uint16_t tool = static_cast<uint16_t>(toolTypeIterator->second);
482 if (uinputAction == UinputAction::PRESS && !handleStylusDown(tool, eventTime)) {
483 return false;
484 }
485 if (!mIsStylusDown) {
486 ALOGE("Action UP or MOVE received with no prior action DOWN for stylus %d.", mFd.get());
487 return false;
488 }
489 if (uinputAction == UinputAction::RELEASE && !handleStylusUp(tool, eventTime)) {
490 return false;
491 }
492 if (!writeInputEvent(EV_ABS, ABS_X, locationX, eventTime)) {
493 ALOGE("Unsupported x-axis location passed for stylus: %d.", locationX);
494 return false;
495 }
496 if (!writeInputEvent(EV_ABS, ABS_Y, locationY, eventTime)) {
497 ALOGE("Unsupported y-axis location passed for stylus: %d.", locationY);
498 return false;
499 }
500 if (!writeInputEvent(EV_ABS, ABS_TILT_X, tiltX, eventTime)) {
501 ALOGE("Unsupported x-axis tilt passed for stylus: %d.", tiltX);
502 return false;
503 }
504 if (!writeInputEvent(EV_ABS, ABS_TILT_Y, tiltY, eventTime)) {
505 ALOGE("Unsupported y-axis tilt passed for stylus: %d.", tiltY);
506 return false;
507 }
508 if (!writeInputEvent(EV_ABS, ABS_PRESSURE, pressure, eventTime)) {
509 ALOGE("Unsupported pressure passed for stylus: %d.", pressure);
510 return false;
511 }
512 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
513 ALOGE("Failed to write SYN_REPORT for stylus motion event.");
514 return false;
515 }
516 return true;
517}
518
519bool VirtualStylus::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
520 std::chrono::nanoseconds eventTime) {
521 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
522 VirtualMouse::BUTTON_ACTION_MAPPING, eventTime);
523}
524
525bool VirtualStylus::handleStylusDown(uint16_t tool, std::chrono::nanoseconds eventTime) {
526 if (mIsStylusDown) {
527 ALOGE("Repetitive action DOWN event received for a stylus that is already down.");
528 return false;
529 }
530 if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::PRESS), eventTime)) {
531 ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool);
532 return false;
533 }
534 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS), eventTime)) {
535 ALOGE("Failed to write BTN_TOUCH for stylus press.");
536 return false;
537 }
538 mIsStylusDown = true;
539 return true;
540}
541
542bool VirtualStylus::handleStylusUp(uint16_t tool, std::chrono::nanoseconds eventTime) {
543 if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::RELEASE), eventTime)) {
544 ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool);
545 return false;
546 }
547 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
548 eventTime)) {
549 ALOGE("Failed to write BTN_TOUCH for stylus release.");
550 return false;
551 }
552 mIsStylusDown = false;
553 return true;
554}
555
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100556// --- VirtualRotaryEncoder ---
Biswarup Palba27d1d2024-07-09 19:57:33 +0000557VirtualRotaryEncoder::VirtualRotaryEncoder(unique_fd fd)
558 : VirtualInputDevice(std::move(fd)), mAccumulatedHighResScrollAmount(0) {}
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100559
560VirtualRotaryEncoder::~VirtualRotaryEncoder() {}
561
562bool VirtualRotaryEncoder::writeScrollEvent(float scrollAmount,
563 std::chrono::nanoseconds eventTime) {
Biswarup Palba27d1d2024-07-09 19:57:33 +0000564 if (!vd_flags::high_resolution_scroll()) {
565 return writeInputEvent(EV_REL, REL_WHEEL, static_cast<int32_t>(scrollAmount), eventTime) &&
566 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
567 }
568
569 const auto highResScrollAmount =
570 static_cast<int32_t>(scrollAmount * kEvdevHighResScrollUnitsPerDetent);
571 if (!writeInputEvent(EV_REL, REL_WHEEL_HI_RES, highResScrollAmount, eventTime)) {
572 return false;
573 }
574
575 // According to evdev spec, a high-resolution scroll device needs to emit REL_WHEEL / REL_HWHEEL
576 // events in addition to high-res scroll events. Regular scroll events can approximate high-res
577 // scroll events, so we send a regular scroll event when the accumulated scroll motion reaches a
578 // detent (single wheel click).
579 mAccumulatedHighResScrollAmount += highResScrollAmount;
580 const int32_t scroll = mAccumulatedHighResScrollAmount / kEvdevHighResScrollUnitsPerDetent;
581 if (scroll != 0) {
582 if (!writeInputEvent(EV_REL, REL_WHEEL, scroll, eventTime)) {
583 return false;
584 }
585 mAccumulatedHighResScrollAmount %= kEvdevHighResScrollUnitsPerDetent;
586 }
587
588 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100589}
590
Zixuan Qudd0635d2023-02-06 04:52:38 +0000591} // namespace android