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