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
16 #include "joystick_event_normalize.h"
17
18 #include "define_multimodal.h"
19 #include "input_device_manager.h"
20
21 #undef MMI_LOG_DOMAIN
22 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "JoystickEventNormalize"
25
26 namespace OHOS {
27 namespace MMI {
OnButtonEvent(struct libinput_event * event)28 std::shared_ptr<KeyEvent> JoystickEventNormalize::OnButtonEvent(struct libinput_event *event)
29 {
30 CHKPP(event);
31 auto inputDev = libinput_event_get_device(event);
32 CHKPP(inputDev);
33 auto processor = GetProcessor(inputDev);
34 CHKPP(processor);
35 return processor->OnButtonEvent(event);
36 }
37
OnAxisEvent(struct libinput_event * event)38 std::shared_ptr<PointerEvent> JoystickEventNormalize::OnAxisEvent(struct libinput_event *event)
39 {
40 CHKPP(event);
41 auto inputDev = libinput_event_get_device(event);
42 CHKPP(inputDev);
43 auto processor = GetProcessor(inputDev);
44 CHKPP(processor);
45 return processor->OnAxisEvent(event);
46 }
47
CheckIntention(std::shared_ptr<PointerEvent> pointerEvent,std::function<void (std::shared_ptr<KeyEvent>)> handler)48 void JoystickEventNormalize::CheckIntention(std::shared_ptr<PointerEvent> pointerEvent,
49 std::function<void(std::shared_ptr<KeyEvent>)> handler)
50 {
51 auto processor = FindProcessor(pointerEvent->GetDeviceId());
52 if (processor == nullptr) {
53 MMI_HILOGE("No processor associated with input device(%{public}d)", pointerEvent->GetDeviceId());
54 return;
55 }
56 processor->CheckIntention(pointerEvent, handler);
57 }
58
GetProcessor(struct libinput_device * inputDev)59 std::shared_ptr<JoystickEventProcessor> JoystickEventNormalize::GetProcessor(struct libinput_device *inputDev)
60 {
61 if (auto iter = processors_.find(inputDev); iter != processors_.end()) {
62 return iter->second;
63 }
64 auto deviceId = INPUT_DEV_MGR->FindInputDeviceId(inputDev);
65 auto [iter, _] = processors_.emplace(inputDev, std::make_shared<JoystickEventProcessor>(deviceId));
66 return iter->second;
67 }
68
FindProcessor(int32_t deviceId) const69 std::shared_ptr<JoystickEventProcessor> JoystickEventNormalize::FindProcessor(int32_t deviceId) const
70 {
71 auto iter = std::find_if(processors_.cbegin(), processors_.cend(),
72 [deviceId](const auto &item) {
73 return ((item.second != nullptr) && (item.second->GetDeviceId() == deviceId));
74 });
75 return (iter != processors_.cend() ? iter->second : nullptr);
76 }
77 } // namespace MMI
78 } // namespace OHOS
79