Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 1 | /* |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 2 | * Copyright 2023 The Android Open Source Project |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 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> |
Biswarup Pal | 8ff5e5e | 2024-06-15 12:58:20 +0000 | [diff] [blame] | 21 | #include <android_companion_virtualdevice_flags.h> |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 22 | #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 | |
| 32 | using 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 | */ |
| 38 | static bool isDebug() { |
| 39 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO); |
| 40 | } |
| 41 | |
| 42 | namespace android { |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 43 | |
Biswarup Pal | 8ff5e5e | 2024-06-15 12:58:20 +0000 | [diff] [blame] | 44 | namespace vd_flags = android::companion::virtualdevice::flags; |
| 45 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 46 | VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {} |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 47 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 48 | VirtualInputDevice::~VirtualInputDevice() { |
| 49 | ioctl(mFd, UI_DEV_DESTROY); |
| 50 | } |
| 51 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 52 | bool 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 Cross | 983cae0 | 2023-05-09 22:45:35 -0700 | [diff] [blame] | 57 | 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 61 | return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev); |
| 62 | } |
| 63 | |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 64 | /** Utility method to write keyboard key events or mouse/stylus button events. */ |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 65 | bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction, |
| 66 | const std::map<int, int>& evKeyCodeMapping, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 67 | const std::map<int, UinputAction>& actionMapping, |
| 68 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 69 | 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 Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 76 | ALOGE("Unsupported native action for android action %d", androidAction); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 79 | 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 85 | if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) { |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 86 | ALOGE("Failed to write SYN_REPORT for EV_KEY event."); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | // --- VirtualKeyboard --- |
| 93 | const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = { |
| 94 | {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS}, |
| 95 | {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE}, |
| 96 | }; |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 97 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 98 | // Keycode mapping from https://source.android.com/devices/input/keyboard-devices |
| 99 | const 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 Komsiyski | 65efbaa | 2023-11-14 09:30:12 +0100 | [diff] [blame] | 206 | {AKEYCODE_LANGUAGE_SWITCH, KEY_LANGUAGE}, |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 207 | }; |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 208 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 209 | VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 210 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 211 | VirtualKeyboard::~VirtualKeyboard() {} |
| 212 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 213 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // --- VirtualDpad --- |
| 220 | // Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices |
| 221 | const 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 | |
| 232 | VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 233 | |
| 234 | VirtualDpad::~VirtualDpad() {} |
| 235 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 236 | bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction, |
| 237 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 238 | return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 239 | VirtualKeyboard::KEY_ACTION_MAPPING, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // --- VirtualMouse --- |
| 243 | const 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 |
| 249 | const 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 Pal | 8ff5e5e | 2024-06-15 12:58:20 +0000 | [diff] [blame] | 259 | VirtualMouse::VirtualMouse(unique_fd fd) |
| 260 | : VirtualInputDevice(std::move(fd)), |
| 261 | mAccumulatedHighResScrollX(0), |
| 262 | mAccumulatedHighResScrollY(0) {} |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 263 | |
| 264 | VirtualMouse::~VirtualMouse() {} |
| 265 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 266 | bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, |
| 267 | std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 268 | return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 269 | BUTTON_ACTION_MAPPING, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 272 | bool 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 Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 279 | bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement, |
| 280 | std::chrono::nanoseconds eventTime) { |
Biswarup Pal | 8ff5e5e | 2024-06-15 12:58:20 +0000 | [diff] [blame] | 281 | if (!vd_flags::high_resolution_scroll()) { |
| 282 | return writeInputEvent(EV_REL, REL_HWHEEL, xAxisMovement, eventTime) && |
| 283 | writeInputEvent(EV_REL, REL_WHEEL, yAxisMovement, eventTime) && |
| 284 | writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime); |
| 285 | } |
| 286 | |
| 287 | const int32_t highResScrollX = xAxisMovement * kEvdevMouseHighResScrollUnitsPerDetent; |
| 288 | const int32_t highResScrollY = yAxisMovement * kEvdevMouseHighResScrollUnitsPerDetent; |
| 289 | bool highResScrollResult = |
| 290 | writeInputEvent(EV_REL, REL_HWHEEL_HI_RES, highResScrollX, eventTime) && |
| 291 | writeInputEvent(EV_REL, REL_WHEEL_HI_RES, highResScrollY, eventTime); |
| 292 | if (!highResScrollResult) { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | // According to evdev spec, a high-resolution mouse needs to emit REL_WHEEL / REL_HWHEEL events |
| 297 | // in addition to high-res scroll events. Regular scroll events can approximate high-res scroll |
| 298 | // events, so we send a regular scroll event when the accumulated scroll motion reaches a detent |
| 299 | // (single mouse wheel click). |
| 300 | mAccumulatedHighResScrollX += highResScrollX; |
| 301 | mAccumulatedHighResScrollY += highResScrollY; |
| 302 | const int32_t scrollX = mAccumulatedHighResScrollX / kEvdevMouseHighResScrollUnitsPerDetent; |
| 303 | const int32_t scrollY = mAccumulatedHighResScrollY / kEvdevMouseHighResScrollUnitsPerDetent; |
| 304 | if (scrollX != 0) { |
| 305 | if (!writeInputEvent(EV_REL, REL_HWHEEL, scrollX, eventTime)) { |
| 306 | return false; |
| 307 | } |
| 308 | mAccumulatedHighResScrollX %= kEvdevMouseHighResScrollUnitsPerDetent; |
| 309 | } |
| 310 | if (scrollY != 0) { |
| 311 | if (!writeInputEvent(EV_REL, REL_WHEEL, scrollY, eventTime)) { |
| 312 | return false; |
| 313 | } |
| 314 | mAccumulatedHighResScrollY %= kEvdevMouseHighResScrollUnitsPerDetent; |
| 315 | } |
| 316 | |
| 317 | return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // --- VirtualTouchscreen --- |
| 321 | const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = { |
| 322 | {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS}, |
| 323 | {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE}, |
| 324 | {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE}, |
| 325 | {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL}, |
| 326 | }; |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 327 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 328 | // Tool type mapping from https://source.android.com/devices/input/touch-devices |
| 329 | const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = { |
| 330 | {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER}, |
| 331 | {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM}, |
| 332 | }; |
| 333 | |
| 334 | VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 335 | |
| 336 | VirtualTouchscreen::~VirtualTouchscreen() {} |
| 337 | |
| 338 | bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) { |
| 339 | if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) { |
| 340 | ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu", |
| 341 | pointerId, MAX_POINTERS - 0); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) { |
| 346 | ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.", |
| 347 | pointerId); |
| 348 | return false; |
| 349 | } |
| 350 | if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) { |
| 351 | ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.", |
| 352 | pointerId, mFd.get()); |
| 353 | return false; |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action, |
| 359 | float locationX, float locationY, float pressure, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 360 | float majorAxisSize, std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 361 | auto actionIterator = TOUCH_ACTION_MAPPING.find(action); |
| 362 | if (actionIterator == TOUCH_ACTION_MAPPING.end()) { |
| 363 | return false; |
| 364 | } |
| 365 | UinputAction uinputAction = actionIterator->second; |
| 366 | if (!isValidPointerId(pointerId, uinputAction)) { |
| 367 | return false; |
| 368 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 369 | if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType); |
| 373 | if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) { |
| 374 | return false; |
| 375 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 376 | if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second), |
| 377 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 380 | if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 381 | return false; |
| 382 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 383 | if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 386 | if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 389 | if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | if (!isnan(pressure)) { |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 393 | if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | if (!isnan(majorAxisSize)) { |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 398 | if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | } |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 402 | return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 405 | bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) { |
| 406 | 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] | 407 | return false; |
| 408 | } |
| 409 | // When a pointer is no longer in touch, remove the pointer id from the corresponding |
| 410 | // entry in the unreleased touches map. |
| 411 | mActivePointers.reset(pointerId); |
| 412 | ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get()); |
| 413 | |
| 414 | // Only sends the BTN UP event when there's no pointers on the touchscreen. |
| 415 | if (mActivePointers.none()) { |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 416 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE), |
| 417 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get()); |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 | |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 425 | bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 426 | // When a new pointer is down on the touchscreen, add the pointer id in the corresponding |
| 427 | // entry in the unreleased touches map. |
| 428 | if (mActivePointers.none()) { |
| 429 | // 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] | 430 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS), |
| 431 | eventTime)) { |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent", |
| 435 | pointerId, mFd.get()); |
| 436 | } |
| 437 | |
| 438 | mActivePointers.set(pointerId); |
| 439 | 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] | 440 | 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] | 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 446 | // --- VirtualStylus --- |
| 447 | const std::map<int, int> VirtualStylus::TOOL_TYPE_MAPPING = { |
| 448 | {AMOTION_EVENT_TOOL_TYPE_STYLUS, BTN_TOOL_PEN}, |
| 449 | {AMOTION_EVENT_TOOL_TYPE_ERASER, BTN_TOOL_RUBBER}, |
| 450 | }; |
| 451 | |
| 452 | // Button code mapping from https://source.android.com/devices/input/touch-devices |
| 453 | const std::map<int, int> VirtualStylus::BUTTON_CODE_MAPPING = { |
| 454 | {AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, BTN_STYLUS}, |
| 455 | {AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, BTN_STYLUS2}, |
| 456 | }; |
| 457 | |
| 458 | VirtualStylus::VirtualStylus(unique_fd fd) |
| 459 | : VirtualInputDevice(std::move(fd)), mIsStylusDown(false) {} |
| 460 | |
| 461 | VirtualStylus::~VirtualStylus() {} |
| 462 | |
| 463 | bool VirtualStylus::writeMotionEvent(int32_t toolType, int32_t action, int32_t locationX, |
| 464 | int32_t locationY, int32_t pressure, int32_t tiltX, |
| 465 | int32_t tiltY, std::chrono::nanoseconds eventTime) { |
| 466 | auto actionIterator = VirtualTouchscreen::TOUCH_ACTION_MAPPING.find(action); |
| 467 | if (actionIterator == VirtualTouchscreen::TOUCH_ACTION_MAPPING.end()) { |
| 468 | ALOGE("Unsupported action passed for stylus: %d.", action); |
| 469 | return false; |
| 470 | } |
| 471 | UinputAction uinputAction = actionIterator->second; |
| 472 | auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType); |
| 473 | if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) { |
| 474 | ALOGE("Unsupported tool type passed for stylus: %d.", toolType); |
| 475 | return false; |
| 476 | } |
| 477 | uint16_t tool = static_cast<uint16_t>(toolTypeIterator->second); |
| 478 | if (uinputAction == UinputAction::PRESS && !handleStylusDown(tool, eventTime)) { |
| 479 | return false; |
| 480 | } |
| 481 | if (!mIsStylusDown) { |
| 482 | ALOGE("Action UP or MOVE received with no prior action DOWN for stylus %d.", mFd.get()); |
| 483 | return false; |
| 484 | } |
| 485 | if (uinputAction == UinputAction::RELEASE && !handleStylusUp(tool, eventTime)) { |
| 486 | return false; |
| 487 | } |
| 488 | if (!writeInputEvent(EV_ABS, ABS_X, locationX, eventTime)) { |
| 489 | ALOGE("Unsupported x-axis location passed for stylus: %d.", locationX); |
| 490 | return false; |
| 491 | } |
| 492 | if (!writeInputEvent(EV_ABS, ABS_Y, locationY, eventTime)) { |
| 493 | ALOGE("Unsupported y-axis location passed for stylus: %d.", locationY); |
| 494 | return false; |
| 495 | } |
| 496 | if (!writeInputEvent(EV_ABS, ABS_TILT_X, tiltX, eventTime)) { |
| 497 | ALOGE("Unsupported x-axis tilt passed for stylus: %d.", tiltX); |
| 498 | return false; |
| 499 | } |
| 500 | if (!writeInputEvent(EV_ABS, ABS_TILT_Y, tiltY, eventTime)) { |
| 501 | ALOGE("Unsupported y-axis tilt passed for stylus: %d.", tiltY); |
| 502 | return false; |
| 503 | } |
| 504 | if (!writeInputEvent(EV_ABS, ABS_PRESSURE, pressure, eventTime)) { |
| 505 | ALOGE("Unsupported pressure passed for stylus: %d.", pressure); |
| 506 | return false; |
| 507 | } |
| 508 | if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) { |
| 509 | ALOGE("Failed to write SYN_REPORT for stylus motion event."); |
| 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | bool VirtualStylus::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, |
| 516 | std::chrono::nanoseconds eventTime) { |
| 517 | return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING, |
| 518 | VirtualMouse::BUTTON_ACTION_MAPPING, eventTime); |
| 519 | } |
| 520 | |
| 521 | bool VirtualStylus::handleStylusDown(uint16_t tool, std::chrono::nanoseconds eventTime) { |
| 522 | if (mIsStylusDown) { |
| 523 | ALOGE("Repetitive action DOWN event received for a stylus that is already down."); |
| 524 | return false; |
| 525 | } |
| 526 | if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::PRESS), eventTime)) { |
| 527 | ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool); |
| 528 | return false; |
| 529 | } |
| 530 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS), eventTime)) { |
| 531 | ALOGE("Failed to write BTN_TOUCH for stylus press."); |
| 532 | return false; |
| 533 | } |
| 534 | mIsStylusDown = true; |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | bool VirtualStylus::handleStylusUp(uint16_t tool, std::chrono::nanoseconds eventTime) { |
| 539 | if (!writeInputEvent(EV_KEY, tool, static_cast<int32_t>(UinputAction::RELEASE), eventTime)) { |
| 540 | ALOGE("Failed to write EV_KEY for stylus tool type: %u.", tool); |
| 541 | return false; |
| 542 | } |
| 543 | if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE), |
| 544 | eventTime)) { |
| 545 | ALOGE("Failed to write BTN_TOUCH for stylus release."); |
| 546 | return false; |
| 547 | } |
| 548 | mIsStylusDown = false; |
| 549 | return true; |
| 550 | } |
| 551 | |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 552 | // --- VirtualRotaryEncoder --- |
| 553 | VirtualRotaryEncoder::VirtualRotaryEncoder(unique_fd fd) : VirtualInputDevice(std::move(fd)) {} |
| 554 | |
| 555 | VirtualRotaryEncoder::~VirtualRotaryEncoder() {} |
| 556 | |
| 557 | bool VirtualRotaryEncoder::writeScrollEvent(float scrollAmount, |
| 558 | std::chrono::nanoseconds eventTime) { |
| 559 | return writeInputEvent(EV_REL, REL_WHEEL, static_cast<int32_t>(scrollAmount), eventTime) && |
| 560 | writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime); |
| 561 | } |
| 562 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 563 | } // namespace android |