1 /*
2 * Copyright (c) 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 #include "mmi_adapter_impl.h"
17
18 #include "nweb_log.h"
19
20 namespace OHOS::NWeb {
21 using namespace MMI;
22
23 const int32_t KEY_DOWN = 0;
24 const int32_t KEY_UP = 1;
25
MMIListenerAdapterImpl(std::shared_ptr<MMIListenerAdapter> listener)26 MMIListenerAdapterImpl::MMIListenerAdapterImpl(std::shared_ptr<MMIListenerAdapter> listener) : listener_(listener) {};
27
~MMIListenerAdapterImpl()28 MMIListenerAdapterImpl::~MMIListenerAdapterImpl()
29 {
30 listener_ = nullptr;
31 };
32
OnDeviceAdded(int32_t deviceId,const std::string & type)33 void MMIListenerAdapterImpl::OnDeviceAdded(int32_t deviceId, const std::string& type)
34 {
35 if (listener_) {
36 listener_->OnDeviceAdded(deviceId, type);
37 }
38 };
39
OnDeviceRemoved(int32_t deviceId,const std::string & type)40 void MMIListenerAdapterImpl::OnDeviceRemoved(int32_t deviceId, const std::string& type)
41 {
42 if (listener_) {
43 listener_->OnDeviceRemoved(deviceId, type);
44 }
45 };
46
MMIInputListenerAdapterImpl(std::shared_ptr<MMIInputListenerAdapter> listener)47 MMIInputListenerAdapterImpl::MMIInputListenerAdapterImpl(std::shared_ptr<MMIInputListenerAdapter> listener)
48 : listener_(listener) {};
49
~MMIInputListenerAdapterImpl()50 MMIInputListenerAdapterImpl::~MMIInputListenerAdapterImpl()
51 {
52 listener_ = nullptr;
53 };
54
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const55 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
56 {
57 if (!listener_) {
58 return;
59 }
60 if (keyEvent->GetKeyAction() != MMI::KeyEvent::KEY_ACTION_DOWN &&
61 keyEvent->GetKeyAction() != MMI::KeyEvent::KEY_ACTION_UP) {
62 return;
63 }
64 int32_t keyAction = (keyEvent->GetKeyAction() == MMI::KeyEvent::KEY_ACTION_DOWN) ? KEY_DOWN : KEY_UP;
65 listener_->OnInputEvent(keyEvent->GetKeyCode(), keyAction);
66 };
67
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const68 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const {};
69
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const70 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const {};
71
KeyCodeToString(int32_t keyCode)72 char* MMIAdapterImpl::KeyCodeToString(int32_t keyCode)
73 {
74 return const_cast<char*>(MMI::KeyEvent::KeyCodeToString(keyCode));
75 }
76
RegisterMMIInputListener(std::shared_ptr<MMIInputListenerAdapter> eventCallback)77 int32_t MMIAdapterImpl::RegisterMMIInputListener(std::shared_ptr<MMIInputListenerAdapter> eventCallback)
78 {
79 if (!eventCallback) {
80 WVLOG_E("register input listener is nullptr");
81 return -1;
82 }
83 inputListener_ = std::make_shared<MMIInputListenerAdapterImpl>(eventCallback);
84 int32_t id = InputManager::GetInstance()->AddMonitor(inputListener_);
85 WVLOG_D("RegisterMMIInputListener id = %{public}d", id);
86 return id;
87 };
88
UnregisterMMIInputListener(int32_t monitorId)89 void MMIAdapterImpl::UnregisterMMIInputListener(int32_t monitorId)
90 {
91 InputManager::GetInstance()->RemoveMonitor(monitorId);
92 };
93
RegisterDevListener(std::string type,std::shared_ptr<MMIListenerAdapter> listener)94 int32_t MMIAdapterImpl::RegisterDevListener(std::string type, std::shared_ptr<MMIListenerAdapter> listener)
95 {
96 if (!listener) {
97 WVLOG_E("register device listener is nullptr");
98 return -1;
99 }
100 devListener_ = std::make_shared<MMIListenerAdapterImpl>(listener);
101 return InputManager::GetInstance()->RegisterDevListener(type, devListener_);
102 };
103
UnregisterDevListener(std::string type)104 int32_t MMIAdapterImpl::UnregisterDevListener(std::string type)
105 {
106 return InputManager::GetInstance()->UnregisterDevListener(type, devListener_);
107 };
108
GetKeyboardType(int32_t deviceId,int32_t & type)109 int32_t MMIAdapterImpl::GetKeyboardType(int32_t deviceId, int32_t& type)
110 {
111 std::function<void(int32_t)> callback = [&type](int32_t param) { type = param; };
112 return InputManager::GetInstance()->GetKeyboardType(deviceId, callback);
113 };
114
GetDeviceIds(std::vector<int32_t> & ids)115 int32_t MMIAdapterImpl::GetDeviceIds(std::vector<int32_t>& ids)
116 {
117 std::function<void(std::vector<int32_t>&)> callback = [&ids](std::vector<int32_t>& param) { ids = param; };
118 return InputManager::GetInstance()->GetDeviceIds(callback);
119 };
120
GetDeviceInfo(int32_t deviceId,std::shared_ptr<MMIDeviceInfoAdapter> info)121 int32_t MMIAdapterImpl::GetDeviceInfo(int32_t deviceId, std::shared_ptr<MMIDeviceInfoAdapter> info)
122 {
123 if (!info) {
124 WVLOG_E("GetDeviceInfo info is nullptr");
125 return -1;
126 }
127
128 std::function<void(std::shared_ptr<MMI::InputDevice>)> callback = [&info](
129 std::shared_ptr<MMI::InputDevice> device) {
130 if (device) {
131 info->SetId(device->GetId());
132 info->SetType(device->GetType());
133 info->SetBus(device->GetBus());
134 info->SetVersion(device->GetVersion());
135 info->SetProduct(device->GetProduct());
136 info->SetVendor(device->GetVendor());
137 info->SetName(device->GetName());
138 info->SetPhys(device->GetPhys());
139 info->SetUniq(device->GetUniq());
140 }
141 };
142
143 int32_t ret = InputManager::GetInstance()->GetDevice(
144 deviceId, [&callback](std::shared_ptr<MMI::InputDevice> device) { callback(device); });
145 if (ret != 0) {
146 WVLOG_E("InputManager GetDevice failed, ret: %{public}d", ret);
147 }
148 return ret;
149 }
150 } // namespace OHOS::NWeb
151