blob: 55e058332d9dce4efb44a8d1a887c31e8ac62e33 [file] [log] [blame]
Cody Heiner166a5af2023-07-07 12:25:00 -07001/*
2 * Copyright 2023 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
17#pragma once
18
19#include <android/input.h>
20#include <attestation/HmacKeyManager.h>
Cody Heiner166a5af2023-07-07 12:25:00 -070021#include <input/Input.h>
22#include <utils/Timers.h> // for nsecs_t, systemTime
23
24#include <vector>
25
26namespace android {
27
28// An arbitrary device id.
29static constexpr uint32_t DEFAULT_DEVICE_ID = 1;
30
31// The default policy flags to use for event injection by tests.
32static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
33
34class PointerBuilder {
35public:
36 PointerBuilder(int32_t id, ToolType toolType) {
37 mProperties.clear();
38 mProperties.id = id;
39 mProperties.toolType = toolType;
40 mCoords.clear();
41 }
42
43 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
44
45 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
46
47 PointerBuilder& axis(int32_t axis, float value) {
48 mCoords.setAxisValue(axis, value);
49 return *this;
50 }
51
52 PointerProperties buildProperties() const { return mProperties; }
53
54 PointerCoords buildCoords() const { return mCoords; }
55
56private:
57 PointerProperties mProperties;
58 PointerCoords mCoords;
59};
60
61class MotionEventBuilder {
62public:
63 MotionEventBuilder(int32_t action, int32_t source) {
64 mAction = action;
65 mSource = source;
66 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
67 mDownTime = mEventTime;
68 }
69
70 MotionEventBuilder& deviceId(int32_t deviceId) {
71 mDeviceId = deviceId;
72 return *this;
73 }
74
75 MotionEventBuilder& downTime(nsecs_t downTime) {
76 mDownTime = downTime;
77 return *this;
78 }
79
80 MotionEventBuilder& eventTime(nsecs_t eventTime) {
81 mEventTime = eventTime;
82 return *this;
83 }
84
Linnan Li13bf76a2024-05-05 19:18:02 +080085 MotionEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Cody Heiner166a5af2023-07-07 12:25:00 -070086 mDisplayId = displayId;
87 return *this;
88 }
89
90 MotionEventBuilder& actionButton(int32_t actionButton) {
91 mActionButton = actionButton;
92 return *this;
93 }
94
95 MotionEventBuilder& buttonState(int32_t buttonState) {
96 mButtonState = buttonState;
97 return *this;
98 }
99
100 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
101 mRawXCursorPosition = rawXCursorPosition;
102 return *this;
103 }
104
105 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
106 mRawYCursorPosition = rawYCursorPosition;
107 return *this;
108 }
109
110 MotionEventBuilder& pointer(PointerBuilder pointer) {
111 mPointers.push_back(pointer);
112 return *this;
113 }
114
115 MotionEventBuilder& addFlag(uint32_t flags) {
116 mFlags |= flags;
117 return *this;
118 }
119
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000120 MotionEventBuilder& transform(ui::Transform t) {
121 mTransform = t;
122 return *this;
123 }
124
125 MotionEventBuilder& rawTransform(ui::Transform t) {
126 mRawTransform = t;
127 return *this;
128 }
129
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700130 MotionEvent build() const {
Cody Heiner166a5af2023-07-07 12:25:00 -0700131 std::vector<PointerProperties> pointerProperties;
132 std::vector<PointerCoords> pointerCoords;
133 for (const PointerBuilder& pointer : mPointers) {
134 pointerProperties.push_back(pointer.buildProperties());
135 pointerCoords.push_back(pointer.buildCoords());
136 }
137
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700138 auto [xCursorPosition, yCursorPosition] =
139 std::make_pair(mRawXCursorPosition, mRawYCursorPosition);
Cody Heiner166a5af2023-07-07 12:25:00 -0700140 // Set mouse cursor position for the most common cases to avoid boilerplate.
141 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700142 !MotionEvent::isValidCursorPosition(xCursorPosition, yCursorPosition)) {
143 xCursorPosition = pointerCoords[0].getX();
144 yCursorPosition = pointerCoords[0].getY();
Cody Heiner166a5af2023-07-07 12:25:00 -0700145 }
146
147 MotionEvent event;
Cody Heiner166a5af2023-07-07 12:25:00 -0700148 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
149 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000150 MotionClassification::NONE, mTransform,
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700151 /*xPrecision=*/0, /*yPrecision=*/0, xCursorPosition, yCursorPosition,
152 mRawTransform, mDownTime, mEventTime, mPointers.size(),
153 pointerProperties.data(), pointerCoords.data());
Cody Heiner166a5af2023-07-07 12:25:00 -0700154 return event;
155 }
156
157private:
158 int32_t mAction;
159 int32_t mDeviceId{DEFAULT_DEVICE_ID};
160 int32_t mSource;
161 nsecs_t mDownTime;
162 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700163 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Cody Heiner166a5af2023-07-07 12:25:00 -0700164 int32_t mActionButton{0};
165 int32_t mButtonState{0};
166 int32_t mFlags{0};
167 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
168 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000169 ui::Transform mTransform;
170 ui::Transform mRawTransform;
Cody Heiner166a5af2023-07-07 12:25:00 -0700171
172 std::vector<PointerBuilder> mPointers;
173};
174
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000175class KeyEventBuilder {
176public:
177 KeyEventBuilder(int32_t action, int32_t source) {
178 mAction = action;
179 mSource = source;
180 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
181 mDownTime = mEventTime;
182 }
183
184 KeyEventBuilder(const KeyEvent& event) {
185 mAction = event.getAction();
186 mDeviceId = event.getDeviceId();
187 mSource = event.getSource();
188 mDownTime = event.getDownTime();
189 mEventTime = event.getEventTime();
190 mDisplayId = event.getDisplayId();
191 mFlags = event.getFlags();
192 mKeyCode = event.getKeyCode();
193 mScanCode = event.getScanCode();
194 mMetaState = event.getMetaState();
195 mRepeatCount = event.getRepeatCount();
196 }
197
198 KeyEventBuilder& deviceId(int32_t deviceId) {
199 mDeviceId = deviceId;
200 return *this;
201 }
202
203 KeyEventBuilder& downTime(nsecs_t downTime) {
204 mDownTime = downTime;
205 return *this;
206 }
207
208 KeyEventBuilder& eventTime(nsecs_t eventTime) {
209 mEventTime = eventTime;
210 return *this;
211 }
212
Linnan Li13bf76a2024-05-05 19:18:02 +0800213 KeyEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000214 mDisplayId = displayId;
215 return *this;
216 }
217
218 KeyEventBuilder& policyFlags(int32_t policyFlags) {
219 mPolicyFlags = policyFlags;
220 return *this;
221 }
222
223 KeyEventBuilder& addFlag(uint32_t flags) {
224 mFlags |= flags;
225 return *this;
226 }
227
228 KeyEventBuilder& keyCode(int32_t keyCode) {
229 mKeyCode = keyCode;
230 return *this;
231 }
232
233 KeyEventBuilder& repeatCount(int32_t repeatCount) {
234 mRepeatCount = repeatCount;
235 return *this;
236 }
237
238 KeyEvent build() const {
239 KeyEvent event{};
240 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
241 mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime,
242 mEventTime);
243 return event;
244 }
245
246private:
247 int32_t mAction;
248 int32_t mDeviceId = DEFAULT_DEVICE_ID;
249 uint32_t mSource;
250 nsecs_t mDownTime;
251 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700252 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000253 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
254 int32_t mFlags{0};
255 int32_t mKeyCode{AKEYCODE_UNKNOWN};
256 int32_t mScanCode{0};
257 int32_t mMetaState{AMETA_NONE};
258 int32_t mRepeatCount{0};
259};
260
Cody Heiner166a5af2023-07-07 12:25:00 -0700261} // namespace android