blob: 48d5a5756a696d0b55275a0c5b8038330a422bd2 [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>
Arthur Hungb9b32002018-12-18 17:39:43 +080022#include <input/DisplayViewport.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070023#include <input/Input.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070024#include <utils/BitSet.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080025#include <utils/Looper.h>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010026#include <utils/RefBase.h>
27
28#include <map>
29#include <memory>
Michael Wright72a89132022-10-22 03:16:31 +010030#include <string>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010031#include <vector>
32
Liam Harringtonc782be62020-07-17 19:48:24 +000033#include "MouseCursorController.h"
34#include "PointerControllerContext.h"
Michael Wrighta0bc6b12020-06-26 20:25:34 +010035#include "SpriteController.h"
Liam Harringtonc782be62020-07-17 19:48:24 +000036#include "TouchSpotController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038namespace android {
39
Jeff Brown2352b972011-04-12 22:39:53 -070040/*
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041 * Tracks pointer movements and draws the pointer sprite to a surface.
42 *
43 * Handles pointer acceleration and animation.
44 */
Michael Wrighta0bc6b12020-06-26 20:25:34 +010045class PointerController : public PointerControllerInterface {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080046public:
Michael Wrighta0bc6b12020-06-26 20:25:34 +010047 static std::shared_ptr<PointerController> create(
48 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
49 const sp<SpriteController>& spriteController);
Jeff Brown05dc66a2011-03-02 14:41:58 -080050
Prabir Pradhanf97fac32021-11-18 16:40:34 +000051 ~PointerController() override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080052
Liam Harringtonc782be62020-07-17 19:48:24 +000053 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080054 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070055 virtual void setButtonState(int32_t buttonState);
56 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080057 virtual void setPosition(float x, float y);
58 virtual void getPosition(float* outX, float* outY) const;
Arthur Hungb9b32002018-12-18 17:39:43 +080059 virtual int32_t getDisplayId() const;
Jeff Brown538881e2011-05-25 18:23:38 -070060 virtual void fade(Transition transition);
61 virtual void unfade(Transition transition);
Garfield Tanb1b07be2020-01-09 11:30:04 -080062 virtual void setDisplayViewport(const DisplayViewport& viewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080063
Jeff Brown2352b972011-04-12 22:39:53 -070064 virtual void setPresentation(Presentation presentation);
Liam Harringtonc782be62020-07-17 19:48:24 +000065 virtual void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
66 BitSet32 spotIdBits, int32_t displayId);
Jeff Brown2352b972011-04-12 22:39:53 -070067 virtual void clearSpots();
68
Brandon Pollack015f5d92022-06-02 06:59:33 +000069 void updatePointerIcon(PointerIconStyle iconId);
Jun Mukaid4eaef72015-10-30 15:54:33 -070070 void setCustomPointerIcon(const SpriteIcon& icon);
Jeff Brown2352b972011-04-12 22:39:53 -070071 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Liam Harringtonc782be62020-07-17 19:48:24 +000072 void doInactivityTimeout();
Jun Mukai19a56012015-11-24 11:25:52 -080073 void reloadPointerResources();
Liam Harringtonc782be62020-07-17 19:48:24 +000074 void onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080075
Prabir Pradhan5693cee2021-12-31 06:51:15 -080076 void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
77 REQUIRES(getLock());
78
Michael Wright72a89132022-10-22 03:16:31 +010079 void dump(std::string& dump);
80
Prabir Pradhan5693cee2021-12-31 06:51:15 -080081protected:
82 using WindowListenerConsumer =
83 std::function<void(const sp<android::gui::WindowInfosListener>&)>;
84
85 // Constructor used to test WindowInfosListener registration.
86 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
87 const sp<SpriteController>& spriteController,
88 WindowListenerConsumer registerListener,
89 WindowListenerConsumer unregisterListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +000090
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -080092 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
93 const sp<SpriteController>& spriteController);
94
Liam Harringtonc782be62020-07-17 19:48:24 +000095 friend PointerControllerContext::LooperCallback;
96 friend PointerControllerContext::MessageHandler;
Jeff Brown2352b972011-04-12 22:39:53 -070097
Prabir Pradhan5693cee2021-12-31 06:51:15 -080098 // PointerController's DisplayInfoListener can outlive the PointerController because when the
99 // listener is registered, a strong pointer to the listener (which can extend its lifecycle)
100 // is given away. To avoid the small overhead of using two separate locks in these two objects,
101 // we use the DisplayInfoListener's lock in PointerController.
102 std::mutex& getLock() const;
Jeff Brown2352b972011-04-12 22:39:53 -0700103
Liam Harringtonc782be62020-07-17 19:48:24 +0000104 PointerControllerContext mContext;
Jeff Brown2352b972011-04-12 22:39:53 -0700105
Liam Harringtonc782be62020-07-17 19:48:24 +0000106 MouseCursorController mCursorController;
Jeff Brown2352b972011-04-12 22:39:53 -0700107
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800108 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700109 Presentation presentation;
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000110 int32_t pointerDisplayId = ADISPLAY_ID_NONE;
Jeff Brown2352b972011-04-12 22:39:53 -0700111
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000112 std::vector<gui::DisplayInfo> mDisplayInfos;
Liam Harringtonc782be62020-07-17 19:48:24 +0000113 std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800114 } mLocked GUARDED_BY(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800115
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000116 class DisplayInfoListener : public gui::WindowInfosListener {
117 public:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800118 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000119 void onWindowInfosChanged(const std::vector<android::gui::WindowInfo>&,
120 const std::vector<android::gui::DisplayInfo>&) override;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800121 void onPointerControllerDestroyed();
122
123 // This lock is also used by PointerController. See PointerController::getLock().
124 std::mutex mLock;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000125
126 private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800127 PointerController* mPointerController GUARDED_BY(mLock);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000128 };
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800129
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000130 sp<DisplayInfoListener> mDisplayInfoListener;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800131 const WindowListenerConsumer mUnregisterWindowInfosListener;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000132
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800133 const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000134
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800135 void clearSpotsLocked() REQUIRES(getLock());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136};
137
138} // namespace android
139
140#endif // _UI_POINTER_CONTROLLER_H