1 /*
2 * Copyright (c) 2021-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 "input_transfer_station.h"
17
18 #include <thread>
19 #include <event_handler.h>
20 #include "vsync_station.h"
21 #include "window_manager_hilog.h"
22 #include "wm_common_inner.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "InputTransferStation"};
28 }
WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)29 WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)
30
31 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
32 {
33 if (keyEvent == nullptr) {
34 WLOGFE("KeyEvent is nullptr");
35 return;
36 }
37 uint32_t windowId = static_cast<uint32_t>(keyEvent->GetAgentWindowId());
38 WLOGFD("Receive keyEvent, windowId: %{public}u", windowId);
39 auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
40 if (channel == nullptr) {
41 WLOGFE("WindowInputChannel is nullptr");
42 return;
43 }
44 channel->HandleKeyEvent(keyEvent);
45 }
46
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const47 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
48 {
49 if (axisEvent == nullptr) {
50 WLOGFE("AxisEvent is nullptr");
51 return;
52 }
53 WLOGFD("Receive axisEvent, windowId: %{public}d", axisEvent->GetAgentWindowId());
54 axisEvent->MarkProcessed();
55 }
56
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const57 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
58 {
59 if (pointerEvent == nullptr) {
60 WLOGFE("PointerEvent is nullptr");
61 return;
62 }
63 // If handling input event at server, client will receive pointEvent that the winId is -1, intercept log error
64 uint32_t invalidId = static_cast<uint32_t>(-1);
65 uint32_t windowId = static_cast<uint32_t>(pointerEvent->GetAgentWindowId());
66 WLOGFD("Receive pointerEvent, windowId: %{public}u", windowId);
67 auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
68 if (channel == nullptr) {
69 if (windowId != invalidId) {
70 WLOGFE("WindowInputChannel is nullptr");
71 }
72 pointerEvent->MarkProcessed();
73 return;
74 }
75 channel->HandlePointerEvent(pointerEvent);
76 }
77
AddInputWindow(const sptr<Window> & window)78 void InputTransferStation::AddInputWindow(const sptr<Window>& window)
79 {
80 uint32_t windowId = window->GetWindowId();
81 WLOGFD("Add input window, windowId: %{public}u", windowId);
82
83 // INPUT_WINDOW_TYPE_SKIPPED should not set input consumer
84 if (INPUT_WINDOW_TYPE_SKIPPED.find(window->GetType()) != INPUT_WINDOW_TYPE_SKIPPED.end()) {
85 WLOGFW("skip window for InputConsumer [id:%{public}u, type:%{public}d]", windowId, window->GetType());
86 return;
87 }
88 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window);
89 std::lock_guard<std::mutex> lock(mtx_);
90 windowInputChannels_.insert(std::make_pair(windowId, inputChannel));
91 if (inputListener_ == nullptr) {
92 WLOGFD("Init input listener, IsMainHandlerAvailable: %{public}u", window->IsMainHandlerAvailable());
93 std::shared_ptr<MMI::IInputEventConsumer> listener = std::make_shared<InputEventListener>(InputEventListener());
94 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
95 if (mainEventRunner != nullptr && window->IsMainHandlerAvailable()) {
96 WLOGFD("MainEventRunner is available");
97 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
98 } else {
99 WLOGFD("MainEventRunner is not available");
100 eventHandler_ = AppExecFwk::EventHandler::Current();
101 auto curThreadId = std::this_thread::get_id();
102 if (!eventHandler_ || (mainEventRunner != nullptr &&
103 mainEventRunner->GetThreadId() == *(reinterpret_cast<uint64_t*>(&curThreadId)))) {
104 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(
105 AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
106 }
107 VsyncStation::GetInstance().SetIsMainHandlerAvailable(false);
108 VsyncStation::GetInstance().SetVsyncEventHandler(eventHandler_);
109 }
110 MMI::InputManager::GetInstance()->SetWindowInputEventConsumer(listener, eventHandler_);
111 inputListener_ = listener;
112 }
113 }
114
RemoveInputWindow(uint32_t windowId)115 void InputTransferStation::RemoveInputWindow(uint32_t windowId)
116 {
117 WLOGFD("Remove input window, windowId: %{public}u", windowId);
118 sptr<WindowInputChannel> inputChannel = nullptr;
119 {
120 std::lock_guard<std::mutex> lock(mtx_);
121 auto iter = windowInputChannels_.find(windowId);
122 if (iter != windowInputChannels_.end()) {
123 inputChannel = iter->second;
124 windowInputChannels_.erase(windowId);
125 }
126 }
127 if (inputChannel != nullptr) {
128 inputChannel->Destroy();
129 } else {
130 WLOGFE("Can not find windowId: %{public}u", windowId);
131 }
132 }
133
GetInputChannel(uint32_t windowId)134 sptr<WindowInputChannel> InputTransferStation::GetInputChannel(uint32_t windowId)
135 {
136 std::lock_guard<std::mutex> lock(mtx_);
137 auto iter = windowInputChannels_.find(windowId);
138 if (iter == windowInputChannels_.end()) {
139 return nullptr;
140 }
141 return iter->second;
142 }
143 }
144 }
145