From bce0d458d8cda70c10d78ea9ec476474f0a6f01a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 2 Apr 2017 23:00:34 +0100 Subject: Add Joystick type detection and Xbox controller support * Add joystick type detection (with joystick_type setting to override it) * Fix multiple joysticks from interfering with each other by only reading from one (add joystick_id setting) * Add support for Xbox controllers --- src/client/joystick_controller.cpp | 104 ++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 12 deletions(-) (limited to 'src/client/joystick_controller.cpp') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index ef8d18ab0..311cd22fb 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "keys.h" #include "settings.h" #include "gettime.h" +#include "../util/string.h" bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const { @@ -42,7 +43,7 @@ bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const #define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C)) #define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D)) -static JoystickLayout create_default_layout() +JoystickLayout create_default_layout() { JoystickLayout jlo; @@ -103,11 +104,59 @@ static JoystickLayout create_default_layout() return jlo; } -static const JoystickLayout default_layout = create_default_layout(); +JoystickLayout create_xbox_layout() +{ + JoystickLayout jlo; + + jlo.axes_dead_border = 7000; + + const JoystickAxisLayout axes[JA_COUNT] = { + {0, 1}, // JA_SIDEWARD_MOVE + {1, 1}, // JA_FORWARD_MOVE + {2, 1}, // JA_FRUSTUM_HORIZONTAL + {3, 1}, // JA_FRUSTUM_VERTICAL + }; + memcpy(jlo.axes, axes, sizeof(jlo.axes)); + + // The back button means "ESC". + JLO_B_PB(KeyType::ESC, 1 << 8, 1 << 8); // back + JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // start + + // 4 Buttons + JLO_B_PB(KeyType::JUMP, 1 << 0, 1 << 0); // A/green + JLO_B_PB(KeyType::ESC, 1 << 1, 1 << 1); // B/red + JLO_B_PB(KeyType::SPECIAL1, 1 << 2, 1 << 2); // X/blue + JLO_B_PB(KeyType::INVENTORY, 1 << 3, 1 << 3); // Y/yellow + + // Analog Sticks + JLO_B_PB(KeyType::SPECIAL1, 1 << 11, 1 << 11); // left + JLO_B_PB(KeyType::SNEAK, 1 << 12, 1 << 12); // right + + // Triggers + JLO_B_PB(KeyType::MOUSE_L, 1 << 6, 1 << 6); // lt + JLO_B_PB(KeyType::MOUSE_R, 1 << 7, 1 << 7); // rt + JLO_B_PB(KeyType::SCROLL_UP, 1 << 4, 1 << 4); // lb + JLO_B_PB(KeyType::SCROLL_DOWN, 1 << 5, 1 << 5); // rb + + // D-PAD + JLO_B_PB(KeyType::ZOOM, 1 << 15, 1 << 15); // up + JLO_B_PB(KeyType::DROP, 1 << 13, 1 << 13); // left + JLO_B_PB(KeyType::SCREENSHOT, 1 << 14, 1 << 14); // right + JLO_B_PB(KeyType::FREEMOVE, 1 << 16, 1 << 16); // down + + // Movement buttons, important for vessels + JLO_A_PB(KeyType::FORWARD, 1, 1, 1024); + JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024); + JLO_A_PB(KeyType::LEFT, 0, 1, 1024); + JLO_A_PB(KeyType::RIGHT, 0, -1, 1024); + + return jlo; +} JoystickController::JoystickController() { - m_layout = &default_layout; + m_joystick_id = 0; + doubling_dtime = g_settings->getFloat("repeat_joystick_button_time"); for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { @@ -116,23 +165,54 @@ JoystickController::JoystickController() clear(); } +void JoystickController::onJoystickConnect(const std::vector &joystick_infos) +{ + s32 id = g_settings->getS32("joystick_id"); + std::string layout = g_settings->get("joystick_type"); + + if (id < 0 || id >= joystick_infos.size()) { + // TODO: auto detection + id = 0; + } + + if (id >= 0 && id < joystick_infos.size()) { + if (layout.empty() || layout == "auto") + setLayoutFromControllerName(joystick_infos[id].Name.c_str()); + else + setLayoutFromControllerName(layout); + } + + m_joystick_id = id; +} + +void JoystickController::setLayoutFromControllerName(std::string name) { + if (lowercase(name).find("xbox") >= 0) { + m_layout = create_xbox_layout(); + } else { + m_layout = create_default_layout(); + } +} + bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) { + if (ev.Joystick != m_joystick_id) + return false; + m_internal_time = getTimeMs() / 1000.f; std::bitset keys_pressed; // First generate a list of keys pressed - for (size_t i = 0; i < m_layout->button_keys.size(); i++) { - if (m_layout->button_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout->button_keys[i].key); + for (size_t i = 0; i < m_layout.button_keys.size(); i++) { + if (m_layout.button_keys[i].isTriggered(ev)) { + keys_pressed.set(m_layout.button_keys[i].key); } } - for (size_t i = 0; i < m_layout->axis_keys.size(); i++) { - if (m_layout->axis_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout->axis_keys[i].key); + for (size_t i = 0; i < m_layout.axis_keys.size(); i++) { + if (m_layout.axis_keys[i].isTriggered(ev)) { + keys_pressed.set(m_layout.axis_keys[i].key); } } @@ -153,7 +233,7 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) } for (size_t i = 0; i < JA_COUNT; i++) { - const JoystickAxisLayout &ax_la = m_layout->axes[i]; + const JoystickAxisLayout &ax_la = m_layout.axes[i]; m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id]; } @@ -172,8 +252,8 @@ void JoystickController::clear() s16 JoystickController::getAxisWithoutDead(JoystickAxis axis) { s16 v = m_axes_vals[axis]; - if (((v > 0) && (v < m_layout->axes_dead_border)) || - ((v < 0) && (v > -m_layout->axes_dead_border))) + if (((v > 0) && (v < m_layout.axes_dead_border)) || + ((v < 0) && (v > -m_layout.axes_dead_border))) return 0; return v; } -- cgit v1.2.3 From 271d7c31e69db0bb6c5cf2cfb6e97ba129045d3e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 7 Apr 2017 19:06:50 +0100 Subject: Fix signed/unsigned conversion warning There was no bug here (as I checked for negativeness), however it's good to get rid of warnings. --- src/client/joystick_controller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/client/joystick_controller.cpp') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index 311cd22fb..e6a572adb 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -170,12 +170,12 @@ void JoystickController::onJoystickConnect(const std::vector s32 id = g_settings->getS32("joystick_id"); std::string layout = g_settings->get("joystick_type"); - if (id < 0 || id >= joystick_infos.size()) { + if (id < 0 || (u16)id >= joystick_infos.size()) { // TODO: auto detection id = 0; } - if (id >= 0 && id < joystick_infos.size()) { + if (id >= 0 && (u16)id < joystick_infos.size()) { if (layout.empty() || layout == "auto") setLayoutFromControllerName(joystick_infos[id].Name.c_str()); else -- cgit v1.2.3 From eddf16eee9cb3627f4f62d37ea2942fc7546fae7 Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Tue, 18 Apr 2017 06:40:53 +0200 Subject: Fix always using the xbox layout (reported by coverity). --- src/client/joystick_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/client/joystick_controller.cpp') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index e6a572adb..3e1442793 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -186,7 +186,7 @@ void JoystickController::onJoystickConnect(const std::vector } void JoystickController::setLayoutFromControllerName(std::string name) { - if (lowercase(name).find("xbox") >= 0) { + if (lowercase(name).find("xbox") != std::string::npos) { m_layout = create_xbox_layout(); } else { m_layout = create_default_layout(); -- cgit v1.2.3 From f98bbe193e0093aca8d8957cec82fdbd28639915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Thu, 20 Apr 2017 00:12:52 +0200 Subject: Fix various copy instead of const ref reported by cppcheck (part 3) (#5616) * Also remove 2 non declared but defined functions * Make some functions around const ref changes const --- src/client/joystick_controller.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/client/joystick_controller.cpp') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index 3e1442793..cb9d64b9f 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -185,7 +185,8 @@ void JoystickController::onJoystickConnect(const std::vector m_joystick_id = id; } -void JoystickController::setLayoutFromControllerName(std::string name) { +void JoystickController::setLayoutFromControllerName(const std::string &name) +{ if (lowercase(name).find("xbox") != std::string::npos) { m_layout = create_xbox_layout(); } else { -- cgit v1.2.3 From b662a4577d692329b9ca83525e6039f2ddcd1ac1 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sun, 6 Mar 2016 14:31:16 -0500 Subject: Clean up getTime helpers This increases size of the getTime return values to 64 bits. It also removes the TimeGetter classes since the getTime functions are now very precise. --- src/client/joystick_controller.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/client/joystick_controller.cpp') diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index cb9d64b9f..905ca6420 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "keys.h" #include "settings.h" #include "gettime.h" +#include "porting.h" #include "../util/string.h" bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const @@ -199,7 +200,7 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) if (ev.Joystick != m_joystick_id) return false; - m_internal_time = getTimeMs() / 1000.f; + m_internal_time = porting::getTimeMs() / 1000.f; std::bitset keys_pressed; -- cgit v1.2.3