• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef KEY_EVENT_SUBSCRIBER_H
16 #define KEY_EVENT_SUBSCRIBER_H
17 
18 #include <algorithm>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <set>
24 #include <thread>
25 #include "key_event.h"
26 #include "key_option.h"
27 #include "singleton.h"
28 #include "uds_server.h"
29 
30 namespace OHOS {
31 namespace MMI {
32 class KeyEventSubscriber : public Singleton<KeyEventSubscriber> {
33 public:
34     KeyEventSubscriber() = default;
35     ~KeyEventSubscriber() = default;
36 
37     int32_t SubscribeKeyEvent(SessionPtr sess, int32_t subscribeId,
38             const std::shared_ptr<OHOS::MMI::KeyOption> keyOption);
39     int32_t UnSubscribeKeyEvent(SessionPtr sess, int32_t subscribeId);
40     bool SubscribeKeyEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent);
41 
42 private:
43     struct Subscriber {
SubscriberSubscriber44         Subscriber(int32_t id, SessionPtr sess, std::shared_ptr<KeyOption> keyOption)
45             : id_(id), sess_(sess), keyOption_(keyOption), timerId_(-1)
46         {
47         }
48         int32_t id_ { -1 };
49         SessionPtr sess_ { nullptr };
50         std::shared_ptr<OHOS::MMI::KeyOption> keyOption_ { nullptr };
51         int32_t timerId_ { -1 };
52         std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
53     };
54 
55 private:
56     bool HandleKeyDown(const std::shared_ptr<KeyEvent>& keyEvent);
57     bool HandleKeyUp(const std::shared_ptr<KeyEvent>& keyEvent);
58     bool HandleKeyCanel(const std::shared_ptr<KeyEvent>& keyEvent);
59 
60     bool IsPreKeysMatch(const std::set<int32_t>& preKeys, const std::vector<int32_t>& pressedKeys) const;
61 
62     void NotifySubscriber(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent,
63             const std::shared_ptr<Subscriber>& subscriber);
64 
65     bool AddTimer(const std::shared_ptr<Subscriber>& subscriber, const std::shared_ptr<KeyEvent>& keyEvent);
66     void ClearTimer(const std::shared_ptr<Subscriber>& subscriber);
67     void OnTimer(const std::shared_ptr<Subscriber> subscriber);
68     void OnSessionDelete(SessionPtr sess);
69     bool InitSessionDeleteCallback();
70 
71     bool CloneKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
72 
73     void RemoveKeyCode(int32_t keyCode, std::vector<int32_t>& keyCodes);
74 
75 private:
76     std::list<std::shared_ptr<Subscriber>> subscribers_ {};
77     bool callbackInitialized_ { false };
78     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
79 };
80 } // namespace MMI
81 } // namespace OHOS
82 #define KeyEventSubscriber_ OHOS::MMI::KeyEventSubscriber::GetInstance()
83 #endif  // KEY_EVENT_SUBSCRIBER_H
84