Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #define LOG_TAG "PointerController" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages about pointer updates |
| 22 | #define DEBUG_POINTER_UPDATES 0 |
| 23 | |
| 24 | #include "PointerController.h" |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | |
Andreas Gampe | 6b83b76 | 2014-11-10 15:55:11 -0800 | [diff] [blame] | 28 | #pragma GCC diagnostic push |
| 29 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 30 | #include <SkBitmap.h> |
| 31 | #include <SkCanvas.h> |
| 32 | #include <SkColor.h> |
| 33 | #include <SkPaint.h> |
| 34 | #include <SkXfermode.h> |
Andreas Gampe | 6b83b76 | 2014-11-10 15:55:11 -0800 | [diff] [blame] | 35 | #pragma GCC diagnostic pop |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | // --- PointerController --- |
| 40 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 41 | // Time to wait before starting the fade when the pointer is inactive. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 42 | static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds |
| 43 | static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds |
| 44 | |
| 45 | // Time to wait between animation frames. |
| 46 | static const nsecs_t ANIMATION_FRAME_INTERVAL = 1000000000LL / 60; |
| 47 | |
| 48 | // Time to spend fading out the spot completely. |
| 49 | static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 50 | |
| 51 | // Time to spend fading out the pointer completely. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 52 | static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 53 | |
| 54 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 55 | // --- PointerController --- |
| 56 | |
| 57 | PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 58 | const sp<Looper>& looper, const sp<SpriteController>& spriteController) : |
| 59 | mPolicy(policy), mLooper(looper), mSpriteController(spriteController) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 60 | mHandler = new WeakMessageHandler(this); |
| 61 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 62 | AutoMutex _l(mLock); |
| 63 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 64 | mLocked.animationPending = false; |
| 65 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 66 | mLocked.displayWidth = -1; |
| 67 | mLocked.displayHeight = -1; |
| 68 | mLocked.displayOrientation = DISPLAY_ORIENTATION_0; |
| 69 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 70 | mLocked.presentation = PRESENTATION_POINTER; |
| 71 | mLocked.presentationChanged = false; |
| 72 | |
| 73 | mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL; |
| 74 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 75 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 76 | mLocked.pointerX = 0; |
| 77 | mLocked.pointerY = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 78 | mLocked.pointerAlpha = 0.0f; // pointer is initially faded |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 79 | mLocked.pointerSprite = mSpriteController->createSprite(); |
| 80 | mLocked.pointerIconChanged = false; |
Jun Mukai | 5ec7420 | 2015-10-07 16:58:09 +0900 | [diff] [blame] | 81 | mLocked.requestedPointerShape = mPolicy->getDefaultPointerIconId(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 82 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 83 | mLocked.buttonState = 0; |
| 84 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 85 | loadResources(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | PointerController::~PointerController() { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 89 | mLooper->removeMessages(mHandler); |
| 90 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 91 | AutoMutex _l(mLock); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 92 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 93 | mLocked.pointerSprite.clear(); |
| 94 | |
| 95 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 96 | delete mLocked.spots.itemAt(i); |
| 97 | } |
| 98 | mLocked.spots.clear(); |
| 99 | mLocked.recycledSprites.clear(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool PointerController::getBounds(float* outMinX, float* outMinY, |
| 103 | float* outMaxX, float* outMaxY) const { |
| 104 | AutoMutex _l(mLock); |
| 105 | |
| 106 | return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY); |
| 107 | } |
| 108 | |
| 109 | bool PointerController::getBoundsLocked(float* outMinX, float* outMinY, |
| 110 | float* outMaxX, float* outMaxY) const { |
| 111 | if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | *outMinX = 0; |
| 116 | *outMinY = 0; |
| 117 | switch (mLocked.displayOrientation) { |
| 118 | case DISPLAY_ORIENTATION_90: |
| 119 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 120 | *outMaxX = mLocked.displayHeight - 1; |
| 121 | *outMaxY = mLocked.displayWidth - 1; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 122 | break; |
| 123 | default: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 124 | *outMaxX = mLocked.displayWidth - 1; |
| 125 | *outMaxY = mLocked.displayHeight - 1; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void PointerController::move(float deltaX, float deltaY) { |
| 132 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 133 | ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 134 | #endif |
| 135 | if (deltaX == 0.0f && deltaY == 0.0f) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | AutoMutex _l(mLock); |
| 140 | |
| 141 | setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY); |
| 142 | } |
| 143 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 144 | void PointerController::setButtonState(int32_t buttonState) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 145 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 146 | ALOGD("Set button state 0x%08x", buttonState); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 147 | #endif |
| 148 | AutoMutex _l(mLock); |
| 149 | |
| 150 | if (mLocked.buttonState != buttonState) { |
| 151 | mLocked.buttonState = buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 155 | int32_t PointerController::getButtonState() const { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 156 | AutoMutex _l(mLock); |
| 157 | |
| 158 | return mLocked.buttonState; |
| 159 | } |
| 160 | |
| 161 | void PointerController::setPosition(float x, float y) { |
| 162 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 163 | ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 164 | #endif |
| 165 | AutoMutex _l(mLock); |
| 166 | |
| 167 | setPositionLocked(x, y); |
| 168 | } |
| 169 | |
| 170 | void PointerController::setPositionLocked(float x, float y) { |
| 171 | float minX, minY, maxX, maxY; |
| 172 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 173 | if (x <= minX) { |
| 174 | mLocked.pointerX = minX; |
| 175 | } else if (x >= maxX) { |
| 176 | mLocked.pointerX = maxX; |
| 177 | } else { |
| 178 | mLocked.pointerX = x; |
| 179 | } |
| 180 | if (y <= minY) { |
| 181 | mLocked.pointerY = minY; |
| 182 | } else if (y >= maxY) { |
| 183 | mLocked.pointerY = maxY; |
| 184 | } else { |
| 185 | mLocked.pointerY = y; |
| 186 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 187 | updatePointerLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | void PointerController::getPosition(float* outX, float* outY) const { |
| 192 | AutoMutex _l(mLock); |
| 193 | |
| 194 | *outX = mLocked.pointerX; |
| 195 | *outY = mLocked.pointerY; |
| 196 | } |
| 197 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 198 | void PointerController::fade(Transition transition) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 199 | AutoMutex _l(mLock); |
| 200 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 201 | // Remove the inactivity timeout, since we are fading now. |
| 202 | removeInactivityTimeoutLocked(); |
| 203 | |
| 204 | // Start fading. |
| 205 | if (transition == TRANSITION_IMMEDIATE) { |
| 206 | mLocked.pointerFadeDirection = 0; |
| 207 | mLocked.pointerAlpha = 0.0f; |
| 208 | updatePointerLocked(); |
| 209 | } else { |
| 210 | mLocked.pointerFadeDirection = -1; |
| 211 | startAnimationLocked(); |
| 212 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 215 | void PointerController::unfade(Transition transition) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 216 | AutoMutex _l(mLock); |
| 217 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 218 | // Always reset the inactivity timer. |
| 219 | resetInactivityTimeoutLocked(); |
| 220 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 221 | // Start unfading. |
| 222 | if (transition == TRANSITION_IMMEDIATE) { |
| 223 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 224 | mLocked.pointerAlpha = 1.0f; |
| 225 | updatePointerLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 226 | } else { |
| 227 | mLocked.pointerFadeDirection = 1; |
| 228 | startAnimationLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 232 | void PointerController::setPresentation(Presentation presentation) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 233 | AutoMutex _l(mLock); |
| 234 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 235 | if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) { |
| 236 | mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources); |
| 237 | } |
| 238 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 239 | if (mLocked.presentation != presentation) { |
| 240 | mLocked.presentation = presentation; |
| 241 | mLocked.presentationChanged = true; |
| 242 | |
| 243 | if (presentation != PRESENTATION_SPOT) { |
| 244 | fadeOutAndReleaseAllSpotsLocked(); |
| 245 | } |
| 246 | |
| 247 | updatePointerLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 251 | void PointerController::setSpots(const PointerCoords* spotCoords, |
| 252 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 253 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 254 | ALOGD("setSpots: idBits=%08x", spotIdBits.value); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 255 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) { |
| 256 | uint32_t id = idBits.firstMarkedBit(); |
| 257 | idBits.clearBit(id); |
| 258 | const PointerCoords& c = spotCoords[spotIdToIndex[id]]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 259 | ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 260 | c.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 261 | c.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 262 | c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | AutoMutex _l(mLock); |
| 267 | |
| 268 | mSpriteController->openTransaction(); |
| 269 | |
| 270 | // Add or move spots for fingers that are down. |
| 271 | for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 272 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 273 | const PointerCoords& c = spotCoords[spotIdToIndex[id]]; |
| 274 | const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0 |
| 275 | ? mResources.spotTouch : mResources.spotHover; |
| 276 | float x = c.getAxisValue(AMOTION_EVENT_AXIS_X); |
| 277 | float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 278 | |
| 279 | Spot* spot = getSpotLocked(id); |
| 280 | if (!spot) { |
| 281 | spot = createAndAddSpotLocked(id); |
| 282 | } |
| 283 | |
| 284 | spot->updateSprite(&icon, x, y); |
| 285 | } |
| 286 | |
| 287 | // Remove spots for fingers that went up. |
| 288 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 289 | Spot* spot = mLocked.spots.itemAt(i); |
| 290 | if (spot->id != Spot::INVALID_ID |
| 291 | && !spotIdBits.hasBit(spot->id)) { |
| 292 | fadeOutAndReleaseSpotLocked(spot); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | mSpriteController->closeTransaction(); |
| 297 | } |
| 298 | |
| 299 | void PointerController::clearSpots() { |
| 300 | #if DEBUG_POINTER_UPDATES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 301 | ALOGD("clearSpots"); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | AutoMutex _l(mLock); |
| 305 | |
| 306 | fadeOutAndReleaseAllSpotsLocked(); |
| 307 | } |
| 308 | |
| 309 | void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) { |
| 310 | AutoMutex _l(mLock); |
| 311 | |
| 312 | if (mLocked.inactivityTimeout != inactivityTimeout) { |
| 313 | mLocked.inactivityTimeout = inactivityTimeout; |
| 314 | resetInactivityTimeoutLocked(); |
| 315 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 318 | void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 319 | AutoMutex _l(mLock); |
| 320 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 321 | // Adjust to use the display's unrotated coordinate frame. |
| 322 | if (orientation == DISPLAY_ORIENTATION_90 |
| 323 | || orientation == DISPLAY_ORIENTATION_270) { |
| 324 | int32_t temp = height; |
| 325 | height = width; |
| 326 | width = temp; |
| 327 | } |
| 328 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 329 | if (mLocked.displayWidth != width || mLocked.displayHeight != height) { |
| 330 | mLocked.displayWidth = width; |
| 331 | mLocked.displayHeight = height; |
| 332 | |
| 333 | float minX, minY, maxX, maxY; |
| 334 | if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) { |
| 335 | mLocked.pointerX = (minX + maxX) * 0.5f; |
| 336 | mLocked.pointerY = (minY + maxY) * 0.5f; |
| 337 | } else { |
| 338 | mLocked.pointerX = 0; |
| 339 | mLocked.pointerY = 0; |
| 340 | } |
| 341 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 342 | fadeOutAndReleaseAllSpotsLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 343 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 344 | |
| 345 | if (mLocked.displayOrientation != orientation) { |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 346 | // Apply offsets to convert from the pixel top-left corner position to the pixel center. |
| 347 | // This creates an invariant frame of reference that we can easily rotate when |
| 348 | // taking into account that the pointer may be located at fractional pixel offsets. |
| 349 | float x = mLocked.pointerX + 0.5f; |
| 350 | float y = mLocked.pointerY + 0.5f; |
| 351 | float temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 352 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 353 | // Undo the previous rotation. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 354 | switch (mLocked.displayOrientation) { |
| 355 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 356 | temp = x; |
| 357 | x = mLocked.displayWidth - y; |
| 358 | y = temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 359 | break; |
| 360 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 361 | x = mLocked.displayWidth - x; |
| 362 | y = mLocked.displayHeight - y; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 363 | break; |
| 364 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 365 | temp = x; |
| 366 | x = y; |
| 367 | y = mLocked.displayHeight - temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 371 | // Perform the new rotation. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 372 | switch (orientation) { |
| 373 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 374 | temp = x; |
| 375 | x = y; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 376 | y = mLocked.displayWidth - temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 377 | break; |
| 378 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 379 | x = mLocked.displayWidth - x; |
| 380 | y = mLocked.displayHeight - y; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 381 | break; |
| 382 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 383 | temp = x; |
| 384 | x = mLocked.displayHeight - y; |
| 385 | y = temp; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | |
Jeff Brown | d41cff2 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 389 | // Apply offsets to convert from the pixel center to the pixel top-left corner position |
| 390 | // and save the results. |
| 391 | mLocked.pointerX = x - 0.5f; |
| 392 | mLocked.pointerY = y - 0.5f; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 393 | mLocked.displayOrientation = orientation; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 394 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 395 | |
| 396 | updatePointerLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 399 | void PointerController::updatePointerShape(int iconId) { |
| 400 | AutoMutex _l(mLock); |
| 401 | if (mLocked.requestedPointerShape != iconId) { |
| 402 | mLocked.requestedPointerShape = iconId; |
| 403 | mLocked.presentationChanged = true; |
| 404 | updatePointerLocked(); |
| 405 | } |
| 406 | } |
| 407 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 408 | void PointerController::setPointerIcon(const SpriteIcon& icon) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 409 | AutoMutex _l(mLock); |
| 410 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 411 | mLocked.pointerIcon = icon.copy(); |
| 412 | mLocked.pointerIconChanged = true; |
| 413 | |
| 414 | updatePointerLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 417 | void PointerController::handleMessage(const Message& message) { |
| 418 | switch (message.what) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 419 | case MSG_ANIMATE: |
| 420 | doAnimate(); |
| 421 | break; |
| 422 | case MSG_INACTIVITY_TIMEOUT: |
| 423 | doInactivityTimeout(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 424 | break; |
| 425 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 428 | void PointerController::doAnimate() { |
| 429 | AutoMutex _l(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 430 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 431 | bool keepAnimating = false; |
| 432 | mLocked.animationPending = false; |
| 433 | nsecs_t frameDelay = systemTime(SYSTEM_TIME_MONOTONIC) - mLocked.animationTime; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 434 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 435 | // Animate pointer fade. |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 436 | if (mLocked.pointerFadeDirection < 0) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 437 | mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 438 | if (mLocked.pointerAlpha <= 0.0f) { |
| 439 | mLocked.pointerAlpha = 0.0f; |
| 440 | mLocked.pointerFadeDirection = 0; |
| 441 | } else { |
| 442 | keepAnimating = true; |
| 443 | } |
| 444 | updatePointerLocked(); |
| 445 | } else if (mLocked.pointerFadeDirection > 0) { |
| 446 | mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION; |
| 447 | if (mLocked.pointerAlpha >= 1.0f) { |
| 448 | mLocked.pointerAlpha = 1.0f; |
| 449 | mLocked.pointerFadeDirection = 0; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 450 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 451 | keepAnimating = true; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 452 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 453 | updatePointerLocked(); |
| 454 | } |
| 455 | |
| 456 | // Animate spots that are fading out and being removed. |
| 457 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 458 | Spot* spot = mLocked.spots.itemAt(i); |
| 459 | if (spot->id == Spot::INVALID_ID) { |
| 460 | spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION; |
| 461 | if (spot->alpha <= 0) { |
| 462 | mLocked.spots.removeAt(i--); |
| 463 | releaseSpotLocked(spot); |
| 464 | } else { |
| 465 | spot->sprite->setAlpha(spot->alpha); |
| 466 | keepAnimating = true; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (keepAnimating) { |
| 472 | startAnimationLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 476 | void PointerController::doInactivityTimeout() { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 477 | fade(TRANSITION_GRADUAL); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 480 | void PointerController::startAnimationLocked() { |
| 481 | if (!mLocked.animationPending) { |
| 482 | mLocked.animationPending = true; |
| 483 | mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 484 | mLooper->sendMessageDelayed(ANIMATION_FRAME_INTERVAL, mHandler, Message(MSG_ANIMATE)); |
| 485 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 488 | void PointerController::resetInactivityTimeoutLocked() { |
| 489 | mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT); |
| 490 | |
| 491 | nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT |
| 492 | ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL; |
| 493 | mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT); |
| 494 | } |
| 495 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 496 | void PointerController::removeInactivityTimeoutLocked() { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 497 | mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void PointerController::updatePointerLocked() { |
| 501 | mSpriteController->openTransaction(); |
| 502 | |
| 503 | mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER); |
| 504 | mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY); |
| 505 | |
| 506 | if (mLocked.pointerAlpha > 0) { |
| 507 | mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha); |
| 508 | mLocked.pointerSprite->setVisible(true); |
| 509 | } else { |
| 510 | mLocked.pointerSprite->setVisible(false); |
| 511 | } |
| 512 | |
| 513 | if (mLocked.pointerIconChanged || mLocked.presentationChanged) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 514 | if (mLocked.presentation == PRESENTATION_POINTER) { |
Jun Mukai | 5ec7420 | 2015-10-07 16:58:09 +0900 | [diff] [blame] | 515 | if (mLocked.requestedPointerShape == mPolicy->getDefaultPointerIconId()) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 516 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 517 | } else { |
| 518 | std::map<int, SpriteIcon>::const_iterator iter = |
| 519 | mLocked.additionalMouseResources.find(mLocked.requestedPointerShape); |
| 520 | if (iter != mLocked.additionalMouseResources.end()) { |
| 521 | mLocked.pointerSprite->setIcon(iter->second); |
| 522 | } else { |
| 523 | ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerShape); |
| 524 | mLocked.pointerSprite->setIcon(mLocked.pointerIcon); |
| 525 | } |
| 526 | } |
| 527 | } else { |
| 528 | mLocked.pointerSprite->setIcon(mResources.spotAnchor); |
| 529 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 530 | mLocked.pointerIconChanged = false; |
| 531 | mLocked.presentationChanged = false; |
| 532 | } |
| 533 | |
| 534 | mSpriteController->closeTransaction(); |
| 535 | } |
| 536 | |
| 537 | PointerController::Spot* PointerController::getSpotLocked(uint32_t id) { |
| 538 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 539 | Spot* spot = mLocked.spots.itemAt(i); |
| 540 | if (spot->id == id) { |
| 541 | return spot; |
| 542 | } |
| 543 | } |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) { |
| 548 | // Remove spots until we have fewer than MAX_SPOTS remaining. |
| 549 | while (mLocked.spots.size() >= MAX_SPOTS) { |
| 550 | Spot* spot = removeFirstFadingSpotLocked(); |
| 551 | if (!spot) { |
| 552 | spot = mLocked.spots.itemAt(0); |
| 553 | mLocked.spots.removeAt(0); |
| 554 | } |
| 555 | releaseSpotLocked(spot); |
| 556 | } |
| 557 | |
| 558 | // Obtain a sprite from the recycled pool. |
| 559 | sp<Sprite> sprite; |
| 560 | if (! mLocked.recycledSprites.isEmpty()) { |
| 561 | sprite = mLocked.recycledSprites.top(); |
| 562 | mLocked.recycledSprites.pop(); |
| 563 | } else { |
| 564 | sprite = mSpriteController->createSprite(); |
| 565 | } |
| 566 | |
| 567 | // Return the new spot. |
| 568 | Spot* spot = new Spot(id, sprite); |
| 569 | mLocked.spots.push(spot); |
| 570 | return spot; |
| 571 | } |
| 572 | |
| 573 | PointerController::Spot* PointerController::removeFirstFadingSpotLocked() { |
| 574 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 575 | Spot* spot = mLocked.spots.itemAt(i); |
| 576 | if (spot->id == Spot::INVALID_ID) { |
| 577 | mLocked.spots.removeAt(i); |
| 578 | return spot; |
| 579 | } |
| 580 | } |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | void PointerController::releaseSpotLocked(Spot* spot) { |
| 585 | spot->sprite->clearIcon(); |
| 586 | |
| 587 | if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) { |
| 588 | mLocked.recycledSprites.push(spot->sprite); |
| 589 | } |
| 590 | |
| 591 | delete spot; |
| 592 | } |
| 593 | |
| 594 | void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) { |
| 595 | if (spot->id != Spot::INVALID_ID) { |
| 596 | spot->id = Spot::INVALID_ID; |
| 597 | startAnimationLocked(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void PointerController::fadeOutAndReleaseAllSpotsLocked() { |
| 602 | for (size_t i = 0; i < mLocked.spots.size(); i++) { |
| 603 | Spot* spot = mLocked.spots.itemAt(i); |
| 604 | fadeOutAndReleaseSpotLocked(spot); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void PointerController::loadResources() { |
| 609 | mPolicy->loadPointerResources(&mResources); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | // --- PointerController::Spot --- |
| 614 | |
| 615 | void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) { |
| 616 | sprite->setLayer(Sprite::BASE_LAYER_SPOT + id); |
| 617 | sprite->setAlpha(alpha); |
| 618 | sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale)); |
| 619 | sprite->setPosition(x, y); |
| 620 | |
| 621 | this->x = x; |
| 622 | this->y = y; |
| 623 | |
| 624 | if (icon != lastIcon) { |
| 625 | lastIcon = icon; |
| 626 | if (icon) { |
| 627 | sprite->setIcon(*icon); |
| 628 | sprite->setVisible(true); |
| 629 | } else { |
| 630 | sprite->setVisible(false); |
| 631 | } |
| 632 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 635 | } // namespace android |