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