foxygit / doom Log in
commit 2bc7ac72bed5f960cb25e31b497757aabe84eb6c
Author:     Michael Day <contact@michaelcday.com>
AuthorDate: Sun Jul 30 23:47:59 2023 -0400
Commit:     Turo Lamminen <turol@users.noreply.github.com>
CommitDate: Wed Aug 23 11:42:50 2023 +0300

    Allow gamepad button remapping
---
 src/setup/joystick.c      |  24 +++++--
 src/setup/txt_joybinput.c | 162 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 180 insertions(+), 6 deletions(-)

diff --git a/src/setup/joystick.c b/src/setup/joystick.c
index 86279925..72886d59 100644
--- a/src/setup/joystick.c
+++ b/src/setup/joystick.c
@@ -1025,15 +1025,30 @@ static void NoJoystick(void)
     SetJoystickButtonLabel();
 }

-static void CalibrateWindowClosed(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused))
+static void RefreshJoystickWindow(TXT_UNCAST_ARG(widget),
+                                  TXT_UNCAST_ARG(unused))
 {
+    ConfigJoystick(NULL, NULL);
+}
+
+static void CalibrateWindowClosed(TXT_UNCAST_ARG(widget),
+                                  TXT_UNCAST_ARG(joystick_window))
+{
+    TXT_CAST_ARG(txt_window_t, joystick_window);
     TXT_SDL_SetEventCallback(NULL, NULL);
     SetJoystickButtonLabel();
     CloseAllJoysticks();
+
+    // Refresh Joystick window to update button and axis widgets.
+    TXT_SignalConnect(joystick_window, "closed", RefreshJoystickWindow, NULL);
+    TXT_CloseWindow(joystick_window);
 }

-static void CalibrateJoystick(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused))
+static void CalibrateJoystick(TXT_UNCAST_ARG(widget),
+                              TXT_UNCAST_ARG(joystick_window))
 {
+    TXT_CAST_ARG(txt_window_t, joystick_window);
+
     // Try to open all available joysticks.  If none are opened successfully,
     // bomb out with an error.

@@ -1059,7 +1074,8 @@ static void CalibrateJoystick(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused))

     TXT_SDL_SetEventCallback(CalibrationEventCallback, NULL);

-    TXT_SignalConnect(calibration_window, "closed", CalibrateWindowClosed, NULL);
+    TXT_SignalConnect(calibration_window, "closed", CalibrateWindowClosed,
+                      joystick_window);

     // Start calibration
     usejoystick = 0;
@@ -1174,7 +1190,7 @@ void ConfigJoystick(TXT_UNCAST_ARG(widget), void *user_data)

     AddJoystickControl(window, "Toggle Automap", &joybautomap);

-    TXT_SignalConnect(joystick_button, "pressed", CalibrateJoystick, NULL);
+    TXT_SignalConnect(joystick_button, "pressed", CalibrateJoystick, window);
     TXT_SetWindowAction(window, TXT_HORIZ_CENTER, TestConfigAction());

     InitJoystick();
diff --git a/src/setup/txt_joybinput.c b/src/setup/txt_joybinput.c
index fc0c7c39..ca469731 100644
--- a/src/setup/txt_joybinput.c
+++ b/src/setup/txt_joybinput.c
@@ -17,6 +17,7 @@
 #include <string.h>

 #include "SDL_joystick.h"
+#include "SDL_gamecontroller.h"

 #include "doomkeys.h"
 #include "joystick.h"
@@ -176,6 +177,64 @@ static int EventCallback(SDL_Event *event, TXT_UNCAST_ARG(joystick_input))
     return 0;
 }

+static int EventCallbackGamepad(SDL_Event *event,
+                                TXT_UNCAST_ARG(joystick_input))
+{
+    TXT_CAST_ARG(txt_joystick_input_t, joystick_input);
+
+    // Got the joystick button press?
+
+    if (event->type == SDL_CONTROLLERBUTTONDOWN ||
+        event->type == SDL_CONTROLLERAXISMOTION)
+    {
+        int vbutton, physbutton, axis;
+
+        // Before changing anything, remap button configuration into
+        // canonical form, to avoid conflicts.
+        CanonicalizeButtons();
+
+        vbutton = VirtualButtonForVariable(joystick_input->variable);
+        axis = event->caxis.axis;
+
+        if (event->type == SDL_CONTROLLERAXISMOTION &&
+            (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
+             axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) &&
+            event->caxis.value > TRIGGER_THRESHOLD)
+        {
+            if (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT)
+            {
+                physbutton = GAMEPAD_BUTTON_TRIGGERLEFT;
+            }
+            else
+            {
+                physbutton = GAMEPAD_BUTTON_TRIGGERRIGHT;
+            }
+        }
+        else if (event->type == SDL_CONTROLLERBUTTONDOWN)
+        {
+            physbutton = event->cbutton.button;
+        }
+        else
+        {
+            return 0;
+        }
+
+        if (joystick_input->check_conflicts)
+        {
+            ClearVariablesUsingButton(physbutton);
+        }
+
+        // Set mapping.
+        *joystick_input->variable = vbutton;
+        joystick_physical_buttons[vbutton] = physbutton;
+
+        TXT_CloseWindow(joystick_input->prompt_window);
+        return 1;
+    }
+
+    return 0;
+}
+
 // When the prompt window is closed, disable the event callback function;
 // we are no longer interested in receiving notification of events.

@@ -189,6 +248,18 @@ static void PromptWindowClosed(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(joystick))
     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 }

+static void PromptWindowClosedGamepad(TXT_UNCAST_ARG(widget),
+                                      TXT_UNCAST_ARG(joystick))
+{
+    TXT_CAST_ARG(SDL_GameController, joystick);
+
+    SDL_GameControllerClose(joystick);
+    TXT_SDL_SetEventCallback(NULL, NULL);
+    SDL_JoystickEventState(SDL_DISABLE);
+    SDL_GameControllerEventState(SDL_DISABLE);
+    SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
+}
+
 static void OpenErrorWindow(void)
 {
     TXT_MessageBox(NULL, "Please configure a controller first!");
@@ -229,6 +300,43 @@ static void OpenPromptWindow(txt_joystick_input_t *joystick_input)
     SDL_JoystickEventState(SDL_ENABLE);
 }

+static void OpenPromptWindowGamepad(txt_joystick_input_t *joystick_input)
+{
+    txt_window_t *window;
+    SDL_GameController *gamepad;
+
+    // Silently update when the shift button is held down.
+
+    joystick_input->check_conflicts = !TXT_GetModifierState(TXT_MOD_SHIFT);
+
+    if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
+    {
+        return;
+    }
+
+    // Check the current joystick is valid
+
+    gamepad = SDL_GameControllerOpen(joystick_index);
+
+    if (gamepad == NULL)
+    {
+        OpenErrorWindow();
+        return;
+    }
+
+    // Open the prompt window
+
+    window = TXT_MessageBox(NULL, "Press the new button on the controller...");
+
+    TXT_SDL_SetEventCallback(EventCallbackGamepad, joystick_input);
+    TXT_SignalConnect(window, "closed", PromptWindowClosedGamepad, gamepad);
+    joystick_input->prompt_window = window;
+
+    // GameController events do not fire if Joystick events are disabled.
+    SDL_JoystickEventState(SDL_ENABLE);
+    SDL_GameControllerEventState(SDL_ENABLE);
+}
+
 static void TXT_JoystickInputSizeCalc(TXT_UNCAST_ARG(joystick_input))
 {
     TXT_CAST_ARG(txt_joystick_input_t, joystick_input);
@@ -284,7 +392,6 @@ static int TXT_JoystickInputKeyPress(TXT_UNCAST_ARG(joystick_input), int key)
     if (key == KEY_ENTER)
     {
         // Open a window to prompt for the new joystick press
-
         OpenPromptWindow(joystick_input);

         return 1;
@@ -298,6 +405,26 @@ static int TXT_JoystickInputKeyPress(TXT_UNCAST_ARG(joystick_input), int key)
     return 0;
 }

+static int TXT_GamepadInputKeyPress(TXT_UNCAST_ARG(joystick_input), int key)
+{
+    TXT_CAST_ARG(txt_joystick_input_t, joystick_input);
+
+    if (key == KEY_ENTER)
+    {
+        // Open a window to prompt for the new joystick press
+        OpenPromptWindowGamepad(joystick_input);
+
+        return 1;
+    }
+
+    if (key == KEY_BACKSPACE || key == KEY_DEL)
+    {
+        *joystick_input->variable = -1;
+    }
+
+    return 0;
+}
+
 static void TXT_JoystickInputMousePress(TXT_UNCAST_ARG(widget),
                                         int x, int y, int b)
 {
@@ -311,6 +438,19 @@ static void TXT_JoystickInputMousePress(TXT_UNCAST_ARG(widget),
     }
 }

+static void TXT_GamepadInputMousePress(TXT_UNCAST_ARG(widget), int x, int y,
+                                       int b)
+{
+    TXT_CAST_ARG(txt_joystick_input_t, widget);
+
+    // Clicking is like pressing enter
+
+    if (b == TXT_MOUSE_LEFT)
+    {
+        TXT_GamepadInputKeyPress(widget, KEY_ENTER);
+    }
+}
+
 txt_widget_class_t txt_joystick_input_class =
 {
     TXT_AlwaysSelectable,
@@ -322,13 +462,31 @@ txt_widget_class_t txt_joystick_input_class =
     NULL,
 };

+txt_widget_class_t txt_gamepad_input_class =
+{
+    TXT_AlwaysSelectable,
+    TXT_JoystickInputSizeCalc,
+    TXT_JoystickInputDrawer,
+    TXT_GamepadInputKeyPress,
+    TXT_JoystickInputDestructor,
+    TXT_GamepadInputMousePress,
+    NULL,
+};
+
 txt_joystick_input_t *TXT_NewJoystickInput(int *variable)
 {
     txt_joystick_input_t *joystick_input;

     joystick_input = malloc(sizeof(txt_joystick_input_t));

-    TXT_InitWidget(joystick_input, &txt_joystick_input_class);
+    if (use_gamepad)
+    {
+        TXT_InitWidget(joystick_input, &txt_gamepad_input_class);
+    }
+    else
+    {
+        TXT_InitWidget(joystick_input, &txt_joystick_input_class);
+    }
     joystick_input->variable = variable;

     return joystick_input;