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 #ifndef JOYSTICK_EVENT_PROCESSOR_H
17 #define JOYSTICK_EVENT_PROCESSOR_H
18 #include <map>
19
20 #include <libinput.h>
21
22 #include "key_event.h"
23 #include "pointer_event.h"
24
25 namespace OHOS {
26 namespace MMI {
27 class JoystickEventProcessor final {
28 struct AxisInfo {
29 std::string name;
30 PointerEvent::AxisType axisType;
31 std::function<double(const struct libinput_event_joystick_axis_abs_info&)> normalize;
32 };
33
34 public:
35 explicit JoystickEventProcessor(int32_t deviceId);
36 ~JoystickEventProcessor() = default;
37 DISALLOW_COPY_AND_MOVE(JoystickEventProcessor);
38
39 int32_t GetDeviceId() const;
40 std::shared_ptr<KeyEvent> OnButtonEvent(struct libinput_event *event);
41 std::shared_ptr<PointerEvent> OnAxisEvent(struct libinput_event *event);
42 void CheckIntention(std::shared_ptr<PointerEvent> pointerEvent,
43 std::function<void(std::shared_ptr<KeyEvent>)> handler);
44
45 private:
46 void PressButton(int32_t button);
47 void LiftButton(int32_t button);
48 bool IsButtonPressed(int32_t button) const;
49 void UpdateButtonState(const KeyEvent::KeyItem &keyItem);
50 void CheckHAT0X(std::shared_ptr<PointerEvent> pointerEvent, std::vector<KeyEvent::KeyItem> &buttonEvents) const;
51 void CheckHAT0Y(std::shared_ptr<PointerEvent> pointerEvent, std::vector<KeyEvent::KeyItem> &buttonEvents) const;
52 std::shared_ptr<KeyEvent> FormatButtonEvent(const KeyEvent::KeyItem &button) const;
53 std::shared_ptr<KeyEvent> CleanUpKeyEvent() const;
54 std::string DumpJoystickAxisEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
55 static double Normalize(const struct libinput_event_joystick_axis_abs_info &axis, double low, double high);
56
57 private:
58 const int32_t deviceId_ { -1 };
59 std::set<int32_t> pressedButtons_;
60 std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
61
62 const std::map<enum libinput_joystick_axis_source, AxisInfo> axesMap_ {
63 {
64 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_X,
65 AxisInfo {
66 .name = "X",
67 .axisType = PointerEvent::AXIS_TYPE_ABS_X,
68 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
69 return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
70 }
71 },
72 },
73 {
74 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Y,
75 AxisInfo {
76 .name = "Y",
77 .axisType = PointerEvent::AXIS_TYPE_ABS_Y,
78 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
79 return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
80 }
81 },
82 },
83 {
84 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Z,
85 AxisInfo {
86 .name = "Z",
87 .axisType = PointerEvent::AXIS_TYPE_ABS_Z,
88 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
89 return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
90 }
91 },
92 },
93 {
94 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_RZ,
95 AxisInfo {
96 .name = "RZ",
97 .axisType = PointerEvent::AXIS_TYPE_ABS_RZ,
98 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
99 return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
100 }
101 },
102 },
103 {
104 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_GAS,
105 AxisInfo {
106 .name = "GAS",
107 .axisType = PointerEvent::AXIS_TYPE_ABS_GAS,
108 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
109 return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
110 }
111 },
112 },
113 {
114 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_BRAKE,
115 AxisInfo {
116 .name = "BRAKE",
117 .axisType = PointerEvent::AXIS_TYPE_ABS_BRAKE,
118 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
119 return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
120 }
121 },
122 },
123 {
124 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0X,
125 AxisInfo {
126 .name = "HAT0X",
127 .axisType = PointerEvent::AXIS_TYPE_ABS_HAT0X,
128 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
129 return axis.value;
130 }
131 },
132 },
133 {
134 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0Y,
135 AxisInfo {
136 .name = "HAT0Y",
137 .axisType = PointerEvent::AXIS_TYPE_ABS_HAT0Y,
138 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
139 return axis.value;
140 }
141 },
142 },
143 {
144 LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_THROTTLE,
145 AxisInfo {
146 .name = "THROTTLE",
147 .axisType = PointerEvent::AXIS_TYPE_ABS_THROTTLE,
148 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
149 return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
150 }
151 },
152 },
153 };
154 };
155
GetDeviceId()156 inline int32_t JoystickEventProcessor::GetDeviceId() const
157 {
158 return deviceId_;
159 }
160
PressButton(int32_t button)161 inline void JoystickEventProcessor::PressButton(int32_t button)
162 {
163 pressedButtons_.emplace(button);
164 }
165
LiftButton(int32_t button)166 inline void JoystickEventProcessor::LiftButton(int32_t button)
167 {
168 pressedButtons_.erase(button);
169 }
170
IsButtonPressed(int32_t button)171 inline bool JoystickEventProcessor::IsButtonPressed(int32_t button) const
172 {
173 return (pressedButtons_.find(button) != pressedButtons_.cend());
174 }
175 } // namespace MMI
176 } // namespace OHOS
177 #endif // JOYSTICK_EVENT_PROCESSOR_H
178