blob: 1696a6269334b302a6c9b4eeb444c9ed1b241e6a [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;
Siarhei Vishniakou67b101b2024-10-12 00:42:44 +0000253 if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
254 mFlags |= AMOTION_EVENT_FLAG_CANCELED;
255 }
Cody Heiner166a5af2023-07-07 12:25:00 -0700256 mSource = source;
257 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
258 mDownTime = mEventTime;
259 }
260
261 MotionEventBuilder& deviceId(int32_t deviceId) {
262 mDeviceId = deviceId;
263 return *this;
264 }
265
266 MotionEventBuilder& downTime(nsecs_t downTime) {
267 mDownTime = downTime;
268 return *this;
269 }
270
271 MotionEventBuilder& eventTime(nsecs_t eventTime) {
272 mEventTime = eventTime;
273 return *this;
274 }
275
Linnan Li13bf76a2024-05-05 19:18:02 +0800276 MotionEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Cody Heiner166a5af2023-07-07 12:25:00 -0700277 mDisplayId = displayId;
278 return *this;
279 }
280
281 MotionEventBuilder& actionButton(int32_t actionButton) {
282 mActionButton = actionButton;
283 return *this;
284 }
285
286 MotionEventBuilder& buttonState(int32_t buttonState) {
287 mButtonState = buttonState;
288 return *this;
289 }
290
291 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
292 mRawXCursorPosition = rawXCursorPosition;
293 return *this;
294 }
295
296 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
297 mRawYCursorPosition = rawYCursorPosition;
298 return *this;
299 }
300
301 MotionEventBuilder& pointer(PointerBuilder pointer) {
302 mPointers.push_back(pointer);
303 return *this;
304 }
305
306 MotionEventBuilder& addFlag(uint32_t flags) {
307 mFlags |= flags;
308 return *this;
309 }
310
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000311 MotionEventBuilder& transform(ui::Transform t) {
312 mTransform = t;
313 return *this;
314 }
315
316 MotionEventBuilder& rawTransform(ui::Transform t) {
317 mRawTransform = t;
318 return *this;
319 }
320
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700321 MotionEvent build() const {
Cody Heiner166a5af2023-07-07 12:25:00 -0700322 std::vector<PointerProperties> pointerProperties;
323 std::vector<PointerCoords> pointerCoords;
324 for (const PointerBuilder& pointer : mPointers) {
325 pointerProperties.push_back(pointer.buildProperties());
326 pointerCoords.push_back(pointer.buildCoords());
327 }
328
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700329 auto [xCursorPosition, yCursorPosition] =
330 std::make_pair(mRawXCursorPosition, mRawYCursorPosition);
Cody Heiner166a5af2023-07-07 12:25:00 -0700331 // Set mouse cursor position for the most common cases to avoid boilerplate.
332 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700333 !MotionEvent::isValidCursorPosition(xCursorPosition, yCursorPosition)) {
334 xCursorPosition = pointerCoords[0].getX();
335 yCursorPosition = pointerCoords[0].getY();
Cody Heiner166a5af2023-07-07 12:25:00 -0700336 }
337
338 MotionEvent event;
Cody Heiner166a5af2023-07-07 12:25:00 -0700339 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
340 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000341 MotionClassification::NONE, mTransform,
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -0700342 /*xPrecision=*/0, /*yPrecision=*/0, xCursorPosition, yCursorPosition,
343 mRawTransform, mDownTime, mEventTime, mPointers.size(),
344 pointerProperties.data(), pointerCoords.data());
Cody Heiner166a5af2023-07-07 12:25:00 -0700345 return event;
346 }
347
348private:
349 int32_t mAction;
350 int32_t mDeviceId{DEFAULT_DEVICE_ID};
351 int32_t mSource;
352 nsecs_t mDownTime;
353 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700354 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Cody Heiner166a5af2023-07-07 12:25:00 -0700355 int32_t mActionButton{0};
356 int32_t mButtonState{0};
357 int32_t mFlags{0};
358 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
359 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000360 ui::Transform mTransform;
361 ui::Transform mRawTransform;
Cody Heiner166a5af2023-07-07 12:25:00 -0700362
363 std::vector<PointerBuilder> mPointers;
364};
365
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000366class KeyEventBuilder {
367public:
368 KeyEventBuilder(int32_t action, int32_t source) {
369 mAction = action;
370 mSource = source;
371 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
372 mDownTime = mEventTime;
373 }
374
375 KeyEventBuilder(const KeyEvent& event) {
376 mAction = event.getAction();
377 mDeviceId = event.getDeviceId();
378 mSource = event.getSource();
379 mDownTime = event.getDownTime();
380 mEventTime = event.getEventTime();
381 mDisplayId = event.getDisplayId();
382 mFlags = event.getFlags();
383 mKeyCode = event.getKeyCode();
384 mScanCode = event.getScanCode();
385 mMetaState = event.getMetaState();
386 mRepeatCount = event.getRepeatCount();
387 }
388
389 KeyEventBuilder& deviceId(int32_t deviceId) {
390 mDeviceId = deviceId;
391 return *this;
392 }
393
394 KeyEventBuilder& downTime(nsecs_t downTime) {
395 mDownTime = downTime;
396 return *this;
397 }
398
399 KeyEventBuilder& eventTime(nsecs_t eventTime) {
400 mEventTime = eventTime;
401 return *this;
402 }
403
Linnan Li13bf76a2024-05-05 19:18:02 +0800404 KeyEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000405 mDisplayId = displayId;
406 return *this;
407 }
408
409 KeyEventBuilder& policyFlags(int32_t policyFlags) {
410 mPolicyFlags = policyFlags;
411 return *this;
412 }
413
414 KeyEventBuilder& addFlag(uint32_t flags) {
415 mFlags |= flags;
416 return *this;
417 }
418
419 KeyEventBuilder& keyCode(int32_t keyCode) {
420 mKeyCode = keyCode;
421 return *this;
422 }
423
424 KeyEventBuilder& repeatCount(int32_t repeatCount) {
425 mRepeatCount = repeatCount;
426 return *this;
427 }
428
429 KeyEvent build() const {
430 KeyEvent event{};
431 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
432 mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime,
433 mEventTime);
434 return event;
435 }
436
437private:
438 int32_t mAction;
439 int32_t mDeviceId = DEFAULT_DEVICE_ID;
440 uint32_t mSource;
441 nsecs_t mDownTime;
442 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700443 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000444 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
445 int32_t mFlags{0};
446 int32_t mKeyCode{AKEYCODE_UNKNOWN};
447 int32_t mScanCode{0};
448 int32_t mMetaState{AMETA_NONE};
449 int32_t mRepeatCount{0};
450};
451
Cody Heiner166a5af2023-07-07 12:25:00 -0700452} // namespace android