• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "tablet_event_input_subscribe_manager.h"
17 #include <cinttypes>
18 #include "bytrace_adapter.h"
19 #include "define_multimodal.h"
20 #include "error_multimodal.h"
21 #include "multimodal_event_handler.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "TabletEventInputSubscribeManager"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr int32_t INVALID_SUBSCRIBE_ID { -1 };
30 } // namespace
31 int32_t TabletEventInputSubscribeManager::subscribeManagerId_ = 0;
32 
TabletEventInputSubscribeManager()33 TabletEventInputSubscribeManager::TabletEventInputSubscribeManager() {}
~TabletEventInputSubscribeManager()34 TabletEventInputSubscribeManager::~TabletEventInputSubscribeManager() {}
35 
SubscribeTabletProximity(std::function<void (std::shared_ptr<PointerEvent>)> callback)36 int32_t TabletEventInputSubscribeManager::SubscribeTabletProximity(
37     std::function<void(std::shared_ptr<PointerEvent>)> callback)
38 {
39     CALL_INFO_TRACE;
40     CHKPR(callback, ERROR_NULL_POINTER);
41     if (!MMIEventHdl.InitClient()) {
42         MMI_HILOGE("Client init failed");
43         return EVENT_REG_FAIL;
44     }
45     std::lock_guard<std::mutex> guard(mtx_);
46     if (TabletEventInputSubscribeManager::subscribeManagerId_ >= INT_MAX) {
47         MMI_HILOGE("The subscribeId has reached the upper limit, cannot continue the subscription");
48         return INVALID_SUBSCRIBE_ID;
49     }
50     int32_t subscribeId = TabletEventInputSubscribeManager::subscribeManagerId_;
51     ++TabletEventInputSubscribeManager::subscribeManagerId_;
52     subscribeInfos_.emplace(std::make_pair(subscribeId, SubscribeTabletEventInfo(callback)));
53     int32_t ret = MMIEventHdl.SubscribeTabletProximity(subscribeId);
54     if (ret != RET_OK) {
55         MMI_HILOGE("Subscribing Tablet event failed, ret:%{public}d", ret);
56         subscribeInfos_.erase(subscribeId);
57         return INVALID_SUBSCRIBE_ID;
58     }
59     MMI_HILOGI("The subscribeId:%{public}d", subscribeId);
60     return subscribeId;
61 }
62 
UnsubscribetabletProximity(int32_t subscribeId)63 int32_t TabletEventInputSubscribeManager::UnsubscribetabletProximity(int32_t subscribeId)
64 {
65     CALL_INFO_TRACE;
66     if (subscribeId < 0) {
67         MMI_HILOGE("The subscribe id is less than 0");
68         return RET_ERR;
69     }
70     if (!MMIEventHdl.InitClient()) {
71         MMI_HILOGE("Client init failed");
72         return INVALID_SUBSCRIBE_ID;
73     }
74     std::lock_guard<std::mutex> guard(mtx_);
75     if (subscribeInfos_.empty()) {
76         MMI_HILOGE("The subscribeInfos is empty");
77         return RET_ERR;
78     }
79     auto it = subscribeInfos_.find(subscribeId);
80     if (it != subscribeInfos_.end()) {
81         if (MMIEventHdl.UnsubscribetabletProximity(subscribeId) != RET_OK) {
82             MMI_HILOGE("Leave, unsubscribe switch event failed");
83             return RET_ERR;
84         }
85         subscribeInfos_.erase(it);
86         return RET_OK;
87     }
88     return RET_ERR;
89 }
90 
OnSubscribeTabletProximityCallback(std::shared_ptr<PointerEvent> event,int32_t subscribeId)91 int32_t TabletEventInputSubscribeManager::OnSubscribeTabletProximityCallback(std::shared_ptr<PointerEvent> event,
92     int32_t subscribeId)
93 {
94     CHK_PID_AND_TID();
95     CHKPR(event, ERROR_NULL_POINTER);
96     if (subscribeId < 0) {
97         MMI_HILOGE("Leave, the subscribe id is less than 0");
98         return RET_ERR;
99     }
100     std::function<void(std::shared_ptr<PointerEvent>)> callback = nullptr;
101     std::lock_guard<std::mutex> guard(mtx_);
102     auto it = subscribeInfos_.find(subscribeId);
103     if (it != subscribeInfos_.end()) {
104         SubscribeTabletEventInfo &subscribeInfo = it->second;
105         callback = subscribeInfo.GetCallback();
106     }
107     CHKPR(callback, ERROR_NULL_POINTER);
108     callback(event);
109     MMI_HILOGI("Tablet event id:%{public}d", subscribeId);
110     return RET_OK;
111 }
112 
OnConnected()113 void TabletEventInputSubscribeManager::OnConnected()
114 {
115     CALL_DEBUG_ENTER;
116     std::lock_guard<std::mutex> guard(mtx_);
117     if (subscribeInfos_.empty()) {
118         MMI_HILOGD("Leave, subscribeInfos_ is empty");
119         return;
120     }
121     for (auto it = subscribeInfos_.begin(); it != subscribeInfos_.end(); ++it) {
122         SubscribeTabletEventInfo &subscribeInfo = it->second;
123         int32_t ret = MMIEventHdl.SubscribeTabletProximity(it->first);
124         if (ret != RET_OK) {
125             MMI_HILOGE("Subscribe switch event failed, ret:%{public}d", ret);
126         }
127     }
128 }
129 } // namespace MMI
130 } // namespace OHOS