1b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown/*
2b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * Copyright (C) 2010 The Android Open Source Project
3b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown *
4b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * you may not use this file except in compliance with the License.
6b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * You may obtain a copy of the License at
7b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown *
8b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown *
10b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * Unless required by applicable law or agreed to in writing, software
11b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * See the License for the specific language governing permissions and
14b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown * limitations under the License.
15b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown */
16b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
17b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#define LOG_TAG "PointerController"
18b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
19b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown//#define LOG_NDEBUG 0
20b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
21b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown// Log debug messages about pointer updates
22b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#define DEBUG_POINTER_UPDATES 0
23b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
24b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include "PointerController.h"
25b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
26b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <cutils/log.h>
27b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
286b83b76dd18e2a727936839b397e6ed2ed4f5a46Andreas Gampe#pragma GCC diagnostic push
296b83b76dd18e2a727936839b397e6ed2ed4f5a46Andreas Gampe#pragma GCC diagnostic ignored "-Wunused-parameter"
30b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <SkBitmap.h>
31b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <SkCanvas.h>
32b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <SkColor.h>
33b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <SkPaint.h>
34b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#include <SkXfermode.h>
356b83b76dd18e2a727936839b397e6ed2ed4f5a46Andreas Gampe#pragma GCC diagnostic pop
36b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
37b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownnamespace android {
38b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
39b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown// --- PointerController ---
40b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
4105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown// Time to wait before starting the fade when the pointer is inactive.
422352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownstatic const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
432352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownstatic const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
4405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
452352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown// Time to spend fading out the spot completely.
462352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownstatic const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
4705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
482352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown// Time to spend fading out the pointer completely.
492352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownstatic const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
5005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
51c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai// The number of events to be read at once for DisplayEventReceiver.
52c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukaistatic const int EVENT_BUFFER_SIZE = 100;
5305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
542352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown// --- PointerController ---
5505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
562352b978a3c94cd88f41d0d908f961333fdac1e9Jeff BrownPointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
572352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
582352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
595541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown    mHandler = new WeakMessageHandler(this);
605541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown
61c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    if (mDisplayEventReceiver.initCheck() == NO_ERROR) {
62c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        mLooper->addFd(mDisplayEventReceiver.getFd(), Looper::POLL_CALLBACK,
63c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai                       Looper::EVENT_INPUT, this, nullptr);
64c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    } else {
65c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        ALOGE("Failed to initialize DisplayEventReceiver.");
66c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    }
67c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai
68b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
69b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
702352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.animationPending = false;
712352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
72b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    mLocked.displayWidth = -1;
73b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    mLocked.displayHeight = -1;
74b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
75b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
762352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.presentation = PRESENTATION_POINTER;
772352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.presentationChanged = false;
782352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
792352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
802352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
81538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    mLocked.pointerFadeDirection = 0;
82b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    mLocked.pointerX = 0;
83b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    mLocked.pointerY = 0;
84538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    mLocked.pointerAlpha = 0.0f; // pointer is initially faded
852352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.pointerSprite = mSpriteController->createSprite();
862352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.pointerIconChanged = false;
87f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright    mLocked.requestedPointerType= mPolicy->getDefaultPointerIconId();
8805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
89808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    mLocked.animationFrameIndex = 0;
90808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    mLocked.lastFrameUpdatedTime = 0;
91808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
922352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.buttonState = 0;
9305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
9419a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    mPolicy->loadPointerIcon(&mLocked.pointerIcon);
9519a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
962352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    loadResources();
9719a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
9819a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    if (mLocked.pointerIcon.isValid()) {
9919a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        mLocked.pointerIconChanged = true;
10019a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        updatePointerLocked();
10119a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    }
102b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
103b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
104b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff BrownPointerController::~PointerController() {
10505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    mLooper->removeMessages(mHandler);
10605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
1075541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown    AutoMutex _l(mLock);
108b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
1092352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.pointerSprite.clear();
1102352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
1112352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
1122352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        delete mLocked.spots.itemAt(i);
1132352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
1142352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.spots.clear();
1152352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.recycledSprites.clear();
116b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
117b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
118b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownbool PointerController::getBounds(float* outMinX, float* outMinY,
119b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        float* outMaxX, float* outMaxY) const {
120b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
121b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
122b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
123b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
124b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
125b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownbool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
126b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        float* outMaxX, float* outMaxY) const {
127b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
128b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        return false;
129b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
130b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
131b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    *outMinX = 0;
132b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    *outMinY = 0;
133b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    switch (mLocked.displayOrientation) {
134b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    case DISPLAY_ORIENTATION_90:
135b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    case DISPLAY_ORIENTATION_270:
136d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        *outMaxX = mLocked.displayHeight - 1;
137d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        *outMaxY = mLocked.displayWidth - 1;
138b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        break;
139b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    default:
140d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        *outMaxX = mLocked.displayWidth - 1;
141d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        *outMaxY = mLocked.displayHeight - 1;
142b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        break;
143b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
144b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    return true;
145b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
146b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
147b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownvoid PointerController::move(float deltaX, float deltaY) {
148b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#if DEBUG_POINTER_UPDATES
1495baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
150b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#endif
151b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (deltaX == 0.0f && deltaY == 0.0f) {
152b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        return;
153b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
154b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
155b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
156b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
157b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
158b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
159b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
160fe9f8ab03a63b1037f07dd85799fbea80ec6adaaJeff Brownvoid PointerController::setButtonState(int32_t buttonState) {
161b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#if DEBUG_POINTER_UPDATES
1625baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("Set button state 0x%08x", buttonState);
163b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#endif
164b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
165b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
166b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (mLocked.buttonState != buttonState) {
167b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        mLocked.buttonState = buttonState;
168b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
169b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
170b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
171fe9f8ab03a63b1037f07dd85799fbea80ec6adaaJeff Brownint32_t PointerController::getButtonState() const {
172b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
173b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
174b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    return mLocked.buttonState;
175b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
176b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
177b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownvoid PointerController::setPosition(float x, float y) {
178b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#if DEBUG_POINTER_UPDATES
1795baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
180b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown#endif
181b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
182b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
183b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    setPositionLocked(x, y);
184b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
185b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
186b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownvoid PointerController::setPositionLocked(float x, float y) {
187b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    float minX, minY, maxX, maxY;
188b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
189b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        if (x <= minX) {
190b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerX = minX;
191b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        } else if (x >= maxX) {
192b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerX = maxX;
193b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        } else {
194b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerX = x;
195b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
196b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        if (y <= minY) {
197b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerY = minY;
198b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        } else if (y >= maxY) {
199b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerY = maxY;
200b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        } else {
201b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerY = y;
202b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
2032352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        updatePointerLocked();
204b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
205b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
206b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
207b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brownvoid PointerController::getPosition(float* outX, float* outY) const {
208b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
209b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
210b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    *outX = mLocked.pointerX;
211b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    *outY = mLocked.pointerY;
212b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
213b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
214538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brownvoid PointerController::fade(Transition transition) {
21505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    AutoMutex _l(mLock);
21605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
217538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    // Remove the inactivity timeout, since we are fading now.
218538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    removeInactivityTimeoutLocked();
219538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown
220538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    // Start fading.
221538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    if (transition == TRANSITION_IMMEDIATE) {
222538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerFadeDirection = 0;
223538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerAlpha = 0.0f;
224538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        updatePointerLocked();
225538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    } else {
226538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerFadeDirection = -1;
227538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        startAnimationLocked();
228538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    }
22905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
23005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
231538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brownvoid PointerController::unfade(Transition transition) {
23205dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    AutoMutex _l(mLock);
23305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
2342352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Always reset the inactivity timer.
2352352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    resetInactivityTimeoutLocked();
2362352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
237538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    // Start unfading.
238538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    if (transition == TRANSITION_IMMEDIATE) {
239538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerFadeDirection = 0;
2402352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerAlpha = 1.0f;
2412352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        updatePointerLocked();
242538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    } else {
243538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerFadeDirection = 1;
244538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        startAnimationLocked();
24505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
24605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
24705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
2482352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::setPresentation(Presentation presentation) {
24905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    AutoMutex _l(mLock);
25005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
2511db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai    if (presentation == PRESENTATION_POINTER && mLocked.additionalMouseResources.empty()) {
252808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
253808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                                              &mLocked.animationResources);
2541db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai    }
2551db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai
2562352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (mLocked.presentation != presentation) {
2572352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.presentation = presentation;
2582352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.presentationChanged = true;
2592352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2602352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (presentation != PRESENTATION_SPOT) {
2612352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            fadeOutAndReleaseAllSpotsLocked();
2622352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
2632352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2642352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        updatePointerLocked();
26505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
26605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
26705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
268cb5ffcf0e41d2597401208221c61589547a00f3dJeff Brownvoid PointerController::setSpots(const PointerCoords* spotCoords,
269cb5ffcf0e41d2597401208221c61589547a00f3dJeff Brown        const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
2702352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown#if DEBUG_POINTER_UPDATES
2715baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("setSpots: idBits=%08x", spotIdBits.value);
2722352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
2732352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        uint32_t id = idBits.firstMarkedBit();
2742352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        idBits.clearBit(id);
2752352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        const PointerCoords& c = spotCoords[spotIdToIndex[id]];
2765baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
2772352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                c.getAxisValue(AMOTION_EVENT_AXIS_X),
2782352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                c.getAxisValue(AMOTION_EVENT_AXIS_Y),
2792352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
2802352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
2812352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown#endif
2822352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2832352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    AutoMutex _l(mLock);
2842352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2852352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mSpriteController->openTransaction();
2862352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2872352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Add or move spots for fingers that are down.
2882352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
289be1aa8250cee7819c49741e819e81659d1d03823Jeff Brown        uint32_t id = idBits.clearFirstMarkedBit();
2902352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        const PointerCoords& c = spotCoords[spotIdToIndex[id]];
2912352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
2922352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                ? mResources.spotTouch : mResources.spotHover;
2932352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
2942352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
2952352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
2962352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = getSpotLocked(id);
2972352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (!spot) {
2982352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            spot = createAndAddSpotLocked(id);
2992352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
3002352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3012352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        spot->updateSprite(&icon, x, y);
3022352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
3032352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3042352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Remove spots for fingers that went up.
3052352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
3062352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = mLocked.spots.itemAt(i);
3072352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (spot->id != Spot::INVALID_ID
3082352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                && !spotIdBits.hasBit(spot->id)) {
3092352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            fadeOutAndReleaseSpotLocked(spot);
3102352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
3112352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
3122352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3132352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mSpriteController->closeTransaction();
3142352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
3152352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3162352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::clearSpots() {
3172352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown#if DEBUG_POINTER_UPDATES
3185baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("clearSpots");
3192352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown#endif
3202352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3212352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    AutoMutex _l(mLock);
3222352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3232352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    fadeOutAndReleaseAllSpotsLocked();
3242352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
3252352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3262352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
3272352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    AutoMutex _l(mLock);
3282352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
3292352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (mLocked.inactivityTimeout != inactivityTimeout) {
3302352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.inactivityTimeout = inactivityTimeout;
3312352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        resetInactivityTimeoutLocked();
3322352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
333b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
334b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
33519a560197950425f7e1856d5bd1216fbc680bf70Jun Mukaivoid PointerController::reloadPointerResources() {
33619a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    AutoMutex _l(mLock);
33719a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
33819a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    loadResources();
33919a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
34019a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    if (mLocked.presentation == PRESENTATION_POINTER) {
34119a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        mLocked.additionalMouseResources.clear();
34219a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        mLocked.animationResources.clear();
34319a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        mPolicy->loadPointerIcon(&mLocked.pointerIcon);
34419a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai        mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
34519a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai                                              &mLocked.animationResources);
34619a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    }
34719a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
34819a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    mLocked.presentationChanged = true;
34919a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai    updatePointerLocked();
35019a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai}
35119a560197950425f7e1856d5bd1216fbc680bf70Jun Mukai
352d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brownvoid PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
353b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    AutoMutex _l(mLock);
354b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
355d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    // Adjust to use the display's unrotated coordinate frame.
356d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    if (orientation == DISPLAY_ORIENTATION_90
357d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown            || orientation == DISPLAY_ORIENTATION_270) {
358d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        int32_t temp = height;
359d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        height = width;
360d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown        width = temp;
361d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    }
362d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown
363b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
364b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        mLocked.displayWidth = width;
365b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        mLocked.displayHeight = height;
366b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
367b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        float minX, minY, maxX, maxY;
368b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
369b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerX = (minX + maxX) * 0.5f;
370b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerY = (minY + maxY) * 0.5f;
371b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        } else {
372b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerX = 0;
373b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            mLocked.pointerY = 0;
374b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
375b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
3762352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        fadeOutAndReleaseAllSpotsLocked();
377b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
378b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
379b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    if (mLocked.displayOrientation != orientation) {
380d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // Apply offsets to convert from the pixel top-left corner position to the pixel center.
381d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // This creates an invariant frame of reference that we can easily rotate when
382d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // taking into account that the pointer may be located at fractional pixel offsets.
383d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        float x = mLocked.pointerX + 0.5f;
384d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        float y = mLocked.pointerY + 0.5f;
385d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        float temp;
386d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown
387d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // Undo the previous rotation.
388b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        switch (mLocked.displayOrientation) {
389b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_90:
390d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            temp = x;
391d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = mLocked.displayWidth - y;
392d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            y = temp;
393b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
394b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_180:
395d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = mLocked.displayWidth - x;
396d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            y = mLocked.displayHeight - y;
397b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
398b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_270:
399d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            temp = x;
400d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = y;
401d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            y = mLocked.displayHeight - temp;
402b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
403b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
404b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
405d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // Perform the new rotation.
406b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        switch (orientation) {
407b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_90:
408d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            temp = x;
409d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = y;
4105541de9ea3513a12d1ac2ad07e7e04a3aa7741a0Jeff Brown            y = mLocked.displayWidth - temp;
411b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
412b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_180:
413d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = mLocked.displayWidth - x;
414d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            y = mLocked.displayHeight - y;
415b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
416b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        case DISPLAY_ORIENTATION_270:
417d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            temp = x;
418d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            x = mLocked.displayHeight - y;
419d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown            y = temp;
420b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown            break;
421b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        }
422b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
423d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // Apply offsets to convert from the pixel center to the pixel top-left corner position
424d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        // and save the results.
425d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        mLocked.pointerX = x - 0.5f;
426d41cff2d3a95963a7aa348e502c6d42bae367235Jeff Brown        mLocked.pointerY = y - 0.5f;
427b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown        mLocked.displayOrientation = orientation;
428b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown    }
429d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown
430d728bf514f257670fcb9aa22c6eaf97626072c93Jeff Brown    updatePointerLocked();
431b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown}
432b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown
433f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wrightvoid PointerController::updatePointerIcon(int32_t iconId) {
4341db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai    AutoMutex _l(mLock);
435f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright    if (mLocked.requestedPointerType != iconId) {
436f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright        mLocked.requestedPointerType = iconId;
4371db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai        mLocked.presentationChanged = true;
4381db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai        updatePointerLocked();
4391db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai    }
4401db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai}
4411db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai
442d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukaivoid PointerController::setCustomPointerIcon(const SpriteIcon& icon) {
443d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai    AutoMutex _l(mLock);
444d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai
445d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai    const int32_t iconId = mPolicy->getCustomPointerIconId();
446d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai    mLocked.additionalMouseResources[iconId] = icon;
447f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright    mLocked.requestedPointerType = iconId;
448d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai    mLocked.presentationChanged = true;
449d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai
450d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai    updatePointerLocked();
451d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai}
452d4eaef7f4c5a5d281de4fff272cd33e892e26264Jun Mukai
45305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brownvoid PointerController::handleMessage(const Message& message) {
45405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    switch (message.what) {
4552352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    case MSG_INACTIVITY_TIMEOUT:
4562352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        doInactivityTimeout();
45705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        break;
45805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
4592352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
4602352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
461c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukaiint PointerController::handleEvent(int /* fd */, int events, void* /* data */) {
462c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
463c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        ALOGE("Display event receiver pipe was closed or an error occurred.  "
464c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai              "events=0x%x", events);
465c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        return 0; // remove the callback
466c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    }
467c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai
468c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    if (!(events & Looper::EVENT_INPUT)) {
469c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        ALOGW("Received spurious callback for unhandled poll event.  "
470c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai              "events=0x%x", events);
471c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        return 1; // keep the callback
472c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    }
473c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai
474c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    bool gotVsync = false;
475c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    ssize_t n;
476c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    nsecs_t timestamp;
477c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
478c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    while ((n = mDisplayEventReceiver.getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
479c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        for (size_t i = 0; i < static_cast<size_t>(n); ++i) {
480c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai            if (buf[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
481c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai                timestamp = buf[i].header.timestamp;
482c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai                gotVsync = true;
483c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai            }
484c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        }
485c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    }
486c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    if (gotVsync) {
487c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        doAnimate(timestamp);
488c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    }
489c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    return 1;  // keep the callback
490c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai}
491c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai
492c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukaivoid PointerController::doAnimate(nsecs_t timestamp) {
4932352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    AutoMutex _l(mLock);
4942352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
4952352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.animationPending = false;
496808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
497808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    bool keepFading = doFadingAnimationLocked(timestamp);
498808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    bool keepBitmapFlipping = doBitmapAnimationLocked(timestamp);
499808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    if (keepFading || keepBitmapFlipping) {
500808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        startAnimationLocked();
501808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    }
502808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai}
503808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
504808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukaibool PointerController::doFadingAnimationLocked(nsecs_t timestamp) {
505808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    bool keepAnimating = false;
506c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai    nsecs_t frameDelay = timestamp - mLocked.animationTime;
5072352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
5082352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Animate pointer fade.
509538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    if (mLocked.pointerFadeDirection < 0) {
5102352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
511538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        if (mLocked.pointerAlpha <= 0.0f) {
512538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown            mLocked.pointerAlpha = 0.0f;
513538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown            mLocked.pointerFadeDirection = 0;
514538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        } else {
515538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown            keepAnimating = true;
516538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        }
517538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        updatePointerLocked();
518538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    } else if (mLocked.pointerFadeDirection > 0) {
519538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
520538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown        if (mLocked.pointerAlpha >= 1.0f) {
521538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown            mLocked.pointerAlpha = 1.0f;
522538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown            mLocked.pointerFadeDirection = 0;
5232352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        } else {
5242352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            keepAnimating = true;
5252352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
5262352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        updatePointerLocked();
5272352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
5282352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
5292352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Animate spots that are fading out and being removed.
5302352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
5312352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = mLocked.spots.itemAt(i);
5322352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (spot->id == Spot::INVALID_ID) {
5332352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
5342352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            if (spot->alpha <= 0) {
5352352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                mLocked.spots.removeAt(i--);
5362352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                releaseSpotLocked(spot);
5372352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            } else {
5382352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                spot->sprite->setAlpha(spot->alpha);
5392352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown                keepAnimating = true;
5402352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            }
5412352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
5422352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
543808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    return keepAnimating;
544808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai}
545808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
546808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukaibool PointerController::doBitmapAnimationLocked(nsecs_t timestamp) {
547808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    std::map<int32_t, PointerAnimation>::const_iterator iter = mLocked.animationResources.find(
548f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright            mLocked.requestedPointerType);
549808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    if (iter == mLocked.animationResources.end()) {
550808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        return false;
551808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    }
5522352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
553808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    if (timestamp - mLocked.lastFrameUpdatedTime > iter->second.durationPerFrame) {
554808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mSpriteController->openTransaction();
555808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
556808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        int incr = (timestamp - mLocked.lastFrameUpdatedTime) / iter->second.durationPerFrame;
557808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mLocked.animationFrameIndex += incr;
558808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mLocked.lastFrameUpdatedTime += iter->second.durationPerFrame * incr;
559808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        while (mLocked.animationFrameIndex >= iter->second.animationFrames.size()) {
560808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai            mLocked.animationFrameIndex -= iter->second.animationFrames.size();
561808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        }
562808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mLocked.pointerSprite->setIcon(iter->second.animationFrames[mLocked.animationFrameIndex]);
563808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
564808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai        mSpriteController->closeTransaction();
56505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
566808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai
567808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    // Keep animating.
568808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai    return true;
56905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
57005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
5712352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::doInactivityTimeout() {
572538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brown    fade(TRANSITION_GRADUAL);
57305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
57405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
5752352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::startAnimationLocked() {
5762352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (!mLocked.animationPending) {
5772352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.animationPending = true;
5782352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
579c0c0ac37abe6f0b1ab780765b4a48beada7d3444Jun Mukai        mDisplayEventReceiver.requestNextVsync();
58005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
58105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
58205dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
5832352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::resetInactivityTimeoutLocked() {
5842352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
5852352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
5862352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
5872352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
5882352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
5892352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
5902352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
591538881e18323a0c983bd8809f8c3b1cdeeeab8a6Jeff Brownvoid PointerController::removeInactivityTimeoutLocked() {
5922352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
5932352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
5942352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
5952352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::updatePointerLocked() {
5962352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mSpriteController->openTransaction();
5972352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
5982352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
5992352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
6002352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6012352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (mLocked.pointerAlpha > 0) {
6022352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
6032352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerSprite->setVisible(true);
6042352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    } else {
6052352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerSprite->setVisible(false);
6062352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6072352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6082352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
6091db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai        if (mLocked.presentation == PRESENTATION_POINTER) {
610f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright            if (mLocked.requestedPointerType== mPolicy->getDefaultPointerIconId()) {
6111db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
6121db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai            } else {
613808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                std::map<int32_t, SpriteIcon>::const_iterator iter =
614f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright                    mLocked.additionalMouseResources.find(mLocked.requestedPointerType);
6151db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                if (iter != mLocked.additionalMouseResources.end()) {
616808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                    std::map<int32_t, PointerAnimation>::const_iterator anim_iter =
617f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright                            mLocked.animationResources.find(mLocked.requestedPointerType);
618808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                    if (anim_iter != mLocked.animationResources.end()) {
619808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                        mLocked.animationFrameIndex = 0;
620808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                        mLocked.lastFrameUpdatedTime = systemTime(SYSTEM_TIME_MONOTONIC);
621808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                        startAnimationLocked();
622808196f139e93395f1f331f6c7e92ddd66c05979Jun Mukai                    }
6231db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                    mLocked.pointerSprite->setIcon(iter->second);
6241db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                } else {
625f9d9ce7705475874c82af04eb9b208a7fb556792Michael Wright                    ALOGW("Can't find the resource for icon id %d", mLocked.requestedPointerType);
6261db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                    mLocked.pointerSprite->setIcon(mLocked.pointerIcon);
6271db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai                }
6281db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai            }
6291db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai        } else {
6301db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai            mLocked.pointerSprite->setIcon(mResources.spotAnchor);
6311db5397d88e722b1ab82ccb2b429ceec1179ccd8Jun Mukai        }
6322352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.pointerIconChanged = false;
6332352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.presentationChanged = false;
63405dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
6352352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6362352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mSpriteController->closeTransaction();
63705dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
63805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
6392352b978a3c94cd88f41d0d908f961333fdac1e9Jeff BrownPointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
6402352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
6412352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = mLocked.spots.itemAt(i);
6422352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (spot->id == id) {
6432352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            return spot;
6442352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
6452352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6462352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    return NULL;
6472352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
6482352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6492352b978a3c94cd88f41d0d908f961333fdac1e9Jeff BrownPointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
6502352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Remove spots until we have fewer than MAX_SPOTS remaining.
6512352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    while (mLocked.spots.size() >= MAX_SPOTS) {
6522352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = removeFirstFadingSpotLocked();
6532352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (!spot) {
6542352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            spot = mLocked.spots.itemAt(0);
6552352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            mLocked.spots.removeAt(0);
6562352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
6572352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        releaseSpotLocked(spot);
6582352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6592352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6602352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Obtain a sprite from the recycled pool.
6612352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    sp<Sprite> sprite;
6622352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (! mLocked.recycledSprites.isEmpty()) {
6632352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        sprite = mLocked.recycledSprites.top();
6642352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.recycledSprites.pop();
6652352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    } else {
6662352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        sprite = mSpriteController->createSprite();
6672352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6682352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6692352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    // Return the new spot.
6702352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    Spot* spot = new Spot(id, sprite);
6712352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mLocked.spots.push(spot);
6722352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    return spot;
6732352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
6742352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6752352b978a3c94cd88f41d0d908f961333fdac1e9Jeff BrownPointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
6762352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
6772352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = mLocked.spots.itemAt(i);
6782352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (spot->id == Spot::INVALID_ID) {
6792352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            mLocked.spots.removeAt(i);
6802352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            return spot;
68105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown        }
6822352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6832352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    return NULL;
6842352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
6852352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6862352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::releaseSpotLocked(Spot* spot) {
6872352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    spot->sprite->clearIcon();
6882352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6892352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
6902352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        mLocked.recycledSprites.push(spot->sprite);
6912352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
6922352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6932352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    delete spot;
6942352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown}
6952352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
6962352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
6972352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (spot->id != Spot::INVALID_ID) {
6982352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        spot->id = Spot::INVALID_ID;
6992352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        startAnimationLocked();
70005dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown    }
70105dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
70205dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
7032352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::fadeOutAndReleaseAllSpotsLocked() {
7042352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    for (size_t i = 0; i < mLocked.spots.size(); i++) {
7052352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        Spot* spot = mLocked.spots.itemAt(i);
7062352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        fadeOutAndReleaseSpotLocked(spot);
7072352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
70805dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
70905dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
7102352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::loadResources() {
7112352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    mPolicy->loadPointerResources(&mResources);
71205dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
71305dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
7142352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
7152352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown// --- PointerController::Spot ---
7162352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
7172352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brownvoid PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
7182352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
7192352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    sprite->setAlpha(alpha);
7202352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
7212352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    sprite->setPosition(x, y);
7222352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
7232352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    this->x = x;
7242352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    this->y = y;
7252352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown
7262352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    if (icon != lastIcon) {
7272352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        lastIcon = icon;
7282352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        if (icon) {
7292352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            sprite->setIcon(*icon);
7302352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            sprite->setVisible(true);
7312352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        } else {
7322352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown            sprite->setVisible(false);
7332352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown        }
7342352b978a3c94cd88f41d0d908f961333fdac1e9Jeff Brown    }
73505dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown}
73605dc66ada6b61a6bdf806ffaa62617ac5394695dJeff Brown
737b4ff35df5c04aec71fce7e90a6d6f9ef7180c2adJeff Brown} // namespace android
738