blob: c7e772d79dc4537a8b726c92ef9b73fb494b3874 [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#ifndef _UI_POINTER_CONTROLLER_H
18#define _UI_POINTER_CONTROLLER_H
19
Michael Wrighta0bc6b12020-06-26 20:25:34 +010020#include <PointerControllerInterface.h>
21#include <gui/DisplayEventReceiver.h>
Patrick Williams8e47a672023-05-01 11:30:37 -050022#include <gui/WindowInfosUpdate.h>
Arthur Hungb9b32002018-12-18 17:39:43 +080023#include <input/DisplayViewport.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070024#include <input/Input.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070025#include <utils/BitSet.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080026#include <utils/Looper.h>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010027#include <utils/RefBase.h>
28
29#include <map>
30#include <memory>
Michael Wright72a89132022-10-22 03:16:31 +010031#include <string>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010032#include <vector>
33
Liam Harringtonc782be62020-07-17 19:48:24 +000034#include "MouseCursorController.h"
35#include "PointerControllerContext.h"
Michael Wrighta0bc6b12020-06-26 20:25:34 +010036#include "SpriteController.h"
Liam Harringtonc782be62020-07-17 19:48:24 +000037#include "TouchSpotController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039namespace android {
40
Jeff Brown2352b972011-04-12 22:39:53 -070041/*
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042 * Tracks pointer movements and draws the pointer sprite to a surface.
43 *
44 * Handles pointer acceleration and animation.
45 */
Michael Wrighta0bc6b12020-06-26 20:25:34 +010046class PointerController : public PointerControllerInterface {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047public:
Michael Wrighta0bc6b12020-06-26 20:25:34 +010048 static std::shared_ptr<PointerController> create(
49 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000050 SpriteController& spriteController);
Jeff Brown05dc66a2011-03-02 14:41:58 -080051
Prabir Pradhanf97fac32021-11-18 16:40:34 +000052 ~PointerController() override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080053
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070054 std::optional<FloatRect> getBounds() const override;
55 void move(float deltaX, float deltaY) override;
56 void setPosition(float x, float y) override;
57 FloatPoint getPosition() const override;
58 int32_t getDisplayId() const override;
59 void fade(Transition transition) override;
60 void unfade(Transition transition) override;
61 void setDisplayViewport(const DisplayViewport& viewport) override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080062
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070063 void setPresentation(Presentation presentation) override;
64 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
65 BitSet32 spotIdBits, int32_t displayId) override;
66 void clearSpots() override;
Jeff Brown2352b972011-04-12 22:39:53 -070067
Brandon Pollack015f5d92022-06-02 06:59:33 +000068 void updatePointerIcon(PointerIconStyle iconId);
Jun Mukaid4eaef72015-10-30 15:54:33 -070069 void setCustomPointerIcon(const SpriteIcon& icon);
Jeff Brown2352b972011-04-12 22:39:53 -070070 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Liam Harringtonc782be62020-07-17 19:48:24 +000071 void doInactivityTimeout();
Jun Mukai19a56012015-11-24 11:25:52 -080072 void reloadPointerResources();
Liam Harringtonc782be62020-07-17 19:48:24 +000073 void onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080074
Prabir Pradhan5693cee2021-12-31 06:51:15 -080075 void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
76 REQUIRES(getLock());
77
Michael Wright72a89132022-10-22 03:16:31 +010078 void dump(std::string& dump);
79
Prabir Pradhan5693cee2021-12-31 06:51:15 -080080protected:
81 using WindowListenerConsumer =
82 std::function<void(const sp<android::gui::WindowInfosListener>&)>;
83
84 // Constructor used to test WindowInfosListener registration.
85 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000086 SpriteController& spriteController, WindowListenerConsumer registerListener,
Prabir Pradhan5693cee2021-12-31 06:51:15 -080087 WindowListenerConsumer unregisterListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +000088
Jeff Brownb4ff35d2011-01-02 16:37:43 -080089private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -080090 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Prabir Pradhan27c6d992023-08-18 19:44:55 +000091 SpriteController& spriteController);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080092
Liam Harringtonc782be62020-07-17 19:48:24 +000093 friend PointerControllerContext::LooperCallback;
94 friend PointerControllerContext::MessageHandler;
Jeff Brown2352b972011-04-12 22:39:53 -070095
Prabir Pradhan5693cee2021-12-31 06:51:15 -080096 // PointerController's DisplayInfoListener can outlive the PointerController because when the
97 // listener is registered, a strong pointer to the listener (which can extend its lifecycle)
98 // is given away. To avoid the small overhead of using two separate locks in these two objects,
99 // we use the DisplayInfoListener's lock in PointerController.
100 std::mutex& getLock() const;
Jeff Brown2352b972011-04-12 22:39:53 -0700101
Liam Harringtonc782be62020-07-17 19:48:24 +0000102 PointerControllerContext mContext;
Jeff Brown2352b972011-04-12 22:39:53 -0700103
Liam Harringtonc782be62020-07-17 19:48:24 +0000104 MouseCursorController mCursorController;
Jeff Brown2352b972011-04-12 22:39:53 -0700105
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800106 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700107 Presentation presentation;
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000108 int32_t pointerDisplayId = ADISPLAY_ID_NONE;
Jeff Brown2352b972011-04-12 22:39:53 -0700109
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000110 std::vector<gui::DisplayInfo> mDisplayInfos;
Liam Harringtonc782be62020-07-17 19:48:24 +0000111 std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800112 } mLocked GUARDED_BY(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800113
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000114 class DisplayInfoListener : public gui::WindowInfosListener {
115 public:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800116 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
Patrick Williams8e47a672023-05-01 11:30:37 -0500117 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800118 void onPointerControllerDestroyed();
119
120 // This lock is also used by PointerController. See PointerController::getLock().
121 std::mutex mLock;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000122
123 private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800124 PointerController* mPointerController GUARDED_BY(mLock);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000125 };
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800126
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000127 sp<DisplayInfoListener> mDisplayInfoListener;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800128 const WindowListenerConsumer mUnregisterWindowInfosListener;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000129
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800130 const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000131
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800132 void clearSpotsLocked() REQUIRES(getLock());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800133};
134
135} // namespace android
136
137#endif // _UI_POINTER_CONTROLLER_H