blob: cae638f7bfd7c5ada0cf690eae4ca0925ac65574 [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>
22#include <input/Input.h>
Cody Heiner166a5af2023-07-07 12:25:00 -070023#include <input/InputEventBuilders.h>
24#include <utils/Timers.h> // for nsecs_t, systemTime
25
Prabir Pradhan9205e422023-05-16 20:06:13 +000026#include <vector>
27
28namespace android {
29
Prabir Pradhan9205e422023-05-16 20:06:13 +000030class MotionArgsBuilder {
31public:
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000032 MotionArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) {
Prabir Pradhan9205e422023-05-16 20:06:13 +000033 mAction = action;
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000034 if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
35 addFlag(AMOTION_EVENT_FLAG_CANCELED);
36 }
Prabir Pradhan9205e422023-05-16 20:06:13 +000037 mSource = source;
38 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
39 mDownTime = mEventTime;
40 }
41
42 MotionArgsBuilder& deviceId(int32_t deviceId) {
43 mDeviceId = deviceId;
44 return *this;
45 }
46
47 MotionArgsBuilder& downTime(nsecs_t downTime) {
48 mDownTime = downTime;
49 return *this;
50 }
51
52 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
53 mEventTime = eventTime;
54 return *this;
55 }
56
Linnan Li13bf76a2024-05-05 19:18:02 +080057 MotionArgsBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhan9205e422023-05-16 20:06:13 +000058 mDisplayId = displayId;
59 return *this;
60 }
61
62 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
63 mPolicyFlags = policyFlags;
64 return *this;
65 }
66
67 MotionArgsBuilder& actionButton(int32_t actionButton) {
68 mActionButton = actionButton;
69 return *this;
70 }
71
72 MotionArgsBuilder& buttonState(int32_t buttonState) {
73 mButtonState = buttonState;
74 return *this;
75 }
76
77 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
78 mRawXCursorPosition = rawXCursorPosition;
79 return *this;
80 }
81
82 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
83 mRawYCursorPosition = rawYCursorPosition;
84 return *this;
85 }
86
87 MotionArgsBuilder& pointer(PointerBuilder pointer) {
88 mPointers.push_back(pointer);
89 return *this;
90 }
91
92 MotionArgsBuilder& addFlag(uint32_t flags) {
93 mFlags |= flags;
94 return *this;
95 }
96
97 MotionArgsBuilder& classification(MotionClassification classification) {
98 mClassification = classification;
99 return *this;
100 }
101
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000102 NotifyMotionArgs build() const {
Prabir Pradhan9205e422023-05-16 20:06:13 +0000103 std::vector<PointerProperties> pointerProperties;
104 std::vector<PointerCoords> pointerCoords;
105 for (const PointerBuilder& pointer : mPointers) {
106 pointerProperties.push_back(pointer.buildProperties());
107 pointerCoords.push_back(pointer.buildCoords());
108 }
109
110 // Set mouse cursor position for the most common cases to avoid boilerplate.
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000111 float resolvedCursorX = mRawXCursorPosition;
112 float resolvedCursorY = mRawYCursorPosition;
Prabir Pradhan9205e422023-05-16 20:06:13 +0000113 if (mSource == AINPUT_SOURCE_MOUSE &&
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900114 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
115 BitSet64::hasBit(pointerCoords[0].bits, AMOTION_EVENT_AXIS_X) &&
116 BitSet64::hasBit(pointerCoords[0].bits, AMOTION_EVENT_AXIS_Y)) {
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000117 resolvedCursorX = pointerCoords[0].getX();
118 resolvedCursorY = pointerCoords[0].getY();
Prabir Pradhan9205e422023-05-16 20:06:13 +0000119 }
120
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000121 return {mEventId,
Prabir Pradhan9205e422023-05-16 20:06:13 +0000122 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,
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000140 resolvedCursorX,
141 resolvedCursorY,
Prabir Pradhan9205e422023-05-16 20:06:13 +0000142 mDownTime,
143 /*videoFrames=*/{}};
144 }
145
146private:
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000147 const int32_t mEventId;
Prabir Pradhan9205e422023-05-16 20:06:13 +0000148 int32_t mAction;
149 int32_t mDeviceId{DEFAULT_DEVICE_ID};
150 uint32_t mSource;
151 nsecs_t mDownTime;
152 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700153 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Prabir Pradhan9205e422023-05-16 20:06:13 +0000154 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
155 int32_t mActionButton{0};
156 int32_t mButtonState{0};
157 int32_t mFlags{0};
158 MotionClassification mClassification{MotionClassification::NONE};
159 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
160 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
161
162 std::vector<PointerBuilder> mPointers;
163};
164
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000165class KeyArgsBuilder {
166public:
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000167 KeyArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) {
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000168 mAction = action;
169 mSource = source;
170 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
171 mDownTime = mEventTime;
172 }
173
174 KeyArgsBuilder& deviceId(int32_t deviceId) {
175 mDeviceId = deviceId;
176 return *this;
177 }
178
179 KeyArgsBuilder& downTime(nsecs_t downTime) {
180 mDownTime = downTime;
181 return *this;
182 }
183
184 KeyArgsBuilder& eventTime(nsecs_t eventTime) {
185 mEventTime = eventTime;
186 return *this;
187 }
188
Linnan Li13bf76a2024-05-05 19:18:02 +0800189 KeyArgsBuilder& displayId(ui::LogicalDisplayId displayId) {
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000190 mDisplayId = displayId;
191 return *this;
192 }
193
194 KeyArgsBuilder& policyFlags(int32_t policyFlags) {
195 mPolicyFlags = policyFlags;
196 return *this;
197 }
198
199 KeyArgsBuilder& addFlag(uint32_t flags) {
200 mFlags |= flags;
201 return *this;
202 }
203
204 KeyArgsBuilder& keyCode(int32_t keyCode) {
205 mKeyCode = keyCode;
206 return *this;
207 }
208
209 NotifyKeyArgs build() const {
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000210 return {mEventId,
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000211 mEventTime,
212 /*readTime=*/mEventTime,
213 mDeviceId,
214 mSource,
215 mDisplayId,
216 mPolicyFlags,
217 mAction,
218 mFlags,
219 mKeyCode,
220 mScanCode,
221 mMetaState,
222 mDownTime};
223 }
224
225private:
Prabir Pradhan9a9897d2024-03-21 21:52:57 +0000226 const int32_t mEventId;
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000227 int32_t mAction;
228 int32_t mDeviceId = DEFAULT_DEVICE_ID;
229 uint32_t mSource;
230 nsecs_t mDownTime;
231 nsecs_t mEventTime;
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700232 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT};
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000233 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
234 int32_t mFlags{0};
235 int32_t mKeyCode{AKEYCODE_UNKNOWN};
236 int32_t mScanCode{0};
237 int32_t mMetaState{AMETA_NONE};
238};
239
Prabir Pradhan9205e422023-05-16 20:06:13 +0000240} // namespace android