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 "window_input_channel.h"
17 #ifdef IMF_ENABLE
18 #include <input_method_controller.h>
19 #endif // IMF_ENABLE
20 #include "window_manager_hilog.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowInputChannel"};
26 }
WindowInputChannel(const sptr<Window> & window)27 WindowInputChannel::WindowInputChannel(const sptr<Window>& window): window_(window), isAvailable_(true)
28 {
29 }
30
~WindowInputChannel()31 WindowInputChannel::~WindowInputChannel()
32 {
33 WLOGFI("windowName: %{public}s, windowId: %{public}d", window_->GetWindowName().c_str(), window_->GetWindowId());
34 window_->SetNeedRemoveWindowInputChannel(false);
35 }
36
HandleKeyEvent(std::shared_ptr<MMI::KeyEvent> & keyEvent)37 void WindowInputChannel::HandleKeyEvent(std::shared_ptr<MMI::KeyEvent>& keyEvent)
38 {
39 if (keyEvent == nullptr) {
40 WLOGFE("keyEvent is nullptr");
41 return;
42 }
43 WLOGFD("Receive key event, windowId: %{public}u, keyCode: %{public}d",
44 window_->GetWindowId(), keyEvent->GetKeyCode());
45 if (window_->GetType() == WindowType::WINDOW_TYPE_DIALOG) {
46 if (keyEvent->GetAgentWindowId() != keyEvent->GetTargetWindowId()) {
47 window_->NotifyTouchDialogTarget();
48 keyEvent->MarkProcessed();
49 return;
50 }
51 if (keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_BACK) {
52 keyEvent->MarkProcessed();
53 return;
54 }
55 }
56
57 bool inputMethodHasProcessed = false;
58 #ifdef IMF_ENABLE
59 bool isKeyboardEvent = IsKeyboardEvent(keyEvent);
60 if (isKeyboardEvent) {
61 WLOGFI("dispatch keyEvent to input method");
62 inputMethodHasProcessed = MiscServices::InputMethodController::GetInstance()->dispatchKeyEvent(keyEvent);
63 }
64 #endif // IMF_ENABLE
65 if (!inputMethodHasProcessed) {
66 WLOGFI("dispatch keyEvent to ACE");
67 window_->ConsumeKeyEvent(keyEvent);
68 }
69 }
70
HandlePointerEvent(std::shared_ptr<MMI::PointerEvent> & pointerEvent)71 void WindowInputChannel::HandlePointerEvent(std::shared_ptr<MMI::PointerEvent>& pointerEvent)
72 {
73 if (pointerEvent == nullptr) {
74 WLOGFE("pointerEvent is nullptr");
75 return;
76 }
77 WLOGFD("Receive pointer event, windowId: %{public}u, action: %{public}d",
78 window_->GetWindowId(), pointerEvent->GetPointerAction());
79 if ((window_->GetType() == WindowType::WINDOW_TYPE_DIALOG) &&
80 (pointerEvent->GetAgentWindowId() != pointerEvent->GetTargetWindowId())) {
81 if (pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_DOWN ||
82 pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
83 window_->NotifyTouchDialogTarget();
84 }
85 pointerEvent->MarkProcessed();
86 return;
87 }
88 window_->ConsumePointerEvent(pointerEvent);
89 }
90
Destroy()91 void WindowInputChannel::Destroy()
92 {
93 std::lock_guard<std::mutex> lock(mtx_);
94 WLOGFI("Destroy WindowInputChannel, windowId:%{public}u", window_->GetWindowId());
95 isAvailable_ = false;
96 }
97
IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const98 bool WindowInputChannel::IsKeyboardEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
99 {
100 int32_t keyCode = keyEvent->GetKeyCode();
101 bool isKeyFN = (keyCode == MMI::KeyEvent::KEYCODE_FN);
102 bool isKeyBack = (keyCode == MMI::KeyEvent::KEYCODE_BACK);
103 bool isKeyboard = (keyCode >= MMI::KeyEvent::KEYCODE_0 && keyCode <= MMI::KeyEvent::KEYCODE_NUMPAD_RIGHT_PAREN);
104 WLOGFI("isKeyFN: %{public}d, isKeyboard: %{public}d", isKeyFN, isKeyboard);
105 return (isKeyFN || isKeyboard || isKeyBack);
106 }
107 }
108 }