• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "window_manager_hilog.h"
21 #include "wm_common_inner.h"
22 #include "gtx_input_event_sender.h"
23 #include <hitrace_meter.h>
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "InputTransferStation"};
29 }
WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)30 WM_IMPLEMENT_SINGLE_INSTANCE(InputTransferStation)
31 
32 InputTransferStation::~InputTransferStation()
33 {
34     std::lock_guard<std::mutex> lock(mtx_);
35     destroyed_ = true;
36 }
37 
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const38 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
39 {
40     if (keyEvent == nullptr) {
41         TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW, "KeyEvent is nullptr");
42         return;
43     }
44     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "IEL:KeyEvent id:%d", keyEvent->GetId());
45     uint32_t windowId = static_cast<uint32_t>(keyEvent->GetAgentWindowId());
46     static uint32_t eventId = 0;
47     TLOGI(WmsLogTag::WMS_INPUT_KEY_FLOW, "eid:%{public}d,InputId:%{public}d,wid:%{public}u",
48         eventId++, keyEvent->GetId(), windowId);
49     auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
50     if (channel == nullptr) {
51         keyEvent->MarkProcessed();
52         TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW,
53             "WindowInputChannel is nullptr InputTracking id:%{public}d windowId:%{public}u",
54             keyEvent->GetId(), windowId);
55         return;
56     }
57     channel->HandleKeyEvent(keyEvent);
58 }
59 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const60 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
61 {
62     if (axisEvent == nullptr) {
63         TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW, "AxisEvent is nullptr");
64         return;
65     }
66     TLOGD(WmsLogTag::WMS_INPUT_KEY_FLOW, "Receive axisEvent, windowId: %{public}d", axisEvent->GetAgentWindowId());
67     axisEvent->MarkProcessed();
68 }
69 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const70 void InputEventListener::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
71 {
72     if (pointerEvent == nullptr) {
73         TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW, "PointerEvent is nullptr");
74         return;
75     }
76     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "IEL:PointerEvent id:%d action:%d",
77         pointerEvent->GetId(), pointerEvent->GetPointerAction());
78     // If handling input event at server, client will receive pointEvent that the winId is -1, intercept log error
79     uint32_t invalidId = static_cast<uint32_t>(-1);
80     uint32_t windowId = static_cast<uint32_t>(pointerEvent->GetAgentWindowId());
81     int32_t action = pointerEvent->GetPointerAction();
82     if (action != MMI::PointerEvent::POINTER_ACTION_MOVE) {
83         static uint32_t eventId = 0;
84         TLOGI(WmsLogTag::WMS_INPUT_KEY_FLOW, "eid:%{public}d,InputId:%{public}d"
85             ",wid:%{public}u,action:%{public}d", eventId++, pointerEvent->GetId(), windowId,
86             pointerEvent->GetPointerAction());
87     }
88     auto channel = InputTransferStation::GetInstance().GetInputChannel(windowId);
89     if (channel == nullptr) {
90         if (windowId != invalidId) {
91             TLOGE(WmsLogTag::WMS_INPUT_KEY_FLOW, "WindowInputChannel is nullptr InputTracking id:%{public}d "
92                 "windowId:%{public}u",
93                 pointerEvent->GetId(), windowId);
94         }
95         pointerEvent->MarkProcessed();
96         return;
97     }
98     channel->HandlePointerEvent(pointerEvent);
99     GtxInputEventSender::GetInstance().SetTouchEvent(channel->GetWindowRect(), pointerEvent);
100 }
101 
AddInputWindow(const sptr<Window> & window)102 void InputTransferStation::AddInputWindow(const sptr<Window>& window)
103 {
104     uint32_t windowId = window->GetWindowId();
105     TLOGD(WmsLogTag::WMS_EVENT, "Add input window, windowId: %{public}u", windowId);
106 
107     // INPUT_WINDOW_TYPE_SKIPPED should not set input consumer
108     if (INPUT_WINDOW_TYPE_SKIPPED.find(window->GetType()) != INPUT_WINDOW_TYPE_SKIPPED.end()) {
109         TLOGW(WmsLogTag::WMS_EVENT, "skip window for InputConsumer [id:%{public}u, type:%{public}d]",
110             windowId, window->GetType());
111         return;
112     }
113 
114     if (windowInputChannels_.count(windowId) == 0) {
115         sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window);
116         std::lock_guard<std::mutex> lock(mtx_);
117         if (destroyed_) {
118             TLOGW(WmsLogTag::WMS_EVENT, "Already destroyed");
119             return;
120         }
121         windowInputChannels_.insert(std::make_pair(windowId, inputChannel));
122     }
123 
124     if (inputListener_ == nullptr) {
125         TLOGD(WmsLogTag::WMS_EVENT, "Init input listener, IsMainHandlerAvailable: %{public}u",
126             window->IsMainHandlerAvailable());
127         std::shared_ptr<MMI::IInputEventConsumer> listener = std::make_shared<InputEventListener>(InputEventListener());
128         auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
129         if (mainEventRunner != nullptr && window->IsMainHandlerAvailable()) {
130             TLOGD(WmsLogTag::WMS_EVENT, "MainEventRunner is available");
131             eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
132         } else {
133             TLOGD(WmsLogTag::WMS_EVENT, "MainEventRunner is not available");
134             eventHandler_ = AppExecFwk::EventHandler::Current();
135             auto curThreadId = std::this_thread::get_id();
136             if (!eventHandler_ || (mainEventRunner != nullptr &&
137                 mainEventRunner->GetThreadId() == *(reinterpret_cast<uint64_t*>(&curThreadId)))) {
138                 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(
139                     AppExecFwk::EventRunner::Create(INPUT_AND_VSYNC_THREAD));
140             }
141         }
142         MMI::InputManager::GetInstance()->SetWindowInputEventConsumer(listener, eventHandler_);
143         TLOGI(WmsLogTag::WMS_EVENT, "SetWindowInputEventConsumer success, windowid:%{public}u", windowId);
144         inputListener_ = listener;
145     } else {
146         auto ret = MMI::InputManager::GetInstance()->SetWindowInputEventConsumer(inputListener_, eventHandler_);
147         TLOGI(WmsLogTag::WMS_EVENT, "SetWindowInputEventConsumer %{public}u, windowid:%{public}u", ret, windowId);
148     }
149 }
150 
RemoveInputWindow(uint32_t windowId)151 void InputTransferStation::RemoveInputWindow(uint32_t windowId)
152 {
153     TLOGD(WmsLogTag::WMS_EVENT, "Remove input window, windowId: %{public}u", windowId);
154     sptr<WindowInputChannel> inputChannel = nullptr;
155     {
156         std::lock_guard<std::mutex> lock(mtx_);
157         if (destroyed_) {
158             WLOGFW("Already destroyed");
159             return;
160         }
161         auto iter = windowInputChannels_.find(windowId);
162         if (iter != windowInputChannels_.end()) {
163             inputChannel = iter->second;
164             windowInputChannels_.erase(windowId);
165         }
166     }
167     if (inputChannel != nullptr) {
168         inputChannel->Destroy();
169     } else {
170         TLOGE(WmsLogTag::WMS_EVENT, "Can not find windowId: %{public}u", windowId);
171     }
172 }
173 
GetInputChannel(uint32_t windowId)174 sptr<WindowInputChannel> InputTransferStation::GetInputChannel(uint32_t windowId)
175 {
176     std::lock_guard<std::mutex> lock(mtx_);
177     if (destroyed_) {
178         TLOGW(WmsLogTag::WMS_EVENT, "Already destroyed");
179         return nullptr;
180     }
181     auto iter = windowInputChannels_.find(windowId);
182     if (iter == windowInputChannels_.end()) {
183         return nullptr;
184     }
185     return iter->second;
186 }
187 }
188 }
189