1 /*
2  * Copyright (c) 2021-2023 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 
34 namespace OHOS {
35 namespace MMI {
36 class KeySubscriberHandler final : public IInputEventHandler {
37 public:
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 #endif // OHOS_BUILD_ENABLE_KEYBOARD
45 #ifdef OHOS_BUILD_ENABLE_POINTER
46     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
47 #endif // OHOS_BUILD_ENABLE_POINTER
48 #ifdef OHOS_BUILD_ENABLE_TOUCH
49     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
50 #endif // OHOS_BUILD_ENABLE_TOUCH
51     int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId,
52             const std::shared_ptr<KeyOption> keyOption);
53     int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId);
54     void RemoveSubscriberKeyUpTimer(int32_t keyCode);
55     int32_t EnableCombineKey(bool enable);
56     void Dump(int32_t fd, const std::vector<std::string> &args);
57 
58 private:
59     struct Subscriber {
SubscriberSubscriber60         Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption)
61             : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {}
62         int32_t id_ { -1 };
63         SessionPtr sess_ { nullptr };
64         std::shared_ptr<KeyOption> keyOption_ { nullptr };
65         int32_t timerId_ { -1 };
66         std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
67     };
68     using SubscriberCollection = std::map<std::shared_ptr<KeyOption>, std::list<std::shared_ptr<Subscriber>>>;
69 
70     size_t CountSubscribers() const;
71     void DumpSubscribers(int32_t fd, const SubscriberCollection &collection) const;
72     void DumpSubscriber(int32_t fd, std::shared_ptr<Subscriber> subscriber) const;
73     void InsertSubScriber(std::shared_ptr<Subscriber> subs);
74     bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
75     bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent);
76     bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent);
77     bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent);
78     bool HandleRingMute(std::shared_ptr<KeyEvent> keyEvent);
79     bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const;
80     void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent,
81         const std::shared_ptr<Subscriber> &subscriber);
82     bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent);
83     void ClearTimer(const std::shared_ptr<Subscriber> &subscriber);
84     void OnTimer(const std::shared_ptr<Subscriber> subscriber);
85     void OnSessionDelete(SessionPtr sess);
86     bool InitSessionDeleteCallback();
87     bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
88     void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes);
89     bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
90     bool IsFunctionKey(const std::shared_ptr<KeyEvent> keyEvent);
91     bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key);
92     bool IsEnableCombineKeySwipe(const std::shared_ptr<KeyEvent> key);
93     void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber);
94     void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber);
95     void SubscriberNotifyNap(const std::shared_ptr<Subscriber> subscriber);
96     bool IsEqualKeyOption(std::shared_ptr<KeyOption> newOption, std::shared_ptr<KeyOption> oldOption);
97     bool IsEqualPreKeys(const std::set<int32_t> &preKeys, const std::set<int32_t> &pressedKeys);
98     void AddKeyGestureSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option);
99     int32_t RemoveKeyGestureSubscriber(SessionPtr sess, int32_t subscribeId);
100     void AddSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option);
101     int32_t RemoveSubscriber(SessionPtr sess, int32_t subscribeId);
102     bool IsMatchForegroundPid(std::list<std::shared_ptr<Subscriber>> subs, std::set<int32_t> foregroundPids);
103     void NotifyKeyDownSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, std::shared_ptr<KeyOption> keyOption,
104         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
105     void NotifyKeyDownRightNow(const std::shared_ptr<KeyEvent> &keyEvent,
106         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
107     void NotifyKeyDownDelay(const std::shared_ptr<KeyEvent> &keyEvent,
108         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
109     void NotifyKeyUpSubscriber(const std::shared_ptr<KeyEvent> &keyEvent,
110         std::list<std::shared_ptr<Subscriber>> subscribers, bool &handled);
111     void PrintKeyOption(const std::shared_ptr<KeyOption> keyOption);
112     void ClearSubscriberTimer(std::list<std::shared_ptr<Subscriber>> subscribers);
113     void GetForegroundPids(std::set<int32_t> &pidList);
114     void PublishKeyPressCommonEvent(std::shared_ptr<KeyEvent> keyEvent);
115     void RemoveSubscriberTimer(std::shared_ptr<KeyEvent> keyEvent);
116 
117 private:
118     SubscriberCollection subscriberMap_;
119     SubscriberCollection keyGestures_;
120     KeyGestureManager keyGestureMgr_;
121     bool callbackInitialized_ { false };
122     bool hasEventExecuting_ { false };
123     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
124     int32_t subscribePowerKeyId_ { -1 };
125     bool subscribePowerKeyState_ { false };
126     bool enableCombineKey_ { true };
127     std::set<int32_t> foregroundPids_ {};
128     bool isForegroundExits_ { false };
129     bool needSkipPowerKeyUp_ { false };
130 };
131 } // namespace MMI
132 } // namespace OHOS
133 #endif  // KEY_SUBSCRIBER_HANDLER_H
134