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