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 <list> 20 #include <map> 21 #include <singleton.h> 22 23 #include "key_event.h" 24 #include "key_option.h" 25 26 namespace OHOS { 27 namespace MMI { 28 class KeyEventInputSubscribeManager final { 29 DECLARE_SINGLETON(KeyEventInputSubscribeManager); 30 31 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 32 struct MonitorIdentity { 33 int32_t key_ { KeyEvent::KEYCODE_UNKNOWN }; 34 int32_t action_ { KeyEvent::KEY_ACTION_UNKNOWN }; 35 bool isRepeat_ { false }; 36 37 bool operator<(const MonitorIdentity &other) const; 38 std::string Dump() const; 39 bool Want(std::shared_ptr<KeyEvent>) const; 40 }; 41 42 struct Monitor { 43 std::function<void(std::shared_ptr<KeyEvent>)> callback_; 44 }; 45 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 46 47 public: 48 class SubscribeKeyEventInfo { 49 public: 50 SubscribeKeyEventInfo(std::shared_ptr<KeyOption> keyOption, 51 std::function<void(std::shared_ptr<KeyEvent>)> callback); 52 ~SubscribeKeyEventInfo() = default; 53 DISALLOW_MOVE(SubscribeKeyEventInfo); 54 SubscribeKeyEventInfo(const SubscribeKeyEventInfo &other); 55 SubscribeKeyEventInfo& operator = (const SubscribeKeyEventInfo &other); 56 GetSubscribeId()57 int32_t GetSubscribeId() const 58 { 59 return subscribeId_; 60 } 61 GetKeyOption()62 std::shared_ptr<KeyOption> GetKeyOption() const 63 { 64 return keyOption_; 65 } 66 GetCallback()67 std::function<void(std::shared_ptr<KeyEvent>)> GetCallback() const 68 { 69 return callback_; 70 } 71 72 bool operator<(const SubscribeKeyEventInfo &other) const; 73 74 private: 75 std::shared_ptr<KeyOption> keyOption_ { nullptr }; 76 std::function<void(std::shared_ptr<KeyEvent>)> callback_ { nullptr }; 77 int32_t subscribeId_ { -1 }; 78 }; 79 80 public: 81 DISALLOW_MOVE(KeyEventInputSubscribeManager); 82 83 int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption, 84 std::function<void(std::shared_ptr<KeyEvent>)> callback); 85 int32_t UnsubscribeKeyEvent(int32_t subscribeId); 86 87 int32_t SubscribeHotkey(std::shared_ptr<KeyOption> keyOption, 88 std::function<void(std::shared_ptr<KeyEvent>)> callback); 89 int32_t UnsubscribeHotkey(int32_t subscriberId); 90 91 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 92 int32_t SubscribeKeyMonitor(const KeyMonitorOption &keyOption, 93 std::function<void(std::shared_ptr<KeyEvent>)> callback); 94 int32_t UnsubscribeKeyMonitor(int32_t subscriberId); 95 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 96 97 int32_t OnSubscribeKeyEventCallback(std::shared_ptr<KeyEvent> event, int32_t subscribeId); 98 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 99 int32_t OnSubscribeKeyMonitor(std::shared_ptr<KeyEvent> event); 100 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 101 102 void OnConnected(); 103 104 private: 105 std::shared_ptr<const KeyEventInputSubscribeManager::SubscribeKeyEventInfo> GetSubscribeKeyEvent(int32_t id); 106 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 107 int32_t GenerateId(); 108 std::vector<std::function<void(std::shared_ptr<KeyEvent>)>> CheckKeyMonitors(std::shared_ptr<KeyEvent> event); 109 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 110 111 private: 112 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 113 std::map<MonitorIdentity, std::map<int32_t, Monitor>> monitors_; 114 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 115 std::set<SubscribeKeyEventInfo> subscribeInfos_; 116 static int32_t subscribeIdManager_; 117 std::mutex mtx_; 118 }; 119 120 #define KeyEventInputSubscribeMgr ::OHOS::Singleton<KeyEventInputSubscribeManager>::GetInstance() 121 } // namespace MMI 122 } // namespace OHOS 123 #endif // KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H