1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef KEY_SUBSCRIBER_HANDLER_H
17c29fa5a6Sopenharmony_ci#define KEY_SUBSCRIBER_HANDLER_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <algorithm>
20c29fa5a6Sopenharmony_ci#include <list>
21c29fa5a6Sopenharmony_ci#include <map>
22c29fa5a6Sopenharmony_ci#include <memory>
23c29fa5a6Sopenharmony_ci#include <mutex>
24c29fa5a6Sopenharmony_ci#include <set>
25c29fa5a6Sopenharmony_ci#include <thread>
26c29fa5a6Sopenharmony_ci
27c29fa5a6Sopenharmony_ci#include "i_input_event_handler.h"
28c29fa5a6Sopenharmony_ci#include "key_event.h"
29c29fa5a6Sopenharmony_ci#include "key_gesture_manager.h"
30c29fa5a6Sopenharmony_ci#include "key_option.h"
31c29fa5a6Sopenharmony_ci#include "uds_server.h"
32c29fa5a6Sopenharmony_ci#include "nap_process.h"
33c29fa5a6Sopenharmony_ci
34c29fa5a6Sopenharmony_cinamespace OHOS {
35c29fa5a6Sopenharmony_cinamespace MMI {
36c29fa5a6Sopenharmony_ciclass KeySubscriberHandler final : public IInputEventHandler {
37c29fa5a6Sopenharmony_cipublic:
38c29fa5a6Sopenharmony_ci    KeySubscriberHandler() = default;
39c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(KeySubscriberHandler);
40c29fa5a6Sopenharmony_ci    ~KeySubscriberHandler() = default;
41c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_KEYBOARD
42c29fa5a6Sopenharmony_ci    void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
43c29fa5a6Sopenharmony_ci    bool IsKeyEventSubscribed(int32_t keyCode, int32_t trrigerType);
44c29fa5a6Sopenharmony_ci    void InitDataShareListener();
45c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_KEYBOARD
46c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POINTER
47c29fa5a6Sopenharmony_ci    void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
48c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER
49c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_TOUCH
50c29fa5a6Sopenharmony_ci    void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
51c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_TOUCH
52c29fa5a6Sopenharmony_ci    int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId, const std::shared_ptr<KeyOption> keyOption);
53c29fa5a6Sopenharmony_ci    int32_t UnsubscribeKeyEvent(SessionPtr sess, int32_t subscribeId);
54c29fa5a6Sopenharmony_ci    int32_t SubscribeHotkey(SessionPtr sess, int32_t subscribeId, std::shared_ptr<KeyOption> keyOption);
55c29fa5a6Sopenharmony_ci    int32_t UnsubscribeHotkey(SessionPtr sess, int32_t subscribeId);
56c29fa5a6Sopenharmony_ci    void RemoveSubscriberKeyUpTimer(int32_t keyCode);
57c29fa5a6Sopenharmony_ci    int32_t EnableCombineKey(bool enable);
58c29fa5a6Sopenharmony_ci    void Dump(int32_t fd, const std::vector<std::string> &args);
59c29fa5a6Sopenharmony_ci
60c29fa5a6Sopenharmony_ciprivate:
61c29fa5a6Sopenharmony_ci    struct Subscriber {
62c29fa5a6Sopenharmony_ci        Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption)
63c29fa5a6Sopenharmony_ci            : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1) {}
64c29fa5a6Sopenharmony_ci        int32_t id_ { -1 };
65c29fa5a6Sopenharmony_ci        SessionPtr sess_ { nullptr };
66c29fa5a6Sopenharmony_ci        std::shared_ptr<KeyOption> keyOption_ { nullptr };
67c29fa5a6Sopenharmony_ci        int32_t timerId_ { -1 };
68c29fa5a6Sopenharmony_ci#ifdef SHORTCUT_KEY_MANAGER_ENABLED
69c29fa5a6Sopenharmony_ci        int32_t shortcutId_ { -1 };
70c29fa5a6Sopenharmony_ci        bool isSystem { true };
71c29fa5a6Sopenharmony_ci#endif // SHORTCUT_KEY_MANAGER_ENABLED
72c29fa5a6Sopenharmony_ci        std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
73c29fa5a6Sopenharmony_ci    };
74c29fa5a6Sopenharmony_ci    using SubscriberCollection = std::map<std::shared_ptr<KeyOption>, std::list<std::shared_ptr<Subscriber>>>;
75c29fa5a6Sopenharmony_ci
76c29fa5a6Sopenharmony_ci    size_t CountSubscribers() const;
77c29fa5a6Sopenharmony_ci    void DumpSubscribers(int32_t fd, const SubscriberCollection &collection) const;
78c29fa5a6Sopenharmony_ci    void DumpSubscriber(int32_t fd, std::shared_ptr<Subscriber> subscriber) const;
79c29fa5a6Sopenharmony_ci    void InsertSubScriber(std::shared_ptr<Subscriber> subs);
80c29fa5a6Sopenharmony_ci    bool OnSubscribeKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
81c29fa5a6Sopenharmony_ci    bool HandleKeyDown(const std::shared_ptr<KeyEvent> &keyEvent);
82c29fa5a6Sopenharmony_ci    bool HandleKeyUp(const std::shared_ptr<KeyEvent> &keyEvent);
83c29fa5a6Sopenharmony_ci    bool HandleKeyCancel(const std::shared_ptr<KeyEvent> &keyEvent);
84c29fa5a6Sopenharmony_ci    bool HandleRingMute(std::shared_ptr<KeyEvent> keyEvent);
85c29fa5a6Sopenharmony_ci    bool IsPreKeysMatch(const std::set<int32_t> &preKeys, const std::vector<int32_t> &pressedKeys) const;
86c29fa5a6Sopenharmony_ci    void NotifySubscriber(std::shared_ptr<KeyEvent> keyEvent,
87c29fa5a6Sopenharmony_ci        const std::shared_ptr<Subscriber> &subscriber);
88c29fa5a6Sopenharmony_ci    bool AddTimer(const std::shared_ptr<Subscriber> &subscriber, const std::shared_ptr<KeyEvent> &keyEvent);
89c29fa5a6Sopenharmony_ci    void ClearTimer(const std::shared_ptr<Subscriber> &subscriber);
90c29fa5a6Sopenharmony_ci    void OnTimer(const std::shared_ptr<Subscriber> subscriber);
91c29fa5a6Sopenharmony_ci    void OnSessionDelete(SessionPtr sess);
92c29fa5a6Sopenharmony_ci    bool InitSessionDeleteCallback();
93c29fa5a6Sopenharmony_ci    bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
94c29fa5a6Sopenharmony_ci    void RemoveKeyCode(int32_t keyCode, std::vector<int32_t> &keyCodes);
95c29fa5a6Sopenharmony_ci    bool IsRepeatedKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
96c29fa5a6Sopenharmony_ci    bool IsFunctionKey(const std::shared_ptr<KeyEvent> keyEvent);
97c29fa5a6Sopenharmony_ci    bool IsEnableCombineKey(const std::shared_ptr<KeyEvent> key);
98c29fa5a6Sopenharmony_ci    bool IsEnableCombineKeySwipe(const std::shared_ptr<KeyEvent> key);
99c29fa5a6Sopenharmony_ci    void HandleKeyUpWithDelay(std::shared_ptr<KeyEvent> keyEvent, const std::shared_ptr<Subscriber> &subscriber);
100c29fa5a6Sopenharmony_ci    void PrintKeyUpLog(const std::shared_ptr<Subscriber> &subscriber);
101c29fa5a6Sopenharmony_ci    void SubscriberNotifyNap(const std::shared_ptr<Subscriber> subscriber);
102c29fa5a6Sopenharmony_ci    bool IsEqualKeyOption(std::shared_ptr<KeyOption> newOption, std::shared_ptr<KeyOption> oldOption);
103c29fa5a6Sopenharmony_ci    bool IsEqualPreKeys(const std::set<int32_t> &preKeys, const std::set<int32_t> &pressedKeys);
104c29fa5a6Sopenharmony_ci    int32_t AddKeyGestureSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option);
105c29fa5a6Sopenharmony_ci    int32_t RemoveKeyGestureSubscriber(SessionPtr sess, int32_t subscribeId);
106c29fa5a6Sopenharmony_ci#ifdef SHORTCUT_KEY_MANAGER_ENABLED
107c29fa5a6Sopenharmony_ci    int32_t RegisterSystemKey(std::shared_ptr<KeyOption> option, int32_t session,
108c29fa5a6Sopenharmony_ci        std::function<void(std::shared_ptr<KeyEvent>)> callback);
109c29fa5a6Sopenharmony_ci    int32_t RegisterHotKey(std::shared_ptr<KeyOption> option, int32_t session,
110c29fa5a6Sopenharmony_ci        std::function<void(std::shared_ptr<KeyEvent>)> callback);
111c29fa5a6Sopenharmony_ci    void UnregisterSystemKey(int32_t shortcutId);
112c29fa5a6Sopenharmony_ci    void UnregisterHotKey(int32_t shortcutId);
113c29fa5a6Sopenharmony_ci    void DeleteShortcutId(std::shared_ptr<Subscriber> subscriber);
114c29fa5a6Sopenharmony_ci#endif // SHORTCUT_KEY_MANAGER_ENABLED
115c29fa5a6Sopenharmony_ci    int32_t AddSubscriber(std::shared_ptr<Subscriber> subscriber, std::shared_ptr<KeyOption> option, bool isSystem);
116c29fa5a6Sopenharmony_ci    int32_t RemoveSubscriber(SessionPtr sess, int32_t subscribeId, bool isSystem);
117c29fa5a6Sopenharmony_ci    bool IsMatchForegroundPid(std::list<std::shared_ptr<Subscriber>> subs, std::set<int32_t> foregroundPids);
118c29fa5a6Sopenharmony_ci    void NotifyKeyDownSubscriber(const std::shared_ptr<KeyEvent> &keyEvent, std::shared_ptr<KeyOption> keyOption,
119c29fa5a6Sopenharmony_ci        std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
120c29fa5a6Sopenharmony_ci    void NotifyKeyDownRightNow(const std::shared_ptr<KeyEvent> &keyEvent,
121c29fa5a6Sopenharmony_ci        std::list<std::shared_ptr<Subscriber>> &subscribers, bool isRepeat, bool &handled);
122c29fa5a6Sopenharmony_ci    void NotifyKeyDownDelay(const std::shared_ptr<KeyEvent> &keyEvent,
123c29fa5a6Sopenharmony_ci        std::list<std::shared_ptr<Subscriber>> &subscribers, bool &handled);
124c29fa5a6Sopenharmony_ci    void NotifyKeyUpSubscriber(const std::shared_ptr<KeyEvent> &keyEvent,
125c29fa5a6Sopenharmony_ci        std::list<std::shared_ptr<Subscriber>> subscribers, bool &handled);
126c29fa5a6Sopenharmony_ci    void PrintKeyOption(const std::shared_ptr<KeyOption> keyOption);
127c29fa5a6Sopenharmony_ci    void ClearSubscriberTimer(std::list<std::shared_ptr<Subscriber>> subscribers);
128c29fa5a6Sopenharmony_ci    void GetForegroundPids(std::set<int32_t> &pidList);
129c29fa5a6Sopenharmony_ci    void PublishKeyPressCommonEvent(std::shared_ptr<KeyEvent> keyEvent);
130c29fa5a6Sopenharmony_ci    bool HandleCallEnded(std::shared_ptr<KeyEvent> keyEvent);
131c29fa5a6Sopenharmony_ci    void HangUpCallProcess();
132c29fa5a6Sopenharmony_ci    void RejectCallProcess();
133c29fa5a6Sopenharmony_ci    void RemoveSubscriberTimer(std::shared_ptr<KeyEvent> keyEvent);
134c29fa5a6Sopenharmony_ci
135c29fa5a6Sopenharmony_ciprivate:
136c29fa5a6Sopenharmony_ci    SubscriberCollection subscriberMap_;
137c29fa5a6Sopenharmony_ci    SubscriberCollection keyGestures_;
138c29fa5a6Sopenharmony_ci    KeyGestureManager keyGestureMgr_;
139c29fa5a6Sopenharmony_ci    bool callbackInitialized_ { false };
140c29fa5a6Sopenharmony_ci    bool hasEventExecuting_ { false };
141c29fa5a6Sopenharmony_ci    std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
142c29fa5a6Sopenharmony_ci    int32_t subscribePowerKeyId_ { -1 };
143c29fa5a6Sopenharmony_ci    bool subscribePowerKeyState_ { false };
144c29fa5a6Sopenharmony_ci    bool enableCombineKey_ { true };
145c29fa5a6Sopenharmony_ci    std::set<int32_t> foregroundPids_ {};
146c29fa5a6Sopenharmony_ci    bool isForegroundExits_ { false };
147c29fa5a6Sopenharmony_ci    bool needSkipPowerKeyUp_ { false };
148c29fa5a6Sopenharmony_ci    bool callBahaviorState_ { false };
149c29fa5a6Sopenharmony_ci};
150c29fa5a6Sopenharmony_ci} // namespace MMI
151c29fa5a6Sopenharmony_ci} // namespace OHOS
152c29fa5a6Sopenharmony_ci#endif // KEY_SUBSCRIBER_HANDLER_H
153