blob: 837e70e114af25ff00c9aaef660ab1003f93e2f2 [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
Cody Heiner166a5af2023-07-07 12:25:00 -0700130 MotionEvent build() {
131 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
138 // Set mouse cursor position for the most common cases to avoid boilerplate.
139 if (mSource == AINPUT_SOURCE_MOUSE &&
140 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
141 mRawXCursorPosition = pointerCoords[0].getX();
142 mRawYCursorPosition = pointerCoords[0].getY();
143 }
144
145 MotionEvent event;
Cody Heiner166a5af2023-07-07 12:25:00 -0700146 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
147 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000148 MotionClassification::NONE, mTransform,
Cody Heiner166a5af2023-07-07 12:25:00 -0700149 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000150 mRawYCursorPosition, mRawTransform, mDownTime, mEventTime,
Cody Heiner166a5af2023-07-07 12:25:00 -0700151 mPointers.size(), pointerProperties.data(), pointerCoords.data());
152 return event;
153 }
154
155private:
156 int32_t mAction;
157 int32_t mDeviceId{DEFAULT_DEVICE_ID};
158 int32_t mSource;
159 nsecs_t mDownTime;
160 nsecs_t mEventTime;
Linnan Li13bf76a2024-05-05 19:18:02 +0800161 ui::LogicalDisplayId mDisplayId{ui::ADISPLAY_ID_DEFAULT};
Cody Heiner166a5af2023-07-07 12:25:00 -0700162 int32_t mActionButton{0};
163 int32_t mButtonState{0};
164 int32_t mFlags{0};
165 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
166 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000167 ui::Transform mTransform;
168 ui::Transform mRawTransform;
Cody Heiner166a5af2023-07-07 12:25:00 -0700169
170 std::vector<PointerBuilder> mPointers;
171};
172
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000173class KeyEventBuilder {
174public:
175 KeyEventBuilder(int32_t action, int32_t source) {
176 mAction = action;
177 mSource = source;
178 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
179 mDownTime = mEventTime;
180 }
181
182 KeyEventBuilder(const KeyEvent& event) {
183 mAction = event.getAction();
184 mDeviceId = event.getDeviceId();
185 mSource = event.getSource();
186 mDownTime = event.getDownTime();
187 mEventTime = event.getEventTime();
188 mDisplayId = event.getDisplayId();
189 mFlags = event.getFlags();
190 mKeyCode = event.getKeyCode();
191 mScanCode = event.getScanCode();
192 mMetaState = event.getMetaState();
193 mRepeatCount = event.getRepeatCount();
194 }
195
196 KeyEventBuilder& deviceId(int32_t deviceId) {
197 mDeviceId = deviceId;
198 return *this;
199 }
200
201 KeyEventBuilder& downTime(nsecs_t downTime) {
202 mDownTime = downTime;
203 return *this;
204 }
205
206 KeyEventBuilder& eventTime(nsecs_t eventTime) {
207 mEventTime = eventTime;
208 return *this;
209 }
210
Linnan Li13bf76a2024-05-05 19:18:02 +0800211 KeyEventBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000212 mDisplayId = displayId;
213 return *this;
214 }
215
216 KeyEventBuilder& policyFlags(int32_t policyFlags) {
217 mPolicyFlags = policyFlags;
218 return *this;
219 }
220
221 KeyEventBuilder& addFlag(uint32_t flags) {
222 mFlags |= flags;
223 return *this;
224 }
225
226 KeyEventBuilder& keyCode(int32_t keyCode) {
227 mKeyCode = keyCode;
228 return *this;
229 }
230
231 KeyEventBuilder& repeatCount(int32_t repeatCount) {
232 mRepeatCount = repeatCount;
233 return *this;
234 }
235
236 KeyEvent build() const {
237 KeyEvent event{};
238 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
239 mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime,
240 mEventTime);
241 return event;
242 }
243
244private:
245 int32_t mAction;
246 int32_t mDeviceId = DEFAULT_DEVICE_ID;
247 uint32_t mSource;
248 nsecs_t mDownTime;
249 nsecs_t mEventTime;
Linnan Li13bf76a2024-05-05 19:18:02 +0800250 ui::LogicalDisplayId mDisplayId{ui::ADISPLAY_ID_DEFAULT};
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000251 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
252 int32_t mFlags{0};
253 int32_t mKeyCode{AKEYCODE_UNKNOWN};
254 int32_t mScanCode{0};
255 int32_t mMetaState{AMETA_NONE};
256 int32_t mRepeatCount{0};
257};
258
Cody Heiner166a5af2023-07-07 12:25:00 -0700259} // namespace android