• 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 
16 #ifndef KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H
17 #define KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H
18 
19 #include <functional>
20 #include <list>
21 #include <memory>
22 #include "key_event.h"
23 #include "key_option.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 class KeyEventInputSubscribeManager : public Singleton<KeyEventInputSubscribeManager> {
29 public:
30     class SubscribeKeyEventInfo {
31     public:
32         explicit SubscribeKeyEventInfo(std::shared_ptr<OHOS::MMI::KeyOption> keyOption,
33             std::function<void(std::shared_ptr<OHOS::MMI::KeyEvent>)> callback);
34         ~SubscribeKeyEventInfo() = default;
35 
GetSubscribeId()36         int32_t GetSubscribeId() const
37         {
38             return subscribeId_;
39         }
40 
GetKeyOption()41         std::shared_ptr<OHOS::MMI::KeyOption> GetKeyOption() const
42         {
43             return keyOption_;
44         }
45 
GetCallback()46         std::function<void(std::shared_ptr<OHOS::MMI::KeyEvent>)> GetCallback() const
47         {
48             return callback_;
49         }
50 
51     private:
52         int32_t subscribeId_ { -1 };
53         std::shared_ptr<OHOS::MMI::KeyOption> keyOption_ { nullptr };
54         std::function<void(std::shared_ptr<OHOS::MMI::KeyEvent>)> callback_ { nullptr };
55     };
56 
57 public:
58     KeyEventInputSubscribeManager() = default;
59     ~KeyEventInputSubscribeManager() = default;
60 
61     int32_t SubscribeKeyEvent(std::shared_ptr<OHOS::MMI::KeyOption> keyOption,
62         std::function<void(std::shared_ptr<OHOS::MMI::KeyEvent>)> callback);
63     int32_t UnSubscribeKeyEvent(int32_t subscribeId);
64 
65     int32_t OnSubscribeKeyEventCallback(std::shared_ptr<OHOS::MMI::KeyEvent> event, int32_t subscribeId);
66     void OnConnected();
67 
68 private:
69     std::list<SubscribeKeyEventInfo> subscribeInfos_;
70     static int32_t subscribeIdManager_;
71 };
72 }  // namespace MMI
73 }  // namespace OHOS
74 
75 #define KeyEventInputSubscribeMgr OHOS::MMI::KeyEventInputSubscribeManager::GetInstance()
76 
77 #endif  // KEY_EVENT_INPUT_SUBSCRIBE_MANAGER_H