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