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