]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/joystick_controller.cpp
Fix always using the xbox layout (reported by coverity).
[dragonfireclient.git] / src / client / joystick_controller.cpp
1 /*
2 Minetest
3 Copyright (C) 2016 est31, <MTest31@outlook.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "joystick_controller.h"
21 #include "irrlichttypes_extrabloated.h"
22 #include "keys.h"
23 #include "settings.h"
24 #include "gettime.h"
25 #include "../util/string.h"
26
27 bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
28 {
29         u32 buttons = ev.ButtonStates;
30
31         buttons &= filter_mask;
32         return buttons == compare_mask;
33 }
34
35 bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
36 {
37         s16 ax_val = ev.Axis[axis_to_compare];
38
39         return (ax_val * direction < 0) && (thresh * direction > ax_val * direction);
40 }
41
42 // spares many characters
43 #define JLO_B_PB(A, B, C)    jlo.button_keys.push_back(JoystickButtonCmb(A, B, C))
44 #define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D))
45
46 JoystickLayout create_default_layout()
47 {
48         JoystickLayout jlo;
49
50         jlo.axes_dead_border = 1024;
51
52         const JoystickAxisLayout axes[JA_COUNT] = {
53                 {0, 1}, // JA_SIDEWARD_MOVE
54                 {1, 1}, // JA_FORWARD_MOVE
55                 {3, 1}, // JA_FRUSTUM_HORIZONTAL
56                 {4, 1}, // JA_FRUSTUM_VERTICAL
57         };
58         memcpy(jlo.axes, axes, sizeof(jlo.axes));
59
60         u32 sb = 1 << 7; // START button mask
61         u32 fb = 1 << 3; // FOUR button mask
62         u32 bm = sb | fb; // Mask for Both Modifiers
63
64         // The back button means "ESC".
65         JLO_B_PB(KeyType::ESC,        1 << 6,      1 << 6);
66
67         // The start button counts as modifier as well as use key.
68         // JLO_B_PB(KeyType::USE,        sb,          sb));
69
70         // Accessible without start modifier button pressed
71         // regardless whether four is pressed or not
72         JLO_B_PB(KeyType::SNEAK,      sb | 1 << 2, 1 << 2);
73
74         // Accessible without four modifier button pressed
75         // regardless whether start is pressed or not
76         JLO_B_PB(KeyType::MOUSE_L,    fb | 1 << 4, 1 << 4);
77         JLO_B_PB(KeyType::MOUSE_R,    fb | 1 << 5, 1 << 5);
78
79         // Accessible without any modifier pressed
80         JLO_B_PB(KeyType::JUMP,       bm | 1 << 0, 1 << 0);
81         JLO_B_PB(KeyType::SPECIAL1,   bm | 1 << 1, 1 << 1);
82
83         // Accessible with start button not pressed, but four pressed
84         // TODO find usage for button 0
85         JLO_B_PB(KeyType::DROP,       bm | 1 << 1, fb | 1 << 1);
86         JLO_B_PB(KeyType::SCROLL_UP,  bm | 1 << 4, fb | 1 << 4);
87         JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5);
88
89         // Accessible with start button and four pressed
90         // TODO find usage for buttons 0, 1 and 4, 5
91
92         // Now about the buttons simulated by the axes
93
94         // Movement buttons, important for vessels
95         JLO_A_PB(KeyType::FORWARD,  1,  1, 1024);
96         JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
97         JLO_A_PB(KeyType::LEFT,     0,  1, 1024);
98         JLO_A_PB(KeyType::RIGHT,    0, -1, 1024);
99
100         // Scroll buttons
101         JLO_A_PB(KeyType::SCROLL_UP,   2, -1, 1024);
102         JLO_A_PB(KeyType::SCROLL_DOWN, 5, -1, 1024);
103
104         return jlo;
105 }
106
107 JoystickLayout create_xbox_layout()
108 {
109         JoystickLayout jlo;
110
111         jlo.axes_dead_border = 7000;
112
113         const JoystickAxisLayout axes[JA_COUNT] = {
114                 {0, 1}, // JA_SIDEWARD_MOVE
115                 {1, 1}, // JA_FORWARD_MOVE
116                 {2, 1}, // JA_FRUSTUM_HORIZONTAL
117                 {3, 1}, // JA_FRUSTUM_VERTICAL
118         };
119         memcpy(jlo.axes, axes, sizeof(jlo.axes));
120
121         // The back button means "ESC".
122         JLO_B_PB(KeyType::ESC,        1 << 8,  1 << 8); // back
123         JLO_B_PB(KeyType::ESC,        1 << 9,  1 << 9); // start
124
125         // 4 Buttons
126         JLO_B_PB(KeyType::JUMP,        1 << 0,  1 << 0); // A/green
127         JLO_B_PB(KeyType::ESC,         1 << 1,  1 << 1); // B/red
128         JLO_B_PB(KeyType::SPECIAL1,    1 << 2,  1 << 2); // X/blue
129         JLO_B_PB(KeyType::INVENTORY,   1 << 3,  1 << 3); // Y/yellow
130
131         // Analog Sticks
132         JLO_B_PB(KeyType::SPECIAL1,    1 << 11, 1 << 11); // left
133         JLO_B_PB(KeyType::SNEAK,       1 << 12, 1 << 12); // right
134
135         // Triggers
136         JLO_B_PB(KeyType::MOUSE_L,     1 << 6,  1 << 6); // lt
137         JLO_B_PB(KeyType::MOUSE_R,     1 << 7,  1 << 7); // rt
138         JLO_B_PB(KeyType::SCROLL_UP,   1 << 4,  1 << 4); // lb
139         JLO_B_PB(KeyType::SCROLL_DOWN, 1 << 5,  1 << 5); // rb
140
141         // D-PAD
142         JLO_B_PB(KeyType::ZOOM,        1 << 15, 1 << 15); // up
143         JLO_B_PB(KeyType::DROP,        1 << 13, 1 << 13); // left
144         JLO_B_PB(KeyType::SCREENSHOT,  1 << 14, 1 << 14); // right
145         JLO_B_PB(KeyType::FREEMOVE,    1 << 16, 1 << 16); // down
146
147         // Movement buttons, important for vessels
148         JLO_A_PB(KeyType::FORWARD,  1,  1, 1024);
149         JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
150         JLO_A_PB(KeyType::LEFT,     0,  1, 1024);
151         JLO_A_PB(KeyType::RIGHT,    0, -1, 1024);
152
153         return jlo;
154 }
155
156 JoystickController::JoystickController()
157 {
158         m_joystick_id = 0;
159
160         doubling_dtime = g_settings->getFloat("repeat_joystick_button_time");
161
162         for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
163                 m_past_pressed_time[i] = 0;
164         }
165         clear();
166 }
167
168 void JoystickController::onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos)
169 {
170         s32         id     = g_settings->getS32("joystick_id");
171         std::string layout = g_settings->get("joystick_type");
172
173         if (id < 0 || (u16)id >= joystick_infos.size()) {
174                 // TODO: auto detection
175                 id = 0;
176         }
177
178         if (id >= 0 && (u16)id < joystick_infos.size()) {
179                 if (layout.empty() || layout == "auto")
180                         setLayoutFromControllerName(joystick_infos[id].Name.c_str());
181                 else
182                         setLayoutFromControllerName(layout);
183         }
184
185         m_joystick_id = id;
186 }
187
188 void JoystickController::setLayoutFromControllerName(std::string name) {
189         if (lowercase(name).find("xbox") != std::string::npos) {
190                 m_layout = create_xbox_layout();
191         } else {
192                 m_layout = create_default_layout();
193         }
194 }
195
196 bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)
197 {
198         if (ev.Joystick != m_joystick_id)
199                 return false;
200
201         m_internal_time = getTimeMs() / 1000.f;
202
203         std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed;
204
205         // First generate a list of keys pressed
206
207         for (size_t i = 0; i < m_layout.button_keys.size(); i++) {
208                 if (m_layout.button_keys[i].isTriggered(ev)) {
209                         keys_pressed.set(m_layout.button_keys[i].key);
210                 }
211         }
212
213         for (size_t i = 0; i < m_layout.axis_keys.size(); i++) {
214                 if (m_layout.axis_keys[i].isTriggered(ev)) {
215                         keys_pressed.set(m_layout.axis_keys[i].key);
216                 }
217         }
218
219         // Then update the values
220
221         for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
222                 if (keys_pressed[i]) {
223                         if (!m_past_pressed_keys[i] &&
224                                         m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
225                                 m_past_pressed_keys[i] = true;
226                                 m_past_pressed_time[i] = m_internal_time;
227                         }
228                 } else if (m_pressed_keys[i]) {
229                         m_past_released_keys[i] = true;
230                 }
231
232                 m_pressed_keys[i] = keys_pressed[i];
233         }
234
235         for (size_t i = 0; i < JA_COUNT; i++) {
236                 const JoystickAxisLayout &ax_la = m_layout.axes[i];
237                 m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
238         }
239
240
241         return true;
242 }
243
244 void JoystickController::clear()
245 {
246         m_pressed_keys.reset();
247         m_past_pressed_keys.reset();
248         m_past_released_keys.reset();
249         memset(m_axes_vals, 0, sizeof(m_axes_vals));
250 }
251
252 s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
253 {
254         s16 v = m_axes_vals[axis];
255         if (((v > 0) && (v < m_layout.axes_dead_border)) ||
256                         ((v < 0) && (v > -m_layout.axes_dead_border)))
257                 return 0;
258         return v;
259 }