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