1 /*
2 * Copyright (c) 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 "joystick_transform_processor.h"
17
18 #include "mmi_log.h"
19
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JoystickTransformProcessor" };
24 } // namespace
25
JoystickTransformProcessor(int32_t deviceId)26 JoystickTransformProcessor::JoystickTransformProcessor(int32_t deviceId) : deviceId_(deviceId)
27 {
28 joystickType.emplace_back(
29 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_X, PointerEvent::AXIS_TYPE_ABS_X));
30 joystickType.emplace_back(
31 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Y, PointerEvent::AXIS_TYPE_ABS_Y));
32 joystickType.emplace_back(
33 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Z, PointerEvent::AXIS_TYPE_ABS_Z));
34 joystickType.emplace_back(
35 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_RZ, PointerEvent::AXIS_TYPE_ABS_RZ));
36 joystickType.emplace_back(
37 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_GAS, PointerEvent::AXIS_TYPE_ABS_GAS));
38 joystickType.emplace_back(
39 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_BRAKE, PointerEvent::AXIS_TYPE_ABS_BRAKE));
40 joystickType.emplace_back(
41 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0X, PointerEvent::AXIS_TYPE_ABS_HAT0X));
42 joystickType.emplace_back(
43 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0Y, PointerEvent::AXIS_TYPE_ABS_HAT0Y));
44 joystickType.emplace_back(
45 std::make_pair(LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_THROTTLE, PointerEvent::AXIS_TYPE_ABS_THROTTLE));
46 }
47
OnEventJoystickButton(struct libinput_event * event)48 bool JoystickTransformProcessor::OnEventJoystickButton(struct libinput_event* event)
49 {
50 CALL_DEBUG_ENTER;
51 CHKPF(event);
52 auto data = libinput_event_get_joystick_button_event(event);
53 CHKPF(data);
54 int64_t time = GetSysClockTime();
55 pointerEvent_->SetActionTime(time);
56 pointerEvent_->SetActionStartTime(time);
57 pointerEvent_->SetDeviceId(deviceId_);
58 uint32_t button = libinput_event_joystick_button_get_key(data);
59 int32_t buttonId = LibinputButtonToPointer(button);
60 if (buttonId == PointerEvent::BUTTON_NONE) {
61 MMI_HILOGE("Unknown btn, btn:%{public}u", button);
62 return false;
63 }
64 pointerEvent_->SetButtonId(buttonId);
65 auto state = libinput_event_joystick_button_get_key_state(data);
66 if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
67 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
68 pointerEvent_->DeleteReleaseButton(buttonId);
69 } else if (state == LIBINPUT_BUTTON_STATE_PRESSED) {
70 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
71 pointerEvent_->SetButtonPressed(buttonId);
72 } else {
73 MMI_HILOGE("Unknown state, state:%{public}u", state);
74 return false;
75 }
76 MMI_HILOGD("button:%{public}u, buttonId:%{public}d, state:%{public}d", button, buttonId, state);
77 return true;
78 }
79
LibinputButtonToPointer(const uint32_t button)80 int32_t JoystickTransformProcessor::LibinputButtonToPointer(const uint32_t button)
81 {
82 auto iter = LibinputChangeToPointer.find(button);
83 return (iter == LibinputChangeToPointer.end() ? PointerEvent::BUTTON_NONE : iter->second);
84 }
85
OnEventJoystickAxis(struct libinput_event * event)86 bool JoystickTransformProcessor::OnEventJoystickAxis(struct libinput_event *event)
87 {
88 CALL_DEBUG_ENTER;
89 CHKPF(event);
90 auto data = libinput_event_get_joystick_axis_event(event);
91 CHKPF(data);
92 int64_t time = GetSysClockTime();
93 pointerEvent_->SetActionTime(time);
94 pointerEvent_->SetActionStartTime(time);
95 pointerEvent_->SetDeviceId(deviceId_);
96 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
97 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
98
99 for (const auto &item : joystickType) {
100 if (libinput_event_get_joystick_axis_value_is_changed(data, item.first) != 0) {
101 struct libinput_event_joystick_axis_abs_info* axisInfo =
102 libinput_event_get_joystick_axis_abs_info(data, item.first);
103 CHKPF(axisInfo);
104 pointerEvent_->SetAxisValue(item.second, axisInfo->value);
105 }
106 }
107 return true;
108 }
109
OnEvent(struct libinput_event * event)110 std::shared_ptr<PointerEvent> JoystickTransformProcessor::OnEvent(struct libinput_event *event)
111 {
112 CALL_DEBUG_ENTER;
113 CHKPP(event);
114 if (pointerEvent_ == nullptr) {
115 pointerEvent_ = PointerEvent::Create();
116 CHKPP(pointerEvent_);
117 }
118 pointerEvent_->ClearAxisValue();
119 auto type = libinput_event_get_type(event);
120 switch (type) {
121 case LIBINPUT_EVENT_JOYSTICK_BUTTON: {
122 if (!OnEventJoystickButton(event)) {
123 MMI_HILOGE("Get OnEventJoystickButton failed");
124 return nullptr;
125 }
126 break;
127 }
128 case LIBINPUT_EVENT_JOYSTICK_AXIS: {
129 if (!OnEventJoystickAxis(event)) {
130 MMI_HILOGE("Get OnEventJoystickAxis failed");
131 return nullptr;
132 }
133 break;
134 }
135 default: {
136 MMI_HILOGE("Unknown event type, joystickType:%{public}d", type);
137 return nullptr;
138 }
139 }
140 WinMgr->UpdateTargetPointer(pointerEvent_);
141
142 return pointerEvent_;
143 }
144 } // namespace MMI
145 } // namespace OHOS
146