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. |
| 35 | std::unique_ptr<D> dev(new D(&args...)); |
| 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; |
| 54 | |
| 55 | UinputDevice(const char* name); |
| 56 | |
| 57 | // Signals which types of events this device supports before it is created. |
| 58 | // This must be overridden by subclasses. |
| 59 | virtual void configureDevice(int fd, uinput_user_dev* device) = 0; |
| 60 | |
| 61 | void injectEvent(uint16_t type, uint16_t code, int32_t value); |
| 62 | |
| 63 | private: |
| 64 | base::unique_fd mDeviceFd; |
| 65 | |
| 66 | // This is called once by the factory method createUinputDevice(). |
| 67 | void init(); |
| 68 | }; |
| 69 | |
| 70 | // --- UinputKeyboard --- |
| 71 | |
| 72 | class UinputKeyboard : public UinputDevice { |
| 73 | public: |
| 74 | static constexpr const char* KEYBOARD_NAME = "Test Keyboard Device"; |
| 75 | |
| 76 | // Injects key press and sync. |
| 77 | void pressKey(int key); |
| 78 | // Injects key release and sync. |
| 79 | void releaseKey(int key); |
| 80 | // Injects 4 events: key press, sync, key release, and sync. |
| 81 | void pressAndReleaseKey(int key); |
| 82 | |
| 83 | template <class D, class... Ts> |
| 84 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 85 | |
| 86 | protected: |
| 87 | UinputKeyboard(std::initializer_list<int> keys = {}); |
| 88 | |
| 89 | private: |
| 90 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 91 | |
| 92 | std::set<int> mKeys; |
| 93 | }; |
| 94 | |
| 95 | // --- UinputHomeKey--- |
| 96 | |
| 97 | // A keyboard device that has a single HOME key. |
| 98 | class UinputHomeKey : public UinputKeyboard { |
| 99 | public: |
| 100 | // Injects 4 events: key press, sync, key release, and sync. |
| 101 | void pressAndReleaseHomeKey(); |
| 102 | |
| 103 | template <class D, class... Ts> |
| 104 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 105 | |
| 106 | private: |
| 107 | UinputHomeKey(); |
| 108 | }; |
| 109 | |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 110 | // A joystick device that sends a BTN_GEAR_DOWN / BTN_WHEEL key. |
| 111 | class UinputSteamController : public UinputKeyboard { |
| 112 | public: |
| 113 | template <class D, class... Ts> |
| 114 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 115 | |
| 116 | private: |
| 117 | UinputSteamController(); |
| 118 | }; |
| 119 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 120 | // --- UinputTouchScreen --- |
| 121 | // A touch screen device with specific size. |
| 122 | class UinputTouchScreen : public UinputDevice { |
| 123 | public: |
| 124 | static constexpr const char* DEVICE_NAME = "Test Touch Screen"; |
| 125 | static const int32_t RAW_TOUCH_MIN = 0; |
| 126 | static const int32_t RAW_TOUCH_MAX = 31; |
| 127 | static const int32_t RAW_ID_MIN = 0; |
| 128 | static const int32_t RAW_ID_MAX = 9; |
| 129 | static const int32_t RAW_SLOT_MIN = 0; |
| 130 | static const int32_t RAW_SLOT_MAX = 9; |
| 131 | static const int32_t RAW_PRESSURE_MIN = 0; |
| 132 | static const int32_t RAW_PRESSURE_MAX = 255; |
| 133 | |
| 134 | template <class D, class... Ts> |
| 135 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 136 | |
| 137 | void sendSlot(int32_t slot); |
| 138 | void sendTrackingId(int32_t trackingId); |
| 139 | void sendDown(const Point& point); |
| 140 | void sendMove(const Point& point); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 141 | void sendPointerUp(); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 142 | void sendUp(); |
| 143 | void sendToolType(int32_t toolType); |
Siarhei Vishniakou | fd97e9d | 2022-01-04 16:59:04 -0800 | [diff] [blame] | 144 | void sendSync(); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 145 | |
| 146 | const Point getCenterPoint(); |
| 147 | |
| 148 | protected: |
| 149 | UinputTouchScreen(const Rect* size); |
| 150 | |
| 151 | private: |
| 152 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 153 | const Rect mSize; |
| 154 | }; |
| 155 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 156 | } // namespace android |