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