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" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | // Log debug messages about pointer updates |
| 21 | #define DEBUG_POINTER_UPDATES 0 |
| 22 | |
| 23 | #include "PointerController.h" |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 24 | #include "MouseCursorController.h" |
| 25 | #include "PointerControllerContext.h" |
| 26 | #include "TouchSpotController.h" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 29 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 30 | #include <SkBitmap.h> |
| 31 | #include <SkBlendMode.h> |
| 32 | #include <SkCanvas.h> |
| 33 | #include <SkColor.h> |
| 34 | #include <SkPaint.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | // --- PointerController --- |
| 39 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 40 | std::shared_ptr<PointerController> PointerController::create( |
| 41 | const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
| 42 | const sp<SpriteController>& spriteController) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 43 | // using 'new' to access non-public constructor |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 44 | std::shared_ptr<PointerController> controller = std::shared_ptr<PointerController>( |
| 45 | new PointerController(policy, looper, spriteController)); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 46 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 47 | /* |
| 48 | * Now we need to hook up the constructed PointerController object to its callbacks. |
| 49 | * |
| 50 | * This must be executed after the constructor but before any other methods on PointerController |
| 51 | * in order to ensure that the fully constructed object is visible on the Looper thread, since |
| 52 | * that may be a different thread than where the PointerController is initially constructed. |
| 53 | * |
| 54 | * Unfortunately, this cannot be done as part of the constructor since we need to hand out |
| 55 | * weak_ptr's which themselves cannot be constructed until there's at least one shared_ptr. |
| 56 | */ |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 57 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 58 | controller->mContext.setHandlerController(controller); |
| 59 | controller->mContext.setCallbackController(controller); |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 60 | return controller; |
| 61 | } |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 62 | |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 63 | PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 64 | const sp<Looper>& looper, |
| 65 | const sp<SpriteController>& spriteController) |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 66 | : mContext(policy, looper, spriteController, *this), mCursorController(mContext) { |
| 67 | std::scoped_lock lock(mLock); |
| 68 | mLocked.presentation = Presentation::SPOT; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 71 | bool PointerController::getBounds(float* outMinX, float* outMinY, float* outMaxX, |
| 72 | float* outMaxY) const { |
| 73 | return mCursorController.getBounds(outMinX, outMinY, outMaxX, outMaxY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void PointerController::move(float deltaX, float deltaY) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 77 | mCursorController.move(deltaX, deltaY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 80 | void PointerController::setButtonState(int32_t buttonState) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 81 | mCursorController.setButtonState(buttonState); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 84 | int32_t PointerController::getButtonState() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 85 | return mCursorController.getButtonState(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void PointerController::setPosition(float x, float y) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 89 | std::scoped_lock lock(mLock); |
| 90 | mCursorController.setPosition(x, y); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void PointerController::getPosition(float* outX, float* outY) const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 94 | mCursorController.getPosition(outX, outY); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 97 | int32_t PointerController::getDisplayId() const { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 98 | return mCursorController.getDisplayId(); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 101 | void PointerController::fade(Transition transition) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 102 | std::scoped_lock lock(mLock); |
| 103 | mCursorController.fade(transition); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 106 | void PointerController::unfade(Transition transition) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 107 | std::scoped_lock lock(mLock); |
| 108 | mCursorController.unfade(transition); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 111 | void PointerController::setPresentation(Presentation presentation) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 112 | std::scoped_lock lock(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 113 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 114 | if (mLocked.presentation == presentation) { |
| 115 | return; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 118 | mLocked.presentation = presentation; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 119 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 120 | if (!mCursorController.isViewportValid()) { |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 124 | if (presentation == Presentation::POINTER) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 125 | mCursorController.getAdditionalMouseResources(); |
| 126 | clearSpotsLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 130 | void PointerController::setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 131 | BitSet32 spotIdBits, int32_t displayId) { |
| 132 | std::scoped_lock lock(mLock); |
| 133 | auto it = mLocked.spotControllers.find(displayId); |
| 134 | if (it == mLocked.spotControllers.end()) { |
| 135 | mLocked.spotControllers.try_emplace(displayId, displayId, mContext); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 136 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 137 | mLocked.spotControllers.at(displayId).setSpots(spotCoords, spotIdToIndex, spotIdBits); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void PointerController::clearSpots() { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 141 | std::scoped_lock lock(mLock); |
| 142 | clearSpotsLocked(); |
| 143 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 144 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 145 | void PointerController::clearSpotsLocked() REQUIRES(mLock) { |
| 146 | for (auto& [displayID, spotController] : mLocked.spotControllers) { |
| 147 | spotController.clearSpots(); |
Prabir Pradhan | ca7d723 | 2020-01-31 17:42:34 -0800 | [diff] [blame] | 148 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 152 | mContext.setInactivityTimeout(inactivityTimeout); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 155 | void PointerController::reloadPointerResources() { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 156 | std::scoped_lock lock(mLock); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 157 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 158 | for (auto& [displayID, spotController] : mLocked.spotControllers) { |
| 159 | spotController.reloadSpotResources(); |
| 160 | } |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 161 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 162 | if (mCursorController.resourcesLoaded()) { |
| 163 | bool getAdditionalMouseResources = false; |
| 164 | if (mLocked.presentation == PointerController::Presentation::POINTER) { |
| 165 | getAdditionalMouseResources = true; |
| 166 | } |
| 167 | mCursorController.reloadPointerResources(getAdditionalMouseResources); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | void PointerController::setDisplayViewport(const DisplayViewport& viewport) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 172 | std::scoped_lock lock(mLock); |
| 173 | |
| 174 | bool getAdditionalMouseResources = false; |
| 175 | if (mLocked.presentation == PointerController::Presentation::POINTER) { |
| 176 | getAdditionalMouseResources = true; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 177 | } |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 178 | mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 181 | void PointerController::updatePointerIcon(int32_t iconId) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 182 | std::scoped_lock lock(mLock); |
| 183 | mCursorController.updatePointerIcon(iconId); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 186 | void PointerController::setCustomPointerIcon(const SpriteIcon& icon) { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 187 | std::scoped_lock lock(mLock); |
| 188 | mCursorController.setCustomPointerIcon(icon); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 191 | void PointerController::doInactivityTimeout() { |
Michael Wright | 6853fe6 | 2020-07-02 00:01:38 +0100 | [diff] [blame] | 192 | fade(Transition::GRADUAL); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 195 | void PointerController::onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports) { |
| 196 | std::unordered_set<int32_t> displayIdSet; |
| 197 | for (DisplayViewport viewport : viewports) { |
| 198 | displayIdSet.insert(viewport.displayId); |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 201 | std::scoped_lock lock(mLock); |
| 202 | for (auto it = mLocked.spotControllers.begin(); it != mLocked.spotControllers.end();) { |
| 203 | int32_t displayID = it->first; |
| 204 | if (!displayIdSet.count(displayID)) { |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Ensures that an in-progress animation won't dereference |
| 207 | * a null pointer to TouchSpotController. |
| 208 | */ |
| 209 | mContext.removeAnimationCallback(displayID); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 210 | it = mLocked.spotControllers.erase(it); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 211 | } else { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 212 | ++it; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 217 | } // namespace android |