Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | */ |
| 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::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {} |
| 43 | VirtualInputDevice::~VirtualInputDevice() { |
| 44 | ioctl(mFd, UI_DEV_DESTROY); |
| 45 | } |
| 46 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 47 | bool 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); |
Colin Cross | 983cae0 | 2023-05-09 22:45:35 -0700 | [diff] [blame] | 52 | struct input_event ev = {.type = type, .code = code, .value = value}; |
| 53 | ev.input_event_sec = static_cast<decltype(ev.input_event_sec)>(seconds.count()); |
| 54 | ev.input_event_usec = static_cast<decltype(ev.input_event_usec)>(microseconds.count()); |
| 55 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 56 | return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev); |
| 57 | } |
| 58 | |
| 59 | /** Utility method to write keyboard key events or mouse button events. */ |
| 60 | bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction, |
| 61 | const std::map<int, int>& evKeyCodeMapping, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 62 | const std::map<int, UinputAction>& actionMapping, |
| 63 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 64 | auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode); |
| 65 | if (evKeyCodeIterator == evKeyCodeMapping.end()) { |
| 66 | ALOGE("Unsupported native EV keycode for android code %d", androidCode); |
| 67 | return false; |
| 68 | } |
| 69 | auto actionIterator = actionMapping.find(androidAction); |
| 70 | if (actionIterator == actionMapping.end()) { |
| 71 | return false; |
| 72 | } |
| 73 | if (!writeInputEvent(EV_KEY, static_cast<uint16_t>(evKeyCodeIterator->second), |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 74 | static_cast<int32_t>(actionIterator->second), eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 77 | if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | // --- VirtualKeyboard --- |
| 84 | const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = { |
| 85 | {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS}, |
| 86 | {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE}, |
| 87 | }; |
| 88 | // Keycode mapping from https://source.android.com/devices/input/keyboard-devices |
| 89 | const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = { |
| 90 | {AKEYCODE_0, KEY_0}, |
| 91 | {AKEYCODE_1, KEY_1}, |
| 92 | {AKEYCODE_2, KEY_2}, |
| 93 | {AKEYCODE_3, KEY_3}, |
| 94 | {AKEYCODE_4, KEY_4}, |
| 95 | {AKEYCODE_5, KEY_5}, |
| 96 | {AKEYCODE_6, KEY_6}, |
| 97 | {AKEYCODE_7, KEY_7}, |
| 98 | {AKEYCODE_8, KEY_8}, |
| 99 | {AKEYCODE_9, KEY_9}, |
| 100 | {AKEYCODE_A, KEY_A}, |
| 101 | {AKEYCODE_B, KEY_B}, |
| 102 | {AKEYCODE_C, KEY_C}, |
| 103 | {AKEYCODE_D, KEY_D}, |
| 104 | {AKEYCODE_E, KEY_E}, |
| 105 | {AKEYCODE_F, KEY_F}, |
| 106 | {AKEYCODE_G, KEY_G}, |
| 107 | {AKEYCODE_H, KEY_H}, |
| 108 | {AKEYCODE_I, KEY_I}, |
| 109 | {AKEYCODE_J, KEY_J}, |
| 110 | {AKEYCODE_K, KEY_K}, |
| 111 | {AKEYCODE_L, KEY_L}, |
| 112 | {AKEYCODE_M, KEY_M}, |
| 113 | {AKEYCODE_N, KEY_N}, |
| 114 | {AKEYCODE_O, KEY_O}, |
| 115 | {AKEYCODE_P, KEY_P}, |
| 116 | {AKEYCODE_Q, KEY_Q}, |
| 117 | {AKEYCODE_R, KEY_R}, |
| 118 | {AKEYCODE_S, KEY_S}, |
| 119 | {AKEYCODE_T, KEY_T}, |
| 120 | {AKEYCODE_U, KEY_U}, |
| 121 | {AKEYCODE_V, KEY_V}, |
| 122 | {AKEYCODE_W, KEY_W}, |
| 123 | {AKEYCODE_X, KEY_X}, |
| 124 | {AKEYCODE_Y, KEY_Y}, |
| 125 | {AKEYCODE_Z, KEY_Z}, |
| 126 | {AKEYCODE_GRAVE, KEY_GRAVE}, |
| 127 | {AKEYCODE_MINUS, KEY_MINUS}, |
| 128 | {AKEYCODE_EQUALS, KEY_EQUAL}, |
| 129 | {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE}, |
| 130 | {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE}, |
| 131 | {AKEYCODE_BACKSLASH, KEY_BACKSLASH}, |
| 132 | {AKEYCODE_SEMICOLON, KEY_SEMICOLON}, |
| 133 | {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE}, |
| 134 | {AKEYCODE_COMMA, KEY_COMMA}, |
| 135 | {AKEYCODE_PERIOD, KEY_DOT}, |
| 136 | {AKEYCODE_SLASH, KEY_SLASH}, |
| 137 | {AKEYCODE_ALT_LEFT, KEY_LEFTALT}, |
| 138 | {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT}, |
| 139 | {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL}, |
| 140 | {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL}, |
| 141 | {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT}, |
| 142 | {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT}, |
| 143 | {AKEYCODE_META_LEFT, KEY_LEFTMETA}, |
| 144 | {AKEYCODE_META_RIGHT, KEY_RIGHTMETA}, |
| 145 | {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK}, |
| 146 | {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK}, |
| 147 | {AKEYCODE_NUM_LOCK, KEY_NUMLOCK}, |
| 148 | {AKEYCODE_ENTER, KEY_ENTER}, |
| 149 | {AKEYCODE_TAB, KEY_TAB}, |
| 150 | {AKEYCODE_SPACE, KEY_SPACE}, |
| 151 | {AKEYCODE_DPAD_DOWN, KEY_DOWN}, |
| 152 | {AKEYCODE_DPAD_UP, KEY_UP}, |
| 153 | {AKEYCODE_DPAD_LEFT, KEY_LEFT}, |
| 154 | {AKEYCODE_DPAD_RIGHT, KEY_RIGHT}, |
| 155 | {AKEYCODE_MOVE_END, KEY_END}, |
| 156 | {AKEYCODE_MOVE_HOME, KEY_HOME}, |
| 157 | {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN}, |
| 158 | {AKEYCODE_PAGE_UP, KEY_PAGEUP}, |
| 159 | {AKEYCODE_DEL, KEY_BACKSPACE}, |
| 160 | {AKEYCODE_FORWARD_DEL, KEY_DELETE}, |
| 161 | {AKEYCODE_INSERT, KEY_INSERT}, |
| 162 | {AKEYCODE_ESCAPE, KEY_ESC}, |
| 163 | {AKEYCODE_BREAK, KEY_PAUSE}, |
| 164 | {AKEYCODE_F1, KEY_F1}, |
| 165 | {AKEYCODE_F2, KEY_F2}, |
| 166 | {AKEYCODE_F3, KEY_F3}, |
| 167 | {AKEYCODE_F4, KEY_F4}, |
| 168 | {AKEYCODE_F5, KEY_F5}, |
| 169 | {AKEYCODE_F6, KEY_F6}, |
| 170 | {AKEYCODE_F7, KEY_F7}, |
| 171 | {AKEYCODE_F8, KEY_F8}, |
| 172 | {AKEYCODE_F9, KEY_F9}, |
| 173 | {AKEYCODE_F10, KEY_F10}, |
| 174 | {AKEYCODE_F11, KEY_F11}, |
| 175 | {AKEYCODE_F12, KEY_F12}, |
| 176 | {AKEYCODE_BACK, KEY_BACK}, |
| 177 | {AKEYCODE_FORWARD, KEY_FORWARD}, |
| 178 | {AKEYCODE_NUMPAD_1, KEY_KP1}, |
| 179 | {AKEYCODE_NUMPAD_2, KEY_KP2}, |
| 180 | {AKEYCODE_NUMPAD_3, KEY_KP3}, |
| 181 | {AKEYCODE_NUMPAD_4, KEY_KP4}, |
| 182 | {AKEYCODE_NUMPAD_5, KEY_KP5}, |
| 183 | {AKEYCODE_NUMPAD_6, KEY_KP6}, |
| 184 | {AKEYCODE_NUMPAD_7, KEY_KP7}, |
| 185 | {AKEYCODE_NUMPAD_8, KEY_KP8}, |
| 186 | {AKEYCODE_NUMPAD_9, KEY_KP9}, |
| 187 | {AKEYCODE_NUMPAD_0, KEY_KP0}, |
| 188 | {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS}, |
| 189 | {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS}, |
| 190 | {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK}, |
| 191 | {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH}, |
| 192 | {AKEYCODE_NUMPAD_DOT, KEY_KPDOT}, |
| 193 | {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER}, |
| 194 | {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL}, |
| 195 | {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA}, |
Vladimir Komsiyski | 65efbaa | 2023-11-14 09:30:12 +0100 | [diff] [blame^] | 196 | {AKEYCODE_LANGUAGE_SWITCH, KEY_LANGUAGE}, |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 197 | }; |
| 198 | VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 199 | VirtualKeyboard::~VirtualKeyboard() {} |
| 200 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 201 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // --- VirtualDpad --- |
| 208 | // Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices |
| 209 | const 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 | |
| 220 | VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 221 | |
| 222 | VirtualDpad::~VirtualDpad() {} |
| 223 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 224 | bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction, |
| 225 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 226 | return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 227 | VirtualKeyboard::KEY_ACTION_MAPPING, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // --- VirtualMouse --- |
| 231 | const 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 |
| 237 | const 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 | |
| 247 | VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 248 | |
| 249 | VirtualMouse::~VirtualMouse() {} |
| 250 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 251 | bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, |
| 252 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 253 | return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 254 | BUTTON_ACTION_MAPPING, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 257 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 264 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // --- VirtualTouchscreen --- |
| 272 | const 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 |
| 279 | const 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 | |
| 284 | VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 285 | |
| 286 | VirtualTouchscreen::~VirtualTouchscreen() {} |
| 287 | |
| 288 | bool 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 | |
| 308 | bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action, |
| 309 | float locationX, float locationY, float pressure, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 310 | float majorAxisSize, std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 311 | 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 Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 319 | if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType); |
| 323 | if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) { |
| 324 | return false; |
| 325 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 326 | if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second), |
| 327 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 328 | return false; |
| 329 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 330 | if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 331 | return false; |
| 332 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 333 | if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 334 | return false; |
| 335 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 336 | if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 337 | return false; |
| 338 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 339 | if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | if (!isnan(pressure)) { |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 343 | if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | } |
| 347 | if (!isnan(majorAxisSize)) { |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 348 | if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 352 | return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 355 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 357 | 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 Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 366 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE), |
| 367 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 368 | 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 Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 375 | bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 376 | // 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 Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 380 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS), |
| 381 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 382 | 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 Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 390 | if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | } // namespace android |