blob: 11527378f586e50336a6f2326e5084ea925eb956 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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 Gampe6b83b762014-11-10 15:55:11 -080028#pragma GCC diagnostic push
29#pragma GCC diagnostic ignored "-Wunused-parameter"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080030#include <SkBitmap.h>
31#include <SkCanvas.h>
32#include <SkColor.h>
33#include <SkPaint.h>
34#include <SkXfermode.h>
Andreas Gampe6b83b762014-11-10 15:55:11 -080035#pragma GCC diagnostic pop
Jeff Brownb4ff35d2011-01-02 16:37:43 -080036
37namespace android {
38
39// --- PointerController ---
40
Jeff Brown05dc66a2011-03-02 14:41:58 -080041// Time to wait before starting the fade when the pointer is inactive.
Jeff Brown2352b972011-04-12 22:39:53 -070042static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
43static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
44
45// Time to wait between animation frames.
46static const nsecs_t ANIMATION_FRAME_INTERVAL = 1000000000LL / 60;
47
48// Time to spend fading out the spot completely.
49static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080050
51// Time to spend fading out the pointer completely.
Jeff Brown2352b972011-04-12 22:39:53 -070052static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080053
54
Jeff Brown2352b972011-04-12 22:39:53 -070055// --- PointerController ---
56
57PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
58 const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
59 mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
Jeff Brown5541de92011-04-11 11:54:25 -070060 mHandler = new WeakMessageHandler(this);
61
Jeff Brownb4ff35d2011-01-02 16:37:43 -080062 AutoMutex _l(mLock);
63
Jeff Brown2352b972011-04-12 22:39:53 -070064 mLocked.animationPending = false;
65
Jeff Brownb4ff35d2011-01-02 16:37:43 -080066 mLocked.displayWidth = -1;
67 mLocked.displayHeight = -1;
68 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
69
Jeff Brown2352b972011-04-12 22:39:53 -070070 mLocked.presentation = PRESENTATION_POINTER;
71 mLocked.presentationChanged = false;
72
73 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
74
Jeff Brown538881e2011-05-25 18:23:38 -070075 mLocked.pointerFadeDirection = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080076 mLocked.pointerX = 0;
77 mLocked.pointerY = 0;
Jeff Brown538881e2011-05-25 18:23:38 -070078 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
Jeff Brown2352b972011-04-12 22:39:53 -070079 mLocked.pointerSprite = mSpriteController->createSprite();
80 mLocked.pointerIconChanged = false;
81
Jeff Brownb4ff35d2011-01-02 16:37:43 -080082 mLocked.buttonState = 0;
83
Jeff Brown2352b972011-04-12 22:39:53 -070084 loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080085}
86
87PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -080088 mLooper->removeMessages(mHandler);
89
Jeff Brown5541de92011-04-11 11:54:25 -070090 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091
Jeff Brown2352b972011-04-12 22:39:53 -070092 mLocked.pointerSprite.clear();
93
94 for (size_t i = 0; i < mLocked.spots.size(); i++) {
95 delete mLocked.spots.itemAt(i);
96 }
97 mLocked.spots.clear();
98 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080099}
100
101bool PointerController::getBounds(float* outMinX, float* outMinY,
102 float* outMaxX, float* outMaxY) const {
103 AutoMutex _l(mLock);
104
105 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
106}
107
108bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
109 float* outMaxX, float* outMaxY) const {
110 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
111 return false;
112 }
113
114 *outMinX = 0;
115 *outMinY = 0;
116 switch (mLocked.displayOrientation) {
117 case DISPLAY_ORIENTATION_90:
118 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800119 *outMaxX = mLocked.displayHeight - 1;
120 *outMaxY = mLocked.displayWidth - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800121 break;
122 default:
Jeff Brownd41cff22011-03-03 02:09:54 -0800123 *outMaxX = mLocked.displayWidth - 1;
124 *outMaxY = mLocked.displayHeight - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800125 break;
126 }
127 return true;
128}
129
130void PointerController::move(float deltaX, float deltaY) {
131#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000132 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800133#endif
134 if (deltaX == 0.0f && deltaY == 0.0f) {
135 return;
136 }
137
138 AutoMutex _l(mLock);
139
140 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
141}
142
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700143void PointerController::setButtonState(int32_t buttonState) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800144#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000145 ALOGD("Set button state 0x%08x", buttonState);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800146#endif
147 AutoMutex _l(mLock);
148
149 if (mLocked.buttonState != buttonState) {
150 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800151 }
152}
153
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700154int32_t PointerController::getButtonState() const {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800155 AutoMutex _l(mLock);
156
157 return mLocked.buttonState;
158}
159
160void PointerController::setPosition(float x, float y) {
161#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000162 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800163#endif
164 AutoMutex _l(mLock);
165
166 setPositionLocked(x, y);
167}
168
169void PointerController::setPositionLocked(float x, float y) {
170 float minX, minY, maxX, maxY;
171 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
172 if (x <= minX) {
173 mLocked.pointerX = minX;
174 } else if (x >= maxX) {
175 mLocked.pointerX = maxX;
176 } else {
177 mLocked.pointerX = x;
178 }
179 if (y <= minY) {
180 mLocked.pointerY = minY;
181 } else if (y >= maxY) {
182 mLocked.pointerY = maxY;
183 } else {
184 mLocked.pointerY = y;
185 }
Jeff Brown2352b972011-04-12 22:39:53 -0700186 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800187 }
188}
189
190void PointerController::getPosition(float* outX, float* outY) const {
191 AutoMutex _l(mLock);
192
193 *outX = mLocked.pointerX;
194 *outY = mLocked.pointerY;
195}
196
Jeff Brown538881e2011-05-25 18:23:38 -0700197void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800198 AutoMutex _l(mLock);
199
Jeff Brown538881e2011-05-25 18:23:38 -0700200 // Remove the inactivity timeout, since we are fading now.
201 removeInactivityTimeoutLocked();
202
203 // Start fading.
204 if (transition == TRANSITION_IMMEDIATE) {
205 mLocked.pointerFadeDirection = 0;
206 mLocked.pointerAlpha = 0.0f;
207 updatePointerLocked();
208 } else {
209 mLocked.pointerFadeDirection = -1;
210 startAnimationLocked();
211 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800212}
213
Jeff Brown538881e2011-05-25 18:23:38 -0700214void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800215 AutoMutex _l(mLock);
216
Jeff Brown2352b972011-04-12 22:39:53 -0700217 // Always reset the inactivity timer.
218 resetInactivityTimeoutLocked();
219
Jeff Brown538881e2011-05-25 18:23:38 -0700220 // Start unfading.
221 if (transition == TRANSITION_IMMEDIATE) {
222 mLocked.pointerFadeDirection = 0;
Jeff Brown2352b972011-04-12 22:39:53 -0700223 mLocked.pointerAlpha = 1.0f;
224 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700225 } else {
226 mLocked.pointerFadeDirection = 1;
227 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800228 }
229}
230
Jeff Brown2352b972011-04-12 22:39:53 -0700231void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800232 AutoMutex _l(mLock);
233
Jeff Brown2352b972011-04-12 22:39:53 -0700234 if (mLocked.presentation != presentation) {
235 mLocked.presentation = presentation;
236 mLocked.presentationChanged = true;
237
238 if (presentation != PRESENTATION_SPOT) {
239 fadeOutAndReleaseAllSpotsLocked();
240 }
241
242 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800243 }
244}
245
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700246void PointerController::setSpots(const PointerCoords* spotCoords,
247 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700248#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000249 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
Jeff Brown2352b972011-04-12 22:39:53 -0700250 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
251 uint32_t id = idBits.firstMarkedBit();
252 idBits.clearBit(id);
253 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
Steve Block5baa3a62011-12-20 16:23:08 +0000254 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
Jeff Brown2352b972011-04-12 22:39:53 -0700255 c.getAxisValue(AMOTION_EVENT_AXIS_X),
256 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
257 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
258 }
259#endif
260
261 AutoMutex _l(mLock);
262
263 mSpriteController->openTransaction();
264
265 // Add or move spots for fingers that are down.
266 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700267 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -0700268 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
269 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
270 ? mResources.spotTouch : mResources.spotHover;
271 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
272 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
273
274 Spot* spot = getSpotLocked(id);
275 if (!spot) {
276 spot = createAndAddSpotLocked(id);
277 }
278
279 spot->updateSprite(&icon, x, y);
280 }
281
282 // Remove spots for fingers that went up.
283 for (size_t i = 0; i < mLocked.spots.size(); i++) {
284 Spot* spot = mLocked.spots.itemAt(i);
285 if (spot->id != Spot::INVALID_ID
286 && !spotIdBits.hasBit(spot->id)) {
287 fadeOutAndReleaseSpotLocked(spot);
288 }
289 }
290
291 mSpriteController->closeTransaction();
292}
293
294void PointerController::clearSpots() {
295#if DEBUG_POINTER_UPDATES
Steve Block5baa3a62011-12-20 16:23:08 +0000296 ALOGD("clearSpots");
Jeff Brown2352b972011-04-12 22:39:53 -0700297#endif
298
299 AutoMutex _l(mLock);
300
301 fadeOutAndReleaseAllSpotsLocked();
302}
303
304void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
305 AutoMutex _l(mLock);
306
307 if (mLocked.inactivityTimeout != inactivityTimeout) {
308 mLocked.inactivityTimeout = inactivityTimeout;
309 resetInactivityTimeoutLocked();
310 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800311}
312
Jeff Brownd728bf52012-09-08 18:05:28 -0700313void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800314 AutoMutex _l(mLock);
315
Jeff Brownd728bf52012-09-08 18:05:28 -0700316 // Adjust to use the display's unrotated coordinate frame.
317 if (orientation == DISPLAY_ORIENTATION_90
318 || orientation == DISPLAY_ORIENTATION_270) {
319 int32_t temp = height;
320 height = width;
321 width = temp;
322 }
323
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800324 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
325 mLocked.displayWidth = width;
326 mLocked.displayHeight = height;
327
328 float minX, minY, maxX, maxY;
329 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
330 mLocked.pointerX = (minX + maxX) * 0.5f;
331 mLocked.pointerY = (minY + maxY) * 0.5f;
332 } else {
333 mLocked.pointerX = 0;
334 mLocked.pointerY = 0;
335 }
336
Jeff Brown2352b972011-04-12 22:39:53 -0700337 fadeOutAndReleaseAllSpotsLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800338 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800339
340 if (mLocked.displayOrientation != orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800341 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
342 // This creates an invariant frame of reference that we can easily rotate when
343 // taking into account that the pointer may be located at fractional pixel offsets.
344 float x = mLocked.pointerX + 0.5f;
345 float y = mLocked.pointerY + 0.5f;
346 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800347
Jeff Brownd41cff22011-03-03 02:09:54 -0800348 // Undo the previous rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800349 switch (mLocked.displayOrientation) {
350 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800351 temp = x;
352 x = mLocked.displayWidth - y;
353 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800354 break;
355 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800356 x = mLocked.displayWidth - x;
357 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800358 break;
359 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800360 temp = x;
361 x = y;
362 y = mLocked.displayHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800363 break;
364 }
365
Jeff Brownd41cff22011-03-03 02:09:54 -0800366 // Perform the new rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800367 switch (orientation) {
368 case DISPLAY_ORIENTATION_90:
Jeff Brownd41cff22011-03-03 02:09:54 -0800369 temp = x;
370 x = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700371 y = mLocked.displayWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800372 break;
373 case DISPLAY_ORIENTATION_180:
Jeff Brownd41cff22011-03-03 02:09:54 -0800374 x = mLocked.displayWidth - x;
375 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800376 break;
377 case DISPLAY_ORIENTATION_270:
Jeff Brownd41cff22011-03-03 02:09:54 -0800378 temp = x;
379 x = mLocked.displayHeight - y;
380 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800381 break;
382 }
383
Jeff Brownd41cff22011-03-03 02:09:54 -0800384 // Apply offsets to convert from the pixel center to the pixel top-left corner position
385 // and save the results.
386 mLocked.pointerX = x - 0.5f;
387 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800388 mLocked.displayOrientation = orientation;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800389 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700390
391 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800392}
393
Jeff Brown2352b972011-04-12 22:39:53 -0700394void PointerController::setPointerIcon(const SpriteIcon& icon) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800395 AutoMutex _l(mLock);
396
Jeff Brown2352b972011-04-12 22:39:53 -0700397 mLocked.pointerIcon = icon.copy();
398 mLocked.pointerIconChanged = true;
399
400 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800401}
402
Jeff Brown05dc66a2011-03-02 14:41:58 -0800403void PointerController::handleMessage(const Message& message) {
404 switch (message.what) {
Jeff Brown2352b972011-04-12 22:39:53 -0700405 case MSG_ANIMATE:
406 doAnimate();
407 break;
408 case MSG_INACTIVITY_TIMEOUT:
409 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800410 break;
411 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800412}
413
Jeff Brown2352b972011-04-12 22:39:53 -0700414void PointerController::doAnimate() {
415 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800416
Jeff Brown2352b972011-04-12 22:39:53 -0700417 bool keepAnimating = false;
418 mLocked.animationPending = false;
419 nsecs_t frameDelay = systemTime(SYSTEM_TIME_MONOTONIC) - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800420
Jeff Brown2352b972011-04-12 22:39:53 -0700421 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700422 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown2352b972011-04-12 22:39:53 -0700423 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700424 if (mLocked.pointerAlpha <= 0.0f) {
425 mLocked.pointerAlpha = 0.0f;
426 mLocked.pointerFadeDirection = 0;
427 } else {
428 keepAnimating = true;
429 }
430 updatePointerLocked();
431 } else if (mLocked.pointerFadeDirection > 0) {
432 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
433 if (mLocked.pointerAlpha >= 1.0f) {
434 mLocked.pointerAlpha = 1.0f;
435 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800436 } else {
Jeff Brown2352b972011-04-12 22:39:53 -0700437 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800438 }
Jeff Brown2352b972011-04-12 22:39:53 -0700439 updatePointerLocked();
440 }
441
442 // Animate spots that are fading out and being removed.
443 for (size_t i = 0; i < mLocked.spots.size(); i++) {
444 Spot* spot = mLocked.spots.itemAt(i);
445 if (spot->id == Spot::INVALID_ID) {
446 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
447 if (spot->alpha <= 0) {
448 mLocked.spots.removeAt(i--);
449 releaseSpotLocked(spot);
450 } else {
451 spot->sprite->setAlpha(spot->alpha);
452 keepAnimating = true;
453 }
454 }
455 }
456
457 if (keepAnimating) {
458 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800459 }
460}
461
Jeff Brown2352b972011-04-12 22:39:53 -0700462void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700463 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800464}
465
Jeff Brown2352b972011-04-12 22:39:53 -0700466void PointerController::startAnimationLocked() {
467 if (!mLocked.animationPending) {
468 mLocked.animationPending = true;
469 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
470 mLooper->sendMessageDelayed(ANIMATION_FRAME_INTERVAL, mHandler, Message(MSG_ANIMATE));
471 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800472}
473
Jeff Brown2352b972011-04-12 22:39:53 -0700474void PointerController::resetInactivityTimeoutLocked() {
475 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
476
477 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
478 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
479 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
480}
481
Jeff Brown538881e2011-05-25 18:23:38 -0700482void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700483 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown2352b972011-04-12 22:39:53 -0700484}
485
486void PointerController::updatePointerLocked() {
487 mSpriteController->openTransaction();
488
489 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
490 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
491
492 if (mLocked.pointerAlpha > 0) {
493 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
494 mLocked.pointerSprite->setVisible(true);
495 } else {
496 mLocked.pointerSprite->setVisible(false);
497 }
498
499 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
500 mLocked.pointerSprite->setIcon(mLocked.presentation == PRESENTATION_POINTER
501 ? mLocked.pointerIcon : mResources.spotAnchor);
502 mLocked.pointerIconChanged = false;
503 mLocked.presentationChanged = false;
504 }
505
506 mSpriteController->closeTransaction();
507}
508
509PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
510 for (size_t i = 0; i < mLocked.spots.size(); i++) {
511 Spot* spot = mLocked.spots.itemAt(i);
512 if (spot->id == id) {
513 return spot;
514 }
515 }
516 return NULL;
517}
518
519PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
520 // Remove spots until we have fewer than MAX_SPOTS remaining.
521 while (mLocked.spots.size() >= MAX_SPOTS) {
522 Spot* spot = removeFirstFadingSpotLocked();
523 if (!spot) {
524 spot = mLocked.spots.itemAt(0);
525 mLocked.spots.removeAt(0);
526 }
527 releaseSpotLocked(spot);
528 }
529
530 // Obtain a sprite from the recycled pool.
531 sp<Sprite> sprite;
532 if (! mLocked.recycledSprites.isEmpty()) {
533 sprite = mLocked.recycledSprites.top();
534 mLocked.recycledSprites.pop();
535 } else {
536 sprite = mSpriteController->createSprite();
537 }
538
539 // Return the new spot.
540 Spot* spot = new Spot(id, sprite);
541 mLocked.spots.push(spot);
542 return spot;
543}
544
545PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
546 for (size_t i = 0; i < mLocked.spots.size(); i++) {
547 Spot* spot = mLocked.spots.itemAt(i);
548 if (spot->id == Spot::INVALID_ID) {
549 mLocked.spots.removeAt(i);
550 return spot;
551 }
552 }
553 return NULL;
554}
555
556void PointerController::releaseSpotLocked(Spot* spot) {
557 spot->sprite->clearIcon();
558
559 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
560 mLocked.recycledSprites.push(spot->sprite);
561 }
562
563 delete spot;
564}
565
566void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
567 if (spot->id != Spot::INVALID_ID) {
568 spot->id = Spot::INVALID_ID;
569 startAnimationLocked();
570 }
571}
572
573void PointerController::fadeOutAndReleaseAllSpotsLocked() {
574 for (size_t i = 0; i < mLocked.spots.size(); i++) {
575 Spot* spot = mLocked.spots.itemAt(i);
576 fadeOutAndReleaseSpotLocked(spot);
577 }
578}
579
580void PointerController::loadResources() {
581 mPolicy->loadPointerResources(&mResources);
582}
583
584
585// --- PointerController::Spot ---
586
587void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
588 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
589 sprite->setAlpha(alpha);
590 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
591 sprite->setPosition(x, y);
592
593 this->x = x;
594 this->y = y;
595
596 if (icon != lastIcon) {
597 lastIcon = icon;
598 if (icon) {
599 sprite->setIcon(*icon);
600 sprite->setVisible(true);
601 } else {
602 sprite->setVisible(false);
603 }
604 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800605}
606
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800607} // namespace android