blob: e0ff8c3d4c73d16ebf2bc78b1375e27dbf5182dd [file] [log] [blame]
Prabir Pradhane0105c92019-12-26 12:32:13 -08001/*
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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhane0105c92019-12-26 12:32:13 -080018
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 Hungaab25622020-01-16 11:22:11 +080024#include <ui/Point.h>
25#include <ui/Rect.h>
Prabir Pradhane0105c92019-12-26 12:32:13 -080026
27#include <memory>
28
29namespace android {
30
31// This is the factory method that must be used to create a UinputDevice.
32template <class D, class... Ts>
33std::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
42class UinputDevice {
43public:
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
52protected:
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
63private:
64 base::unique_fd mDeviceFd;
65
66 // This is called once by the factory method createUinputDevice().
67 void init();
68};
69
70// --- UinputKeyboard ---
71
72class UinputKeyboard : public UinputDevice {
73public:
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
86protected:
87 UinputKeyboard(std::initializer_list<int> keys = {});
88
89private:
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.
98class UinputHomeKey : public UinputKeyboard {
99public:
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
106private:
107 UinputHomeKey();
108};
109
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700110// A joystick device that sends a BTN_GEAR_DOWN / BTN_WHEEL key.
111class UinputSteamController : public UinputKeyboard {
112public:
113 template <class D, class... Ts>
114 friend std::unique_ptr<D> createUinputDevice(Ts... args);
115
116private:
117 UinputSteamController();
118};
119
Arthur Hungaab25622020-01-16 11:22:11 +0800120// --- UinputTouchScreen ---
121// A touch screen device with specific size.
122class UinputTouchScreen : public UinputDevice {
123public:
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);
arthurhungcc7f9802020-04-30 17:55:40 +0800141 void sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +0800142 void sendUp();
143 void sendToolType(int32_t toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800144 void sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +0800145
146 const Point getCenterPoint();
147
148protected:
149 UinputTouchScreen(const Rect* size);
150
151private:
152 void configureDevice(int fd, uinput_user_dev* device) override;
153 const Rect mSize;
154};
155
Prabir Pradhane0105c92019-12-26 12:32:13 -0800156} // namespace android