• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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_intercept.h"
17 #include "window_input_intercept_consumer.h"
18 #include "window_manager_hilog.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 
WM_IMPLEMENT_SINGLE_INSTANCE(WindowInputIntercept)23 WM_IMPLEMENT_SINGLE_INSTANCE(WindowInputIntercept)
24 void WindowInputIntercept::RegisterInputEventIntercept(const int32_t deviceId,
25                                                        const std::shared_ptr<IInputEventInterceptConsumer>& consumer)
26 {
27     if (consumer == nullptr) {
28         TLOGE(WmsLogTag::WMS_EVENT, "GameController call RegisterInputEventIntercept failed. the consumer is nullptr");
29         return;
30     }
31     std::lock_guard<std::mutex> lock(registerMutex_);
32     auto iter = inputIntercept_.find(deviceId);
33     if (iter == inputIntercept_.end()) {
34         TLOGI(WmsLogTag::WMS_EVENT, "GameController call RegisterInputEventIntercept success."
35                                     " the deviceId is %{public}d", deviceId);
36         inputIntercept_.insert(std::make_pair(deviceId, consumer));
37     }
38 }
39 
UnRegisterInputEventIntercept(const int32_t deviceId)40 void WindowInputIntercept::UnRegisterInputEventIntercept(const int32_t deviceId)
41 {
42     std::lock_guard<std::mutex> lock(registerMutex_);
43     auto iter = inputIntercept_.find(deviceId);
44     if (iter != inputIntercept_.end()) {
45         TLOGI(WmsLogTag::WMS_EVENT, "GameController call UnRegisterInputEventIntercept success."
46                                     " the deviceId is %{public}d", deviceId);
47         inputIntercept_.erase(deviceId);
48     }
49 }
50 
51 
IsInputIntercept(const std::shared_ptr<MMI::KeyEvent> & keyEvent)52 bool WindowInputIntercept::IsInputIntercept(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
53 {
54     if (keyEvent == nullptr) {
55         TLOGW(WmsLogTag::WMS_EVENT, "IsInputInterceptByKeyEvent keyEvent is null.");
56         return false;
57     }
58     std::shared_ptr<IInputEventInterceptConsumer> consumer = nullptr;
59     {
60         std::lock_guard<std::mutex> lock(registerMutex_);
61         auto iter = inputIntercept_.find(keyEvent->GetDeviceId());
62         if (iter == inputIntercept_.end()) {
63             return false;
64         }
65         consumer = iter->second;
66     }
67     if (consumer == nullptr) {
68         TLOGW(WmsLogTag::WMS_EVENT, "IsInputInterceptByKeyEvent consumer is null. the deviceId is %{public}d",
69             keyEvent->GetDeviceId());
70         return false;
71     }
72 
73     // Transfer the KeyEvent to GameController
74     consumer->OnInputEvent(keyEvent);
75     return true;
76 }
77 
IsInputIntercept(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)78 bool WindowInputIntercept::IsInputIntercept(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
79 {
80     if (pointerEvent == nullptr) {
81         TLOGW(WmsLogTag::WMS_EVENT, "IsInputInterceptByPointerEvent pointerEvent is null.");
82         return false;
83     }
84     std::shared_ptr<IInputEventInterceptConsumer> consumer = nullptr;
85     {
86         std::lock_guard<std::mutex> lock(registerMutex_);
87         auto iter = inputIntercept_.find(pointerEvent->GetDeviceId());
88         if (iter == inputIntercept_.end()) {
89             return false;
90         }
91         consumer = iter->second;
92     }
93     if (consumer == nullptr) {
94         TLOGW(WmsLogTag::WMS_EVENT, "IsInputInterceptByPointerEvent consumer is null. the deviceId is %{public}d",
95             pointerEvent->GetDeviceId());
96         return false;
97     }
98 
99     // Transfer the PointerEvent to GameController
100     consumer->OnInputEvent(pointerEvent);
101     return true;
102 }
103 }
104 }