| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef _UI_POINTER_CONTROLLER_CONTEXT_H |
| 18 |
#define _UI_POINTER_CONTROLLER_CONTEXT_H |
| 19 |
|
| 20 |
#include <PointerControllerInterface.h> |
| 21 |
#include <gui/DisplayEventReceiver.h> |
| 22 |
#include <input/DisplayViewport.h> |
| 23 |
#include <input/Input.h> |
| 24 |
#include <utils/BitSet.h> |
| 25 |
#include <utils/Looper.h> |
| 26 |
#include <utils/RefBase.h> |
| 27 |
|
| 28 |
#include <functional> |
| 29 |
#include <map> |
| 30 |
#include <memory> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
#include "SpriteController.h" |
| 34 |
|
| 35 |
namespace android { |
| 36 |
|
| 37 |
class PointerController; |
| 38 |
class MouseCursorController; |
| 39 |
class TouchSpotController; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
struct PointerResources { |
| 45 |
SpriteIcon spotHover; |
| 46 |
SpriteIcon spotTouch; |
| 47 |
SpriteIcon spotAnchor; |
| 48 |
}; |
| 49 |
|
| 50 |
struct PointerAnimation { |
| 51 |
std::vector<SpriteIcon> animationFrames; |
| 52 |
nsecs_t durationPerFrame; |
| 53 |
}; |
| 54 |
|
| 55 |
enum class InactivityTimeout { |
| 56 |
NORMAL = 0, |
| 57 |
SHORT = 1, |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
class PointerControllerPolicyInterface : public virtual RefBase { |
| 70 |
protected: |
| 71 |
PointerControllerPolicyInterface() {} |
| 72 |
virtual ~PointerControllerPolicyInterface() {} |
| 73 |
|
| 74 |
public: |
| 75 |
virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0; |
| 76 |
virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0; |
| 77 |
virtual void loadAdditionalMouseResources( |
| 78 |
std::map<int32_t, SpriteIcon>* outResources, |
| 79 |
std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0; |
| 80 |
virtual int32_t getDefaultPointerIconId() = 0; |
| 81 |
virtual int32_t getCustomPointerIconId() = 0; |
| 82 |
virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) = 0; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
class PointerControllerContext { |
| 91 |
public: |
| 92 |
PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy, |
| 93 |
const sp<Looper>& looper, const sp<SpriteController>& spriteController, |
| 94 |
PointerController& controller); |
| 95 |
~PointerControllerContext(); |
| 96 |
|
| 97 |
void removeInactivityTimeout(); |
| 98 |
void resetInactivityTimeout(); |
| 99 |
void startAnimation(); |
| 100 |
void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
| 101 |
|
| 102 |
nsecs_t getAnimationTime(); |
| 103 |
|
| 104 |
void clearSpotsByDisplay(int32_t displayId); |
| 105 |
|
| 106 |
void setHandlerController(std::shared_ptr<PointerController> controller); |
| 107 |
void setCallbackController(std::shared_ptr<PointerController> controller); |
| 108 |
|
| 109 |
sp<PointerControllerPolicyInterface> getPolicy(); |
| 110 |
sp<SpriteController> getSpriteController(); |
| 111 |
|
| 112 |
void handleDisplayEvents(); |
| 113 |
|
| 114 |
void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback); |
| 115 |
void removeAnimationCallback(int32_t displayId); |
| 116 |
|
| 117 |
class MessageHandler : public virtual android::MessageHandler { |
| 118 |
public: |
| 119 |
enum { |
| 120 |
MSG_INACTIVITY_TIMEOUT, |
| 121 |
}; |
| 122 |
|
| 123 |
void handleMessage(const Message& message) override; |
| 124 |
std::weak_ptr<PointerController> pointerController; |
| 125 |
}; |
| 126 |
|
| 127 |
class LooperCallback : public virtual android::LooperCallback { |
| 128 |
public: |
| 129 |
int handleEvent(int fd, int events, void* data) override; |
| 130 |
std::weak_ptr<PointerController> pointerController; |
| 131 |
}; |
| 132 |
|
| 133 |
private: |
| 134 |
class PointerAnimator { |
| 135 |
public: |
| 136 |
PointerAnimator(PointerControllerContext& context); |
| 137 |
|
| 138 |
void addCallback(int32_t displayId, std::function<bool(nsecs_t)> callback); |
| 139 |
void removeCallback(int32_t displayId); |
| 140 |
void handleVsyncEvents(); |
| 141 |
nsecs_t getAnimationTimeLocked(); |
| 142 |
|
| 143 |
mutable std::mutex mLock; |
| 144 |
|
| 145 |
private: |
| 146 |
struct Locked { |
| 147 |
bool animationPending{false}; |
| 148 |
nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)}; |
| 149 |
|
| 150 |
std::unordered_map<int32_t, std::function<bool(nsecs_t)>> callbacks; |
| 151 |
} mLocked GUARDED_BY(mLock); |
| 152 |
|
| 153 |
DisplayEventReceiver mDisplayEventReceiver; |
| 154 |
|
| 155 |
PointerControllerContext& mContext; |
| 156 |
|
| 157 |
void initializeDisplayEventReceiver(); |
| 158 |
void startAnimationLocked(); |
| 159 |
void handleCallbacksLocked(nsecs_t timestamp); |
| 160 |
}; |
| 161 |
|
| 162 |
sp<PointerControllerPolicyInterface> mPolicy; |
| 163 |
sp<Looper> mLooper; |
| 164 |
sp<SpriteController> mSpriteController; |
| 165 |
sp<MessageHandler> mHandler; |
| 166 |
sp<LooperCallback> mCallback; |
| 167 |
|
| 168 |
PointerController& mController; |
| 169 |
|
| 170 |
PointerAnimator mAnimator; |
| 171 |
|
| 172 |
mutable std::mutex mLock; |
| 173 |
|
| 174 |
struct Locked { |
| 175 |
InactivityTimeout inactivityTimeout; |
| 176 |
} mLocked GUARDED_BY(mLock); |
| 177 |
|
| 178 |
void resetInactivityTimeoutLocked(); |
| 179 |
}; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
#endif |