• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ResetSkipPowerKeyUpFlag();
48     void Dump(int32_t fd, const std::vector<std::string> &args);
49 
50 private:
51     struct Subscriber {
SubscriberSubscriber52         Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption)
53             : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {}
54         int32_t id_ { -1 };
55         SessionPtr sess_ { nullptr };
56         std::shared_ptr<KeyOption> keyOption_ { nullptr };
57         int32_t timerId_ { -1 };
58 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
59         int32_t shortcutId_ { -1 };
60         bool isSystem { true };
61 #endif // SHORTCUT_KEY_MANAGER_ENABLED
62         std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
63     };
64     using SubscriberCollection = std::map<std::shared_ptr<KeyOption>, std::list<std::shared_ptr<Subscriber>>>;
65 
66     size_t CountSubscribers() const;
67     void DumpSubscribers(int32_t fd, const SubscriberCollection &collection) const;
68     void DumpSubscriber(int32_t fd, std::shared_ptr<Subscriber> subscriber) const;
69     void InsertSubScriber(std::shared_ptr<Subscriber> subs);
70     bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
71     bool ProcessKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
72     bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent);
73     bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent);
74     bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent);
75 #ifdef OHOS_BUILD_ENABLE_CALL_MANAGER
76     bool HandleRingMute(std::shared_ptr<KeyEvent> keyEvent);
77 #endif // OHOS_BUILD_ENABLE_CALL_MANAGER
78     bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const;
79     void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent,
80         const std::shared_ptr<Subscriber> &subscriber);
81     bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent);
82     void ClearTimer(const std::shared_ptr<Subscriber> &subscriber);
83     void OnTimer(const std::shared_ptr<Subscriber> subscriber);
84     void OnSessionDelete(SessionPtr sess);
85     bool InitSessionDeleteCallback();
86     bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
87     void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes);
88     bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
89     bool IsFunctionKey(const std::shared_ptr<KeyEvent> keyEvent);
90     bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key);
91     bool IsEnableCombineKeySwipe(const std::shared_ptr<KeyEvent> key);
92     bool IsEnableCombineKeyRecord(const std::shared_ptr<KeyEvent> keyEvent);
93     bool InterceptByVm(const std::shared_ptr<KeyEvent> key);
94     void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber);
95     void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber);
96     void SubscriberNotifyNap(const std::shared_ptr<Subscriber> subscriber);
97     bool IsEqualKeyOption(std::shared_ptr<KeyOption> newOption, std::shared_ptr<KeyOption> oldOption);
98     bool IsEqualPreKeys(const std::set<int32_t> &preKeys, const std::set<int32_t> &pressedKeys);
99     int32_t AddKeyGestureSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option);
100     int32_t RemoveKeyGestureSubscriber(SessionPtr sess, int32_t subscribeId);
101 #ifdef SHORTCUT_KEY_MANAGER_ENABLED
102     int32_t RegisterSystemKey(std::shared_ptr<KeyOption> option, int32_t session,
103         std::function<void(std::shared_ptr<KeyEvent>)> callback);
104     int32_t RegisterHotKey(std::shared_ptr<KeyOption> option, int32_t session,
105         std::function<void(std::shared_ptr<KeyEvent>)> callback);
106     void UnregisterSystemKey(int32_t shortcutId);
107     void UnregisterHotKey(int32_t shortcutId);
108     void DeleteShortcutId(std::shared_ptr<Subscriber> subscriber);
109 #endif // SHORTCUT_KEY_MANAGER_ENABLED
110     int32_t AddSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option, bool isSystem);
111     int32_t RemoveSubscriber(SessionPtr sess, int32_t subscribeId, bool isSystem);
112     bool IsMatchForegroundPid(std::list<std::shared_ptr<Subscriber>> subs, std::set<int32_t> foregroundPids);
113     int32_t GetHighestPrioritySubscriber(const std::list<std::shared_ptr<Subscriber>> &subscribers);
114     void NotifyKeyDownSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, std::shared_ptr<KeyOption> keyOption,
115         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
116     void NotifyKeyDownRightNow(const std::shared_ptr<KeyEvent> &keyEvent,
117         std::list<std::shared_ptr<Subscriber>> &subscribers, bool isRepeat, bool &handled);
118     void NotifyKeyDownDelay(const std::shared_ptr<KeyEvent> &keyEvent,
119         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
120     void NotifyKeyUpSubscriber(const std::shared_ptr<KeyEvent> &keyEvent,
121         std::list<std::shared_ptr<Subscriber>> subscribers, bool &handled);
122     void PrintKeyOption(const std::shared_ptr<KeyOption> keyOption);
123     void ClearSubscriberTimer(std::list<std::shared_ptr<Subscriber>> subscribers);
124     void GetForegroundPids(std::set<int32_t> &pidList);
125     void PublishKeyPressCommonEvent(std::shared_ptr<KeyEvent> keyEvent);
126     void RemoveSubscriberTimer(std::shared_ptr<KeyEvent> keyEvent);
127 #ifdef OHOS_BUILD_ENABLE_CALL_MANAGER
128     bool HandleCallEnded(std::shared_ptr<KeyEvent> keyEvent);
129     void HangUpCallProcess();
130     void RejectCallProcess();
131 #endif // OHOS_BUILD_ENABLE_CALL_MANAGER
132 
133 private:
134     SubscriberCollection subscriberMap_;
135     std::mutex subscriberMapMutex_;
136     SubscriberCollection keyGestures_;
137     KeyGestureManager keyGestureMgr_;
138     bool callbackInitialized_ { false };
139     bool hasEventExecuting_ { false };
140     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
141     int32_t subscribePowerKeyId_ { -1 };
142     bool subscribePowerKeyState_ { false };
143     bool enableCombineKey_ { true };
144     std::set<int32_t> foregroundPids_ {};
145     bool isForegroundExits_ { false };
146     std::atomic_bool needSkipPowerKeyUp_ { false };
147     bool callBahaviorState_ { false };
148     std::set<int32_t> pendingKeys_;
149     std::atomic_bool callEndKeyUp_ { false };
150 };
151 } // namespace MMI
152 } // namespace OHOS
153 #endif // KEY_SUBSCRIBER_HANDLER_H
154