1 /* 2 * Copyright (c) 2025 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 INPUT_ACTIVE_SUBSCRIBER_HANDLER_H 17 #define INPUT_ACTIVE_SUBSCRIBER_HANDLER_H 18 19 #include <mutex> 20 #include "i_input_event_handler.h" 21 #include "uds_server.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class InputActiveSubscriberHandler final : public IInputEventHandler { 26 public: 27 InputActiveSubscriberHandler() = default; 28 DISALLOW_COPY_AND_MOVE(InputActiveSubscriberHandler); 29 ~InputActiveSubscriberHandler() = default; 30 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 31 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 32 #endif // OHOS_BUILD_ENABLE_KEYBOARD 33 #ifdef OHOS_BUILD_ENABLE_POINTER 34 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 35 #endif // OHOS_BUILD_ENABLE_POINTER 36 #ifdef OHOS_BUILD_ENABLE_TOUCH 37 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 38 #endif // OHOS_BUILD_ENABLE_TOUCH 39 #ifdef OHOS_BUILD_ENABLE_SWITCH 40 void HandleSwitchEvent(const std::shared_ptr<SwitchEvent> switchEvent) override; 41 #endif // OHOS_BUILD_ENABLE_SWITCH 42 int32_t SubscribeInputActive(SessionPtr sess, int32_t subscribeId, int64_t interval); 43 int32_t UnsubscribeInputActive(SessionPtr sess, int32_t subscribeId); 44 void Dump(int32_t fd, const std::vector<std::string> &args); 45 private: 46 enum EventType : uint32_t { 47 EVENTTYPE_INVALID = 0, 48 EVENTTYPE_KEY, 49 EVENTTYPE_POINTER, 50 }; 51 struct Subscriber { SubscriberSubscriber52 Subscriber(int32_t id, SessionPtr sess, int64_t interval) : id_(id), sess_(sess), interval_(interval) {} 53 int32_t id_ { -1 }; 54 SessionPtr sess_ { nullptr }; 55 int32_t timerId_ { INVALID_TIMERID }; 56 int64_t interval_ { 0 }; 57 EventType lastEventType_ { EVENTTYPE_INVALID }; 58 int64_t sendEventLastTime_ { 0 }; 59 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 60 std::shared_ptr<PointerEvent> pointerEvent_ { nullptr }; 61 }; 62 63 void InsertSubscriber(std::shared_ptr<Subscriber> subscriber); 64 void OnSubscribeInputActive(const std::shared_ptr<KeyEvent> keyEvent); 65 void OnSubscribeInputActive(const std::shared_ptr<PointerEvent> pointerEvent); 66 void NotifySubscriber(const std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> subscriber); 67 void NotifySubscriber( 68 const std::shared_ptr<PointerEvent> pointerEvent, const std::shared_ptr<Subscriber> subscriber); 69 void OnSessionDelete(SessionPtr sess); 70 bool InitSessionDeleteCallback(); 71 bool IsImmediateNotifySubscriber(std::shared_ptr<Subscriber> subscriber, int64_t eventTime); 72 void StartIntervalTimer(std::shared_ptr<Subscriber> subscriber, int64_t eventTime); 73 void CleanSubscribeInfo(std::shared_ptr<Subscriber> subscriber, int64_t eventTime); 74 private: 75 std::list<std::shared_ptr<Subscriber>> subscribers_; 76 std::atomic_bool callbackInitialized_ { false }; 77 static constexpr int32_t INVALID_TIMERID = -1; 78 }; 79 } // namespace MMI 80 } // namespace OHOS 81 #endif // INPUT_ACTIVE_SUBSCRIBER_HANDLER_H