Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 1 | /* |
| 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 Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 23 | #include <input/InputEventBuilders.h> |
| 24 | #include <utils/Timers.h> // for nsecs_t, systemTime |
| 25 | |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | namespace android { |
| 29 | |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 30 | class MotionArgsBuilder { |
| 31 | public: |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 32 | MotionArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) { |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 33 | mAction = action; |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 34 | if (mAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 35 | addFlag(AMOTION_EVENT_FLAG_CANCELED); |
| 36 | } |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 37 | 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 Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 57 | MotionArgsBuilder& displayId(ui::LogicalDisplayId displayId) { |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 58 | 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 Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 102 | NotifyMotionArgs build() const { |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 103 | 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 Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 111 | float resolvedCursorX = mRawXCursorPosition; |
| 112 | float resolvedCursorY = mRawYCursorPosition; |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 113 | if (mSource == AINPUT_SOURCE_MOUSE && |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 114 | !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 Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 117 | resolvedCursorX = pointerCoords[0].getX(); |
| 118 | resolvedCursorY = pointerCoords[0].getY(); |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 121 | return {mEventId, |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 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, |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 140 | resolvedCursorX, |
| 141 | resolvedCursorY, |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 142 | mDownTime, |
| 143 | /*videoFrames=*/{}}; |
| 144 | } |
| 145 | |
| 146 | private: |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 147 | const int32_t mEventId; |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 148 | int32_t mAction; |
| 149 | int32_t mDeviceId{DEFAULT_DEVICE_ID}; |
| 150 | uint32_t mSource; |
| 151 | nsecs_t mDownTime; |
| 152 | nsecs_t mEventTime; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 153 | ui::LogicalDisplayId mDisplayId{ui::ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 154 | 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 Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 165 | class KeyArgsBuilder { |
| 166 | public: |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 167 | KeyArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) { |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 168 | 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 Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 189 | KeyArgsBuilder& displayId(ui::LogicalDisplayId displayId) { |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 190 | 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 Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 210 | return {mEventId, |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 211 | 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 | |
| 225 | private: |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 226 | const int32_t mEventId; |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 227 | int32_t mAction; |
| 228 | int32_t mDeviceId = DEFAULT_DEVICE_ID; |
| 229 | uint32_t mSource; |
| 230 | nsecs_t mDownTime; |
| 231 | nsecs_t mEventTime; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 232 | ui::LogicalDisplayId mDisplayId{ui::ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 233 | 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 Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 240 | } // namespace android |