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