• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 PRE_MONITOR_MANAGER_H
17 #define PRE_MONITOR_MANAGER_H
18 
19 #include <limits>
20 #include <map>
21 #include <mutex>
22 #include <iterator>
23 #include "singleton.h"
24 
25 #include "input_handler_type.h"
26 #include "i_input_event_consumer.h"
27 
28 namespace OHOS {
29 namespace MMI {
30 class PreMonitorManager final {
31     DECLARE_SINGLETON(PreMonitorManager);
32 
33 public:
34     DISALLOW_MOVE(PreMonitorManager);
35 
36 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
37     void OnPreKeyEvent(std::shared_ptr<KeyEvent> keyEvent, int32_t handlerId);
38 #endif // OHOS_BUILD_ENABLE_KEYBOARD
39 #if defined(OHOS_BUILD_ENABLE_MONITOR)
40     void OnConnected();
41 #endif // OHOS_BUILD_ENABLE_MONITOR
42     HandleEventType GetEventType() const;
43     int32_t AddHandler(
44         std::shared_ptr<IInputEventConsumer> consumer, HandleEventType eventType, std::vector<int32_t> keys);
45     int32_t RemoveHandler(int32_t handlerId);
46 
47 private:
48     struct Handler {
49         int32_t handlerId_ { 0 };
50         HandleEventType eventType_ { HANDLE_EVENT_TYPE_PRE_KEY };
51         std::shared_ptr<IInputEventConsumer> callback_ { nullptr };
52         std::vector<int32_t> keys_;
53     };
54 
55 private:
56     int32_t GetNextId();
57     int32_t AddLocal(int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys,
58         std::shared_ptr<IInputEventConsumer> consumer);
59     int32_t AddToServer(int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys);
60     int32_t RemoveLocal(int32_t handlerId);
61     int32_t RemoveFromServer(int32_t handlerId);
62     std::shared_ptr<IInputEventConsumer> FindHandler(int32_t handlerId);
63 
64 private:
65     std::map<int32_t, Handler> monitorHandlers_;
66     int32_t nextId_ { 1 };
67     std::mutex mtxHandlers_;
68 };
69 #define PRE_MONITOR_MGR ::OHOS::Singleton<PreMonitorManager>::GetInstance()
70 } // namespace MMI
71 } // namespace OHOS
72 #endif // PRE_MONITOR_MANAGER_H
73