1 /* 2 * Copyright (c) 2023 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 SWITCH_SUBSCRIBER_HANDLER_H 17 #define SWITCH_SUBSCRIBER_HANDLER_H 18 19 #include <unordered_map> 20 #include "i_input_event_handler.h" 21 #include "uds_server.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class SwitchSubscriberHandler final : public IInputEventHandler { 26 public: 27 SwitchSubscriberHandler() = default; 28 DISALLOW_COPY_AND_MOVE(SwitchSubscriberHandler); 29 ~SwitchSubscriberHandler() = 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 SubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId, int32_t switchType); 43 int32_t UnsubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId); 44 void Dump(int32_t fd, const std::vector<std::string> &args); 45 bool UpdateSwitchState(const std::shared_ptr<SwitchEvent> switchEvent); 46 int32_t QuerySwitchStatus(int32_t switchType, int32_t& state); 47 private: 48 struct Subscriber { SubscriberSubscriber49 Subscriber(int32_t id, SessionPtr sess, int32_t switchType) 50 : id_(id), sess_(sess), switchType_(switchType), timerId_(-1) {} 51 int32_t id_ { -1 }; 52 SessionPtr sess_ { nullptr }; 53 int32_t switchType_ { -1 }; 54 int32_t timerId_ { -1 }; 55 std::shared_ptr<SwitchEvent> switchEvent_ { nullptr }; 56 }; 57 void InsertSubScriber(std::shared_ptr<Subscriber> subs); 58 59 private: 60 bool OnSubscribeSwitchEvent(std::shared_ptr<SwitchEvent> keyEvent); 61 void NotifySubscriber(std::shared_ptr<SwitchEvent> keyEvent, 62 const std::shared_ptr<Subscriber> &subscriber); 63 void OnSessionDelete(SessionPtr sess); 64 bool InitSessionDeleteCallback(); 65 66 private: 67 std::list<std::shared_ptr<Subscriber>> subscribers_ {}; 68 std::atomic_bool callbackInitialized_ { false }; 69 std::shared_ptr<SwitchEvent> switchEvent_ { nullptr }; 70 std::unordered_map<int32_t, int32_t> switchStateRecord_; 71 }; 72 } // namespace MMI 73 } // namespace OHOS 74 #endif // SWITCH_SUBSCRIBER_HANDLER_H 75