• 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 
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);
53     std::shared_ptr<KeyEvent> CleanUpKeyEvent();
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     std::shared_ptr<KeyEvent> keyEvent_ { nullptr };
62 
63     const std::map<enum libinput_joystick_axis_source, AxisInfo> axesMap_ {
64         {
65             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_X,
66             AxisInfo {
67                 .name = "X",
68                 .axisType = PointerEvent::AXIS_TYPE_ABS_X,
69                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
70                     return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
71                 }
72             },
73         },
74         {
75             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Y,
76             AxisInfo {
77                 .name = "Y",
78                 .axisType = PointerEvent::AXIS_TYPE_ABS_Y,
79                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
80                     return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
81                 }
82             },
83         },
84         {
85             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_Z,
86             AxisInfo {
87                 .name = "Z",
88                 .axisType = PointerEvent::AXIS_TYPE_ABS_Z,
89                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
90                     return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
91                 }
92             },
93         },
94         {
95             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_RZ,
96             AxisInfo {
97                 .name = "RZ",
98                 .axisType = PointerEvent::AXIS_TYPE_ABS_RZ,
99                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
100                     return JoystickEventProcessor::Normalize(axis, -1.0, 1.0);
101                 }
102             },
103         },
104         {
105             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_GAS,
106             AxisInfo {
107                 .name = "GAS",
108                 .axisType = PointerEvent::AXIS_TYPE_ABS_GAS,
109                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
110                     return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
111                 }
112             },
113         },
114         {
115             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_BRAKE,
116             AxisInfo {
117                 .name = "BRAKE",
118                 .axisType = PointerEvent::AXIS_TYPE_ABS_BRAKE,
119                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
120                     return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
121                 }
122             },
123         },
124         {
125             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0X,
126             AxisInfo {
127                 .name = "HAT0X",
128                 .axisType = PointerEvent::AXIS_TYPE_ABS_HAT0X,
129                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
130                     return axis.value;
131                 }
132             },
133         },
134         {
135             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_HAT0Y,
136             AxisInfo {
137                 .name = "HAT0Y",
138                 .axisType = PointerEvent::AXIS_TYPE_ABS_HAT0Y,
139                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
140                     return axis.value;
141                 }
142             },
143         },
144         {
145             LIBINPUT_JOYSTICK_AXIS_SOURCE_ABS_THROTTLE,
146             AxisInfo {
147                 .name = "THROTTLE",
148                 .axisType = PointerEvent::AXIS_TYPE_ABS_THROTTLE,
149                 .normalize = [](const struct libinput_event_joystick_axis_abs_info &axis) {
150                     return JoystickEventProcessor::Normalize(axis, 0.0, 1.0);
151                 }
152             },
153         },
154     };
155 };
156 
GetDeviceId()157 inline int32_t JoystickEventProcessor::GetDeviceId() const
158 {
159     return deviceId_;
160 }
161 
PressButton(int32_t button)162 inline void JoystickEventProcessor::PressButton(int32_t button)
163 {
164     pressedButtons_.emplace(button);
165 }
166 
LiftButton(int32_t button)167 inline void JoystickEventProcessor::LiftButton(int32_t button)
168 {
169     pressedButtons_.erase(button);
170 }
171 
IsButtonPressed(int32_t button)172 inline bool JoystickEventProcessor::IsButtonPressed(int32_t button) const
173 {
174     return (pressedButtons_.find(button) != pressedButtons_.cend());
175 }
176 } // namespace MMI
177 } // namespace OHOS
178 #endif // JOYSTICK_EVENT_PROCESSOR_H
179