blob: b73ee655040b01491b776fb192272c61e8229606 [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>
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 {
Biswarup Pal590eb732023-11-30 17:59:57 +000042
Zixuan Qudd0635d2023-02-06 04:52:38 +000043VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {}
Biswarup Pal590eb732023-11-30 17:59:57 +000044
Zixuan Qudd0635d2023-02-06 04:52:38 +000045VirtualInputDevice::~VirtualInputDevice() {
46 ioctl(mFd, UI_DEV_DESTROY);
47}
48
Biswarup Pale9fe2df2023-04-05 18:20:54 +000049bool VirtualInputDevice::writeInputEvent(uint16_t type, uint16_t code, int32_t value,
50 std::chrono::nanoseconds eventTime) {
51 std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(eventTime);
52 std::chrono::microseconds microseconds =
53 std::chrono::duration_cast<std::chrono::microseconds>(eventTime - seconds);
Colin Cross983cae02023-05-09 22:45:35 -070054 struct input_event ev = {.type = type, .code = code, .value = value};
55 ev.input_event_sec = static_cast<decltype(ev.input_event_sec)>(seconds.count());
56 ev.input_event_usec = static_cast<decltype(ev.input_event_usec)>(microseconds.count());
57
Zixuan Qudd0635d2023-02-06 04:52:38 +000058 return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
59}
60
Biswarup Pal590eb732023-11-30 17:59:57 +000061/** Utility method to write keyboard key events or mouse/stylus button events. */
Zixuan Qudd0635d2023-02-06 04:52:38 +000062bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
63 const std::map<int, int>& evKeyCodeMapping,
Biswarup Pale9fe2df2023-04-05 18:20:54 +000064 const std::map<int, UinputAction>& actionMapping,
65 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +000066 auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode);
67 if (evKeyCodeIterator == evKeyCodeMapping.end()) {
68 ALOGE("Unsupported native EV keycode for android code %d", androidCode);
69 return false;
70 }
71 auto actionIterator = actionMapping.find(androidAction);
72 if (actionIterator == actionMapping.end()) {
Biswarup Pal590eb732023-11-30 17:59:57 +000073 ALOGE("Unsupported native action for android action %d", androidAction);
Zixuan Qudd0635d2023-02-06 04:52:38 +000074 return false;
75 }
Biswarup Pal590eb732023-11-30 17:59:57 +000076 int32_t action = static_cast<int32_t>(actionIterator->second);
77 uint16_t evKeyCode = static_cast<uint16_t>(evKeyCodeIterator->second);
78 if (!writeInputEvent(EV_KEY, evKeyCode, action, eventTime)) {
79 ALOGE("Failed to write native action %d and EV keycode %u.", action, evKeyCode);
Zixuan Qudd0635d2023-02-06 04:52:38 +000080 return false;
81 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +000082 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
Biswarup Pal590eb732023-11-30 17:59:57 +000083 ALOGE("Failed to write SYN_REPORT for EV_KEY event.");
Zixuan Qudd0635d2023-02-06 04:52:38 +000084 return false;
85 }
86 return true;
87}
88
89// --- VirtualKeyboard ---
90const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
91 {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
92 {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
93};
Biswarup Pal590eb732023-11-30 17:59:57 +000094
Zixuan Qudd0635d2023-02-06 04:52:38 +000095// Keycode mapping from https://source.android.com/devices/input/keyboard-devices
96const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = {
97 {AKEYCODE_0, KEY_0},
98 {AKEYCODE_1, KEY_1},
99 {AKEYCODE_2, KEY_2},
100 {AKEYCODE_3, KEY_3},
101 {AKEYCODE_4, KEY_4},
102 {AKEYCODE_5, KEY_5},
103 {AKEYCODE_6, KEY_6},
104 {AKEYCODE_7, KEY_7},
105 {AKEYCODE_8, KEY_8},
106 {AKEYCODE_9, KEY_9},
107 {AKEYCODE_A, KEY_A},
108 {AKEYCODE_B, KEY_B},
109 {AKEYCODE_C, KEY_C},
110 {AKEYCODE_D, KEY_D},
111 {AKEYCODE_E, KEY_E},
112 {AKEYCODE_F, KEY_F},
113 {AKEYCODE_G, KEY_G},
114 {AKEYCODE_H, KEY_H},
115 {AKEYCODE_I, KEY_I},
116 {AKEYCODE_J, KEY_J},
117 {AKEYCODE_K, KEY_K},
118 {AKEYCODE_L, KEY_L},
119 {AKEYCODE_M, KEY_M},
120 {AKEYCODE_N, KEY_N},
121 {AKEYCODE_O, KEY_O},
122 {AKEYCODE_P, KEY_P},
123 {AKEYCODE_Q, KEY_Q},
124 {AKEYCODE_R, KEY_R},
125 {AKEYCODE_S, KEY_S},
126 {AKEYCODE_T, KEY_T},
127 {AKEYCODE_U, KEY_U},
128 {AKEYCODE_V, KEY_V},
129 {AKEYCODE_W, KEY_W},
130 {AKEYCODE_X, KEY_X},
131 {AKEYCODE_Y, KEY_Y},
132 {AKEYCODE_Z, KEY_Z},
133 {AKEYCODE_GRAVE, KEY_GRAVE},
134 {AKEYCODE_MINUS, KEY_MINUS},
135 {AKEYCODE_EQUALS, KEY_EQUAL},
136 {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE},
137 {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE},
138 {AKEYCODE_BACKSLASH, KEY_BACKSLASH},
139 {AKEYCODE_SEMICOLON, KEY_SEMICOLON},
140 {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE},
141 {AKEYCODE_COMMA, KEY_COMMA},
142 {AKEYCODE_PERIOD, KEY_DOT},
143 {AKEYCODE_SLASH, KEY_SLASH},
144 {AKEYCODE_ALT_LEFT, KEY_LEFTALT},
145 {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT},
146 {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL},
147 {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL},
148 {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT},
149 {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT},
150 {AKEYCODE_META_LEFT, KEY_LEFTMETA},
151 {AKEYCODE_META_RIGHT, KEY_RIGHTMETA},
152 {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK},
153 {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK},
154 {AKEYCODE_NUM_LOCK, KEY_NUMLOCK},
155 {AKEYCODE_ENTER, KEY_ENTER},
156 {AKEYCODE_TAB, KEY_TAB},
157 {AKEYCODE_SPACE, KEY_SPACE},
158 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
159 {AKEYCODE_DPAD_UP, KEY_UP},
160 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
161 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
162 {AKEYCODE_MOVE_END, KEY_END},
163 {AKEYCODE_MOVE_HOME, KEY_HOME},
164 {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN},
165 {AKEYCODE_PAGE_UP, KEY_PAGEUP},
166 {AKEYCODE_DEL, KEY_BACKSPACE},
167 {AKEYCODE_FORWARD_DEL, KEY_DELETE},
168 {AKEYCODE_INSERT, KEY_INSERT},
169 {AKEYCODE_ESCAPE, KEY_ESC},
170 {AKEYCODE_BREAK, KEY_PAUSE},
171 {AKEYCODE_F1, KEY_F1},
172 {AKEYCODE_F2, KEY_F2},
173 {AKEYCODE_F3, KEY_F3},
174 {AKEYCODE_F4, KEY_F4},
175 {AKEYCODE_F5, KEY_F5},
176 {AKEYCODE_F6, KEY_F6},
177 {AKEYCODE_F7, KEY_F7},
178 {AKEYCODE_F8, KEY_F8},
179 {AKEYCODE_F9, KEY_F9},
180 {AKEYCODE_F10, KEY_F10},
181 {AKEYCODE_F11, KEY_F11},
182 {AKEYCODE_F12, KEY_F12},
183 {AKEYCODE_BACK, KEY_BACK},
184 {AKEYCODE_FORWARD, KEY_FORWARD},
185 {AKEYCODE_NUMPAD_1, KEY_KP1},
186 {AKEYCODE_NUMPAD_2, KEY_KP2},
187 {AKEYCODE_NUMPAD_3, KEY_KP3},
188 {AKEYCODE_NUMPAD_4, KEY_KP4},
189 {AKEYCODE_NUMPAD_5, KEY_KP5},
190 {AKEYCODE_NUMPAD_6, KEY_KP6},
191 {AKEYCODE_NUMPAD_7, KEY_KP7},
192 {AKEYCODE_NUMPAD_8, KEY_KP8},
193 {AKEYCODE_NUMPAD_9, KEY_KP9},
194 {AKEYCODE_NUMPAD_0, KEY_KP0},
195 {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS},
196 {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS},
197 {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK},
198 {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH},
199 {AKEYCODE_NUMPAD_DOT, KEY_KPDOT},
200 {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER},
201 {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL},
202 {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA},
Vladimir Komsiyski65efbaa2023-11-14 09:30:12 +0100203 {AKEYCODE_LANGUAGE_SWITCH, KEY_LANGUAGE},
Zixuan Qudd0635d2023-02-06 04:52:38 +0000204};
Biswarup Pal590eb732023-11-30 17:59:57 +0000205
Zixuan Qudd0635d2023-02-06 04:52:38 +0000206VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
Biswarup Pal590eb732023-11-30 17:59:57 +0000207
Zixuan Qudd0635d2023-02-06 04:52:38 +0000208VirtualKeyboard::~VirtualKeyboard() {}
209
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000210bool VirtualKeyboard::writeKeyEvent(int32_t androidKeyCode, int32_t androidAction,
211 std::chrono::nanoseconds eventTime) {
212 return writeEvKeyEvent(androidKeyCode, androidAction, KEY_CODE_MAPPING, KEY_ACTION_MAPPING,
213 eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000214}
215
216// --- VirtualDpad ---
217// Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
218const std::map<int, int> VirtualDpad::DPAD_KEY_CODE_MAPPING = {
219 // clang-format off
220 {AKEYCODE_DPAD_DOWN, KEY_DOWN},
221 {AKEYCODE_DPAD_UP, KEY_UP},
222 {AKEYCODE_DPAD_LEFT, KEY_LEFT},
223 {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
224 {AKEYCODE_DPAD_CENTER, KEY_SELECT},
225 {AKEYCODE_BACK, KEY_BACK},
226 // clang-format on
227};
228
229VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
230
231VirtualDpad::~VirtualDpad() {}
232
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000233bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
234 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000235 return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000236 VirtualKeyboard::KEY_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000237}
238
239// --- VirtualMouse ---
240const std::map<int, UinputAction> VirtualMouse::BUTTON_ACTION_MAPPING = {
241 {AMOTION_EVENT_ACTION_BUTTON_PRESS, UinputAction::PRESS},
242 {AMOTION_EVENT_ACTION_BUTTON_RELEASE, UinputAction::RELEASE},
243};
244
245// Button code mapping from https://source.android.com/devices/input/touch-devices
246const std::map<int, int> VirtualMouse::BUTTON_CODE_MAPPING = {
247 // clang-format off
248 {AMOTION_EVENT_BUTTON_PRIMARY, BTN_LEFT},
249 {AMOTION_EVENT_BUTTON_SECONDARY, BTN_RIGHT},
250 {AMOTION_EVENT_BUTTON_TERTIARY, BTN_MIDDLE},
251 {AMOTION_EVENT_BUTTON_BACK, BTN_BACK},
252 {AMOTION_EVENT_BUTTON_FORWARD, BTN_FORWARD},
253 // clang-format on
254};
255
256VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
257
258VirtualMouse::~VirtualMouse() {}
259
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000260bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
261 std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000262 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000263 BUTTON_ACTION_MAPPING, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000264}
265
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000266bool VirtualMouse::writeRelativeEvent(float relativeX, float relativeY,
267 std::chrono::nanoseconds eventTime) {
268 return writeInputEvent(EV_REL, REL_X, relativeX, eventTime) &&
269 writeInputEvent(EV_REL, REL_Y, relativeY, eventTime) &&
270 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000271}
272
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000273bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement,
274 std::chrono::nanoseconds eventTime) {
275 return writeInputEvent(EV_REL, REL_HWHEEL, xAxisMovement, eventTime) &&
276 writeInputEvent(EV_REL, REL_WHEEL, yAxisMovement, eventTime) &&
277 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000278}
279
280// --- VirtualTouchscreen ---
281const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = {
282 {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS},
283 {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE},
284 {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE},
285 {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL},
286};
Biswarup Pal590eb732023-11-30 17:59:57 +0000287
Zixuan Qudd0635d2023-02-06 04:52:38 +0000288// Tool type mapping from https://source.android.com/devices/input/touch-devices
289const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = {
290 {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER},
291 {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM},
292};
293
294VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
295
296VirtualTouchscreen::~VirtualTouchscreen() {}
297
298bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) {
299 if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) {
300 ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu",
301 pointerId, MAX_POINTERS - 0);
302 return false;
303 }
304
305 if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) {
306 ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.",
307 pointerId);
308 return false;
309 }
310 if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) {
311 ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.",
312 pointerId, mFd.get());
313 return false;
314 }
315 return true;
316}
317
318bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
319 float locationX, float locationY, float pressure,
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000320 float majorAxisSize, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000321 auto actionIterator = TOUCH_ACTION_MAPPING.find(action);
322 if (actionIterator == TOUCH_ACTION_MAPPING.end()) {
323 return false;
324 }
325 UinputAction uinputAction = actionIterator->second;
326 if (!isValidPointerId(pointerId, uinputAction)) {
327 return false;
328 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000329 if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000330 return false;
331 }
332 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
333 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
334 return false;
335 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000336 if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second),
337 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000338 return false;
339 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000340 if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000341 return false;
342 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000343 if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000344 return false;
345 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000346 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000347 return false;
348 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000349 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000350 return false;
351 }
352 if (!isnan(pressure)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000353 if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000354 return false;
355 }
356 }
357 if (!isnan(majorAxisSize)) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000358 if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000359 return false;
360 }
361 }
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000362 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000363}
364
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000365bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) {
366 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000367 return false;
368 }
369 // When a pointer is no longer in touch, remove the pointer id from the corresponding
370 // entry in the unreleased touches map.
371 mActivePointers.reset(pointerId);
372 ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get());
373
374 // Only sends the BTN UP event when there's no pointers on the touchscreen.
375 if (mActivePointers.none()) {
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000376 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
377 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000378 return false;
379 }
380 ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
381 }
382 return true;
383}
384
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000385bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000386 // When a new pointer is down on the touchscreen, add the pointer id in the corresponding
387 // entry in the unreleased touches map.
388 if (mActivePointers.none()) {
389 // Only sends the BTN Down event when the first pointer on the touchscreen is down.
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000390 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS),
391 eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000392 return false;
393 }
394 ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent",
395 pointerId, mFd.get());
396 }
397
398 mActivePointers.set(pointerId);
399 ALOGD_IF(isDebug(), "Added pointer %d under touchscreen %d in the map", pointerId, mFd.get());
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000400 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) {
Zixuan Qudd0635d2023-02-06 04:52:38 +0000401 return false;
402 }
403 return true;
404}
405
Biswarup Pal590eb732023-11-30 17:59:57 +0000406// --- VirtualStylus ---
407const std::map<int, int> VirtualStylus::TOOL_TYPE_MAPPING = {
408 {AMOTION_EVENT_TOOL_TYPE_STYLUS, BTN_TOOL_PEN},
409 {AMOTION_EVENT_TOOL_TYPE_ERASER, BTN_TOOL_RUBBER},
410};
411
412// Button code mapping from https://source.android.com/devices/input/touch-devices
413const std::map<int, int> VirtualStylus::BUTTON_CODE_MAPPING = {
414 {AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, BTN_STYLUS},
415 {AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, BTN_STYLUS2},
416};
417
418VirtualStylus::VirtualStylus(unique_fd fd)
419 : VirtualInputDevice(std::move(fd)), mIsStylusDown(false) {}
420
421VirtualStylus::~VirtualStylus() {}
422
423bool VirtualStylus::writeMotionEvent(int32_t toolType, int32_t action, int32_t locationX,
424 int32_t locationY, int32_t pressure, int32_t tiltX,
425 int32_t tiltY, std::chrono::nanoseconds eventTime) {
426 auto actionIterator = VirtualTouchscreen::TOUCH_ACTION_MAPPING.find(action);
427 if (actionIterator == VirtualTouchscreen::TOUCH_ACTION_MAPPING.end()) {
428 ALOGE("Unsupported action passed for stylus: %d.", action);
429 return false;
430 }
431 UinputAction uinputAction = actionIterator->second;
432 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
433 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
434 ALOGE("Unsupported tool type passed for stylus: %d.", toolType);
435 return false;
436 }
437 uint16_t tool = static_cast<uint16_t>(toolTypeIterator->second);
438 if (uinputAction == UinputAction::PRESS && !handleStylusDown(tool, eventTime)) {
439 return false;
440 }
441 if (!mIsStylusDown) {
442 ALOGE("Action UP or MOVE received with no prior action DOWN for stylus %d.", mFd.get());
443 return false;
444 }
445 if (uinputAction == UinputAction::RELEASE && !handleStylusUp(tool, eventTime)) {
446 return false;
447 }
448 if (!writeInputEvent(EV_ABS, ABS_X, locationX, eventTime)) {
449 ALOGE("Unsupported x-axis location passed for stylus: %d.", locationX);
450 return false;
451 }
452 if (!writeInputEvent(EV_ABS, ABS_Y, locationY, eventTime)) {
453 ALOGE("Unsupported y-axis location passed for stylus: %d.", locationY);
454 return false;
455 }
456 if (!writeInputEvent(EV_ABS, ABS_TILT_X, tiltX, eventTime)) {
457 ALOGE("Unsupported x-axis tilt passed for stylus: %d.", tiltX);
458 return false;
459 }
460 if (!writeInputEvent(EV_ABS, ABS_TILT_Y, tiltY, eventTime)) {
461 ALOGE("Unsupported y-axis tilt passed for stylus: %d.", tiltY);
462 return false;
463 }
464 if (!writeInputEvent(EV_ABS, ABS_PRESSURE, pressure, eventTime)) {
465 ALOGE("Unsupported pressure passed for stylus: %d.", pressure);
466 return false;
467 }
468 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
469 ALOGE("Failed to write SYN_REPORT for stylus motion event.");
470 return false;
471 }
472 return true;
473}
474
475bool VirtualStylus::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
476 std::chrono::nanoseconds eventTime) {
477 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
478 VirtualMouse::BUTTON_ACTION_MAPPING, eventTime);
479}
480
481bool VirtualStylus::handleStylusDown(uint16_t tool, std::chrono::nanoseconds eventTime) {
482 if (mIsStylusDown) {
483 ALOGE("Repetitive action DOWN event received for a stylus that is already down.");
484 return false;
485 }
486 if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::PRESS), eventTime)) {
487 ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool);
488 return false;
489 }
490 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS), eventTime)) {
491 ALOGE("Failed to write BTN_TOUCH for stylus press.");
492 return false;
493 }
494 mIsStylusDown = true;
495 return true;
496}
497
498bool VirtualStylus::handleStylusUp(uint16_t tool, std::chrono::nanoseconds eventTime) {
499 if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::RELEASE), eventTime)) {
500 ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool);
501 return false;
502 }
503 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
504 eventTime)) {
505 ALOGE("Failed to write BTN_TOUCH for stylus release.");
506 return false;
507 }
508 mIsStylusDown = false;
509 return true;
510}
511
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100512// --- VirtualRotaryEncoder ---
513VirtualRotaryEncoder::VirtualRotaryEncoder(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
514
515VirtualRotaryEncoder::~VirtualRotaryEncoder() {}
516
517bool VirtualRotaryEncoder::writeScrollEvent(float scrollAmount,
518 std::chrono::nanoseconds eventTime) {
519 return writeInputEvent(EV_REL, REL_WHEEL, static_cast<int32_t>(scrollAmount), eventTime) &&
520 writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
521}
522
Zixuan Qudd0635d2023-02-06 04:52:38 +0000523} // namespace android