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(const InputEventCallback & listener)47 MMIInputListenerAdapterImpl::MMIInputListenerAdapterImpl(const InputEventCallback& listener) : listener_(listener) {};
48
~MMIInputListenerAdapterImpl()49 MMIInputListenerAdapterImpl::~MMIInputListenerAdapterImpl()
50 {
51 listener_ = nullptr;
52 };
53
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const54 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
55 {
56 if (!listener_) {
57 return;
58 }
59 if (keyEvent->GetKeyAction() != MMI::KeyEvent::KEY_ACTION_DOWN &&
60 keyEvent->GetKeyAction() != MMI::KeyEvent::KEY_ACTION_UP) {
61 return;
62 }
63 int32_t keyAction = (keyEvent->GetKeyAction() == MMI::KeyEvent::KEY_ACTION_DOWN) ? KEY_DOWN : KEY_UP;
64 listener_(keyEvent->GetKeyCode(), keyAction);
65 };
66
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const67 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
68 {
69 };
70
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const71 void MMIInputListenerAdapterImpl::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
72 {
73 };
74
KeyCodeToString(int32_t keyCode)75 const char* MMIAdapterImpl::KeyCodeToString(int32_t keyCode)
76 {
77 return MMI::KeyEvent::KeyCodeToString(keyCode);
78 }
79
RegisterMMIInputListener(const InputEventCallback && eventCallback)80 int32_t MMIAdapterImpl::RegisterMMIInputListener(const InputEventCallback&& eventCallback)
81 {
82 if (!eventCallback) {
83 WVLOG_E("register input listener is nullptr");
84 return -1;
85 }
86 inputListener_ = std::make_shared<MMIInputListenerAdapterImpl>(eventCallback);
87 int32_t id = InputManager::GetInstance()->AddMonitor(inputListener_);
88 WVLOG_D("RegisterMMIInputListener id = %{public}d", id);
89 return id;
90 };
91
UnregisterMMIInputListener(int32_t monitorId)92 void MMIAdapterImpl::UnregisterMMIInputListener(int32_t monitorId)
93 {
94 InputManager::GetInstance()->RemoveMonitor(monitorId);
95 };
96
RegisterDevListener(std::string type,std::shared_ptr<MMIListenerAdapter> listener)97 int32_t MMIAdapterImpl::RegisterDevListener(std::string type, std::shared_ptr<MMIListenerAdapter> listener)
98 {
99 if (!listener) {
100 WVLOG_E("register device listener is nullptr");
101 return -1;
102 }
103 devListener_ = std::make_shared<MMIListenerAdapterImpl>(listener);
104 return InputManager::GetInstance()->RegisterDevListener(type, devListener_);
105 };
106
UnregisterDevListener(std::string type)107 int32_t MMIAdapterImpl::UnregisterDevListener(std::string type)
108 {
109 return InputManager::GetInstance()->UnregisterDevListener(type, devListener_);
110 };
111
GetKeyboardType(int32_t deviceId,std::function<void (int32_t)> callback)112 int32_t MMIAdapterImpl::GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback)
113 {
114 return InputManager::GetInstance()->GetKeyboardType(deviceId, callback);
115 };
116
GetDeviceIds(std::function<void (std::vector<int32_t> &)> callback)117 int32_t MMIAdapterImpl::GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback)
118 {
119 return InputManager::GetInstance()->GetDeviceIds(callback);
120 };
121 } // namespace OHOS::NWeb