• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LONG_PRESS_SUBSCRIBER_HANDLER_H
17 #define LONG_PRESS_SUBSCRIBER_HANDLER_H
18 
19 #include "singleton.h"
20 
21 #include "long_press_event.h"
22 #include "pointer_event.h"
23 #include "uds_server.h"
24 
25 namespace OHOS {
26 namespace MMI {
27 struct DurationTimer {
28     int32_t duration = -1;
29     int32_t timerId = -1;
30 };
31 
32 struct FingerGesture {
33     inline static constexpr auto MAX_TOUCH_NUM = 2;
34     struct {
35         int32_t id { 0 };
36         int32_t x { 0 };
37         int32_t y { 0 };
38         int64_t downTime { 0 };
39     } touches[MAX_TOUCH_NUM];
40 };
41 
42 struct Subscriber {
SubscriberSubscriber43     Subscriber(int32_t id, SessionPtr sess, int32_t fingerCount, int32_t duration)
44         : id_(id), sess_(sess), fingerCount_(fingerCount), duration_(duration) {}
45     int32_t id_ { -1 };
46     SessionPtr sess_ { nullptr };
47     int32_t fingerCount_ { -1 };
48     int32_t duration_ {-1};
49 };
50 
51 class LongPressSubscriberHandler final {
52     DECLARE_DELAYED_SINGLETON(LongPressSubscriberHandler);
53 public:
54     DISALLOW_COPY_AND_MOVE(LongPressSubscriberHandler);
55 
56     int32_t SubscribeLongPressEvent(SessionPtr sess, int32_t subscribeId, const LongPressRequest &longPressRequest);
57     int32_t UnsubscribeLongPressEvent(SessionPtr sess, int32_t subscribeId);
58     void HandleFingerGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent);
59     void HandleFingerGestureMoveEvent(const std::shared_ptr<PointerEvent> touchEvent);
60     void HandleFingerGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent);
61     int32_t GetBundleName(std::string &bundleName, int32_t windowPid) const;
62 
63 private:
64     void OnSubscribeLongPressEvent(int32_t fingerCount, int32_t duration);
65     void OnSubscribeLongPressCancelEvent(SessionPtr sess, int32_t fingerCount, int32_t duration) const;
66     void NotifySubscriber(std::shared_ptr<Subscriber> subscriber, int32_t result) const;
67     void OnSessionDelete(SessionPtr sess);
68     void InsertSubScriber(const std::shared_ptr<Subscriber> subscriber);
69     bool InitSessionDeleteCallback();
70     void StopFingerGesture();
71     int32_t ConvertVPToPX(int32_t vp) const;
72     bool CheckFingerGestureAction(int32_t fingerCount) const;
73     void StartFingerGesture(int32_t fingerCount);
74     void AddDurationTimer(int32_t duration);
75     void RemoveDurationTimer(int32_t fingerCount, int32_t duration);
76     void AddSessSubscriber(const std::shared_ptr<Subscriber> subscriber);
77     void RemoveSessSubscriber(SessionPtr sess, int32_t subscribeId);
78     void CheckFingerGestureCancelEvent(const std::shared_ptr<PointerEvent> touchEvent) const;
79 
80 private:
81     std::map<std::pair<int32_t, int32_t>, std::vector<std::shared_ptr<Subscriber>>> subscriberInfos_;
82     std::vector<DurationTimer> durationTimers_;
83     std::map<SessionPtr, std::vector<std::shared_ptr<Subscriber>>> sessManager_;
84     FingerGesture fingerGesture_;
85     std::shared_ptr<PointerEvent> touchEvent_ { nullptr };
86     std::atomic_bool callbackInitialized_ { false };
87     bool isAllTimerClosed = false;
88 };
89 #define LONG_PRESS_EVENT_HANDLER ::OHOS::DelayedSingleton<LongPressSubscriberHandler>::GetInstance()
90 } // namespace MMI
91 } // namespace OHOS
92 #endif // LONG_PRESS_SUBSCRIBER_HANDLER_H
93