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