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> 22 #include <input/Input.h> 23 #include <input/InputEventBuilders.h> 24 #include <input/Keyboard.h> 25 #include <utils/Timers.h> // for nsecs_t, systemTime 26 27 #include <vector> 28 29 namespace android { 30 31 class MotionArgsBuilder { 32 public: MotionArgsBuilder(int32_t action,int32_t source)33 MotionArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) { 34 mAction = action; 35 if (mAction == AMOTION_EVENT_ACTION_CANCEL) { 36 addFlag(AMOTION_EVENT_FLAG_CANCELED); 37 } 38 mSource = source; 39 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); 40 mDownTime = mEventTime; 41 } 42 deviceId(int32_t deviceId)43 MotionArgsBuilder& deviceId(int32_t deviceId) { 44 mDeviceId = deviceId; 45 return *this; 46 } 47 downTime(nsecs_t downTime)48 MotionArgsBuilder& downTime(nsecs_t downTime) { 49 mDownTime = downTime; 50 return *this; 51 } 52 eventTime(nsecs_t eventTime)53 MotionArgsBuilder& eventTime(nsecs_t eventTime) { 54 mEventTime = eventTime; 55 return *this; 56 } 57 displayId(ui::LogicalDisplayId displayId)58 MotionArgsBuilder& displayId(ui::LogicalDisplayId displayId) { 59 mDisplayId = displayId; 60 return *this; 61 } 62 policyFlags(int32_t policyFlags)63 MotionArgsBuilder& policyFlags(int32_t policyFlags) { 64 mPolicyFlags = policyFlags; 65 return *this; 66 } 67 actionButton(int32_t actionButton)68 MotionArgsBuilder& actionButton(int32_t actionButton) { 69 mActionButton = actionButton; 70 return *this; 71 } 72 buttonState(int32_t buttonState)73 MotionArgsBuilder& buttonState(int32_t buttonState) { 74 mButtonState = buttonState; 75 return *this; 76 } 77 rawXCursorPosition(float rawXCursorPosition)78 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) { 79 mRawXCursorPosition = rawXCursorPosition; 80 return *this; 81 } 82 rawYCursorPosition(float rawYCursorPosition)83 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) { 84 mRawYCursorPosition = rawYCursorPosition; 85 return *this; 86 } 87 pointer(PointerBuilder pointer)88 MotionArgsBuilder& pointer(PointerBuilder pointer) { 89 mPointers.push_back(pointer); 90 return *this; 91 } 92 addFlag(uint32_t flags)93 MotionArgsBuilder& addFlag(uint32_t flags) { 94 mFlags |= flags; 95 return *this; 96 } 97 classification(MotionClassification classification)98 MotionArgsBuilder& classification(MotionClassification classification) { 99 mClassification = classification; 100 return *this; 101 } 102 build()103 NotifyMotionArgs build() const { 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. 112 float resolvedCursorX = mRawXCursorPosition; 113 float resolvedCursorY = mRawYCursorPosition; 114 if (mSource == AINPUT_SOURCE_MOUSE && 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)) { 118 resolvedCursorX = pointerCoords[0].getX(); 119 resolvedCursorY = pointerCoords[0].getY(); 120 } 121 122 return {mEventId, 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, 141 resolvedCursorX, 142 resolvedCursorY, 143 mDownTime, 144 /*videoFrames=*/{}}; 145 } 146 147 private: 148 const int32_t mEventId; 149 int32_t mAction; 150 int32_t mDeviceId{DEFAULT_DEVICE_ID}; 151 uint32_t mSource; 152 nsecs_t mDownTime; 153 nsecs_t mEventTime; 154 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::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 166 class KeyArgsBuilder { 167 public: KeyArgsBuilder(int32_t action,int32_t source)168 KeyArgsBuilder(int32_t action, int32_t source) : mEventId(InputEvent::nextId()) { 169 mAction = action; 170 mSource = source; 171 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); 172 mDownTime = mEventTime; 173 } 174 deviceId(int32_t deviceId)175 KeyArgsBuilder& deviceId(int32_t deviceId) { 176 mDeviceId = deviceId; 177 return *this; 178 } 179 downTime(nsecs_t downTime)180 KeyArgsBuilder& downTime(nsecs_t downTime) { 181 mDownTime = downTime; 182 return *this; 183 } 184 eventTime(nsecs_t eventTime)185 KeyArgsBuilder& eventTime(nsecs_t eventTime) { 186 mEventTime = eventTime; 187 return *this; 188 } 189 displayId(ui::LogicalDisplayId displayId)190 KeyArgsBuilder& displayId(ui::LogicalDisplayId displayId) { 191 mDisplayId = displayId; 192 return *this; 193 } 194 policyFlags(int32_t policyFlags)195 KeyArgsBuilder& policyFlags(int32_t policyFlags) { 196 mPolicyFlags = policyFlags; 197 return *this; 198 } 199 addFlag(uint32_t flags)200 KeyArgsBuilder& addFlag(uint32_t flags) { 201 mFlags |= flags; 202 return *this; 203 } 204 keyCode(int32_t keyCode)205 KeyArgsBuilder& keyCode(int32_t keyCode) { 206 mKeyCode = keyCode; 207 return *this; 208 } 209 metaState(int32_t metaState)210 KeyArgsBuilder& metaState(int32_t metaState) { 211 mMetaState |= metaState; 212 mMetaState = normalizeMetaState(/*oldMetaState=*/mMetaState); 213 return *this; 214 } 215 build()216 NotifyKeyArgs build() const { 217 return {mEventId, 218 mEventTime, 219 /*readTime=*/mEventTime, 220 mDeviceId, 221 mSource, 222 mDisplayId, 223 mPolicyFlags, 224 mAction, 225 mFlags, 226 mKeyCode, 227 mScanCode, 228 mMetaState, 229 mDownTime}; 230 } 231 232 private: 233 const int32_t mEventId; 234 int32_t mAction; 235 int32_t mDeviceId = DEFAULT_DEVICE_ID; 236 uint32_t mSource; 237 nsecs_t mDownTime; 238 nsecs_t mEventTime; 239 ui::LogicalDisplayId mDisplayId{ui::LogicalDisplayId::DEFAULT}; 240 uint32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; 241 int32_t mFlags{0}; 242 int32_t mKeyCode{AKEYCODE_UNKNOWN}; 243 int32_t mScanCode{0}; 244 int32_t mMetaState{AMETA_NONE}; 245 }; 246 247 } // namespace android 248