blob: 1899a661594ded31c36941dfad4bbfe4c26bac7f [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>
Paul Ramirez7dfaa322024-08-21 17:44:45 +000022#include <input/InputTransport.h>
23#include <ui/LogicalDisplayId.h>
Paul Ramirez8f87ade2024-10-01 15:45:39 +000024#include <ui/Transform.h>
Cody Heiner166a5af2023-07-07 12:25:00 -070025#include <utils/Timers.h> // for nsecs_t, systemTime
26
27#include <vector>
28
29namespace android {
30
31// An arbitrary device id.
32static constexpr uint32_t DEFAULT_DEVICE_ID = 1;
33
34// The default policy flags to use for event injection by tests.
35static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
36
37class PointerBuilder {
38public:
39 PointerBuilder(int32_t id, ToolType toolType) {
40 mProperties.clear();
41 mProperties.id = id;
42 mProperties.toolType = toolType;
43 mCoords.clear();
44 }
45
46 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
47
48 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
49
Paul Ramirez7dfaa322024-08-21 17:44:45 +000050 PointerBuilder& isResampled(bool isResampled) {
51 mCoords.isResampled = isResampled;
52 return *this;
53 }
54
Cody Heiner166a5af2023-07-07 12:25:00 -070055 PointerBuilder& axis(int32_t axis, float value) {
56 mCoords.setAxisValue(axis, value);
57 return *this;
58 }
59
60 PointerProperties buildProperties() const { return mProperties; }
61
62 PointerCoords buildCoords() const { return mCoords; }
63
64private:
65 PointerProperties mProperties;
66 PointerCoords mCoords;
67};
68
Paul Ramirez7dfaa322024-08-21 17:44:45 +000069class InputMessageBuilder {
70public:
71 InputMessageBuilder(InputMessage::Type type, uint32_t seq) : mType{type}, mSeq{seq} {}
72
73 InputMessageBuilder& eventId(int32_t eventId) {
74 mEventId = eventId;
75 return *this;
76 }
77
78 InputMessageBuilder& eventTime(nsecs_t eventTime) {
79 mEventTime = eventTime;
80 return *this;
81 }
82
83 InputMessageBuilder& deviceId(DeviceId deviceId) {
84 mDeviceId = deviceId;
85 return *this;
86 }
87
88 InputMessageBuilder& source(int32_t source) {
89 mSource = source;
90 return *this;
91 }
92
93 InputMessageBuilder& displayId(ui::LogicalDisplayId displayId) {
94 mDisplayId = displayId;
95 return *this;
96 }
97
Paul Ramirez8f87ade2024-10-01 15:45:39 +000098 InputMessageBuilder& hmac(const std::array<uint8_t, 32>& hmac) {
99 mHmac = hmac;
100 return *this;
101 }
102
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000103 InputMessageBuilder& action(int32_t action) {
104 mAction = action;
105 return *this;
106 }
107
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000108 InputMessageBuilder& actionButton(int32_t actionButton) {
109 mActionButton = actionButton;
110 return *this;
111 }
112
113 InputMessageBuilder& flags(int32_t flags) {
114 mFlags = flags;
115 return *this;
116 }
117
118 InputMessageBuilder& metaState(int32_t metaState) {
119 mMetaState = metaState;
120 return *this;
121 }
122
123 InputMessageBuilder& buttonState(int32_t buttonState) {
124 mButtonState = buttonState;
125 return *this;
126 }
127
128 InputMessageBuilder& classification(MotionClassification classification) {
129 mClassification = classification;
130 return *this;
131 }
132
133 InputMessageBuilder& edgeFlags(int32_t edgeFlags) {
134 mEdgeFlags = edgeFlags;
135 return *this;
136 }
137
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000138 InputMessageBuilder& downTime(nsecs_t downTime) {
139 mDownTime = downTime;
140 return *this;
141 }
142
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000143 InputMessageBuilder& transform(const ui::Transform& transform) {
144 mTransform = transform;
145 return *this;
146 }
147
148 InputMessageBuilder& xPrecision(float xPrecision) {
149 mXPrecision = xPrecision;
150 return *this;
151 }
152
153 InputMessageBuilder& yPrecision(float yPrecision) {
154 mYPrecision = yPrecision;
155 return *this;
156 }
157
158 InputMessageBuilder& xCursorPosition(float xCursorPosition) {
159 mXCursorPosition = xCursorPosition;
160 return *this;
161 }
162
163 InputMessageBuilder& yCursorPosition(float yCursorPosition) {
164 mYCursorPosition = yCursorPosition;
165 return *this;
166 }
167
168 InputMessageBuilder& rawTransform(const ui::Transform& rawTransform) {
169 mRawTransform = rawTransform;
170 return *this;
171 }
172
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000173 InputMessageBuilder& pointer(PointerBuilder pointerBuilder) {
174 mPointers.push_back(pointerBuilder);
175 return *this;
176 }
177
178 InputMessage build() const {
179 InputMessage message{};
180 // Header
181 message.header.type = mType;
182 message.header.seq = mSeq;
183 // Body
184 message.body.motion.eventId = mEventId;
185 message.body.motion.pointerCount = mPointers.size();
186 message.body.motion.eventTime = mEventTime;
187 message.body.motion.deviceId = mDeviceId;
188 message.body.motion.source = mSource;
189 message.body.motion.displayId = mDisplayId.val();
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000190 message.body.motion.hmac = std::move(mHmac);
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000191 message.body.motion.action = mAction;
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000192 message.body.motion.actionButton = mActionButton;
193 message.body.motion.flags = mFlags;
194 message.body.motion.metaState = mMetaState;
195 message.body.motion.buttonState = mButtonState;
196 message.body.motion.edgeFlags = mEdgeFlags;
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000197 message.body.motion.downTime = mDownTime;
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000198 message.body.motion.dsdx = mTransform.dsdx();
199 message.body.motion.dtdx = mTransform.dtdx();
200 message.body.motion.dtdy = mTransform.dtdy();
201 message.body.motion.dsdy = mTransform.dsdy();
202 message.body.motion.tx = mTransform.ty();
203 message.body.motion.ty = mTransform.tx();
204 message.body.motion.xPrecision = mXPrecision;
205 message.body.motion.yPrecision = mYPrecision;
206 message.body.motion.xCursorPosition = mXCursorPosition;
207 message.body.motion.yCursorPosition = mYCursorPosition;
208 message.body.motion.dsdxRaw = mRawTransform.dsdx();
209 message.body.motion.dtdxRaw = mRawTransform.dtdx();
210 message.body.motion.dtdyRaw = mRawTransform.dtdy();
211 message.body.motion.dsdyRaw = mRawTransform.dsdy();
212 message.body.motion.txRaw = mRawTransform.ty();
213 message.body.motion.tyRaw = mRawTransform.tx();
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000214
215 for (size_t i = 0; i < mPointers.size(); ++i) {
216 message.body.motion.pointers[i].properties = mPointers[i].buildProperties();
217 message.body.motion.pointers[i].coords = mPointers[i].buildCoords();
218 }
219 return message;
220 }
221
222private:
223 const InputMessage::Type mType;
224 const uint32_t mSeq;
225
226 int32_t mEventId{InputEvent::nextId()};
227 nsecs_t mEventTime{systemTime(SYSTEM_TIME_MONOTONIC)};
228 DeviceId mDeviceId{DEFAULT_DEVICE_ID};
229 int32_t mSource{AINPUT_SOURCE_TOUCHSCREEN};
230 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000231 std::array<uint8_t, 32> mHmac{INVALID_HMAC};
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000232 int32_t mAction{AMOTION_EVENT_ACTION_MOVE};
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000233 int32_t mActionButton{0};
234 int32_t mFlags{0};
235 int32_t mMetaState{AMETA_NONE};
236 int32_t mButtonState{0};
237 MotionClassification mClassification{MotionClassification::NONE};
238 int32_t mEdgeFlags{0};
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000239 nsecs_t mDownTime{mEventTime};
Paul Ramirez8f87ade2024-10-01 15:45:39 +0000240 ui::Transform mTransform{};
241 float mXPrecision{1.0f};
242 float mYPrecision{1.0f};
243 float mXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
244 float mYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
245 ui::Transform mRawTransform{};
Paul Ramirez7dfaa322024-08-21 17:44:45 +0000246 std::vector<PointerBuilder> mPointers;
247};
248
Cody Heiner166a5af2023-07-07 12:25:00 -0700249class MotionEventBuilder {
250public:
251 MotionEventBuilder(int32_t action, int32_t source) {
252 mAction = action;
253 mSource = source;
254 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
255 mDownTime = mEventTime;
256 }
257
258 MotionEventBuilder& deviceId(int32_t deviceId) {
259 mDeviceId = deviceId;
260 return *this;
261 }
262
263 MotionEventBuilder& downTime(nsecs_t downTime) {
264 mDownTime = downTime;
265 return *this;
266 }
267
268 MotionEventBuilder& eventTime(nsecs_t eventTime) {
269 mEventTime = eventTime;
270 return *this;
271 }
272
Linnan Li13bf76a2024-05-05 19:18:02 +0800273 MotionEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Cody Heiner166a5af2023-07-07 12:25:00 -0700274 mDisplayId = displayId;
275 return *this;
276 }
277
278 MotionEventBuilder& actionButton(int32_t actionButton) {
279 mActionButton = actionButton;
280 return *this;
281 }
282
283 MotionEventBuilder& buttonState(int32_t buttonState) {
284 mButtonState = buttonState;
285 return *this;
286 }
287
288 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
289 mRawXCursorPosition = rawXCursorPosition;
290 return *this;
291 }
292
293 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
294 mRawYCursorPosition = rawYCursorPosition;
295 return *this;
296 }
297
298 MotionEventBuilder& pointer(PointerBuilder pointer) {
299 mPointers.push_back(pointer);
300 return *this;
301 }
302
303 MotionEventBuilder& addFlag(uint32_t flags) {
304 mFlags |= flags;
305 return *this;
306 }
307
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000308 MotionEventBuilder& transform(ui::Transform t) {
309 mTransform = t;
310 return *this;
311 }
312
313 MotionEventBuilder& rawTransform(ui::Transform t) {
314 mRawTransform = t;
315 return *this;
316 }
317
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700318 MotionEvent build() const {
Cody Heiner166a5af2023-07-07 12:25:00 -0700319 std::vector<PointerProperties> pointerProperties;
320 std::vector<PointerCoords> pointerCoords;
321 for (const PointerBuilder& pointer : mPointers) {
322 pointerProperties.push_back(pointer.buildProperties());
323 pointerCoords.push_back(pointer.buildCoords());
324 }
325
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700326 auto [xCursorPosition, yCursorPosition] =
327 std::make_pair(mRawXCursorPosition, mRawYCursorPosition);
Cody Heiner166a5af2023-07-07 12:25:00 -0700328 // Set mouse cursor position for the most common cases to avoid boilerplate.
329 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700330 !MotionEvent::isValidCursorPosition(xCursorPosition, yCursorPosition)) {
331 xCursorPosition = pointerCoords[0].getX();
332 yCursorPosition = pointerCoords[0].getY();
Cody Heiner166a5af2023-07-07 12:25:00 -0700333 }
334
335 MotionEvent event;
Cody Heiner166a5af2023-07-07 12:25:00 -0700336 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
337 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000338 MotionClassification::NONE, mTransform,
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700339 /*xPrecision=*/0, /*yPrecision=*/0, xCursorPosition, yCursorPosition,
340 mRawTransform, mDownTime, mEventTime, mPointers.size(),
341 pointerProperties.data(), pointerCoords.data());
Cody Heiner166a5af2023-07-07 12:25:00 -0700342 return event;
343 }
344
345private:
346 int32_t mAction;
347 int32_t mDeviceId{DEFAULT_DEVICE_ID};
348 int32_t mSource;
349 nsecs_t mDownTime;
350 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700351 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Cody Heiner166a5af2023-07-07 12:25:00 -0700352 int32_t mActionButton{0};
353 int32_t mButtonState{0};
354 int32_t mFlags{0};
355 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
356 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000357 ui::Transform mTransform;
358 ui::Transform mRawTransform;
Cody Heiner166a5af2023-07-07 12:25:00 -0700359
360 std::vector<PointerBuilder> mPointers;
361};
362
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000363class KeyEventBuilder {
364public:
365 KeyEventBuilder(int32_t action, int32_t source) {
366 mAction = action;
367 mSource = source;
368 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
369 mDownTime = mEventTime;
370 }
371
372 KeyEventBuilder(const KeyEvent& event) {
373 mAction = event.getAction();
374 mDeviceId = event.getDeviceId();
375 mSource = event.getSource();
376 mDownTime = event.getDownTime();
377 mEventTime = event.getEventTime();
378 mDisplayId = event.getDisplayId();
379 mFlags = event.getFlags();
380 mKeyCode = event.getKeyCode();
381 mScanCode = event.getScanCode();
382 mMetaState = event.getMetaState();
383 mRepeatCount = event.getRepeatCount();
384 }
385
386 KeyEventBuilder& deviceId(int32_t deviceId) {
387 mDeviceId = deviceId;
388 return *this;
389 }
390
391 KeyEventBuilder& downTime(nsecs_t downTime) {
392 mDownTime = downTime;
393 return *this;
394 }
395
396 KeyEventBuilder& eventTime(nsecs_t eventTime) {
397 mEventTime = eventTime;
398 return *this;
399 }
400
Linnan Li13bf76a2024-05-05 19:18:02 +0800401 KeyEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000402 mDisplayId = displayId;
403 return *this;
404 }
405
406 KeyEventBuilder& policyFlags(int32_t policyFlags) {
407 mPolicyFlags = policyFlags;
408 return *this;
409 }
410
411 KeyEventBuilder& addFlag(uint32_t flags) {
412 mFlags |= flags;
413 return *this;
414 }
415
416 KeyEventBuilder& keyCode(int32_t keyCode) {
417 mKeyCode = keyCode;
418 return *this;
419 }
420
421 KeyEventBuilder& repeatCount(int32_t repeatCount) {
422 mRepeatCount = repeatCount;
423 return *this;
424 }
425
426 KeyEvent build() const {
427 KeyEvent event{};
428 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
429 mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime,
430 mEventTime);
431 return event;
432 }
433
434private:
435 int32_t mAction;
436 int32_t mDeviceId = DEFAULT_DEVICE_ID;
437 uint32_t mSource;
438 nsecs_t mDownTime;
439 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700440 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000441 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
442 int32_t mFlags{0};
443 int32_t mKeyCode{AKEYCODE_UNKNOWN};
444 int32_t mScanCode{0};
445 int32_t mMetaState{AMETA_NONE};
446 int32_t mRepeatCount{0};
447};
448
Cody Heiner166a5af2023-07-07 12:25:00 -0700449} // namespace android