3 Copyright (C) 2016 est31, <MTest31@outlook.com>
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.
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.
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.
20 #include "joystick_controller.h"
21 #include "irrlichttypes_extrabloated.h"
26 #include "../util/string.h"
28 bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
30 u32 buttons = ev.ButtonStates;
32 buttons &= filter_mask;
33 return buttons == compare_mask;
36 bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
38 s16 ax_val = ev.Axis[axis_to_compare];
40 return (ax_val * direction < 0) && (thresh * direction > ax_val * direction);
43 // spares many characters
44 #define JLO_B_PB(A, B, C) jlo.button_keys.emplace_back(A, B, C)
45 #define JLO_A_PB(A, B, C, D) jlo.axis_keys.emplace_back(A, B, C, D)
47 JoystickLayout create_default_layout()
51 jlo.axes_dead_border = 1024;
53 const JoystickAxisLayout axes[JA_COUNT] = {
54 {0, 1}, // JA_SIDEWARD_MOVE
55 {1, 1}, // JA_FORWARD_MOVE
56 {3, 1}, // JA_FRUSTUM_HORIZONTAL
57 {4, 1}, // JA_FRUSTUM_VERTICAL
59 memcpy(jlo.axes, axes, sizeof(jlo.axes));
61 u32 sb = 1 << 7; // START button mask
62 u32 fb = 1 << 3; // FOUR button mask
63 u32 bm = sb | fb; // Mask for Both Modifiers
65 // The back button means "ESC".
66 JLO_B_PB(KeyType::ESC, 1 << 6, 1 << 6);
68 // The start button counts as modifier as well as use key.
69 // JLO_B_PB(KeyType::USE, sb, sb));
71 // Accessible without start modifier button pressed
72 // regardless whether four is pressed or not
73 JLO_B_PB(KeyType::SNEAK, sb | 1 << 2, 1 << 2);
75 // Accessible without four modifier button pressed
76 // regardless whether start is pressed or not
77 JLO_B_PB(KeyType::MOUSE_L, fb | 1 << 4, 1 << 4);
78 JLO_B_PB(KeyType::MOUSE_R, fb | 1 << 5, 1 << 5);
80 // Accessible without any modifier pressed
81 JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0);
82 JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1);
84 // Accessible with start button not pressed, but four pressed
85 // TODO find usage for button 0
86 JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1);
87 JLO_B_PB(KeyType::SCROLL_UP, bm | 1 << 4, fb | 1 << 4);
88 JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5);
90 // Accessible with start button and four pressed
91 // TODO find usage for buttons 0, 1 and 4, 5
93 // Now about the buttons simulated by the axes
95 // Movement buttons, important for vessels
96 JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
97 JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
98 JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
99 JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
102 JLO_A_PB(KeyType::SCROLL_UP, 2, -1, 1024);
103 JLO_A_PB(KeyType::SCROLL_DOWN, 5, -1, 1024);
108 JoystickLayout create_xbox_layout()
112 jlo.axes_dead_border = 7000;
114 const JoystickAxisLayout axes[JA_COUNT] = {
115 {0, 1}, // JA_SIDEWARD_MOVE
116 {1, 1}, // JA_FORWARD_MOVE
117 {2, 1}, // JA_FRUSTUM_HORIZONTAL
118 {3, 1}, // JA_FRUSTUM_VERTICAL
120 memcpy(jlo.axes, axes, sizeof(jlo.axes));
122 // The back button means "ESC".
123 JLO_B_PB(KeyType::ESC, 1 << 8, 1 << 8); // back
124 JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // start
127 JLO_B_PB(KeyType::JUMP, 1 << 0, 1 << 0); // A/green
128 JLO_B_PB(KeyType::ESC, 1 << 1, 1 << 1); // B/red
129 JLO_B_PB(KeyType::SPECIAL1, 1 << 2, 1 << 2); // X/blue
130 JLO_B_PB(KeyType::INVENTORY, 1 << 3, 1 << 3); // Y/yellow
133 JLO_B_PB(KeyType::SPECIAL1, 1 << 11, 1 << 11); // left
134 JLO_B_PB(KeyType::SNEAK, 1 << 12, 1 << 12); // right
137 JLO_B_PB(KeyType::MOUSE_L, 1 << 6, 1 << 6); // lt
138 JLO_B_PB(KeyType::MOUSE_R, 1 << 7, 1 << 7); // rt
139 JLO_B_PB(KeyType::SCROLL_UP, 1 << 4, 1 << 4); // lb
140 JLO_B_PB(KeyType::SCROLL_DOWN, 1 << 5, 1 << 5); // rb
143 JLO_B_PB(KeyType::ZOOM, 1 << 15, 1 << 15); // up
144 JLO_B_PB(KeyType::DROP, 1 << 13, 1 << 13); // left
145 JLO_B_PB(KeyType::SCREENSHOT, 1 << 14, 1 << 14); // right
146 JLO_B_PB(KeyType::FREEMOVE, 1 << 16, 1 << 16); // down
148 // Movement buttons, important for vessels
149 JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
150 JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
151 JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
152 JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
157 JoystickController::JoystickController() :
158 doubling_dtime(g_settings->getFloat("repeat_joystick_button_time"))
160 for (float &i : m_past_pressed_time) {
166 void JoystickController::onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos)
168 s32 id = g_settings->getS32("joystick_id");
169 std::string layout = g_settings->get("joystick_type");
171 if (id < 0 || (u16)id >= joystick_infos.size()) {
172 // TODO: auto detection
176 if (id >= 0 && (u16)id < joystick_infos.size()) {
177 if (layout.empty() || layout == "auto")
178 setLayoutFromControllerName(joystick_infos[id].Name.c_str());
180 setLayoutFromControllerName(layout);
186 void JoystickController::setLayoutFromControllerName(const std::string &name)
188 if (lowercase(name).find("xbox") != std::string::npos) {
189 m_layout = create_xbox_layout();
191 m_layout = create_default_layout();
195 bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)
197 if (ev.Joystick != m_joystick_id)
200 m_internal_time = porting::getTimeMs() / 1000.f;
202 std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed;
204 // First generate a list of keys pressed
206 for (const auto &button_key : m_layout.button_keys) {
207 if (button_key.isTriggered(ev)) {
208 keys_pressed.set(button_key.key);
212 for (const auto &axis_key : m_layout.axis_keys) {
213 if (axis_key.isTriggered(ev)) {
214 keys_pressed.set(axis_key.key);
218 // Then update the values
220 for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
221 if (keys_pressed[i]) {
222 if (!m_past_pressed_keys[i] &&
223 m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
224 m_past_pressed_keys[i] = true;
225 m_past_pressed_time[i] = m_internal_time;
227 } else if (m_pressed_keys[i]) {
228 m_past_released_keys[i] = true;
231 m_pressed_keys[i] = keys_pressed[i];
234 for (size_t i = 0; i < JA_COUNT; i++) {
235 const JoystickAxisLayout &ax_la = m_layout.axes[i];
236 m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
243 void JoystickController::clear()
245 m_pressed_keys.reset();
246 m_past_pressed_keys.reset();
247 m_past_released_keys.reset();
248 memset(m_axes_vals, 0, sizeof(m_axes_vals));
251 s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
253 s16 v = m_axes_vals[axis];
254 if (((v > 0) && (v < m_layout.axes_dead_border)) ||
255 ((v < 0) && (v > -m_layout.axes_dead_border)))