Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [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 <android/input.h> |
| 20 | #include <attestation/HmacKeyManager.h> |
| 21 | #include <gui/constants.h> |
| 22 | #include <input/Input.h> |
| 23 | #include <utils/Timers.h> // for nsecs_t, systemTime |
| 24 | |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | // An arbitrary device id. |
| 30 | static constexpr uint32_t DEFAULT_DEVICE_ID = 1; |
| 31 | |
| 32 | // The default policy flags to use for event injection by tests. |
| 33 | static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 34 | |
| 35 | class PointerBuilder { |
| 36 | public: |
| 37 | PointerBuilder(int32_t id, ToolType toolType) { |
| 38 | mProperties.clear(); |
| 39 | mProperties.id = id; |
| 40 | mProperties.toolType = toolType; |
| 41 | mCoords.clear(); |
| 42 | } |
| 43 | |
| 44 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 45 | |
| 46 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 47 | |
| 48 | PointerBuilder& axis(int32_t axis, float value) { |
| 49 | mCoords.setAxisValue(axis, value); |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | PointerProperties buildProperties() const { return mProperties; } |
| 54 | |
| 55 | PointerCoords buildCoords() const { return mCoords; } |
| 56 | |
| 57 | private: |
| 58 | PointerProperties mProperties; |
| 59 | PointerCoords mCoords; |
| 60 | }; |
| 61 | |
| 62 | class MotionEventBuilder { |
| 63 | public: |
| 64 | MotionEventBuilder(int32_t action, int32_t source) { |
| 65 | mAction = action; |
| 66 | mSource = source; |
| 67 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 68 | mDownTime = mEventTime; |
| 69 | } |
| 70 | |
| 71 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 72 | mDeviceId = deviceId; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 77 | mDownTime = downTime; |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 82 | mEventTime = eventTime; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | MotionEventBuilder& displayId(int32_t displayId) { |
| 87 | mDisplayId = displayId; |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 92 | mActionButton = actionButton; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 97 | mButtonState = buttonState; |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 102 | mRawXCursorPosition = rawXCursorPosition; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 107 | mRawYCursorPosition = rawYCursorPosition; |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 112 | mPointers.push_back(pointer); |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 117 | mFlags |= flags; |
| 118 | return *this; |
| 119 | } |
| 120 | |
Prabir Pradhan | bf9b0a8 | 2024-02-29 02:23:50 +0000 | [diff] [blame^] | 121 | MotionEventBuilder& transform(ui::Transform t) { |
| 122 | mTransform = t; |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | MotionEventBuilder& rawTransform(ui::Transform t) { |
| 127 | mRawTransform = t; |
| 128 | return *this; |
| 129 | } |
| 130 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 131 | MotionEvent build() { |
| 132 | std::vector<PointerProperties> pointerProperties; |
| 133 | std::vector<PointerCoords> pointerCoords; |
| 134 | for (const PointerBuilder& pointer : mPointers) { |
| 135 | pointerProperties.push_back(pointer.buildProperties()); |
| 136 | pointerCoords.push_back(pointer.buildCoords()); |
| 137 | } |
| 138 | |
| 139 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 140 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 141 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) { |
| 142 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 143 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 144 | } |
| 145 | |
| 146 | MotionEvent event; |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 147 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
| 148 | mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState, |
Prabir Pradhan | bf9b0a8 | 2024-02-29 02:23:50 +0000 | [diff] [blame^] | 149 | MotionClassification::NONE, mTransform, |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 150 | /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition, |
Prabir Pradhan | bf9b0a8 | 2024-02-29 02:23:50 +0000 | [diff] [blame^] | 151 | mRawYCursorPosition, mRawTransform, mDownTime, mEventTime, |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 152 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
| 153 | return event; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | int32_t mAction; |
| 158 | int32_t mDeviceId{DEFAULT_DEVICE_ID}; |
| 159 | int32_t mSource; |
| 160 | nsecs_t mDownTime; |
| 161 | nsecs_t mEventTime; |
| 162 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 163 | int32_t mActionButton{0}; |
| 164 | int32_t mButtonState{0}; |
| 165 | int32_t mFlags{0}; |
| 166 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 167 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
Prabir Pradhan | bf9b0a8 | 2024-02-29 02:23:50 +0000 | [diff] [blame^] | 168 | ui::Transform mTransform; |
| 169 | ui::Transform mRawTransform; |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 170 | |
| 171 | std::vector<PointerBuilder> mPointers; |
| 172 | }; |
| 173 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 174 | class KeyEventBuilder { |
| 175 | public: |
| 176 | KeyEventBuilder(int32_t action, int32_t source) { |
| 177 | mAction = action; |
| 178 | mSource = source; |
| 179 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 180 | mDownTime = mEventTime; |
| 181 | } |
| 182 | |
| 183 | KeyEventBuilder(const KeyEvent& event) { |
| 184 | mAction = event.getAction(); |
| 185 | mDeviceId = event.getDeviceId(); |
| 186 | mSource = event.getSource(); |
| 187 | mDownTime = event.getDownTime(); |
| 188 | mEventTime = event.getEventTime(); |
| 189 | mDisplayId = event.getDisplayId(); |
| 190 | mFlags = event.getFlags(); |
| 191 | mKeyCode = event.getKeyCode(); |
| 192 | mScanCode = event.getScanCode(); |
| 193 | mMetaState = event.getMetaState(); |
| 194 | mRepeatCount = event.getRepeatCount(); |
| 195 | } |
| 196 | |
| 197 | KeyEventBuilder& deviceId(int32_t deviceId) { |
| 198 | mDeviceId = deviceId; |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | KeyEventBuilder& downTime(nsecs_t downTime) { |
| 203 | mDownTime = downTime; |
| 204 | return *this; |
| 205 | } |
| 206 | |
| 207 | KeyEventBuilder& eventTime(nsecs_t eventTime) { |
| 208 | mEventTime = eventTime; |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | KeyEventBuilder& displayId(int32_t displayId) { |
| 213 | mDisplayId = displayId; |
| 214 | return *this; |
| 215 | } |
| 216 | |
| 217 | KeyEventBuilder& policyFlags(int32_t policyFlags) { |
| 218 | mPolicyFlags = policyFlags; |
| 219 | return *this; |
| 220 | } |
| 221 | |
| 222 | KeyEventBuilder& addFlag(uint32_t flags) { |
| 223 | mFlags |= flags; |
| 224 | return *this; |
| 225 | } |
| 226 | |
| 227 | KeyEventBuilder& keyCode(int32_t keyCode) { |
| 228 | mKeyCode = keyCode; |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | KeyEventBuilder& repeatCount(int32_t repeatCount) { |
| 233 | mRepeatCount = repeatCount; |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | KeyEvent build() const { |
| 238 | KeyEvent event{}; |
| 239 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
| 240 | mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime, |
| 241 | mEventTime); |
| 242 | return event; |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | int32_t mAction; |
| 247 | int32_t mDeviceId = DEFAULT_DEVICE_ID; |
| 248 | uint32_t mSource; |
| 249 | nsecs_t mDownTime; |
| 250 | nsecs_t mEventTime; |
| 251 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 252 | uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 253 | int32_t mFlags{0}; |
| 254 | int32_t mKeyCode{AKEYCODE_UNKNOWN}; |
| 255 | int32_t mScanCode{0}; |
| 256 | int32_t mMetaState{AMETA_NONE}; |
| 257 | int32_t mRepeatCount{0}; |
| 258 | }; |
| 259 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 260 | } // namespace android |