1 /* 2 * Copyright (c) 2021-2022 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 KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H 17 #define KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H 18 19 #include <functional> 20 #include <memory> 21 #include <set> 22 23 #include <singleton.h> 24 25 #include "key_event.h" 26 #include "key_option.h" 27 28 namespace OHOS { 29 namespace MMI { 30 class KeyEventInputSubscribeManager final { 31 DECLARE_SINGLETON(KeyEventInputSubscribeManager); 32 33 public: 34 class SubscribeKeyEventInfo { 35 public: 36 SubscribeKeyEventInfo(std::shared_ptr<KeyOption> keyOption, 37 std::function<void(std::shared_ptr<KeyEvent>)> callback); 38 ~SubscribeKeyEventInfo() = default; 39 GetSubscribeId()40 int32_t GetSubscribeId() const 41 { 42 return subscribeId_; 43 } 44 GetKeyOption()45 std::shared_ptr<KeyOption> GetKeyOption() const 46 { 47 return keyOption_; 48 } 49 GetCallback()50 std::function<void(std::shared_ptr<KeyEvent>)> GetCallback() const 51 { 52 return callback_; 53 } 54 55 bool operator<(const SubscribeKeyEventInfo &other) const; 56 57 private: 58 std::shared_ptr<KeyOption> keyOption_ { nullptr }; 59 std::function<void(std::shared_ptr<KeyEvent>)> callback_ { nullptr }; 60 int32_t subscribeId_ { -1 }; 61 }; 62 63 public: 64 DISALLOW_MOVE(KeyEventInputSubscribeManager); 65 66 int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption, 67 std::function<void(std::shared_ptr<KeyEvent>)> callback); 68 int32_t UnsubscribeKeyEvent(int32_t subscribeId); 69 70 int32_t OnSubscribeKeyEventCallback(std::shared_ptr<KeyEvent> event, int32_t subscribeId); 71 void OnConnected(); 72 73 private: 74 const SubscribeKeyEventInfo* GetSubscribeKeyEvent(int32_t id); 75 76 private: 77 std::set<SubscribeKeyEventInfo> subscribeInfos_; 78 static int32_t subscribeIdManager_; 79 std::mutex mtx_; 80 }; 81 82 #define KeyEventInputSubscribeMgr ::OHOS::Singleton<KeyEventInputSubscribeManager>::GetInstance() 83 } // namespace MMI 84 } // namespace OHOS 85 #endif // KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H