1 /*
2 * Copyright (c) 2021 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 "input_transfer_station.h"
17 #include <window_manager_hilog.h>
18
19 namespace OHOS {
20 namespace Rosen {
21 namespace {
22 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "InputTransferStation"};
23 }
WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)24 WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)
25
26 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
27 {
28 if (keyEvent == nullptr) {
29 WLOGFE("KeyEvent is nullptr");
30 return;
31 }
32 uint32_t windowId = static_cast<uint32_t>(keyEvent->GetAgentWindowId());
33 WLOGFI("Receive keyEvent, windowId: %{public}u", windowId);
34 auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
35 if (channel == nullptr) {
36 WLOGFE("WindowInputChannel is nullptr");
37 return;
38 }
39 channel->HandleKeyEvent(keyEvent);
40 }
41
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const42 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
43 {
44 if (axisEvent == nullptr) {
45 WLOGFE("AxisEvent is nullptr");
46 return;
47 }
48 WLOGFI("Receive axisEvent, windowId: %{public}d", axisEvent->GetAgentWindowId());
49 axisEvent->MarkProcessed();
50 }
51
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const52 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
53 {
54 if (pointerEvent == nullptr) {
55 WLOGFE("PointerEvent is nullptr");
56 return;
57 }
58 uint32_t windowId = static_cast<uint32_t>(pointerEvent->GetAgentWindowId());
59 WLOGFI("Receive pointerEvent, windowId: %{public}u", windowId);
60 auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
61 if (channel == nullptr) {
62 WLOGFE("WindowInputChannel is nullptr");
63 return;
64 }
65 channel->HandlePointerEvent(pointerEvent);
66 }
67
AddInputWindow(const sptr<Window> & window)68 void InputTransferStation::AddInputWindow(const sptr<Window>& window)
69 {
70 uint32_t windowId = window->GetWindowId();
71 WLOGFI("Add input window, windowId: %{public}u", windowId);
72 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window);
73 std::lock_guard<std::mutex> lock(mtx_);
74 windowInputChannels_.insert(std::make_pair(windowId, inputChannel));
75 if (inputListener_ == nullptr) {
76 WLOGFI("Init input listener");
77 std::shared_ptr<MMI::IInputEventConsumer> listener = std::make_shared<InputEventListener>(InputEventListener());
78 MMI::InputManager::GetInstance()->SetWindowInputEventConsumer(listener);
79 inputListener_ = listener;
80 }
81 }
82
RemoveInputWindow(const sptr<Window> & window)83 void InputTransferStation::RemoveInputWindow(const sptr<Window>& window)
84 {
85 uint32_t windowId = window->GetWindowId();
86 WLOGFI("Remove input window, windowId: %{public}u", windowId);
87 std::lock_guard<std::mutex> lock(mtx_);
88 auto iter = windowInputChannels_.find(windowId);
89 if (iter != windowInputChannels_.end()) {
90 auto inputChannel = iter->second;
91 windowInputChannels_.erase(windowId);
92 if (inputChannel != nullptr) {
93 inputChannel->Destroy();
94 }
95 } else {
96 WLOGFE("Can not find windowId: %{public}d", windowId);
97 }
98 }
99
SetInputListener(uint32_t windowId,const std::shared_ptr<MMI::IInputEventConsumer> & listener)100 void InputTransferStation::SetInputListener(
101 uint32_t windowId, const std::shared_ptr<MMI::IInputEventConsumer>& listener)
102 {
103 WLOGFI("windowId: %{public}u", windowId);
104 auto channel = GetInputChannel(windowId);
105 if (channel == nullptr) {
106 WLOGFE("WindowInputChannel is nullptr, windowId: %{public}u", windowId);
107 return;
108 }
109 channel->SetInputListener(listener);
110 }
111
GetInputChannel(uint32_t windowId)112 sptr<WindowInputChannel> InputTransferStation::GetInputChannel(uint32_t windowId)
113 {
114 std::lock_guard<std::mutex> lock(mtx_);
115 auto iter = windowInputChannels_.find(windowId);
116 if (iter == windowInputChannels_.end()) {
117 WLOGFE("Can not find channel according to windowId: %{public}d", windowId);
118 return nullptr;
119 }
120 return iter->second;
121 }
122 }
123 }
124