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 "json_parser.h" 22 #include "long_press_event.h" 23 #include "pointer_event.h" 24 #include "uds_server.h" 25 26 namespace OHOS { 27 namespace MMI { 28 struct DurationTimer { 29 int32_t duration = -1; 30 int32_t timerId = -1; 31 }; 32 33 struct FingerGesture { 34 inline static constexpr auto MAX_TOUCH_NUM = 2; 35 struct { 36 int32_t id { 0 }; 37 int32_t x { 0 }; 38 int32_t y { 0 }; 39 int64_t downTime { 0 }; 40 } touches[MAX_TOUCH_NUM]; 41 }; 42 43 struct Subscriber { SubscriberSubscriber44 Subscriber(int32_t id, SessionPtr sess, int32_t fingerCount, int32_t duration) 45 : id_(id), sess_(sess), fingerCount_(fingerCount), duration_(duration) {} 46 int32_t id_ { -1 }; 47 SessionPtr sess_ { nullptr }; 48 int32_t fingerCount_ { -1 }; 49 int32_t duration_ {-1}; 50 }; 51 52 class LongPressSubscriberHandler final { 53 DECLARE_DELAYED_SINGLETON(LongPressSubscriberHandler); 54 public: 55 DISALLOW_COPY_AND_MOVE(LongPressSubscriberHandler); 56 57 int32_t SubscribeLongPressEvent(SessionPtr sess, int32_t subscribeId, const LongPressRequest &longPressRequest); 58 int32_t UnsubscribeLongPressEvent(SessionPtr sess, int32_t subscribeId); 59 void HandleFingerGestureDownEvent(const std::shared_ptr<PointerEvent> touchEvent); 60 void HandleFingerGestureMoveEvent(const std::shared_ptr<PointerEvent> touchEvent); 61 void HandleFingerGestureUpEvent(const std::shared_ptr<PointerEvent> touchEvent); 62 int32_t GetBundleName(std::string &bundleName, int32_t windowPid) const; 63 64 private: 65 void OnSubscribeLongPressEvent(int32_t fingerCount, int32_t duration); 66 void OnSubscribeLongPressCancelEvent(SessionPtr sess, int32_t fingerCount, int32_t duration) const; 67 void NotifySubscriber(std::shared_ptr<Subscriber> subscriber, int32_t result) const; 68 void OnSessionDelete(SessionPtr sess); 69 void InsertSubScriber(const std::shared_ptr<Subscriber> subscriber); 70 bool InitSessionDeleteCallback(); 71 void StopFingerGesture(); 72 int32_t ConvertVPToPX(int32_t vp) const; 73 bool CheckFingerGestureAction(int32_t fingerCount) const; 74 void StartFingerGesture(int32_t fingerCount); 75 void AddDurationTimer(int32_t duration); 76 void RemoveDurationTimer(int32_t fingerCount, int32_t duration); 77 void AddSessSubscriber(const std::shared_ptr<Subscriber> subscriber); 78 void RemoveSessSubscriber(SessionPtr sess, int32_t subscribeId); 79 void CheckFingerGestureCancelEvent(const std::shared_ptr<PointerEvent> touchEvent) const; 80 81 private: 82 std::map<std::pair<int32_t, int32_t>, std::vector<std::shared_ptr<Subscriber>>> subscriberInfos_; 83 std::vector<DurationTimer> durationTimers_; 84 std::map<SessionPtr, std::vector<std::shared_ptr<Subscriber>>> sessManager_; 85 FingerGesture fingerGesture_; 86 std::shared_ptr<PointerEvent> touchEvent_ { nullptr }; 87 std::atomic_bool callbackInitialized_ { false }; 88 bool isAllTimerClosed = false; 89 }; 90 #define LONG_PRESS_EVENT_HANDLER ::OHOS::DelayedSingleton<LongPressSubscriberHandler>::GetInstance() 91 } // namespace MMI 92 } // namespace OHOS 93 #endif // LONG_PRESS_SUBSCRIBER_HANDLER_H 94