shithub: choc

Download patch

ref: 4890591ba50bfc8b11cda684b223cb7c551e6dd3
parent: 96ff3f7bc127889c9f47e4ec17faa445ade623f1
author: Simon Howard <[email protected]>
date: Wed Apr 30 18:56:04 EDT 2014

joystick: Add virtual-physical button mapping.

The solution to solving #386 is to add a layer of indirection: the
game code can only support up to ~20 joystick buttons, but this
doesn't matter as long as we never want to bind more than 20 buttons
to actions anyway. Redefine the game's notion of buttons to be based
on "virtual" joystick buttons, and map these buttons to physical
(SDL) buttons based on configuration file variables.

--- a/src/i_joystick.c
+++ b/src/i_joystick.c
@@ -37,6 +37,7 @@
 #include "i_system.h"
 
 #include "m_config.h"
+#include "m_misc.h"
 
 // When an axis is within the dead zone, it is set to zero.
 // This is 5% of the full range:
@@ -72,6 +73,12 @@
 static int joystick_strafe_axis = -1;
 static int joystick_strafe_invert = 0;
 
+// Virtual to physical button joystick button mapping. By default this
+// is a straight mapping.
+static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = {
+    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
 void I_ShutdownJoystick(void)
 {
     if (joystick != NULL)
@@ -159,32 +166,64 @@
     I_AtExit(I_ShutdownJoystick, true);
 }
 
-static int ButtonAxisMask(void)
+static boolean IsAxisButton(int physbutton)
 {
-    int result = 0;
-
     if (IS_BUTTON_AXIS(joystick_x_axis))
     {
-        result |= 1 << BUTTON_AXIS_NEG(joystick_x_axis);
-        result |= 1 << BUTTON_AXIS_POS(joystick_x_axis);
+        if (physbutton == BUTTON_AXIS_NEG(joystick_x_axis)
+         || physbutton == BUTTON_AXIS_POS(joystick_x_axis))
+        {
+            return true;
+        }
     }
     if (IS_BUTTON_AXIS(joystick_y_axis))
     {
-        result |= 1 << BUTTON_AXIS_NEG(joystick_y_axis);
-        result |= 1 << BUTTON_AXIS_POS(joystick_y_axis);
+        if (physbutton == BUTTON_AXIS_NEG(joystick_y_axis)
+         || physbutton == BUTTON_AXIS_POS(joystick_y_axis))
+        {
+            return true;
+        }
     }
     if (IS_BUTTON_AXIS(joystick_strafe_axis))
     {
-        result |= 1 << BUTTON_AXIS_NEG(joystick_strafe_axis);
-        result |= 1 << BUTTON_AXIS_POS(joystick_strafe_axis);
+        if (physbutton == BUTTON_AXIS_NEG(joystick_strafe_axis)
+         || physbutton == BUTTON_AXIS_POS(joystick_strafe_axis))
+        {
+            return true;
+        }
     }
 
-    return result;
+    return false;
 }
 
+// Get the state of the given virtual button.
+
+static int ReadButtonState(int vbutton)
+{
+    int physbutton;
+
+    // Map from virtual button to physical (SDL) button.
+    if (vbutton < NUM_VIRTUAL_BUTTONS)
+    {
+        physbutton = joystick_physical_buttons[vbutton];
+    }
+    else
+    {
+        physbutton = vbutton;
+    }
+
+    // Never read axis buttons as buttons.
+    if (IsAxisButton(physbutton))
+    {
+        return 0;
+    }
+
+    return SDL_JoystickGetButton(joystick, physbutton);
+}
+
 // Get a bitmask of all currently-pressed buttons
 
-static int GetButtonState(void)
+static int GetButtonsState(void)
 {
     int i;
     int result;
@@ -191,17 +230,14 @@
 
     result = 0;
 
-    for (i=0; i<SDL_JoystickNumButtons(joystick); ++i) 
+    for (i = 0; i < 20; ++i)
     {
-        if (SDL_JoystickGetButton(joystick, i))
+        if (ReadButtonState(i))
         {
             result |= 1 << i;
         }
     }
 
-    // Mask out any buttons that are actually used in axes.
-    result &= ~ButtonAxisMask();
-
     return result;
 }
 
@@ -287,7 +323,7 @@
         event_t ev;
 
         ev.type = ev_joystick;
-        ev.data1 = GetButtonState();
+        ev.data1 = GetButtonsState();
         ev.data2 = GetAxisState(joystick_x_axis, joystick_x_invert);
         ev.data3 = GetAxisState(joystick_y_axis, joystick_y_invert);
         ev.data4 = GetAxisState(joystick_strafe_axis, joystick_strafe_invert);
@@ -298,6 +334,8 @@
 
 void I_BindJoystickVariables(void)
 {
+    int i;
+
     M_BindVariable("use_joystick",          &usejoystick);
     M_BindVariable("joystick_index",        &joystick_index);
     M_BindVariable("joystick_x_axis",       &joystick_x_axis);
@@ -306,5 +344,12 @@
     M_BindVariable("joystick_x_invert",     &joystick_x_invert);
     M_BindVariable("joystick_y_invert",     &joystick_y_invert);
     M_BindVariable("joystick_strafe_invert",&joystick_strafe_invert);
+
+    for (i = 0; i < NUM_VIRTUAL_BUTTONS; ++i)
+    {
+        char name[32];
+        M_snprintf(name, sizeof(name), "joystick_physical_button%i", i);
+        M_BindVariable(name, &joystick_physical_buttons[i]);
+    }
 }
 
--- a/src/i_joystick.h
+++ b/src/i_joystick.h
@@ -27,6 +27,11 @@
 #ifndef __I_JOYSTICK__
 #define __I_JOYSTICK__
 
+// Number of "virtual" joystick buttons defined in configuration files.
+// This needs to be at least as large as the number of different key
+// bindings supported by the higher-level game code (joyb* variables).
+#define NUM_VIRTUAL_BUTTONS 10
+
 // If this bit is set in a configuration file axis value, the axis is
 // not actually a joystick axis, but instead is a "button axis". This
 // means that instead of reading an SDL joystick axis, we read the
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -939,6 +939,66 @@
     CONFIG_VARIABLE_INT(joystick_strafe_invert),
 
     //!
+    // The physical joystick button that corresponds to button #0.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button0),
+
+    //!
+    // The physical joystick button that corresponds to button #1.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button1),
+
+    //!
+    // The physical joystick button that corresponds to button #2.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button2),
+
+    //!
+    // The physical joystick button that corresponds to button #3.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button3),
+
+    //!
+    // The physical joystick button that corresponds to button #4.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button4),
+
+    //!
+    // The physical joystick button that corresponds to button #5.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button5),
+
+    //!
+    // The physical joystick button that corresponds to button #6.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button6),
+
+    //!
+    // The physical joystick button that corresponds to button #7.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button7),
+
+    //!
+    // The physical joystick button that corresponds to button #8.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button8),
+
+    //!
+    // The physical joystick button that corresponds to button #9.
+    //
+
+    CONFIG_VARIABLE_INT(joystick_physical_button9),
+
+    //!
     // Joystick button to strafe left.
     //
 
--- a/src/m_controls.h
+++ b/src/m_controls.h
@@ -157,6 +157,8 @@
 extern int joybprevweapon;
 extern int joybnextweapon;
 
+extern int joy_physical_buttons[10];
+
 extern int joybmenu;
 
 extern int dclick_use;
--- a/src/setup/joystick.c
+++ b/src/setup/joystick.c
@@ -26,6 +26,7 @@
 #include "i_joystick.h"
 #include "m_config.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "textscreen.h"
 
 #include "execute.h"
@@ -88,6 +89,11 @@
 static int joystick_strafe_axis = -1;
 static int joystick_strafe_invert = 0;
 
+// Virtual to physical mapping.
+static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = {
+    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
 static txt_button_t *joystick_button;
 
 static int *all_joystick_buttons[] = {
@@ -682,6 +688,8 @@
 
 void BindJoystickVariables(void)
 {
+    int i;
+
     M_BindVariable("use_joystick",          &usejoystick);
     M_BindVariable("joystick_index",        &joystick_index);
     M_BindVariable("joystick_x_axis",       &joystick_x_axis);
@@ -690,5 +698,12 @@
     M_BindVariable("joystick_x_invert",     &joystick_x_invert);
     M_BindVariable("joystick_y_invert",     &joystick_y_invert);
     M_BindVariable("joystick_strafe_invert",&joystick_strafe_invert);
+
+    for (i = 0; i < NUM_VIRTUAL_BUTTONS; ++i)
+    {
+        char name[32];
+        M_snprintf(name, sizeof(name), "joystick_physical_button%i", i);
+        M_BindVariable(name, &joystick_physical_buttons[i]);
+    }
 }