Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 18 | |
| 19 | #include <android-base/unique_fd.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <linux/uinput.h> |
| 23 | #include <log/log.h> |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 24 | #include <ui/Point.h> |
| 25 | #include <ui/Rect.h> |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 26 | |
| 27 | #include <memory> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // This is the factory method that must be used to create a UinputDevice. |
| 32 | template <class D, class... Ts> |
| 33 | std::unique_ptr<D> createUinputDevice(Ts... args) { |
| 34 | // Using `new` to access non-public constructors. |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 35 | std::unique_ptr<D> dev(new D(args...)); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 36 | EXPECT_NO_FATAL_FAILURE(dev->init()); |
| 37 | return dev; |
| 38 | } |
| 39 | |
| 40 | // --- UinputDevice --- |
| 41 | |
| 42 | class UinputDevice { |
| 43 | public: |
| 44 | virtual ~UinputDevice(); |
| 45 | |
| 46 | inline const char* getName() const { return mName; } |
| 47 | |
| 48 | // Subclasses must either provide a public constructor or must be-friend the factory method. |
| 49 | template <class D, class... Ts> |
| 50 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 51 | |
| 52 | protected: |
| 53 | const char* mName; |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 54 | const int16_t mProductId; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 55 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 56 | explicit UinputDevice(const char* name, int16_t productId); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 57 | |
| 58 | // Signals which types of events this device supports before it is created. |
| 59 | // This must be overridden by subclasses. |
| 60 | virtual void configureDevice(int fd, uinput_user_dev* device) = 0; |
| 61 | |
| 62 | void injectEvent(uint16_t type, uint16_t code, int32_t value); |
| 63 | |
| 64 | private: |
| 65 | base::unique_fd mDeviceFd; |
| 66 | |
| 67 | // This is called once by the factory method createUinputDevice(). |
| 68 | void init(); |
| 69 | }; |
| 70 | |
| 71 | // --- UinputKeyboard --- |
| 72 | |
| 73 | class UinputKeyboard : public UinputDevice { |
| 74 | public: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 75 | static constexpr const char* KEYBOARD_NAME = "Test Uinput Keyboard Device"; |
| 76 | static constexpr int16_t PRODUCT_ID = 42; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 77 | |
| 78 | // Injects key press and sync. |
| 79 | void pressKey(int key); |
| 80 | // Injects key release and sync. |
| 81 | void releaseKey(int key); |
| 82 | // Injects 4 events: key press, sync, key release, and sync. |
| 83 | void pressAndReleaseKey(int key); |
| 84 | |
| 85 | template <class D, class... Ts> |
| 86 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 87 | |
| 88 | protected: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 89 | explicit UinputKeyboard(const char* name, int16_t productId = PRODUCT_ID, |
| 90 | std::initializer_list<int> keys = {}); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 91 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 92 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 93 | |
Prabir Pradhan | 484d55a | 2022-10-14 23:17:16 +0000 | [diff] [blame] | 94 | private: |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 95 | std::set<int> mKeys; |
| 96 | }; |
| 97 | |
| 98 | // --- UinputHomeKey--- |
| 99 | |
| 100 | // A keyboard device that has a single HOME key. |
| 101 | class UinputHomeKey : public UinputKeyboard { |
| 102 | public: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 103 | static constexpr const char* DEVICE_NAME = "Test Uinput Home Key"; |
| 104 | static constexpr int16_t PRODUCT_ID = 43; |
| 105 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 106 | // Injects 4 events: key press, sync, key release, and sync. |
| 107 | void pressAndReleaseHomeKey(); |
| 108 | |
| 109 | template <class D, class... Ts> |
| 110 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 111 | |
| 112 | private: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 113 | explicit UinputHomeKey(); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 116 | // --- UinputSteamController --- |
| 117 | |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 118 | // A joystick device that sends a BTN_GEAR_DOWN / BTN_WHEEL key. |
| 119 | class UinputSteamController : public UinputKeyboard { |
| 120 | public: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 121 | static constexpr const char* DEVICE_NAME = "Test Uinput Steam Controller"; |
| 122 | static constexpr int16_t PRODUCT_ID = 44; |
| 123 | |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 124 | template <class D, class... Ts> |
| 125 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 126 | |
| 127 | private: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 128 | explicit UinputSteamController(); |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 131 | // --- UinputExternalStylus --- |
| 132 | |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 133 | // A stylus that reports button presses. |
| 134 | class UinputExternalStylus : public UinputKeyboard { |
| 135 | public: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 136 | static constexpr const char* DEVICE_NAME = "Test Uinput External Stylus"; |
| 137 | static constexpr int16_t PRODUCT_ID = 45; |
| 138 | |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 139 | template <class D, class... Ts> |
| 140 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 141 | |
| 142 | private: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 143 | explicit UinputExternalStylus(); |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Prabir Pradhan | 484d55a | 2022-10-14 23:17:16 +0000 | [diff] [blame] | 146 | // --- UinputExternalStylusWithPressure --- |
| 147 | |
| 148 | // A stylus that reports button presses and pressure values. |
| 149 | class UinputExternalStylusWithPressure : public UinputKeyboard { |
| 150 | public: |
| 151 | static constexpr const char* DEVICE_NAME = "Test Uinput External Stylus With Pressure"; |
| 152 | static constexpr int16_t PRODUCT_ID = 46; |
| 153 | |
| 154 | static constexpr int32_t RAW_PRESSURE_MIN = 0; |
| 155 | static constexpr int32_t RAW_PRESSURE_MAX = 255; |
| 156 | |
| 157 | void setPressure(int32_t pressure); |
| 158 | |
| 159 | template <class D, class... Ts> |
| 160 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 161 | |
| 162 | private: |
| 163 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 164 | |
| 165 | explicit UinputExternalStylusWithPressure(); |
| 166 | }; |
| 167 | |
Prabir Pradhan | 37a819b | 2023-08-22 23:20:16 +0000 | [diff] [blame] | 168 | // --- UinputKeyboardWithUsage --- |
| 169 | // A keyboard that supports EV_MSC MSC_SCAN through which it can report HID usage codes. |
| 170 | |
| 171 | class UinputKeyboardWithHidUsage : public UinputKeyboard { |
| 172 | public: |
| 173 | static constexpr const char* DEVICE_NAME = "Test Uinput Keyboard With Usage"; |
| 174 | static constexpr int16_t PRODUCT_ID = 47; |
| 175 | |
| 176 | template <class D, class... Ts> |
| 177 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 178 | |
| 179 | protected: |
| 180 | explicit UinputKeyboardWithHidUsage(std::initializer_list<int> keys); |
| 181 | |
| 182 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 183 | }; |
| 184 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 185 | // --- UinputTouchScreen --- |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 186 | |
| 187 | // A multi-touch touchscreen device with specific size that also supports styluses. |
Prabir Pradhan | 124ea44 | 2022-10-28 20:27:44 +0000 | [diff] [blame] | 188 | class UinputTouchScreen : public UinputKeyboard { |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 189 | public: |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 190 | static constexpr const char* DEVICE_NAME = "Test Uinput Touch Screen"; |
Prabir Pradhan | 37a819b | 2023-08-22 23:20:16 +0000 | [diff] [blame] | 191 | static constexpr int16_t PRODUCT_ID = 48; |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 192 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 193 | static const int32_t RAW_TOUCH_MIN = 0; |
| 194 | static const int32_t RAW_TOUCH_MAX = 31; |
| 195 | static const int32_t RAW_ID_MIN = 0; |
| 196 | static const int32_t RAW_ID_MAX = 9; |
| 197 | static const int32_t RAW_SLOT_MIN = 0; |
| 198 | static const int32_t RAW_SLOT_MAX = 9; |
| 199 | static const int32_t RAW_PRESSURE_MIN = 0; |
| 200 | static const int32_t RAW_PRESSURE_MAX = 255; |
| 201 | |
| 202 | template <class D, class... Ts> |
| 203 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 204 | |
| 205 | void sendSlot(int32_t slot); |
| 206 | void sendTrackingId(int32_t trackingId); |
| 207 | void sendDown(const Point& point); |
| 208 | void sendMove(const Point& point); |
Prabir Pradhan | c09ec6d | 2023-08-14 22:31:43 +0000 | [diff] [blame] | 209 | void sendPressure(int32_t pressure); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 210 | void sendPointerUp(); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 211 | void sendUp(); |
| 212 | void sendToolType(int32_t toolType); |
Siarhei Vishniakou | fd97e9d | 2022-01-04 16:59:04 -0800 | [diff] [blame] | 213 | void sendSync(); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 214 | |
| 215 | const Point getCenterPoint(); |
| 216 | |
| 217 | protected: |
Arpit Singh | 440bf65 | 2023-08-09 09:23:43 +0000 | [diff] [blame] | 218 | explicit UinputTouchScreen(const Rect& size, const std::string& physicalPort = ""); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 219 | |
| 220 | private: |
| 221 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 222 | const Rect mSize; |
Arpit Singh | 440bf65 | 2023-08-09 09:23:43 +0000 | [diff] [blame] | 223 | const std::string mPhysicalPort; |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 224 | }; |
| 225 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 226 | } // namespace android |