• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_option.h"
30 #include "uds_server.h"
31 #include "nap_process.h"
32 
33 namespace OHOS {
34 namespace MMI {
35 class KeySubscriberHandler final : public IInputEventHandler {
36 public:
37     KeySubscriberHandler() = default;
38     DISALLOW_COPY_AND_MOVE(KeySubscriberHandler);
39     ~KeySubscriberHandler() = default;
40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
41     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
42     bool IsKeyEventSubscribed(int32_t keyCode, int32_t trrigerType);
43 #endif // OHOS_BUILD_ENABLE_KEYBOARD
44 #ifdef OHOS_BUILD_ENABLE_POINTER
45     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
46 #endif // OHOS_BUILD_ENABLE_POINTER
47 #ifdef OHOS_BUILD_ENABLE_TOUCH
48     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
49 #endif // OHOS_BUILD_ENABLE_TOUCH
50     int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId,
51             const std::shared_ptr<KeyOption> keyOption);
52     int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId);
53     void RemoveSubscriberKeyUpTimer(int32_t keyCode);
54     int32_t EnableCombineKey(bool enable);
55     void Dump(int32_t fd, const std::vector<std::string> &args);
56 private:
57     struct Subscriber {
SubscriberSubscriber58         Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption)
59             : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {}
60         int32_t id_ { -1 };
61         SessionPtr sess_ { nullptr };
62         std::shared_ptr<KeyOption> keyOption_ { nullptr };
63         int32_t timerId_ { -1 };
64         std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
65     };
66     void InsertSubScriber(std::shared_ptr<Subscriber> subs);
67 
68 private:
69     bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
70     bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent);
71     bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent);
72     bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent);
73     bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const;
74     void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent,
75         const std::shared_ptr<Subscriber> &subscriber);
76     bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent);
77     void ClearTimer(const std::shared_ptr<Subscriber> &subscriber);
78     void OnTimer(const std::shared_ptr<Subscriber> subscriber);
79     void OnSessionDelete(SessionPtr sess);
80     bool InitSessionDeleteCallback();
81     bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
82     void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes);
83     bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
84     bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key);
85     bool IsNotifyPowerKeySubsciber(int32_t keyCode, const std::vector<int32_t> &keyCodes);
86     void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber);
87     void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber);
88     void SubscriberNotifyNap(const std::shared_ptr<Subscriber> subscriber);
89     bool IsEqualKeyOption(std::shared_ptr<KeyOption> newOption, std::shared_ptr<KeyOption> oldOption);
90     bool IsEqualPreKeys(const std::set<int32_t> &preKeys, const std::set<int32_t> &pressedKeys);
91     void AddSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option);
92     int32_t RemoveSubscriber(SessionPtr sess, int32_t subscribeId);
93     bool IsMatchForegroundPid(std::list<std::shared_ptr<Subscriber>> subs, std::set<int32_t> foregroundPids);
94     void NotifyKeyDownSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, std::shared_ptr<KeyOption> keyOption,
95         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
96     void NotifyKeyDownRightNow(const std::shared_ptr<KeyEvent> &keyEvent,
97         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
98     void NotifyKeyDownDelay(const std::shared_ptr<KeyEvent> &keyEvent,
99         std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
100     void NotifyKeyUpSubscriber(const std::shared_ptr<KeyEvent> &keyEvent,
101         std::list<std::shared_ptr<Subscriber>> subscribers, bool &handled);
102     void PrintKeyOption(const std::shared_ptr<KeyOption> keyOption);
103     void ClearSubscriberTimer(std::list<std::shared_ptr<Subscriber>> subscribers);
104     void GetForegroundPids(std::set<int32_t> &pidList);
105 
106 private:
107     std::map<std::shared_ptr<KeyOption>, std::list<std::shared_ptr<Subscriber>>> subscriberMap_;
108     bool callbackInitialized_ { false };
109     bool hasEventExecuting_ { false };
110     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
111     int32_t subscribePowerKeyId_ { -1 };
112     bool subscribePowerKeyState_ { false };
113     bool enableCombineKey_ { true };
114     std::set<int32_t> foregroundPids_ {};
115     bool isForegroundExits_ { false };
116 };
117 } // namespace MMI
118 } // namespace OHOS
119 #endif  // KEY_SUBSCRIBER_HANDLER_H
120