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: |
| 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 && |
| 110 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) { |
| 111 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 112 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 113 | } |
| 114 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 115 | if (mAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 116 | addFlag(AMOTION_EVENT_FLAG_CANCELED); |
| 117 | } |
| 118 | |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 119 | return {InputEvent::nextId(), |
| 120 | mEventTime, |
| 121 | /*readTime=*/mEventTime, |
| 122 | mDeviceId, |
| 123 | mSource, |
| 124 | mDisplayId, |
| 125 | mPolicyFlags, |
| 126 | mAction, |
| 127 | mActionButton, |
| 128 | mFlags, |
| 129 | AMETA_NONE, |
| 130 | mButtonState, |
| 131 | mClassification, |
| 132 | /*edgeFlags=*/0, |
| 133 | static_cast<uint32_t>(mPointers.size()), |
| 134 | pointerProperties.data(), |
| 135 | pointerCoords.data(), |
| 136 | /*xPrecision=*/0, |
| 137 | /*yPrecision=*/0, |
| 138 | mRawXCursorPosition, |
| 139 | mRawYCursorPosition, |
| 140 | mDownTime, |
| 141 | /*videoFrames=*/{}}; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | int32_t mAction; |
| 146 | int32_t mDeviceId{DEFAULT_DEVICE_ID}; |
| 147 | uint32_t mSource; |
| 148 | nsecs_t mDownTime; |
| 149 | nsecs_t mEventTime; |
| 150 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 151 | uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 152 | int32_t mActionButton{0}; |
| 153 | int32_t mButtonState{0}; |
| 154 | int32_t mFlags{0}; |
| 155 | MotionClassification mClassification{MotionClassification::NONE}; |
| 156 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 157 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 158 | |
| 159 | std::vector<PointerBuilder> mPointers; |
| 160 | }; |
| 161 | |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 162 | class KeyArgsBuilder { |
| 163 | public: |
| 164 | KeyArgsBuilder(int32_t action, int32_t source) { |
| 165 | mAction = action; |
| 166 | mSource = source; |
| 167 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 168 | mDownTime = mEventTime; |
| 169 | } |
| 170 | |
| 171 | KeyArgsBuilder& deviceId(int32_t deviceId) { |
| 172 | mDeviceId = deviceId; |
| 173 | return *this; |
| 174 | } |
| 175 | |
| 176 | KeyArgsBuilder& downTime(nsecs_t downTime) { |
| 177 | mDownTime = downTime; |
| 178 | return *this; |
| 179 | } |
| 180 | |
| 181 | KeyArgsBuilder& eventTime(nsecs_t eventTime) { |
| 182 | mEventTime = eventTime; |
| 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | KeyArgsBuilder& displayId(int32_t displayId) { |
| 187 | mDisplayId = displayId; |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | KeyArgsBuilder& policyFlags(int32_t policyFlags) { |
| 192 | mPolicyFlags = policyFlags; |
| 193 | return *this; |
| 194 | } |
| 195 | |
| 196 | KeyArgsBuilder& addFlag(uint32_t flags) { |
| 197 | mFlags |= flags; |
| 198 | return *this; |
| 199 | } |
| 200 | |
| 201 | KeyArgsBuilder& keyCode(int32_t keyCode) { |
| 202 | mKeyCode = keyCode; |
| 203 | return *this; |
| 204 | } |
| 205 | |
| 206 | NotifyKeyArgs build() const { |
| 207 | return {InputEvent::nextId(), |
| 208 | mEventTime, |
| 209 | /*readTime=*/mEventTime, |
| 210 | mDeviceId, |
| 211 | mSource, |
| 212 | mDisplayId, |
| 213 | mPolicyFlags, |
| 214 | mAction, |
| 215 | mFlags, |
| 216 | mKeyCode, |
| 217 | mScanCode, |
| 218 | mMetaState, |
| 219 | mDownTime}; |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | int32_t mAction; |
| 224 | int32_t mDeviceId = DEFAULT_DEVICE_ID; |
| 225 | uint32_t mSource; |
| 226 | nsecs_t mDownTime; |
| 227 | nsecs_t mEventTime; |
| 228 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 229 | uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 230 | int32_t mFlags{0}; |
| 231 | int32_t mKeyCode{AKEYCODE_UNKNOWN}; |
| 232 | int32_t mScanCode{0}; |
| 233 | int32_t mMetaState{AMETA_NONE}; |
| 234 | }; |
| 235 | |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 236 | } // namespace android |