1 /*
2 * Copyright 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
31 using 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 */
isDebug()37 static bool isDebug() {
38 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
39 }
40
41 namespace android {
42
VirtualInputDevice(unique_fd fd)43 VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {}
44
~VirtualInputDevice()45 VirtualInputDevice::~VirtualInputDevice() {
46 ioctl(mFd, UI_DEV_DESTROY);
47 }
48
writeInputEvent(uint16_t type,uint16_t code,int32_t value,std::chrono::nanoseconds eventTime)49 bool 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);
54 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
58 return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
59 }
60
61 /** Utility method to write keyboard key events or mouse/stylus button events. */
writeEvKeyEvent(int32_t androidCode,int32_t androidAction,const std::map<int,int> & evKeyCodeMapping,const std::map<int,UinputAction> & actionMapping,std::chrono::nanoseconds eventTime)62 bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
63 const std::map<int, int>& evKeyCodeMapping,
64 const std::map<int, UinputAction>& actionMapping,
65 std::chrono::nanoseconds eventTime) {
66 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()) {
73 ALOGE("Unsupported native action for android action %d", androidAction);
74 return false;
75 }
76 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);
80 return false;
81 }
82 if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
83 ALOGE("Failed to write SYN_REPORT for EV_KEY event.");
84 return false;
85 }
86 return true;
87 }
88
89 // --- VirtualKeyboard ---
90 const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
91 {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
92 {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
93 };
94
95 // Keycode mapping from https://source.android.com/devices/input/keyboard-devices
96 const 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},
203 {AKEYCODE_LANGUAGE_SWITCH, KEY_LANGUAGE},
204 };
205
VirtualKeyboard(unique_fd fd)206 VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
207
~VirtualKeyboard()208 VirtualKeyboard::~VirtualKeyboard() {}
209
writeKeyEvent(int32_t androidKeyCode,int32_t androidAction,std::chrono::nanoseconds eventTime)210 bool 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);
214 }
215
216 // --- VirtualDpad ---
217 // Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
218 const 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
VirtualDpad(unique_fd fd)229 VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
230
~VirtualDpad()231 VirtualDpad::~VirtualDpad() {}
232
writeDpadKeyEvent(int32_t androidKeyCode,int32_t androidAction,std::chrono::nanoseconds eventTime)233 bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
234 std::chrono::nanoseconds eventTime) {
235 return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
236 VirtualKeyboard::KEY_ACTION_MAPPING, eventTime);
237 }
238
239 // --- VirtualMouse ---
240 const 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
246 const 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
VirtualMouse(unique_fd fd)256 VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
257
~VirtualMouse()258 VirtualMouse::~VirtualMouse() {}
259
writeButtonEvent(int32_t androidButtonCode,int32_t androidAction,std::chrono::nanoseconds eventTime)260 bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
261 std::chrono::nanoseconds eventTime) {
262 return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
263 BUTTON_ACTION_MAPPING, eventTime);
264 }
265
writeRelativeEvent(float relativeX,float relativeY,std::chrono::nanoseconds eventTime)266 bool 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);
271 }
272
writeScrollEvent(float xAxisMovement,float yAxisMovement,std::chrono::nanoseconds eventTime)273 bool 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);
278 }
279
280 // --- VirtualTouchscreen ---
281 const 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 };
287
288 // Tool type mapping from https://source.android.com/devices/input/touch-devices
289 const 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
VirtualTouchscreen(unique_fd fd)294 VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
295
~VirtualTouchscreen()296 VirtualTouchscreen::~VirtualTouchscreen() {}
297
isValidPointerId(int32_t pointerId,UinputAction uinputAction)298 bool 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
writeTouchEvent(int32_t pointerId,int32_t toolType,int32_t action,float locationX,float locationY,float pressure,float majorAxisSize,std::chrono::nanoseconds eventTime)318 bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
319 float locationX, float locationY, float pressure,
320 float majorAxisSize, std::chrono::nanoseconds eventTime) {
321 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 }
329 if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) {
330 return false;
331 }
332 auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
333 if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
334 return false;
335 }
336 if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second),
337 eventTime)) {
338 return false;
339 }
340 if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) {
341 return false;
342 }
343 if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) {
344 return false;
345 }
346 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) {
347 return false;
348 }
349 if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) {
350 return false;
351 }
352 if (!isnan(pressure)) {
353 if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) {
354 return false;
355 }
356 }
357 if (!isnan(majorAxisSize)) {
358 if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) {
359 return false;
360 }
361 }
362 return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
363 }
364
handleTouchUp(int32_t pointerId,std::chrono::nanoseconds eventTime)365 bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) {
366 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1), eventTime)) {
367 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()) {
376 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
377 eventTime)) {
378 return false;
379 }
380 ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
381 }
382 return true;
383 }
384
handleTouchDown(int32_t pointerId,std::chrono::nanoseconds eventTime)385 bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) {
386 // 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.
390 if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS),
391 eventTime)) {
392 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());
400 if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) {
401 return false;
402 }
403 return true;
404 }
405
406 // --- VirtualStylus ---
407 const 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
413 const 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
VirtualStylus(unique_fd fd)418 VirtualStylus::VirtualStylus(unique_fd fd)
419 : VirtualInputDevice(std::move(fd)), mIsStylusDown(false) {}
420
~VirtualStylus()421 VirtualStylus::~VirtualStylus() {}
422
writeMotionEvent(int32_t toolType,int32_t action,int32_t locationX,int32_t locationY,int32_t pressure,int32_t tiltX,int32_t tiltY,std::chrono::nanoseconds eventTime)423 bool 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
writeButtonEvent(int32_t androidButtonCode,int32_t androidAction,std::chrono::nanoseconds eventTime)475 bool 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
handleStylusDown(uint16_t tool,std::chrono::nanoseconds eventTime)481 bool 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
handleStylusUp(uint16_t tool,std::chrono::nanoseconds eventTime)498 bool 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
512 } // namespace android
513