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 <input/InputTransport.h> 23 #include <ui/LogicalDisplayId.h> 24 #include <ui/Transform.h> 25 #include <utils/Timers.h> // for nsecs_t, systemTime 26 27 #include <vector> 28 29 namespace android { 30 31 // An arbitrary device id. 32 static constexpr uint32_t DEFAULT_DEVICE_ID = 1; 33 34 // The default policy flags to use for event injection by tests. 35 static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; 36 37 class PointerBuilder { 38 public: PointerBuilder(int32_t id,ToolType toolType)39 PointerBuilder(int32_t id, ToolType toolType) { 40 mProperties.clear(); 41 mProperties.id = id; 42 mProperties.toolType = toolType; 43 mCoords.clear(); 44 } 45 x(float x)46 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } 47 y(float y)48 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } 49 isResampled(bool isResampled)50 PointerBuilder& isResampled(bool isResampled) { 51 mCoords.isResampled = isResampled; 52 return *this; 53 } 54 axis(int32_t axis,float value)55 PointerBuilder& axis(int32_t axis, float value) { 56 mCoords.setAxisValue(axis, value); 57 return *this; 58 } 59 buildProperties()60 PointerProperties buildProperties() const { return mProperties; } 61 buildCoords()62 PointerCoords buildCoords() const { return mCoords; } 63 64 private: 65 PointerProperties mProperties; 66 PointerCoords mCoords; 67 }; 68 69 class InputMessageBuilder { 70 public: InputMessageBuilder(InputMessage::Type type,uint32_t seq)71 InputMessageBuilder(InputMessage::Type type, uint32_t seq) : mType{type}, mSeq{seq} {} 72 eventId(int32_t eventId)73 InputMessageBuilder& eventId(int32_t eventId) { 74 mEventId = eventId; 75 return *this; 76 } 77 eventTime(nsecs_t eventTime)78 InputMessageBuilder& eventTime(nsecs_t eventTime) { 79 mEventTime = eventTime; 80 return *this; 81 } 82 deviceId(DeviceId deviceId)83 InputMessageBuilder& deviceId(DeviceId deviceId) { 84 mDeviceId = deviceId; 85 return *this; 86 } 87 source(int32_t source)88 InputMessageBuilder& source(int32_t source) { 89 mSource = source; 90 return *this; 91 } 92 displayId(ui::LogicalDisplayId displayId)93 InputMessageBuilder& displayId(ui::LogicalDisplayId displayId) { 94 mDisplayId = displayId; 95 return *this; 96 } 97 hmac(const std::array<uint8_t,32> & hmac)98 InputMessageBuilder& hmac(const std::array<uint8_t, 32>& hmac) { 99 mHmac = hmac; 100 return *this; 101 } 102 action(int32_t action)103 InputMessageBuilder& action(int32_t action) { 104 mAction = action; 105 return *this; 106 } 107 actionButton(int32_t actionButton)108 InputMessageBuilder& actionButton(int32_t actionButton) { 109 mActionButton = actionButton; 110 return *this; 111 } 112 flags(int32_t flags)113 InputMessageBuilder& flags(int32_t flags) { 114 mFlags = flags; 115 return *this; 116 } 117 metaState(int32_t metaState)118 InputMessageBuilder& metaState(int32_t metaState) { 119 mMetaState = metaState; 120 return *this; 121 } 122 buttonState(int32_t buttonState)123 InputMessageBuilder& buttonState(int32_t buttonState) { 124 mButtonState = buttonState; 125 return *this; 126 } 127 classification(MotionClassification classification)128 InputMessageBuilder& classification(MotionClassification classification) { 129 mClassification = classification; 130 return *this; 131 } 132 edgeFlags(int32_t edgeFlags)133 InputMessageBuilder& edgeFlags(int32_t edgeFlags) { 134 mEdgeFlags = edgeFlags; 135 return *this; 136 } 137 downTime(nsecs_t downTime)138 InputMessageBuilder& downTime(nsecs_t downTime) { 139 mDownTime = downTime; 140 return *this; 141 } 142 transform(const ui::Transform & transform)143 InputMessageBuilder& transform(const ui::Transform& transform) { 144 mTransform = transform; 145 return *this; 146 } 147 xPrecision(float xPrecision)148 InputMessageBuilder& xPrecision(float xPrecision) { 149 mXPrecision = xPrecision; 150 return *this; 151 } 152 yPrecision(float yPrecision)153 InputMessageBuilder& yPrecision(float yPrecision) { 154 mYPrecision = yPrecision; 155 return *this; 156 } 157 xCursorPosition(float xCursorPosition)158 InputMessageBuilder& xCursorPosition(float xCursorPosition) { 159 mXCursorPosition = xCursorPosition; 160 return *this; 161 } 162 yCursorPosition(float yCursorPosition)163 InputMessageBuilder& yCursorPosition(float yCursorPosition) { 164 mYCursorPosition = yCursorPosition; 165 return *this; 166 } 167 rawTransform(const ui::Transform & rawTransform)168 InputMessageBuilder& rawTransform(const ui::Transform& rawTransform) { 169 mRawTransform = rawTransform; 170 return *this; 171 } 172 pointer(PointerBuilder pointerBuilder)173 InputMessageBuilder& pointer(PointerBuilder pointerBuilder) { 174 mPointers.push_back(pointerBuilder); 175 return *this; 176 } 177 build()178 InputMessage build() const { 179 InputMessage message{}; 180 // Header 181 message.header.type = mType; 182 message.header.seq = mSeq; 183 // Body 184 message.body.motion.eventId = mEventId; 185 message.body.motion.pointerCount = mPointers.size(); 186 message.body.motion.eventTime = mEventTime; 187 message.body.motion.deviceId = mDeviceId; 188 message.body.motion.source = mSource; 189 message.body.motion.displayId = mDisplayId.val(); 190 message.body.motion.hmac = std::move(mHmac); 191 message.body.motion.action = mAction; 192 message.body.motion.actionButton = mActionButton; 193 message.body.motion.flags = mFlags; 194 message.body.motion.metaState = mMetaState; 195 message.body.motion.buttonState = mButtonState; 196 message.body.motion.edgeFlags = mEdgeFlags; 197 message.body.motion.downTime = mDownTime; 198 message.body.motion.dsdx = mTransform.dsdx(); 199 message.body.motion.dtdx = mTransform.dtdx(); 200 message.body.motion.dtdy = mTransform.dtdy(); 201 message.body.motion.dsdy = mTransform.dsdy(); 202 message.body.motion.tx = mTransform.ty(); 203 message.body.motion.ty = mTransform.tx(); 204 message.body.motion.xPrecision = mXPrecision; 205 message.body.motion.yPrecision = mYPrecision; 206 message.body.motion.xCursorPosition = mXCursorPosition; 207 message.body.motion.yCursorPosition = mYCursorPosition; 208 message.body.motion.dsdxRaw = mRawTransform.dsdx(); 209 message.body.motion.dtdxRaw = mRawTransform.dtdx(); 210 message.body.motion.dtdyRaw = mRawTransform.dtdy(); 211 message.body.motion.dsdyRaw = mRawTransform.dsdy(); 212 message.body.motion.txRaw = mRawTransform.ty(); 213 message.body.motion.tyRaw = mRawTransform.tx(); 214 215 for (size_t i = 0; i < mPointers.size(); ++i) { 216 message.body.motion.pointers[i].properties = mPointers[i].buildProperties(); 217 message.body.motion.pointers[i].coords = mPointers[i].buildCoords(); 218 } 219 return message; 220 } 221 222 private: 223 const InputMessage::Type mType; 224 const uint32_t mSeq; 225 226 int32_t mEventId{InputEvent::nextId()}; 227 nsecs_t mEventTime{systemTime(SYSTEM_TIME_MONOTONIC)}; 228 DeviceId mDeviceId{DEFAULT_DEVICE_ID}; 229 int32_t mSource{AINPUT_SOURCE_TOUCHSCREEN}; 230 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT}; 231 std::array<uint8_t, 32> mHmac{INVALID_HMAC}; 232 int32_t mAction{AMOTION_EVENT_ACTION_MOVE}; 233 int32_t mActionButton{0}; 234 int32_t mFlags{0}; 235 int32_t mMetaState{AMETA_NONE}; 236 int32_t mButtonState{0}; 237 MotionClassification mClassification{MotionClassification::NONE}; 238 int32_t mEdgeFlags{0}; 239 nsecs_t mDownTime{mEventTime}; 240 ui::Transform mTransform{}; 241 float mXPrecision{1.0f}; 242 float mYPrecision{1.0f}; 243 float mXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; 244 float mYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; 245 ui::Transform mRawTransform{}; 246 std::vector<PointerBuilder> mPointers; 247 }; 248 249 class MotionEventBuilder { 250 public: MotionEventBuilder(int32_t action,int32_t source)251 MotionEventBuilder(int32_t action, int32_t source) { 252 mAction = action; 253 if (mAction == AMOTION_EVENT_ACTION_CANCEL) { 254 mFlags |= AMOTION_EVENT_FLAG_CANCELED; 255 } 256 mSource = source; 257 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); 258 mDownTime = mEventTime; 259 } 260 deviceId(int32_t deviceId)261 MotionEventBuilder& deviceId(int32_t deviceId) { 262 mDeviceId = deviceId; 263 return *this; 264 } 265 downTime(nsecs_t downTime)266 MotionEventBuilder& downTime(nsecs_t downTime) { 267 mDownTime = downTime; 268 return *this; 269 } 270 eventTime(nsecs_t eventTime)271 MotionEventBuilder& eventTime(nsecs_t eventTime) { 272 mEventTime = eventTime; 273 return *this; 274 } 275 displayId(ui::LogicalDisplayId displayId)276 MotionEventBuilder& displayId(ui::LogicalDisplayId displayId) { 277 mDisplayId = displayId; 278 return *this; 279 } 280 actionButton(int32_t actionButton)281 MotionEventBuilder& actionButton(int32_t actionButton) { 282 mActionButton = actionButton; 283 return *this; 284 } 285 buttonState(int32_t buttonState)286 MotionEventBuilder& buttonState(int32_t buttonState) { 287 mButtonState = buttonState; 288 return *this; 289 } 290 rawXCursorPosition(float rawXCursorPosition)291 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { 292 mRawXCursorPosition = rawXCursorPosition; 293 return *this; 294 } 295 rawYCursorPosition(float rawYCursorPosition)296 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { 297 mRawYCursorPosition = rawYCursorPosition; 298 return *this; 299 } 300 pointer(PointerBuilder pointer)301 MotionEventBuilder& pointer(PointerBuilder pointer) { 302 mPointers.push_back(pointer); 303 return *this; 304 } 305 addFlag(uint32_t flags)306 MotionEventBuilder& addFlag(uint32_t flags) { 307 mFlags |= flags; 308 return *this; 309 } 310 transform(ui::Transform t)311 MotionEventBuilder& transform(ui::Transform t) { 312 mTransform = t; 313 return *this; 314 } 315 rawTransform(ui::Transform t)316 MotionEventBuilder& rawTransform(ui::Transform t) { 317 mRawTransform = t; 318 return *this; 319 } 320 build()321 MotionEvent build() const { 322 std::vector<PointerProperties> pointerProperties; 323 std::vector<PointerCoords> pointerCoords; 324 for (const PointerBuilder& pointer : mPointers) { 325 pointerProperties.push_back(pointer.buildProperties()); 326 pointerCoords.push_back(pointer.buildCoords()); 327 } 328 329 auto [xCursorPosition, yCursorPosition] = 330 std::make_pair(mRawXCursorPosition, mRawYCursorPosition); 331 // Set mouse cursor position for the most common cases to avoid boilerplate. 332 if (mSource == AINPUT_SOURCE_MOUSE && 333 !MotionEvent::isValidCursorPosition(xCursorPosition, yCursorPosition)) { 334 xCursorPosition = pointerCoords[0].getX(); 335 yCursorPosition = pointerCoords[0].getY(); 336 } 337 338 MotionEvent event; 339 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, 340 mAction, mActionButton, mFlags, /*edgeFlags=*/0, AMETA_NONE, mButtonState, 341 MotionClassification::NONE, mTransform, 342 /*xPrecision=*/0, /*yPrecision=*/0, xCursorPosition, yCursorPosition, 343 mRawTransform, mDownTime, mEventTime, mPointers.size(), 344 pointerProperties.data(), pointerCoords.data()); 345 return event; 346 } 347 348 private: 349 int32_t mAction; 350 int32_t mDeviceId{DEFAULT_DEVICE_ID}; 351 int32_t mSource; 352 nsecs_t mDownTime; 353 nsecs_t mEventTime; 354 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT}; 355 int32_t mActionButton{0}; 356 int32_t mButtonState{0}; 357 int32_t mFlags{0}; 358 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; 359 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; 360 ui::Transform mTransform; 361 ui::Transform mRawTransform; 362 363 std::vector<PointerBuilder> mPointers; 364 }; 365 366 class KeyEventBuilder { 367 public: KeyEventBuilder(int32_t action,int32_t source)368 KeyEventBuilder(int32_t action, int32_t source) { 369 mAction = action; 370 mSource = source; 371 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); 372 mDownTime = mEventTime; 373 } 374 KeyEventBuilder(const KeyEvent & event)375 KeyEventBuilder(const KeyEvent& event) { 376 mAction = event.getAction(); 377 mDeviceId = event.getDeviceId(); 378 mSource = event.getSource(); 379 mDownTime = event.getDownTime(); 380 mEventTime = event.getEventTime(); 381 mDisplayId = event.getDisplayId(); 382 mFlags = event.getFlags(); 383 mKeyCode = event.getKeyCode(); 384 mScanCode = event.getScanCode(); 385 mMetaState = event.getMetaState(); 386 mRepeatCount = event.getRepeatCount(); 387 } 388 deviceId(int32_t deviceId)389 KeyEventBuilder& deviceId(int32_t deviceId) { 390 mDeviceId = deviceId; 391 return *this; 392 } 393 downTime(nsecs_t downTime)394 KeyEventBuilder& downTime(nsecs_t downTime) { 395 mDownTime = downTime; 396 return *this; 397 } 398 eventTime(nsecs_t eventTime)399 KeyEventBuilder& eventTime(nsecs_t eventTime) { 400 mEventTime = eventTime; 401 return *this; 402 } 403 displayId(ui::LogicalDisplayId displayId)404 KeyEventBuilder& displayId(ui::LogicalDisplayId displayId) { 405 mDisplayId = displayId; 406 return *this; 407 } 408 policyFlags(int32_t policyFlags)409 KeyEventBuilder& policyFlags(int32_t policyFlags) { 410 mPolicyFlags = policyFlags; 411 return *this; 412 } 413 addFlag(uint32_t flags)414 KeyEventBuilder& addFlag(uint32_t flags) { 415 mFlags |= flags; 416 return *this; 417 } 418 keyCode(int32_t keyCode)419 KeyEventBuilder& keyCode(int32_t keyCode) { 420 mKeyCode = keyCode; 421 return *this; 422 } 423 repeatCount(int32_t repeatCount)424 KeyEventBuilder& repeatCount(int32_t repeatCount) { 425 mRepeatCount = repeatCount; 426 return *this; 427 } 428 build()429 KeyEvent build() const { 430 KeyEvent event{}; 431 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, 432 mAction, mFlags, mKeyCode, mScanCode, mMetaState, mRepeatCount, mDownTime, 433 mEventTime); 434 return event; 435 } 436 437 private: 438 int32_t mAction; 439 int32_t mDeviceId = DEFAULT_DEVICE_ID; 440 uint32_t mSource; 441 nsecs_t mDownTime; 442 nsecs_t mEventTime; 443 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT}; 444 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; 445 int32_t mFlags{0}; 446 int32_t mKeyCode{AKEYCODE_UNKNOWN}; 447 int32_t mScanCode{0}; 448 int32_t mMetaState{AMETA_NONE}; 449 int32_t mRepeatCount{0}; 450 }; 451 452 } // namespace android 453