1 /* 2 * Copyright (c) 2021-2024 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_SUBSCRIBER_HANDLER_H 17 #define KEY_SUBSCRIBER_HANDLER_H 18 19 #include "i_input_event_handler.h" 20 #include "key_gesture_manager.h" 21 #include "nap_process.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class KeySubscriberHandler final : public IInputEventHandler { 26 public: 27 KeySubscriberHandler() = default; 28 DISALLOW_COPY_AND_MOVE(KeySubscriberHandler); 29 ~KeySubscriberHandler() = default; 30 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 31 void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override; 32 bool IsKeyEventSubscribed(int32_t keyCode, int32_t trrigerType); 33 void InitDataShareListener(); 34 #endif // OHOS_BUILD_ENABLE_KEYBOARD 35 #ifdef OHOS_BUILD_ENABLE_POINTER 36 void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 37 #endif // OHOS_BUILD_ENABLE_POINTER 38 #ifdef OHOS_BUILD_ENABLE_TOUCH 39 void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override; 40 #endif // OHOS_BUILD_ENABLE_TOUCH 41 int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId, const std::shared_ptr<KeyOption> keyOption); 42 int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId); 43 int32_t SubscribeHotkey(SessionPtr sess, int32_t subscribeId, std::shared_ptr<KeyOption> keyOption); 44 int32_t UnsubscribeHotkey(SessionPtr sess, int32_t subscribeId); 45 void RemoveSubscriberKeyUpTimer(int32_t keyCode); 46 int32_t EnableCombineKey(bool enable); 47 void Dump(int32_t fd, const std::vector<std::string> &args); 48 49 private: 50 struct Subscriber { SubscriberSubscriber51 Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption) 52 : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {} 53 int32_t id_ { -1 }; 54 SessionPtr sess_ { nullptr }; 55 std::shared_ptr<KeyOption> keyOption_ { nullptr }; 56 int32_t timerId_ { -1 }; 57 #ifdef SHORTCUT_KEY_MANAGER_ENABLED 58 int32_t shortcutId_ { -1 }; 59 bool isSystem { true }; 60 #endif // SHORTCUT_KEY_MANAGER_ENABLED 61 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 62 }; 63 using SubscriberCollection = std::map<std::shared_ptr<KeyOption>, std::list<std::shared_ptr<Subscriber>>>; 64 65 size_t CountSubscribers() const; 66 void DumpSubscribers(int32_t fd, const SubscriberCollection &collection) const; 67 void DumpSubscriber(int32_t fd, std::shared_ptr<Subscriber> subscriber) const; 68 void InsertSubScriber(std::shared_ptr<Subscriber> subs); 69 bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 70 bool ProcessKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 71 bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent); 72 bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent); 73 bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent); 74 bool HandleRingMute(std::shared_ptr<KeyEvent> keyEvent); 75 bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const; 76 void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent, 77 const std::shared_ptr<Subscriber> &subscriber); 78 bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent); 79 void ClearTimer(const std::shared_ptr<Subscriber> &subscriber); 80 void OnTimer(const std::shared_ptr<Subscriber> subscriber); 81 void OnSessionDelete(SessionPtr sess); 82 bool InitSessionDeleteCallback(); 83 bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 84 void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes); 85 bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 86 bool IsFunctionKey(const std::shared_ptr<KeyEvent> keyEvent); 87 bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key); 88 bool IsEnableCombineKeySwipe(const std::shared_ptr<KeyEvent> key); 89 bool IsEnableCombineKeyRecord(const std::shared_ptr<KeyEvent> keyEvent); 90 bool InterceptByVm(const std::shared_ptr<KeyEvent> key); 91 void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber); 92 void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber); 93 void SubscriberNotifyNap(const std::shared_ptr<Subscriber> subscriber); 94 bool IsEqualKeyOption(std::shared_ptr<KeyOption> newOption, std::shared_ptr<KeyOption> oldOption); 95 bool IsEqualPreKeys(const std::set<int32_t> &preKeys, const std::set<int32_t> &pressedKeys); 96 int32_t AddKeyGestureSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option); 97 int32_t RemoveKeyGestureSubscriber(SessionPtr sess, int32_t subscribeId); 98 #ifdef SHORTCUT_KEY_MANAGER_ENABLED 99 int32_t RegisterSystemKey(std::shared_ptr<KeyOption> option, int32_t session, 100 std::function<void(std::shared_ptr<KeyEvent>)> callback); 101 int32_t RegisterHotKey(std::shared_ptr<KeyOption> option, int32_t session, 102 std::function<void(std::shared_ptr<KeyEvent>)> callback); 103 void UnregisterSystemKey(int32_t shortcutId); 104 void UnregisterHotKey(int32_t shortcutId); 105 void DeleteShortcutId(std::shared_ptr<Subscriber> subscriber); 106 #endif // SHORTCUT_KEY_MANAGER_ENABLED 107 int32_t AddSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option, bool isSystem); 108 int32_t RemoveSubscriber(SessionPtr sess, int32_t subscribeId, bool isSystem); 109 bool IsMatchForegroundPid(std::list<std::shared_ptr<Subscriber>> subs, std::set<int32_t> foregroundPids); 110 void NotifyKeyDownSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, std::shared_ptr<KeyOption> keyOption, 111 std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled); 112 void NotifyKeyDownRightNow(const std::shared_ptr<KeyEvent> &keyEvent, 113 std::list<std::shared_ptr<Subscriber>> &subscribers, bool isRepeat, bool &handled); 114 void NotifyKeyDownDelay(const std::shared_ptr<KeyEvent> &keyEvent, 115 std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled); 116 void NotifyKeyUpSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, 117 std::list<std::shared_ptr<Subscriber>> subscribers, bool &handled); 118 void PrintKeyOption(const std::shared_ptr<KeyOption> keyOption); 119 void ClearSubscriberTimer(std::list<std::shared_ptr<Subscriber>> subscribers); 120 void GetForegroundPids(std::set<int32_t> &pidList); 121 void PublishKeyPressCommonEvent(std::shared_ptr<KeyEvent> keyEvent); 122 void RemoveSubscriberTimer(std::shared_ptr<KeyEvent> keyEvent); 123 bool HandleCallEnded(std::shared_ptr<KeyEvent> keyEvent); 124 void HangUpCallProcess(); 125 void RejectCallProcess(); 126 127 private: 128 SubscriberCollection subscriberMap_; 129 std::mutex subscriberMapMutex_; 130 SubscriberCollection keyGestures_; 131 KeyGestureManager keyGestureMgr_; 132 bool callbackInitialized_ { false }; 133 bool hasEventExecuting_ { false }; 134 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 135 int32_t subscribePowerKeyId_ { -1 }; 136 bool subscribePowerKeyState_ { false }; 137 bool enableCombineKey_ { true }; 138 std::set<int32_t> foregroundPids_ {}; 139 bool isForegroundExits_ { false }; 140 bool needSkipPowerKeyUp_ { false }; 141 bool callBahaviorState_ { false }; 142 }; 143 } // namespace MMI 144 } // namespace OHOS 145 #endif // KEY_SUBSCRIBER_HANDLER_H 146