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 | #pragma once |
| 18 | |
| 19 | #include <android-base/unique_fd.h> |
Siarhei Vishniakou | 8508b66 | 2024-08-08 17:04:37 -0700 | [diff] [blame^] | 20 | #include <input/Input.h> |
| 21 | #include <map> |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
Siarhei Vishniakou | 8508b66 | 2024-08-08 17:04:37 -0700 | [diff] [blame^] | 25 | enum class DeviceType { |
| 26 | KEYBOARD, |
| 27 | MOUSE, |
| 28 | TOUCHSCREEN, |
| 29 | DPAD, |
| 30 | STYLUS, |
| 31 | ROTARY_ENCODER, |
| 32 | }; |
| 33 | |
| 34 | android::base::unique_fd openUinput(const char* readableName, int32_t vendorId, int32_t productId, |
| 35 | const char* phys, DeviceType deviceType, int32_t screenHeight, |
| 36 | int32_t screenWidth); |
| 37 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 38 | enum class UinputAction { |
| 39 | RELEASE = 0, |
| 40 | PRESS = 1, |
| 41 | MOVE = 2, |
| 42 | CANCEL = 3, |
Siarhei Vishniakou | 8508b66 | 2024-08-08 17:04:37 -0700 | [diff] [blame^] | 43 | ftl_last = CANCEL, |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class VirtualInputDevice { |
| 47 | public: |
| 48 | VirtualInputDevice(android::base::unique_fd fd); |
| 49 | virtual ~VirtualInputDevice(); |
| 50 | |
| 51 | protected: |
| 52 | const android::base::unique_fd mFd; |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 53 | bool writeInputEvent(uint16_t type, uint16_t code, int32_t value, |
| 54 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 55 | bool writeEvKeyEvent(int32_t androidCode, int32_t androidAction, |
| 56 | const std::map<int, int>& evKeyCodeMapping, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 57 | const std::map<int, UinputAction>& actionMapping, |
| 58 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class VirtualKeyboard : public VirtualInputDevice { |
| 62 | public: |
| 63 | static const std::map<int, int> KEY_CODE_MAPPING; |
| 64 | // Expose to share with VirtualDpad. |
| 65 | static const std::map<int, UinputAction> KEY_ACTION_MAPPING; |
| 66 | VirtualKeyboard(android::base::unique_fd fd); |
| 67 | virtual ~VirtualKeyboard() override; |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 68 | bool writeKeyEvent(int32_t androidKeyCode, int32_t androidAction, |
| 69 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | class VirtualDpad : public VirtualInputDevice { |
| 73 | public: |
| 74 | static const std::map<int, int> DPAD_KEY_CODE_MAPPING; |
| 75 | VirtualDpad(android::base::unique_fd fd); |
| 76 | virtual ~VirtualDpad() override; |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 77 | bool writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction, |
| 78 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class VirtualMouse : public VirtualInputDevice { |
| 82 | public: |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 83 | // Expose to share with VirtualStylus. |
| 84 | static const std::map<int, UinputAction> BUTTON_ACTION_MAPPING; |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 85 | VirtualMouse(android::base::unique_fd fd); |
| 86 | virtual ~VirtualMouse() override; |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 87 | bool writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, |
| 88 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 89 | // TODO(b/259554911): changing float parameters to int32_t. |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 90 | bool writeRelativeEvent(float relativeX, float relativeY, std::chrono::nanoseconds eventTime); |
| 91 | bool writeScrollEvent(float xAxisMovement, float yAxisMovement, |
| 92 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 93 | |
| 94 | private: |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 95 | static const std::map<int, int> BUTTON_CODE_MAPPING; |
Biswarup Pal | 8ff5e5e | 2024-06-15 12:58:20 +0000 | [diff] [blame] | 96 | int32_t mAccumulatedHighResScrollX; |
| 97 | int32_t mAccumulatedHighResScrollY; |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class VirtualTouchscreen : public VirtualInputDevice { |
| 101 | public: |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 102 | // Expose to share with VirtualStylus. |
| 103 | static const std::map<int, UinputAction> TOUCH_ACTION_MAPPING; |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 104 | VirtualTouchscreen(android::base::unique_fd fd); |
| 105 | virtual ~VirtualTouchscreen() override; |
| 106 | // TODO(b/259554911): changing float parameters to int32_t. |
| 107 | bool writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action, float locationX, |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 108 | float locationY, float pressure, float majorAxisSize, |
| 109 | std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 112 | static const std::map<int, int> TOOL_TYPE_MAPPING; |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 113 | /* The set of active touch pointers on this device. |
| 114 | * We only allow pointer id to go up to MAX_POINTERS because the maximum slots of virtual |
| 115 | * touchscreen is set up with MAX_POINTERS. Note that in other cases Android allows pointer id |
| 116 | * to go up to MAX_POINTERS_ID. |
| 117 | */ |
| 118 | std::bitset<MAX_POINTERS> mActivePointers{}; |
| 119 | bool isValidPointerId(int32_t pointerId, UinputAction uinputAction); |
Biswarup Pal | e9fe2df | 2023-04-05 18:20:54 +0000 | [diff] [blame] | 120 | bool handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime); |
| 121 | bool handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime); |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 122 | }; |
Biswarup Pal | 590eb73 | 2023-11-30 17:59:57 +0000 | [diff] [blame] | 123 | |
| 124 | class VirtualStylus : public VirtualInputDevice { |
| 125 | public: |
| 126 | VirtualStylus(android::base::unique_fd fd); |
| 127 | ~VirtualStylus() override; |
| 128 | bool writeMotionEvent(int32_t toolType, int32_t action, int32_t locationX, int32_t locationY, |
| 129 | int32_t pressure, int32_t tiltX, int32_t tiltY, |
| 130 | std::chrono::nanoseconds eventTime); |
| 131 | bool writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, |
| 132 | std::chrono::nanoseconds eventTime); |
| 133 | |
| 134 | private: |
| 135 | static const std::map<int, int> TOOL_TYPE_MAPPING; |
| 136 | static const std::map<int, int> BUTTON_CODE_MAPPING; |
| 137 | // True if the stylus is touching or hovering on the screen. |
| 138 | bool mIsStylusDown; |
| 139 | bool handleStylusDown(uint16_t tool, std::chrono::nanoseconds eventTime); |
| 140 | bool handleStylusUp(uint16_t tool, std::chrono::nanoseconds eventTime); |
| 141 | }; |
| 142 | |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 143 | class VirtualRotaryEncoder : public VirtualInputDevice { |
| 144 | public: |
| 145 | VirtualRotaryEncoder(android::base::unique_fd fd); |
| 146 | virtual ~VirtualRotaryEncoder() override; |
| 147 | bool writeScrollEvent(float scrollAmount, std::chrono::nanoseconds eventTime); |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | int32_t mAccumulatedHighResScrollAmount; |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Zixuan Qu | dd0635d | 2023-02-06 04:52:38 +0000 | [diff] [blame] | 153 | } // namespace android |