blob: 09438e996756fda977418b3a06e411c449b186c8 [file] [log] [blame]
Prabir Pradhan9205e422023-05-16 20:06:13 +00001/*
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 <NotifyArgs.h>
20#include <android/input.h>
21#include <attestation/HmacKeyManager.h>
22#include <input/Input.h>
23#include <vector>
24
25namespace android {
26
27// An arbitrary device id.
28static constexpr uint32_t DEFAULT_DEVICE_ID = 1;
29
30// The default policy flags to use for event injection by tests.
31static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
32
33class PointerBuilder {
34public:
35 PointerBuilder(int32_t id, ToolType toolType) {
36 mProperties.clear();
37 mProperties.id = id;
38 mProperties.toolType = toolType;
39 mCoords.clear();
40 }
41
42 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
43
44 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
45
46 PointerBuilder& axis(int32_t axis, float value) {
47 mCoords.setAxisValue(axis, value);
48 return *this;
49 }
50
51 PointerProperties buildProperties() const { return mProperties; }
52
53 PointerCoords buildCoords() const { return mCoords; }
54
55private:
56 PointerProperties mProperties;
57 PointerCoords mCoords;
58};
59
60class MotionEventBuilder {
61public:
62 MotionEventBuilder(int32_t action, int32_t source) {
63 mAction = action;
64 mSource = source;
65 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
66 mDownTime = mEventTime;
67 }
68
69 MotionEventBuilder& deviceId(int32_t deviceId) {
70 mDeviceId = deviceId;
71 return *this;
72 }
73
74 MotionEventBuilder& downTime(nsecs_t downTime) {
75 mDownTime = downTime;
76 return *this;
77 }
78
79 MotionEventBuilder& eventTime(nsecs_t eventTime) {
80 mEventTime = eventTime;
81 return *this;
82 }
83
84 MotionEventBuilder& displayId(int32_t displayId) {
85 mDisplayId = displayId;
86 return *this;
87 }
88
89 MotionEventBuilder& actionButton(int32_t actionButton) {
90 mActionButton = actionButton;
91 return *this;
92 }
93
94 MotionEventBuilder& buttonState(int32_t buttonState) {
95 mButtonState = buttonState;
96 return *this;
97 }
98
99 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
100 mRawXCursorPosition = rawXCursorPosition;
101 return *this;
102 }
103
104 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
105 mRawYCursorPosition = rawYCursorPosition;
106 return *this;
107 }
108
109 MotionEventBuilder& pointer(PointerBuilder pointer) {
110 mPointers.push_back(pointer);
111 return *this;
112 }
113
114 MotionEventBuilder& addFlag(uint32_t flags) {
115 mFlags |= flags;
116 return *this;
117 }
118
119 MotionEvent build() {
120 std::vector<PointerProperties> pointerProperties;
121 std::vector<PointerCoords> pointerCoords;
122 for (const PointerBuilder& pointer : mPointers) {
123 pointerProperties.push_back(pointer.buildProperties());
124 pointerCoords.push_back(pointer.buildCoords());
125 }
126
127 // Set mouse cursor position for the most common cases to avoid boilerplate.
128 if (mSource == AINPUT_SOURCE_MOUSE &&
129 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
130 mRawXCursorPosition = pointerCoords[0].getX();
131 mRawYCursorPosition = pointerCoords[0].getY();
132 }
133
134 MotionEvent event;
135 static const ui::Transform kIdentityTransform;
136 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
137 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState,
138 MotionClassification::NONE, kIdentityTransform,
139 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
140 mRawYCursorPosition, kIdentityTransform, mDownTime, mEventTime,
141 mPointers.size(), pointerProperties.data(), pointerCoords.data());
142 return event;
143 }
144
145private:
146 int32_t mAction;
147 int32_t mDeviceId{DEFAULT_DEVICE_ID};
148 int32_t mSource;
149 nsecs_t mDownTime;
150 nsecs_t mEventTime;
151 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
152 int32_t mActionButton{0};
153 int32_t mButtonState{0};
154 int32_t mFlags{0};
155 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
156 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
157
158 std::vector<PointerBuilder> mPointers;
159};
160
161class MotionArgsBuilder {
162public:
163 MotionArgsBuilder(int32_t action, int32_t source) {
164 mAction = action;
165 mSource = source;
166 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
167 mDownTime = mEventTime;
168 }
169
170 MotionArgsBuilder& deviceId(int32_t deviceId) {
171 mDeviceId = deviceId;
172 return *this;
173 }
174
175 MotionArgsBuilder& downTime(nsecs_t downTime) {
176 mDownTime = downTime;
177 return *this;
178 }
179
180 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
181 mEventTime = eventTime;
182 return *this;
183 }
184
185 MotionArgsBuilder& displayId(int32_t displayId) {
186 mDisplayId = displayId;
187 return *this;
188 }
189
190 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
191 mPolicyFlags = policyFlags;
192 return *this;
193 }
194
195 MotionArgsBuilder& actionButton(int32_t actionButton) {
196 mActionButton = actionButton;
197 return *this;
198 }
199
200 MotionArgsBuilder& buttonState(int32_t buttonState) {
201 mButtonState = buttonState;
202 return *this;
203 }
204
205 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
206 mRawXCursorPosition = rawXCursorPosition;
207 return *this;
208 }
209
210 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
211 mRawYCursorPosition = rawYCursorPosition;
212 return *this;
213 }
214
215 MotionArgsBuilder& pointer(PointerBuilder pointer) {
216 mPointers.push_back(pointer);
217 return *this;
218 }
219
220 MotionArgsBuilder& addFlag(uint32_t flags) {
221 mFlags |= flags;
222 return *this;
223 }
224
225 MotionArgsBuilder& classification(MotionClassification classification) {
226 mClassification = classification;
227 return *this;
228 }
229
230 NotifyMotionArgs build() {
231 std::vector<PointerProperties> pointerProperties;
232 std::vector<PointerCoords> pointerCoords;
233 for (const PointerBuilder& pointer : mPointers) {
234 pointerProperties.push_back(pointer.buildProperties());
235 pointerCoords.push_back(pointer.buildCoords());
236 }
237
238 // Set mouse cursor position for the most common cases to avoid boilerplate.
239 if (mSource == AINPUT_SOURCE_MOUSE &&
240 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
241 mRawXCursorPosition = pointerCoords[0].getX();
242 mRawYCursorPosition = pointerCoords[0].getY();
243 }
244
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700245 if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
246 addFlag(AMOTION_EVENT_FLAG_CANCELED);
247 }
248
Prabir Pradhan9205e422023-05-16 20:06:13 +0000249 return {InputEvent::nextId(),
250 mEventTime,
251 /*readTime=*/mEventTime,
252 mDeviceId,
253 mSource,
254 mDisplayId,
255 mPolicyFlags,
256 mAction,
257 mActionButton,
258 mFlags,
259 AMETA_NONE,
260 mButtonState,
261 mClassification,
262 /*edgeFlags=*/0,
263 static_cast<uint32_t>(mPointers.size()),
264 pointerProperties.data(),
265 pointerCoords.data(),
266 /*xPrecision=*/0,
267 /*yPrecision=*/0,
268 mRawXCursorPosition,
269 mRawYCursorPosition,
270 mDownTime,
271 /*videoFrames=*/{}};
272 }
273
274private:
275 int32_t mAction;
276 int32_t mDeviceId{DEFAULT_DEVICE_ID};
277 uint32_t mSource;
278 nsecs_t mDownTime;
279 nsecs_t mEventTime;
280 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
281 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
282 int32_t mActionButton{0};
283 int32_t mButtonState{0};
284 int32_t mFlags{0};
285 MotionClassification mClassification{MotionClassification::NONE};
286 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
287 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
288
289 std::vector<PointerBuilder> mPointers;
290};
291
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000292class KeyArgsBuilder {
293public:
294 KeyArgsBuilder(int32_t action, int32_t source) {
295 mAction = action;
296 mSource = source;
297 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
298 mDownTime = mEventTime;
299 }
300
301 KeyArgsBuilder& deviceId(int32_t deviceId) {
302 mDeviceId = deviceId;
303 return *this;
304 }
305
306 KeyArgsBuilder& downTime(nsecs_t downTime) {
307 mDownTime = downTime;
308 return *this;
309 }
310
311 KeyArgsBuilder& eventTime(nsecs_t eventTime) {
312 mEventTime = eventTime;
313 return *this;
314 }
315
316 KeyArgsBuilder& displayId(int32_t displayId) {
317 mDisplayId = displayId;
318 return *this;
319 }
320
321 KeyArgsBuilder& policyFlags(int32_t policyFlags) {
322 mPolicyFlags = policyFlags;
323 return *this;
324 }
325
326 KeyArgsBuilder& addFlag(uint32_t flags) {
327 mFlags |= flags;
328 return *this;
329 }
330
331 KeyArgsBuilder& keyCode(int32_t keyCode) {
332 mKeyCode = keyCode;
333 return *this;
334 }
335
336 NotifyKeyArgs build() const {
337 return {InputEvent::nextId(),
338 mEventTime,
339 /*readTime=*/mEventTime,
340 mDeviceId,
341 mSource,
342 mDisplayId,
343 mPolicyFlags,
344 mAction,
345 mFlags,
346 mKeyCode,
347 mScanCode,
348 mMetaState,
349 mDownTime};
350 }
351
352private:
353 int32_t mAction;
354 int32_t mDeviceId = DEFAULT_DEVICE_ID;
355 uint32_t mSource;
356 nsecs_t mDownTime;
357 nsecs_t mEventTime;
358 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
359 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
360 int32_t mFlags{0};
361 int32_t mKeyCode{AKEYCODE_UNKNOWN};
362 int32_t mScanCode{0};
363 int32_t mMetaState{AMETA_NONE};
364};
365
Prabir Pradhan9205e422023-05-16 20:06:13 +0000366} // namespace android