1 /*
2 * Copyright (c) 2024 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 #include "cstdint"
16 #include "functional"
17 #include "oh_window_comm.h"
18 #include "oh_window_event_filter.h"
19 #include "key_event.h"
20 #include "window.h"
21 #include "window_manager_hilog.h"
22 #include "wm_common.h"
23
24 using namespace OHOS::Rosen;
25
26 static const std::unordered_map<int32_t, Input_KeyEventAction> keyEventActionMap = {
27 {OHOS::MMI::KeyEvent::KeyEvent::KEY_ACTION_CANCEL, Input_KeyEventAction::KEY_ACTION_CANCEL},
28 {OHOS::MMI::KeyEvent::KeyEvent::KEY_ACTION_DOWN, Input_KeyEventAction::KEY_ACTION_DOWN},
29 {OHOS::MMI::KeyEvent::KeyEvent::KEY_ACTION_UP, Input_KeyEventAction::KEY_ACTION_UP},
30 };
31
convert2Func(OH_NativeWindowManager_KeyEventFilter filter)32 KeyEventFilterFunc convert2Func(OH_NativeWindowManager_KeyEventFilter filter)
33 {
34 std::function<bool(OHOS::MMI::KeyEvent&)> func = [filter](OHOS::MMI::KeyEvent& keyEvent) {
35 Input_KeyEvent *input = OH_Input_CreateKeyEvent();
36 OH_Input_SetKeyEventKeyCode(input, keyEvent.GetKeyCode());
37 auto iter = keyEventActionMap.find(keyEvent.GetKeyAction());
38 if (iter == keyEventActionMap.end()) {
39 return false;
40 }
41 OH_Input_SetKeyEventAction(input, iter->second);
42 OH_Input_SetKeyEventActionTime(input, keyEvent.GetActionTime());
43 return filter(input);
44 };
45 return func ;
46 }
47
OH_NativeWindowManager_RegisterKeyEventFilter(int32_t windowId,OH_NativeWindowManager_KeyEventFilter filter)48 WindowManager_ErrorCode OH_NativeWindowManager_RegisterKeyEventFilter(int32_t windowId,
49 OH_NativeWindowManager_KeyEventFilter filter)
50 {
51 TLOGI(WmsLogTag::WMS_EVENT, "register keyEventCallback, windowId:%{public}d", windowId);
52 auto mainWindow = OHOS::Rosen::Window::GetWindowWithId(windowId);
53 if (mainWindow == nullptr) {
54 TLOGE(WmsLogTag::WMS_EVENT, "window is null, windowId:%{public}d", windowId);
55 return WindowManager_ErrorCode::INVAILD_WINDOW_ID;
56 }
57 auto res = mainWindow->SetKeyEventFilter(convert2Func(filter));
58
59 return res == WMError::WM_OK ? WindowManager_ErrorCode::OK : WindowManager_ErrorCode::SERVICE_ERROR;
60 }
61
OH_NativeWindowManager_UnregisterKeyEventFilter(int32_t windowId)62 WindowManager_ErrorCode OH_NativeWindowManager_UnregisterKeyEventFilter(int32_t windowId)
63 {
64 TLOGI(WmsLogTag::WMS_EVENT, "clear keyEventCallback, windowId:%{public}d", windowId);
65 auto mainWindow = OHOS::Rosen::Window::GetWindowWithId(windowId);
66 if (mainWindow == nullptr) {
67 TLOGE(WmsLogTag::WMS_EVENT, "window is null, windowId:%{public}d", windowId);
68 return WindowManager_ErrorCode::INVAILD_WINDOW_ID;
69 }
70 auto res = mainWindow->ClearKeyEventFilter();
71 return res == WMError::WM_OK ? WindowManager_ErrorCode::OK : WindowManager_ErrorCode::SERVICE_ERROR;
72 }
73