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 KEY_MONITOR_MANAGER_H 17 #define KEY_MONITOR_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 22 #include "key_event.h" 23 #include "key_option.h" 24 25 namespace OHOS { 26 namespace MMI { 27 class KeyMonitorManager final { 28 struct PendingMonitor { 29 int32_t timerId_ { -1 }; 30 std::shared_ptr<KeyEvent> keyEvent_; 31 }; 32 33 public: 34 struct Monitor { 35 int32_t session_ { -1 }; 36 int32_t key_ { KeyEvent::KEYCODE_UNKNOWN }; 37 int32_t action_ { KeyEvent::KEY_ACTION_UNKNOWN }; 38 bool isRepeat_ { false }; 39 40 bool operator<(const Monitor &other) const; 41 std::string Dump() const; 42 bool IsFocused() const; 43 bool Want(std::shared_ptr<KeyEvent> keyEvent) const; 44 }; 45 46 KeyMonitorManager(); 47 ~KeyMonitorManager() = default; 48 DISALLOW_COPY_AND_MOVE(KeyMonitorManager); 49 50 int32_t AddMonitor(const Monitor &monitor); 51 void RemoveMonitor(const Monitor &monitor); 52 bool Intercept(std::shared_ptr<KeyEvent> keyEvent); 53 bool Intercept(std::shared_ptr<KeyEvent> KeyEvent, int32_t delay); 54 void NotifyPendingMonitors(); 55 void ResetAll(int32_t keyCode); 56 57 static std::shared_ptr<KeyMonitorManager> GetInstance(); 58 59 private: 60 void OnSessionLost(int32_t session); 61 bool CheckMonitor(const Monitor &monitor); 62 void NotifyKeyMonitor(std::shared_ptr<KeyEvent> keyEvent, int32_t session); 63 64 std::set<Monitor> monitors_; 65 std::map<Monitor, PendingMonitor> pending_; 66 static const std::set<int32_t> allowedKeys_; 67 }; 68 69 #define KEY_MONITOR_MGR KeyMonitorManager::GetInstance() 70 } // namespace MMI 71 } // namespace OHOS 72 #endif // KEY_MONITOR_MANAGER_H 73