blob: 8ffbc11a13e90008e454619ddb11e246957f0397 [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>
Cody Heiner166a5af2023-07-07 12:25:00 -070022#include <gui/constants.h>
Prabir Pradhan9205e422023-05-16 20:06:13 +000023#include <input/Input.h>
Cody Heiner166a5af2023-07-07 12:25:00 -070024#include <input/InputEventBuilders.h>
25#include <utils/Timers.h> // for nsecs_t, systemTime
26
Prabir Pradhan9205e422023-05-16 20:06:13 +000027#include <vector>
28
29namespace android {
30
Prabir Pradhan9205e422023-05-16 20:06:13 +000031class MotionArgsBuilder {
32public:
33 MotionArgsBuilder(int32_t action, int32_t source) {
34 mAction = action;
35 mSource = source;
36 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
37 mDownTime = mEventTime;
38 }
39
40 MotionArgsBuilder& deviceId(int32_t deviceId) {
41 mDeviceId = deviceId;
42 return *this;
43 }
44
45 MotionArgsBuilder& downTime(nsecs_t downTime) {
46 mDownTime = downTime;
47 return *this;
48 }
49
50 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
51 mEventTime = eventTime;
52 return *this;
53 }
54
55 MotionArgsBuilder& displayId(int32_t displayId) {
56 mDisplayId = displayId;
57 return *this;
58 }
59
60 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
61 mPolicyFlags = policyFlags;
62 return *this;
63 }
64
65 MotionArgsBuilder& actionButton(int32_t actionButton) {
66 mActionButton = actionButton;
67 return *this;
68 }
69
70 MotionArgsBuilder& buttonState(int32_t buttonState) {
71 mButtonState = buttonState;
72 return *this;
73 }
74
75 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
76 mRawXCursorPosition = rawXCursorPosition;
77 return *this;
78 }
79
80 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
81 mRawYCursorPosition = rawYCursorPosition;
82 return *this;
83 }
84
85 MotionArgsBuilder& pointer(PointerBuilder pointer) {
86 mPointers.push_back(pointer);
87 return *this;
88 }
89
90 MotionArgsBuilder& addFlag(uint32_t flags) {
91 mFlags |= flags;
92 return *this;
93 }
94
95 MotionArgsBuilder& classification(MotionClassification classification) {
96 mClassification = classification;
97 return *this;
98 }
99
100 NotifyMotionArgs build() {
101 std::vector<PointerProperties> pointerProperties;
102 std::vector<PointerCoords> pointerCoords;
103 for (const PointerBuilder& pointer : mPointers) {
104 pointerProperties.push_back(pointer.buildProperties());
105 pointerCoords.push_back(pointer.buildCoords());
106 }
107
108 // Set mouse cursor position for the most common cases to avoid boilerplate.
109 if (mSource == AINPUT_SOURCE_MOUSE &&
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900110 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
111 BitSet64::hasBit(pointerCoords[0].bits, AMOTION_EVENT_AXIS_X) &&
112 BitSet64::hasBit(pointerCoords[0].bits, AMOTION_EVENT_AXIS_Y)) {
Prabir Pradhan9205e422023-05-16 20:06:13 +0000113 mRawXCursorPosition = pointerCoords[0].getX();
114 mRawYCursorPosition = pointerCoords[0].getY();
115 }
116
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700117 if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
118 addFlag(AMOTION_EVENT_FLAG_CANCELED);
119 }
120
Prabir Pradhan9205e422023-05-16 20:06:13 +0000121 return {InputEvent::nextId(),
122 mEventTime,
123 /*readTime=*/mEventTime,
124 mDeviceId,
125 mSource,
126 mDisplayId,
127 mPolicyFlags,
128 mAction,
129 mActionButton,
130 mFlags,
131 AMETA_NONE,
132 mButtonState,
133 mClassification,
134 /*edgeFlags=*/0,
135 static_cast<uint32_t>(mPointers.size()),
136 pointerProperties.data(),
137 pointerCoords.data(),
138 /*xPrecision=*/0,
139 /*yPrecision=*/0,
140 mRawXCursorPosition,
141 mRawYCursorPosition,
142 mDownTime,
143 /*videoFrames=*/{}};
144 }
145
146private:
147 int32_t mAction;
148 int32_t mDeviceId{DEFAULT_DEVICE_ID};
149 uint32_t mSource;
150 nsecs_t mDownTime;
151 nsecs_t mEventTime;
152 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
153 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
154 int32_t mActionButton{0};
155 int32_t mButtonState{0};
156 int32_t mFlags{0};
157 MotionClassification mClassification{MotionClassification::NONE};
158 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
159 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
160
161 std::vector<PointerBuilder> mPointers;
162};
163
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000164class KeyArgsBuilder {
165public:
166 KeyArgsBuilder(int32_t action, int32_t source) {
167 mAction = action;
168 mSource = source;
169 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
170 mDownTime = mEventTime;
171 }
172
173 KeyArgsBuilder& deviceId(int32_t deviceId) {
174 mDeviceId = deviceId;
175 return *this;
176 }
177
178 KeyArgsBuilder& downTime(nsecs_t downTime) {
179 mDownTime = downTime;
180 return *this;
181 }
182
183 KeyArgsBuilder& eventTime(nsecs_t eventTime) {
184 mEventTime = eventTime;
185 return *this;
186 }
187
188 KeyArgsBuilder& displayId(int32_t displayId) {
189 mDisplayId = displayId;
190 return *this;
191 }
192
193 KeyArgsBuilder& policyFlags(int32_t policyFlags) {
194 mPolicyFlags = policyFlags;
195 return *this;
196 }
197
198 KeyArgsBuilder& addFlag(uint32_t flags) {
199 mFlags |= flags;
200 return *this;
201 }
202
203 KeyArgsBuilder& keyCode(int32_t keyCode) {
204 mKeyCode = keyCode;
205 return *this;
206 }
207
208 NotifyKeyArgs build() const {
209 return {InputEvent::nextId(),
210 mEventTime,
211 /*readTime=*/mEventTime,
212 mDeviceId,
213 mSource,
214 mDisplayId,
215 mPolicyFlags,
216 mAction,
217 mFlags,
218 mKeyCode,
219 mScanCode,
220 mMetaState,
221 mDownTime};
222 }
223
224private:
225 int32_t mAction;
226 int32_t mDeviceId = DEFAULT_DEVICE_ID;
227 uint32_t mSource;
228 nsecs_t mDownTime;
229 nsecs_t mEventTime;
230 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
231 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
232 int32_t mFlags{0};
233 int32_t mKeyCode{AKEYCODE_UNKNOWN};
234 int32_t mScanCode{0};
235 int32_t mMetaState{AMETA_NONE};
236};
237
Prabir Pradhan9205e422023-05-16 20:06:13 +0000238} // namespace android