Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 17 | #include "../Macros.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include "CursorInputMapper.h" |
| 20 | |
| 21 | #include "CursorButtonAccumulator.h" |
| 22 | #include "CursorScrollAccumulator.h" |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame^] | 23 | #include "PointerControllerInterface.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 24 | #include "TouchCursorInputMapperCommon.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | // --- CursorMotionAccumulator --- |
| 29 | |
| 30 | CursorMotionAccumulator::CursorMotionAccumulator() { |
| 31 | clearRelativeAxes(); |
| 32 | } |
| 33 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 34 | void CursorMotionAccumulator::reset(InputDeviceContext& deviceContext) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 35 | clearRelativeAxes(); |
| 36 | } |
| 37 | |
| 38 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 39 | mRelX = 0; |
| 40 | mRelY = 0; |
| 41 | } |
| 42 | |
| 43 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 44 | if (rawEvent->type == EV_REL) { |
| 45 | switch (rawEvent->code) { |
| 46 | case REL_X: |
| 47 | mRelX = rawEvent->value; |
| 48 | break; |
| 49 | case REL_Y: |
| 50 | mRelY = rawEvent->value; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void CursorMotionAccumulator::finishSync() { |
| 57 | clearRelativeAxes(); |
| 58 | } |
| 59 | |
| 60 | // --- CursorInputMapper --- |
| 61 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 62 | CursorInputMapper::CursorInputMapper(InputDeviceContext& deviceContext) |
| 63 | : InputMapper(deviceContext) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 64 | |
| 65 | CursorInputMapper::~CursorInputMapper() {} |
| 66 | |
| 67 | uint32_t CursorInputMapper::getSources() { |
| 68 | return mSource; |
| 69 | } |
| 70 | |
| 71 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 72 | InputMapper::populateDeviceInfo(info); |
| 73 | |
| 74 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 75 | float minX, minY, maxX, maxY; |
| 76 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 77 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f); |
| 78 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f); |
| 79 | } |
| 80 | } else { |
| 81 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f); |
| 82 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f); |
| 83 | } |
| 84 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
| 85 | |
| 86 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 87 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
| 88 | } |
| 89 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 90 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void CursorInputMapper::dump(std::string& dump) { |
| 95 | dump += INDENT2 "Cursor Input Mapper:\n"; |
| 96 | dumpParameters(dump); |
| 97 | dump += StringPrintf(INDENT3 "XScale: %0.3f\n", mXScale); |
| 98 | dump += StringPrintf(INDENT3 "YScale: %0.3f\n", mYScale); |
| 99 | dump += StringPrintf(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 100 | dump += StringPrintf(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 101 | dump += StringPrintf(INDENT3 "HaveVWheel: %s\n", |
| 102 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
| 103 | dump += StringPrintf(INDENT3 "HaveHWheel: %s\n", |
| 104 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
| 105 | dump += StringPrintf(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 106 | dump += StringPrintf(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
| 107 | dump += StringPrintf(INDENT3 "Orientation: %d\n", mOrientation); |
| 108 | dump += StringPrintf(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 109 | dump += StringPrintf(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 110 | dump += StringPrintf(INDENT3 "DownTime: %" PRId64 "\n", mDownTime); |
| 111 | } |
| 112 | |
| 113 | void CursorInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config, |
| 114 | uint32_t changes) { |
| 115 | InputMapper::configure(when, config, changes); |
| 116 | |
| 117 | if (!changes) { // first time only |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 118 | mCursorScrollAccumulator.configure(getDeviceContext()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 119 | |
| 120 | // Configure basic parameters. |
| 121 | configureParameters(); |
| 122 | |
| 123 | // Configure device mode. |
| 124 | switch (mParameters.mode) { |
| 125 | case Parameters::MODE_POINTER_RELATIVE: |
| 126 | // Should not happen during first time configuration. |
| 127 | ALOGE("Cannot start a device in MODE_POINTER_RELATIVE, starting in MODE_POINTER"); |
| 128 | mParameters.mode = Parameters::MODE_POINTER; |
| 129 | [[fallthrough]]; |
| 130 | case Parameters::MODE_POINTER: |
| 131 | mSource = AINPUT_SOURCE_MOUSE; |
| 132 | mXPrecision = 1.0f; |
| 133 | mYPrecision = 1.0f; |
| 134 | mXScale = 1.0f; |
| 135 | mYScale = 1.0f; |
Prabir Pradhan | c7ef27e | 2020-02-03 19:19:15 -0800 | [diff] [blame] | 136 | mPointerController = getContext()->getPointerController(getDeviceId()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 137 | break; |
| 138 | case Parameters::MODE_NAVIGATION: |
| 139 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 140 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 141 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 142 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 143 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | mVWheelScale = 1.0f; |
| 148 | mHWheelScale = 1.0f; |
| 149 | } |
| 150 | |
| 151 | if ((!changes && config->pointerCapture) || |
| 152 | (changes & InputReaderConfiguration::CHANGE_POINTER_CAPTURE)) { |
| 153 | if (config->pointerCapture) { |
| 154 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 155 | mParameters.mode = Parameters::MODE_POINTER_RELATIVE; |
| 156 | mSource = AINPUT_SOURCE_MOUSE_RELATIVE; |
| 157 | // Keep PointerController around in order to preserve the pointer position. |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame^] | 158 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 159 | } else { |
| 160 | ALOGE("Cannot request pointer capture, device is not in MODE_POINTER"); |
| 161 | } |
| 162 | } else { |
| 163 | if (mParameters.mode == Parameters::MODE_POINTER_RELATIVE) { |
| 164 | mParameters.mode = Parameters::MODE_POINTER; |
| 165 | mSource = AINPUT_SOURCE_MOUSE; |
| 166 | } else { |
| 167 | ALOGE("Cannot release pointer capture, device is not in MODE_POINTER_RELATIVE"); |
| 168 | } |
| 169 | } |
| 170 | bumpGeneration(); |
| 171 | if (changes) { |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 172 | NotifyDeviceResetArgs args(getContext()->getNextId(), when, getDeviceId()); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 173 | getListener()->notifyDeviceReset(&args); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 178 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 179 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 180 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 181 | } |
| 182 | |
| 183 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 184 | mOrientation = DISPLAY_ORIENTATION_0; |
| 185 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
| 186 | std::optional<DisplayViewport> internalViewport = |
| 187 | config->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL); |
| 188 | if (internalViewport) { |
| 189 | mOrientation = internalViewport->orientation; |
| 190 | } |
| 191 | } |
| 192 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 193 | bumpGeneration(); |
| 194 | } |
| 195 | } |
| 196 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 197 | void CursorInputMapper::configureParameters() { |
| 198 | mParameters.mode = Parameters::MODE_POINTER; |
| 199 | String8 cursorModeString; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 200 | if (getDeviceContext().getConfiguration().tryGetProperty(String8("cursor.mode"), |
| 201 | cursorModeString)) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 202 | if (cursorModeString == "navigation") { |
| 203 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 204 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 205 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | mParameters.orientationAware = false; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 210 | getDeviceContext().getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
| 211 | mParameters.orientationAware); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 212 | |
| 213 | mParameters.hasAssociatedDisplay = false; |
| 214 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 215 | mParameters.hasAssociatedDisplay = true; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void CursorInputMapper::dumpParameters(std::string& dump) { |
| 220 | dump += INDENT3 "Parameters:\n"; |
| 221 | dump += StringPrintf(INDENT4 "HasAssociatedDisplay: %s\n", |
| 222 | toString(mParameters.hasAssociatedDisplay)); |
| 223 | |
| 224 | switch (mParameters.mode) { |
| 225 | case Parameters::MODE_POINTER: |
| 226 | dump += INDENT4 "Mode: pointer\n"; |
| 227 | break; |
| 228 | case Parameters::MODE_POINTER_RELATIVE: |
| 229 | dump += INDENT4 "Mode: relative pointer\n"; |
| 230 | break; |
| 231 | case Parameters::MODE_NAVIGATION: |
| 232 | dump += INDENT4 "Mode: navigation\n"; |
| 233 | break; |
| 234 | default: |
| 235 | ALOG_ASSERT(false); |
| 236 | } |
| 237 | |
| 238 | dump += StringPrintf(INDENT4 "OrientationAware: %s\n", toString(mParameters.orientationAware)); |
| 239 | } |
| 240 | |
| 241 | void CursorInputMapper::reset(nsecs_t when) { |
| 242 | mButtonState = 0; |
| 243 | mDownTime = 0; |
| 244 | |
| 245 | mPointerVelocityControl.reset(); |
| 246 | mWheelXVelocityControl.reset(); |
| 247 | mWheelYVelocityControl.reset(); |
| 248 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 249 | mCursorButtonAccumulator.reset(getDeviceContext()); |
| 250 | mCursorMotionAccumulator.reset(getDeviceContext()); |
| 251 | mCursorScrollAccumulator.reset(getDeviceContext()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 252 | |
| 253 | InputMapper::reset(when); |
| 254 | } |
| 255 | |
| 256 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
| 257 | mCursorButtonAccumulator.process(rawEvent); |
| 258 | mCursorMotionAccumulator.process(rawEvent); |
| 259 | mCursorScrollAccumulator.process(rawEvent); |
| 260 | |
| 261 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
| 262 | sync(rawEvent->when); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void CursorInputMapper::sync(nsecs_t when) { |
| 267 | int32_t lastButtonState = mButtonState; |
| 268 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 269 | mButtonState = currentButtonState; |
| 270 | |
| 271 | bool wasDown = isPointerDown(lastButtonState); |
| 272 | bool down = isPointerDown(currentButtonState); |
| 273 | bool downChanged; |
| 274 | if (!wasDown && down) { |
| 275 | mDownTime = when; |
| 276 | downChanged = true; |
| 277 | } else if (wasDown && !down) { |
| 278 | downChanged = true; |
| 279 | } else { |
| 280 | downChanged = false; |
| 281 | } |
| 282 | nsecs_t downTime = mDownTime; |
| 283 | bool buttonsChanged = currentButtonState != lastButtonState; |
| 284 | int32_t buttonsPressed = currentButtonState & ~lastButtonState; |
| 285 | int32_t buttonsReleased = lastButtonState & ~currentButtonState; |
| 286 | |
| 287 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 288 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 289 | bool moved = deltaX != 0 || deltaY != 0; |
| 290 | |
| 291 | // Rotate delta according to orientation if needed. |
| 292 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay && |
| 293 | (deltaX != 0.0f || deltaY != 0.0f)) { |
| 294 | rotateDelta(mOrientation, &deltaX, &deltaY); |
| 295 | } |
| 296 | |
| 297 | // Move the pointer. |
| 298 | PointerProperties pointerProperties; |
| 299 | pointerProperties.clear(); |
| 300 | pointerProperties.id = 0; |
| 301 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 302 | |
| 303 | PointerCoords pointerCoords; |
| 304 | pointerCoords.clear(); |
| 305 | |
| 306 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 307 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 308 | bool scrolled = vscroll != 0 || hscroll != 0; |
| 309 | |
| 310 | mWheelYVelocityControl.move(when, nullptr, &vscroll); |
| 311 | mWheelXVelocityControl.move(when, &hscroll, nullptr); |
| 312 | |
| 313 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 314 | |
| 315 | int32_t displayId; |
| 316 | float xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; |
| 317 | float yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION; |
| 318 | if (mSource == AINPUT_SOURCE_MOUSE) { |
| 319 | if (moved || scrolled || buttonsChanged) { |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame^] | 320 | mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 321 | |
| 322 | if (moved) { |
| 323 | mPointerController->move(deltaX, deltaY); |
| 324 | } |
| 325 | |
| 326 | if (buttonsChanged) { |
| 327 | mPointerController->setButtonState(currentButtonState); |
| 328 | } |
| 329 | |
Michael Wright | ca5bede | 2020-07-02 00:00:29 +0100 | [diff] [blame^] | 330 | mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | mPointerController->getPosition(&xCursorPosition, &yCursorPosition); |
| 334 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, xCursorPosition); |
| 335 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, yCursorPosition); |
| 336 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX); |
| 337 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY); |
| 338 | displayId = mPointerController->getDisplayId(); |
| 339 | } else { |
| 340 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 341 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 342 | displayId = ADISPLAY_ID_NONE; |
| 343 | } |
| 344 | |
| 345 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
| 346 | |
| 347 | // Moving an external trackball or mouse should wake the device. |
| 348 | // We don't do this for internal cursor devices to prevent them from waking up |
| 349 | // the device in your pocket. |
| 350 | // TODO: Use the input device configuration to control this behavior more finely. |
| 351 | uint32_t policyFlags = 0; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 352 | if ((buttonsPressed || moved || scrolled) && getDeviceContext().isExternal()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 353 | policyFlags |= POLICY_FLAG_WAKE; |
| 354 | } |
| 355 | |
| 356 | // Synthesize key down from buttons if needed. |
| 357 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 358 | displayId, policyFlags, lastButtonState, currentButtonState); |
| 359 | |
| 360 | // Send motion event. |
| 361 | if (downChanged || moved || scrolled || buttonsChanged) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 362 | int32_t metaState = getContext()->getGlobalMetaState(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 363 | int32_t buttonState = lastButtonState; |
| 364 | int32_t motionEventAction; |
| 365 | if (downChanged) { |
| 366 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 367 | } else if (down || (mSource != AINPUT_SOURCE_MOUSE)) { |
| 368 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 369 | } else { |
| 370 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 371 | } |
| 372 | |
| 373 | if (buttonsReleased) { |
| 374 | BitSet32 released(buttonsReleased); |
| 375 | while (!released.isEmpty()) { |
| 376 | int32_t actionButton = BitSet32::valueForBit(released.clearFirstMarkedBit()); |
| 377 | buttonState &= ~actionButton; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 378 | NotifyMotionArgs releaseArgs(getContext()->getNextId(), when, getDeviceId(), |
| 379 | mSource, displayId, policyFlags, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 380 | AMOTION_EVENT_ACTION_BUTTON_RELEASE, actionButton, 0, |
| 381 | metaState, buttonState, MotionClassification::NONE, |
| 382 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 383 | &pointerCoords, mXPrecision, mYPrecision, |
| 384 | xCursorPosition, yCursorPosition, downTime, |
| 385 | /* videoFrames */ {}); |
| 386 | getListener()->notifyMotion(&releaseArgs); |
| 387 | } |
| 388 | } |
| 389 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 390 | NotifyMotionArgs args(getContext()->getNextId(), when, getDeviceId(), mSource, displayId, |
| 391 | policyFlags, motionEventAction, 0, 0, metaState, currentButtonState, |
| 392 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 393 | &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, |
| 394 | xCursorPosition, yCursorPosition, downTime, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 395 | /* videoFrames */ {}); |
| 396 | getListener()->notifyMotion(&args); |
| 397 | |
| 398 | if (buttonsPressed) { |
| 399 | BitSet32 pressed(buttonsPressed); |
| 400 | while (!pressed.isEmpty()) { |
| 401 | int32_t actionButton = BitSet32::valueForBit(pressed.clearFirstMarkedBit()); |
| 402 | buttonState |= actionButton; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 403 | NotifyMotionArgs pressArgs(getContext()->getNextId(), when, getDeviceId(), mSource, |
| 404 | displayId, policyFlags, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 405 | AMOTION_EVENT_ACTION_BUTTON_PRESS, actionButton, 0, |
| 406 | metaState, buttonState, MotionClassification::NONE, |
| 407 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 408 | &pointerCoords, mXPrecision, mYPrecision, |
| 409 | xCursorPosition, yCursorPosition, downTime, |
| 410 | /* videoFrames */ {}); |
| 411 | getListener()->notifyMotion(&pressArgs); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | ALOG_ASSERT(buttonState == currentButtonState); |
| 416 | |
| 417 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 418 | if (motionEventAction == AMOTION_EVENT_ACTION_UP && (mSource == AINPUT_SOURCE_MOUSE)) { |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 419 | NotifyMotionArgs hoverArgs(getContext()->getNextId(), when, getDeviceId(), mSource, |
| 420 | displayId, policyFlags, AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 421 | 0, metaState, currentButtonState, MotionClassification::NONE, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 422 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 423 | &pointerCoords, mXPrecision, mYPrecision, xCursorPosition, |
| 424 | yCursorPosition, downTime, /* videoFrames */ {}); |
| 425 | getListener()->notifyMotion(&hoverArgs); |
| 426 | } |
| 427 | |
| 428 | // Send scroll events. |
| 429 | if (scrolled) { |
| 430 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 431 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 432 | |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 433 | NotifyMotionArgs scrollArgs(getContext()->getNextId(), when, getDeviceId(), mSource, |
| 434 | displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, |
| 435 | metaState, currentButtonState, MotionClassification::NONE, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 436 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 437 | &pointerCoords, mXPrecision, mYPrecision, xCursorPosition, |
| 438 | yCursorPosition, downTime, /* videoFrames */ {}); |
| 439 | getListener()->notifyMotion(&scrollArgs); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Synthesize key up from buttons if needed. |
| 444 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 445 | displayId, policyFlags, lastButtonState, currentButtonState); |
| 446 | |
| 447 | mCursorMotionAccumulator.finishSync(); |
| 448 | mCursorScrollAccumulator.finishSync(); |
| 449 | } |
| 450 | |
| 451 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 452 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 453 | return getDeviceContext().getScanCodeState(scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 454 | } else { |
| 455 | return AKEY_STATE_UNKNOWN; |
| 456 | } |
| 457 | } |
| 458 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 459 | std::optional<int32_t> CursorInputMapper::getAssociatedDisplayId() { |
| 460 | if (mParameters.hasAssociatedDisplay) { |
| 461 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 462 | return std::make_optional(mPointerController->getDisplayId()); |
| 463 | } else { |
| 464 | // If the device is orientationAware and not a mouse, |
| 465 | // it expects to dispatch events to any display |
| 466 | return std::make_optional(ADISPLAY_ID_NONE); |
| 467 | } |
| 468 | } |
| 469 | return std::nullopt; |
| 470 | } |
| 471 | |
| 472 | } // namespace android |