2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* Gamepad mapping generator */
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Gabriel Jacobo <gabomdq@gmail.com> */
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
2023-07-09 20:22:29 -06:00
|
|
|
|
2022-04-12 06:07:18 -06:00
|
|
|
#include "testutils.h"
|
2023-07-09 20:22:29 -06:00
|
|
|
#include "gamepadutils.h"
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
/* Define this for verbose output while mapping gamepads */
|
|
|
|
#define DEBUG_GAMEPADMAP
|
2019-11-21 11:09:26 -07:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
#define SCREEN_WIDTH 512
|
|
|
|
#define SCREEN_HEIGHT 320
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2016-12-27 02:39:07 -07:00
|
|
|
enum
|
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_GAMEPAD_BINDING_AXIS_LEFTX_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_LEFTX_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_LEFTY_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_LEFTY_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_RIGHTX_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_RIGHTX_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_RIGHTY_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_RIGHTY_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_TRIGGERLEFT,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_TRIGGERRIGHT,
|
|
|
|
SDL_GAMEPAD_BINDING_AXIS_MAX,
|
2016-12-27 02:39:07 -07:00
|
|
|
};
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
#define BINDING_COUNT (SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_MAX)
|
2016-12-15 15:27:22 -07:00
|
|
|
|
2021-03-26 14:01:06 -06:00
|
|
|
static int s_arrBindingOrder[] = {
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_GAMEPAD_BUTTON_A,
|
|
|
|
SDL_GAMEPAD_BUTTON_B,
|
|
|
|
SDL_GAMEPAD_BUTTON_Y,
|
|
|
|
SDL_GAMEPAD_BUTTON_X,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_LEFTX_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_LEFTX_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_LEFTY_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_LEFTY_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_LEFT_STICK,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_RIGHTX_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_RIGHTX_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_RIGHTY_NEGATIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_RIGHTY_POSITIVE,
|
|
|
|
SDL_GAMEPAD_BUTTON_RIGHT_STICK,
|
|
|
|
SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_TRIGGERLEFT,
|
|
|
|
SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
|
|
|
|
SDL_GAMEPAD_BUTTON_MAX + SDL_GAMEPAD_BINDING_AXIS_TRIGGERRIGHT,
|
|
|
|
SDL_GAMEPAD_BUTTON_DPAD_UP,
|
|
|
|
SDL_GAMEPAD_BUTTON_DPAD_RIGHT,
|
|
|
|
SDL_GAMEPAD_BUTTON_DPAD_DOWN,
|
|
|
|
SDL_GAMEPAD_BUTTON_DPAD_LEFT,
|
|
|
|
SDL_GAMEPAD_BUTTON_BACK,
|
|
|
|
SDL_GAMEPAD_BUTTON_GUIDE,
|
|
|
|
SDL_GAMEPAD_BUTTON_START,
|
|
|
|
SDL_GAMEPAD_BUTTON_MISC1,
|
|
|
|
SDL_GAMEPAD_BUTTON_PADDLE1,
|
|
|
|
SDL_GAMEPAD_BUTTON_PADDLE2,
|
|
|
|
SDL_GAMEPAD_BUTTON_PADDLE3,
|
|
|
|
SDL_GAMEPAD_BUTTON_PADDLE4,
|
|
|
|
SDL_GAMEPAD_BUTTON_TOUCHPAD,
|
2016-12-15 15:27:22 -07:00
|
|
|
};
|
2021-03-26 14:01:06 -06:00
|
|
|
SDL_COMPILE_TIME_ASSERT(s_arrBindingOrder, SDL_arraysize(s_arrBindingOrder) == BINDING_COUNT);
|
2016-12-15 15:27:22 -07:00
|
|
|
|
2016-12-27 02:39:07 -07:00
|
|
|
typedef struct
|
|
|
|
{
|
2022-12-27 10:46:24 -07:00
|
|
|
SDL_GamepadBindingType bindType;
|
2016-12-27 02:39:07 -07:00
|
|
|
union
|
|
|
|
{
|
|
|
|
int button;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
struct
|
|
|
|
{
|
2016-12-27 02:39:07 -07:00
|
|
|
int axis;
|
|
|
|
int axis_min;
|
|
|
|
int axis_max;
|
|
|
|
} axis;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
struct
|
|
|
|
{
|
2016-12-27 02:39:07 -07:00
|
|
|
int hat;
|
|
|
|
int hat_mask;
|
|
|
|
} hat;
|
|
|
|
|
|
|
|
} value;
|
|
|
|
|
2017-01-20 17:40:11 -07:00
|
|
|
SDL_bool committed;
|
|
|
|
|
2016-12-27 02:39:07 -07:00
|
|
|
} SDL_GameControllerExtendedBind;
|
|
|
|
|
|
|
|
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT];
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
SDL_bool m_bMoving;
|
2019-12-05 14:18:56 -07:00
|
|
|
int m_nLastValue;
|
2016-12-27 02:39:07 -07:00
|
|
|
int m_nStartingValue;
|
|
|
|
int m_nFarthestValue;
|
|
|
|
} AxisState;
|
|
|
|
|
|
|
|
static int s_nNumAxes;
|
|
|
|
static AxisState *s_arrAxisState;
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
static int s_iCurrentBinding;
|
2022-12-02 02:17:17 -07:00
|
|
|
static Uint64 s_unPendingAdvanceTime;
|
2016-12-15 15:27:22 -07:00
|
|
|
static SDL_bool s_bBindingComplete;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
static SDL_Window *window;
|
2021-08-03 02:25:54 -06:00
|
|
|
static SDL_Renderer *screen;
|
2023-07-09 20:22:29 -06:00
|
|
|
static GamepadImage *image;
|
2020-04-18 22:41:37 -06:00
|
|
|
static SDL_bool done = SDL_FALSE;
|
2021-11-29 10:00:26 -07:00
|
|
|
static SDL_bool bind_touchpad = SDL_FALSE;
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2016-12-27 02:39:07 -07:00
|
|
|
static int
|
|
|
|
StandardizeAxisValue(int nValue)
|
2016-12-15 15:27:22 -07:00
|
|
|
{
|
2022-11-30 13:51:59 -07:00
|
|
|
if (nValue > SDL_JOYSTICK_AXIS_MAX / 2) {
|
2016-12-27 02:39:07 -07:00
|
|
|
return SDL_JOYSTICK_AXIS_MAX;
|
2022-11-30 13:51:59 -07:00
|
|
|
} else if (nValue < SDL_JOYSTICK_AXIS_MIN / 2) {
|
2016-12-27 02:39:07 -07:00
|
|
|
return SDL_JOYSTICK_AXIS_MIN;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetCurrentBinding(int iBinding)
|
|
|
|
{
|
2023-07-09 20:22:29 -06:00
|
|
|
int iIndex, iElement;
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind *pBinding;
|
2023-07-09 20:22:29 -06:00
|
|
|
SDL_bool on_front;
|
2016-12-15 15:27:22 -07:00
|
|
|
|
|
|
|
if (iBinding < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iBinding == BINDING_COUNT) {
|
|
|
|
s_bBindingComplete = SDL_TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
if (s_arrBindingOrder[iBinding] == -1) {
|
2021-03-26 14:01:06 -06:00
|
|
|
SetCurrentBinding(iBinding + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (s_arrBindingOrder[iBinding] == SDL_GAMEPAD_BUTTON_TOUCHPAD &&
|
2022-11-30 13:51:59 -07:00
|
|
|
!bind_touchpad) {
|
2021-11-29 10:00:26 -07:00
|
|
|
SetCurrentBinding(iBinding + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
s_iCurrentBinding = iBinding;
|
|
|
|
|
|
|
|
pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
|
|
|
|
SDL_zerop(pBinding);
|
|
|
|
|
2017-01-04 00:39:28 -07:00
|
|
|
for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
|
|
|
|
s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
|
|
|
|
}
|
2016-12-27 02:39:07 -07:00
|
|
|
|
2023-07-09 20:22:29 -06:00
|
|
|
iElement = s_arrBindingOrder[iBinding];
|
|
|
|
on_front = SDL_TRUE;
|
|
|
|
if (iElement >= SDL_GAMEPAD_BUTTON_PADDLE1 &&
|
|
|
|
iElement <= SDL_GAMEPAD_BUTTON_PADDLE4) {
|
|
|
|
on_front = SDL_FALSE;
|
|
|
|
}
|
|
|
|
SetGamepadImageShowingFront(image, on_front);
|
|
|
|
|
|
|
|
ClearGamepadImage(image);
|
|
|
|
if (iElement < SDL_GAMEPAD_BUTTON_MAX) {
|
|
|
|
SDL_GamepadButton eButton = (SDL_GamepadButton)iElement;
|
|
|
|
SetGamepadImageButton(image, eButton, SDL_TRUE);
|
|
|
|
} else {
|
|
|
|
switch (iElement - SDL_GAMEPAD_BUTTON_MAX) {
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTX_NEGATIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_LEFTX, -1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTX_POSITIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_LEFTX, 1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTY_NEGATIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_LEFTY, -1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTY_POSITIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_LEFTY, 1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTX_NEGATIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_RIGHTX, -1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTX_POSITIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_RIGHTX, 1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTY_NEGATIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_RIGHTY, -1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTY_POSITIVE:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_RIGHTY, 1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_TRIGGERLEFT:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 1);
|
|
|
|
break;
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_TRIGGERRIGHT:
|
|
|
|
SetGamepadImageAxis(image, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
s_unPendingAdvanceTime = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-04 00:39:28 -07:00
|
|
|
static SDL_bool
|
|
|
|
BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
|
|
|
|
{
|
2022-11-27 09:38:43 -07:00
|
|
|
if (pBindingA->bindType != pBindingB->bindType) {
|
2017-01-04 00:39:28 -07:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
switch (pBindingA->bindType) {
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_AXIS:
|
2017-01-04 00:39:28 -07:00
|
|
|
if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
2017-01-20 17:40:11 -07:00
|
|
|
if (!pBindingA->committed) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
2017-01-04 00:39:28 -07:00
|
|
|
{
|
|
|
|
int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
|
|
|
|
int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
|
|
|
|
int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
|
|
|
|
int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
|
2022-11-27 09:38:43 -07:00
|
|
|
return minA <= minB && maxA >= maxB;
|
2017-01-04 00:39:28 -07:00
|
|
|
}
|
|
|
|
/* Not reached */
|
|
|
|
default:
|
|
|
|
return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
static void
|
2016-12-27 02:39:07 -07:00
|
|
|
ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
|
2016-12-15 15:27:22 -07:00
|
|
|
{
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind *pCurrent;
|
2016-12-15 15:27:22 -07:00
|
|
|
int iIndex;
|
|
|
|
int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
|
|
|
|
|
|
|
|
/* Do we already have this binding? */
|
|
|
|
for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
|
2017-01-04 00:39:28 -07:00
|
|
|
pCurrent = &s_arrBindings[iIndex];
|
|
|
|
if (BBindingContainsBinding(pCurrent, pBinding)) {
|
2022-12-27 10:46:24 -07:00
|
|
|
if (iIndex == SDL_GAMEPAD_BUTTON_A && iCurrentElement != SDL_GAMEPAD_BUTTON_B) {
|
2016-12-15 15:27:22 -07:00
|
|
|
/* Skip to the next binding */
|
|
|
|
SetCurrentBinding(s_iCurrentBinding + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (iIndex == SDL_GAMEPAD_BUTTON_B) {
|
2016-12-15 15:27:22 -07:00
|
|
|
/* Go back to the previous binding */
|
|
|
|
SetCurrentBinding(s_iCurrentBinding - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Already have this binding, ignore it */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
#ifdef DEBUG_GAMEPADMAP
|
2022-11-30 13:51:59 -07:00
|
|
|
switch (pBinding->bindType) {
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_NONE:
|
2022-11-30 13:51:59 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_BUTTON:
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Configuring button binding for button %d\n", pBinding->value.button);
|
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_AXIS:
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Configuring axis binding for axis %d %d/%d committed = %s\n", pBinding->value.axis.axis, pBinding->value.axis.axis_min, pBinding->value.axis.axis_max, pBinding->committed ? "true" : "false");
|
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_HAT:
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Log("Configuring hat binding for hat %d %d\n", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
|
|
|
|
break;
|
2017-01-20 17:40:11 -07:00
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
#endif /* DEBUG_GAMEPADMAP */
|
2017-01-20 17:40:11 -07:00
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
/* Should the new binding override the existing one? */
|
|
|
|
pCurrent = &s_arrBindings[iCurrentElement];
|
2022-12-27 10:46:24 -07:00
|
|
|
if (pCurrent->bindType != SDL_GAMEPAD_BINDTYPE_NONE) {
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_bool bNativeDPad, bCurrentDPad;
|
|
|
|
SDL_bool bNativeAxis, bCurrentAxis;
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
bNativeDPad = (iCurrentElement == SDL_GAMEPAD_BUTTON_DPAD_UP ||
|
|
|
|
iCurrentElement == SDL_GAMEPAD_BUTTON_DPAD_DOWN ||
|
|
|
|
iCurrentElement == SDL_GAMEPAD_BUTTON_DPAD_LEFT ||
|
|
|
|
iCurrentElement == SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
|
|
|
|
bCurrentDPad = (pCurrent->bindType == SDL_GAMEPAD_BINDTYPE_HAT);
|
2017-01-20 17:40:11 -07:00
|
|
|
if (bNativeDPad && bCurrentDPad) {
|
2016-12-15 15:27:22 -07:00
|
|
|
/* We already have a binding of the type we want, ignore the new one */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
bNativeAxis = (iCurrentElement >= SDL_GAMEPAD_BUTTON_MAX);
|
|
|
|
bCurrentAxis = (pCurrent->bindType == SDL_GAMEPAD_BINDTYPE_AXIS);
|
2017-01-04 00:39:28 -07:00
|
|
|
if (bNativeAxis == bCurrentAxis &&
|
2022-12-27 10:46:24 -07:00
|
|
|
(pBinding->bindType != SDL_GAMEPAD_BINDTYPE_AXIS ||
|
2017-01-04 00:39:28 -07:00
|
|
|
pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
|
2016-12-15 15:27:22 -07:00
|
|
|
/* We already have a binding of the type we want, ignore the new one */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pCurrent = *pBinding;
|
|
|
|
|
2017-01-20 17:40:11 -07:00
|
|
|
if (pBinding->committed) {
|
|
|
|
s_unPendingAdvanceTime = SDL_GetTicks();
|
|
|
|
} else {
|
|
|
|
s_unPendingAdvanceTime = 0;
|
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 02:39:07 -07:00
|
|
|
static SDL_bool
|
|
|
|
BMergeAxisBindings(int iIndex)
|
|
|
|
{
|
|
|
|
SDL_GameControllerExtendedBind *pBindingA = &s_arrBindings[iIndex];
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_GameControllerExtendedBind *pBindingB = &s_arrBindings[iIndex + 1];
|
2022-12-27 10:46:24 -07:00
|
|
|
if (pBindingA->bindType == SDL_GAMEPAD_BINDTYPE_AXIS &&
|
|
|
|
pBindingB->bindType == SDL_GAMEPAD_BINDTYPE_AXIS &&
|
2016-12-27 02:39:07 -07:00
|
|
|
pBindingA->value.axis.axis == pBindingB->value.axis.axis) {
|
|
|
|
if (pBindingA->value.axis.axis_min == pBindingB->value.axis.axis_min) {
|
|
|
|
pBindingA->value.axis.axis_min = pBindingA->value.axis.axis_max;
|
|
|
|
pBindingA->value.axis.axis_max = pBindingB->value.axis.axis_max;
|
2022-12-27 10:46:24 -07:00
|
|
|
pBindingB->bindType = SDL_GAMEPAD_BINDTYPE_NONE;
|
2016-12-27 02:39:07 -07:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
static void
|
2022-11-30 13:51:59 -07:00
|
|
|
WatchJoystick(SDL_Joystick *joystick)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
const char *name = NULL;
|
|
|
|
SDL_Event event;
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_JoystickID nJoystickID;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-07-09 20:22:29 -06:00
|
|
|
image = CreateGamepadImage(screen);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_RaiseWindow(window);
|
|
|
|
|
|
|
|
/* scale for platforms that don't give you the window size you asked for. */
|
2023-02-03 13:25:46 -07:00
|
|
|
SDL_SetRenderLogicalPresentation(screen, SCREEN_WIDTH, SCREEN_HEIGHT,
|
|
|
|
SDL_LOGICAL_PRESENTATION_LETTERBOX,
|
2023-02-03 14:23:02 -07:00
|
|
|
SDL_SCALEMODE_LINEAR);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* Print info about the joystick we are watching */
|
2022-12-27 06:50:46 -07:00
|
|
|
name = SDL_GetJoystickName(joystick);
|
|
|
|
SDL_Log("Watching joystick %" SDL_PRIs32 ": (%s)\n", SDL_GetJoystickInstanceID(joystick),
|
2022-10-09 23:31:14 -06:00
|
|
|
name ? name : "Unknown Joystick");
|
2022-12-05 13:47:48 -07:00
|
|
|
SDL_Log("Joystick has %d axes, %d hats, and %d buttons\n",
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_GetNumJoystickAxes(joystick), SDL_GetNumJoystickHats(joystick), SDL_GetNumJoystickButtons(joystick));
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Log("\n\n\
|
|
|
|
====================================================================================\n\
|
2022-12-27 10:46:24 -07:00
|
|
|
Press the buttons on your gamepad when indicated\n\
|
|
|
|
(Your gamepad may look different than the picture)\n\
|
2015-06-21 09:33:46 -06:00
|
|
|
If you want to correct a mistake, press backspace or the back button on your device\n\
|
|
|
|
To skip a button, press SPACE or click/touch the screen\n\
|
|
|
|
To exit, press ESC\n\
|
|
|
|
====================================================================================\n");
|
2016-12-15 15:27:22 -07:00
|
|
|
|
2022-12-27 06:50:46 -07:00
|
|
|
nJoystickID = SDL_GetJoystickInstanceID(joystick);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-12-27 06:50:46 -07:00
|
|
|
s_nNumAxes = SDL_GetNumJoystickAxes(joystick);
|
2017-03-04 15:05:47 -07:00
|
|
|
s_arrAxisState = (AxisState *)SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
|
2016-12-27 02:39:07 -07:00
|
|
|
|
2021-03-26 14:01:06 -06:00
|
|
|
/* Skip any spurious events at start */
|
|
|
|
while (SDL_PollEvent(&event) > 0) {
|
|
|
|
}
|
2020-01-17 12:06:02 -07:00
|
|
|
|
2023-07-09 20:22:29 -06:00
|
|
|
SetCurrentBinding(0);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Loop, getting joystick events! */
|
2016-12-15 15:27:22 -07:00
|
|
|
while (!done && !s_bBindingComplete) {
|
|
|
|
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(screen);
|
2023-07-09 20:22:29 -06:00
|
|
|
RenderGamepadImage(image);
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_RenderPresent(screen);
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
while (SDL_PollEvent(&event) > 0) {
|
|
|
|
switch (event.type) {
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_REMOVED:
|
2016-12-15 15:27:22 -07:00
|
|
|
if (event.jaxis.which == nJoystickID) {
|
|
|
|
done = SDL_TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_AXIS_MOTION:
|
2016-12-15 15:27:22 -07:00
|
|
|
if (event.jaxis.which == nJoystickID) {
|
2022-12-27 10:46:24 -07:00
|
|
|
const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 gamepad needed 96 */
|
2016-12-27 02:39:07 -07:00
|
|
|
AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
|
|
|
|
int nValue = event.jaxis.value;
|
|
|
|
int nCurrentDistance, nFarthestDistance;
|
|
|
|
if (!pAxisState->m_bMoving) {
|
2019-11-28 12:44:15 -07:00
|
|
|
Sint16 nInitialValue;
|
2022-12-27 06:50:46 -07:00
|
|
|
pAxisState->m_bMoving = SDL_GetJoystickAxisInitialState(joystick, event.jaxis.axis, &nInitialValue);
|
2020-03-13 14:05:40 -06:00
|
|
|
pAxisState->m_nLastValue = nValue;
|
2019-11-28 12:44:15 -07:00
|
|
|
pAxisState->m_nStartingValue = nInitialValue;
|
|
|
|
pAxisState->m_nFarthestValue = nInitialValue;
|
2019-12-05 14:18:56 -07:00
|
|
|
} else if (SDL_abs(nValue - pAxisState->m_nLastValue) <= MAX_ALLOWED_JITTER) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pAxisState->m_nLastValue = nValue;
|
2016-12-27 02:39:07 -07:00
|
|
|
}
|
|
|
|
nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
|
|
|
|
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
|
|
|
|
if (nCurrentDistance > nFarthestDistance) {
|
|
|
|
pAxisState->m_nFarthestValue = nValue;
|
2017-01-20 17:40:11 -07:00
|
|
|
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
|
2016-12-27 02:39:07 -07:00
|
|
|
}
|
2017-01-20 17:40:11 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
#ifdef DEBUG_GAMEPADMAP
|
2017-01-20 17:40:11 -07:00
|
|
|
SDL_Log("AXIS %d nValue %d nCurrentDistance %d nFarthestDistance %d\n", event.jaxis.axis, nValue, nCurrentDistance, nFarthestDistance);
|
|
|
|
#endif
|
|
|
|
if (nFarthestDistance >= 16000) {
|
|
|
|
/* If we've gone out far enough and started to come back, let's bind this axis */
|
|
|
|
SDL_bool bCommitBinding = (nCurrentDistance <= 10000) ? SDL_TRUE : SDL_FALSE;
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind binding;
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_zero(binding);
|
2022-12-27 10:46:24 -07:00
|
|
|
binding.bindType = SDL_GAMEPAD_BINDTYPE_AXIS;
|
2016-12-27 02:39:07 -07:00
|
|
|
binding.value.axis.axis = event.jaxis.axis;
|
|
|
|
binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
|
|
|
|
binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
|
2017-01-20 17:40:11 -07:00
|
|
|
binding.committed = bCommitBinding;
|
2016-12-15 15:27:22 -07:00
|
|
|
ConfigureBinding(&binding);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
break;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_HAT_MOTION:
|
2016-12-15 15:27:22 -07:00
|
|
|
if (event.jhat.which == nJoystickID) {
|
|
|
|
if (event.jhat.value != SDL_HAT_CENTERED) {
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind binding;
|
2017-01-20 17:40:11 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
#ifdef DEBUG_GAMEPADMAP
|
2017-01-20 17:40:11 -07:00
|
|
|
SDL_Log("HAT %d %d\n", event.jhat.hat, event.jhat.value);
|
|
|
|
#endif
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_zero(binding);
|
2022-12-27 10:46:24 -07:00
|
|
|
binding.bindType = SDL_GAMEPAD_BINDTYPE_HAT;
|
2016-12-15 15:27:22 -07:00
|
|
|
binding.value.hat.hat = event.jhat.hat;
|
|
|
|
binding.value.hat.hat_mask = event.jhat.value;
|
2017-01-20 17:40:11 -07:00
|
|
|
binding.committed = SDL_TRUE;
|
2016-12-15 15:27:22 -07:00
|
|
|
ConfigureBinding(&binding);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
break;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_JOYSTICK_BUTTON_UP:
|
2016-12-15 15:27:22 -07:00
|
|
|
if (event.jbutton.which == nJoystickID) {
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind binding;
|
2017-01-20 17:40:11 -07:00
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
#ifdef DEBUG_GAMEPADMAP
|
2017-01-20 17:40:11 -07:00
|
|
|
SDL_Log("BUTTON %d\n", event.jbutton.button);
|
|
|
|
#endif
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_zero(binding);
|
2022-12-27 10:46:24 -07:00
|
|
|
binding.bindType = SDL_GAMEPAD_BINDTYPE_BUTTON;
|
2016-12-15 15:27:22 -07:00
|
|
|
binding.value.button = event.jbutton.button;
|
2017-01-20 17:40:11 -07:00
|
|
|
binding.committed = SDL_TRUE;
|
2016-12-15 15:27:22 -07:00
|
|
|
ConfigureBinding(&binding);
|
|
|
|
}
|
|
|
|
break;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_FINGER_DOWN:
|
2023-01-29 20:06:08 -07:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
2016-12-15 15:27:22 -07:00
|
|
|
/* Skip this step */
|
|
|
|
SetCurrentBinding(s_iCurrentBinding + 1);
|
|
|
|
break;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_KEY_DOWN:
|
2016-12-15 15:27:22 -07:00
|
|
|
if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
|
|
|
|
SetCurrentBinding(s_iCurrentBinding - 1);
|
2015-06-21 09:33:46 -06:00
|
|
|
break;
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
if (event.key.keysym.sym == SDLK_SPACE) {
|
|
|
|
SetCurrentBinding(s_iCurrentBinding + 1);
|
2015-06-21 09:33:46 -06:00
|
|
|
break;
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 14:08:42 -07:00
|
|
|
if (event.key.keysym.sym != SDLK_ESCAPE) {
|
2015-06-21 09:33:46 -06:00
|
|
|
break;
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 15:38:12 -06:00
|
|
|
SDL_FALLTHROUGH;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_QUIT:
|
2016-12-15 15:27:22 -07:00
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_Delay(15);
|
|
|
|
|
2023-01-03 13:47:40 -07:00
|
|
|
/* Wait 30 ms for joystick events to stop coming in,
|
2022-12-27 10:46:24 -07:00
|
|
|
in case a gamepad sends multiple events for a single control (e.g. axis and button for trigger)
|
2016-12-15 15:27:22 -07:00
|
|
|
*/
|
2023-01-03 13:47:40 -07:00
|
|
|
if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 30) {
|
2016-12-15 15:27:22 -07:00
|
|
|
SetCurrentBinding(s_iCurrentBinding + 1);
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2016-12-15 15:27:22 -07:00
|
|
|
if (s_bBindingComplete) {
|
2022-08-27 19:55:55 -06:00
|
|
|
SDL_JoystickGUID guid;
|
|
|
|
Uint16 crc;
|
2016-12-15 15:27:22 -07:00
|
|
|
char mapping[1024];
|
|
|
|
char trimmed_name[128];
|
|
|
|
char *spot;
|
|
|
|
int iIndex;
|
|
|
|
char pszElement[12];
|
|
|
|
|
|
|
|
SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
|
|
|
|
while (SDL_isspace(trimmed_name[0])) {
|
|
|
|
SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
|
|
|
|
}
|
|
|
|
while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
|
|
|
|
trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
|
|
|
|
}
|
|
|
|
while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
|
|
|
|
SDL_memmove(spot, spot + 1, SDL_strlen(spot));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize mapping with GUID and name */
|
2022-12-27 06:50:46 -07:00
|
|
|
guid = SDL_GetJoystickGUID(joystick);
|
2022-08-27 19:55:55 -06:00
|
|
|
SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
|
|
|
|
if (crc) {
|
|
|
|
/* Clear the CRC from the GUID for the mapping */
|
|
|
|
guid.data[2] = 0;
|
|
|
|
guid.data[3] = 0;
|
|
|
|
}
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_GetJoystickGUIDString(guid, mapping, SDL_arraysize(mapping));
|
2016-12-15 15:27:22 -07:00
|
|
|
SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
|
2022-08-27 19:55:55 -06:00
|
|
|
if (crc) {
|
|
|
|
char crc_string[5];
|
|
|
|
|
|
|
|
SDL_strlcat(mapping, "crc:", SDL_arraysize(mapping));
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(crc_string, sizeof(crc_string), "%.4x", crc);
|
2022-08-27 19:55:55 -06:00
|
|
|
SDL_strlcat(mapping, crc_string, SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
|
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
|
|
|
|
for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
|
2022-12-27 10:46:24 -07:00
|
|
|
if (pBinding->bindType == SDL_GAMEPAD_BINDTYPE_NONE) {
|
2016-12-15 15:27:22 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-27 10:46:24 -07:00
|
|
|
if (iIndex < SDL_GAMEPAD_BUTTON_MAX) {
|
|
|
|
SDL_GamepadButton eButton = (SDL_GamepadButton)iIndex;
|
|
|
|
SDL_strlcat(mapping, SDL_GetGamepadStringForButton(eButton), SDL_arraysize(mapping));
|
2016-12-15 15:27:22 -07:00
|
|
|
} else {
|
2022-11-14 15:56:48 -07:00
|
|
|
const char *pszAxisName = NULL;
|
2022-12-27 10:46:24 -07:00
|
|
|
switch (iIndex - SDL_GAMEPAD_BUTTON_MAX) {
|
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTX_NEGATIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
if (!BMergeAxisBindings(iIndex)) {
|
|
|
|
SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTX);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTX_POSITIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTX);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTY_NEGATIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
if (!BMergeAxisBindings(iIndex)) {
|
|
|
|
SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTY);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_LEFTY_POSITIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFTY);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTX_NEGATIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
if (!BMergeAxisBindings(iIndex)) {
|
|
|
|
SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTX);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTX_POSITIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTX);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTY_NEGATIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
if (!BMergeAxisBindings(iIndex)) {
|
|
|
|
SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
|
|
|
|
}
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTY);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_RIGHTY_POSITIVE:
|
2016-12-27 02:39:07 -07:00
|
|
|
SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
|
2022-12-27 10:46:24 -07:00
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHTY);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_TRIGGERLEFT:
|
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_LEFT_TRIGGER);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDING_AXIS_TRIGGERRIGHT:
|
|
|
|
pszAxisName = SDL_GetGamepadStringForAxis(SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
|
2016-12-27 02:39:07 -07:00
|
|
|
break;
|
|
|
|
}
|
2022-11-14 15:56:48 -07:00
|
|
|
if (pszAxisName) {
|
|
|
|
SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
|
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
}
|
|
|
|
SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
|
|
|
|
|
|
|
|
pszElement[0] = '\0';
|
|
|
|
switch (pBinding->bindType) {
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_BUTTON:
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
|
2016-12-15 15:27:22 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_AXIS:
|
2016-12-27 02:39:07 -07:00
|
|
|
if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
|
|
|
|
/* The negative half axis */
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
|
2016-12-27 02:39:07 -07:00
|
|
|
} else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
|
|
|
|
/* The positive half axis */
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
|
2016-12-27 02:39:07 -07:00
|
|
|
} else {
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
|
2016-12-27 02:39:07 -07:00
|
|
|
if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
|
|
|
|
/* Invert the axis */
|
|
|
|
SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 15:27:22 -07:00
|
|
|
break;
|
2022-12-27 10:46:24 -07:00
|
|
|
case SDL_GAMEPAD_BINDTYPE_HAT:
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
|
2016-12-15 15:27:22 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SDL_assert(!"Unknown bind type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
|
|
|
|
SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
|
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Log("Mapping:\n\n%s\n\n", mapping);
|
|
|
|
}
|
2016-12-27 02:39:07 -07:00
|
|
|
|
|
|
|
SDL_free(s_arrAxisState);
|
|
|
|
s_arrAxisState = NULL;
|
2022-11-30 13:51:59 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_DestroyRenderer(screen);
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
static SDL_bool HasJoysticks(void)
|
2022-12-30 00:07:30 -07:00
|
|
|
{
|
|
|
|
int num_joysticks = 0;
|
|
|
|
SDL_JoystickID *joysticks;
|
|
|
|
|
|
|
|
joysticks = SDL_GetJoysticks(&num_joysticks);
|
|
|
|
if (joysticks) {
|
|
|
|
SDL_free(joysticks);
|
|
|
|
}
|
|
|
|
if (num_joysticks > 0) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
} else {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int i;
|
2022-12-27 19:10:06 -07:00
|
|
|
int num_joysticks = 0;
|
2022-12-30 00:07:30 -07:00
|
|
|
SDL_JoystickID *joysticks;
|
2022-09-07 02:58:42 -06:00
|
|
|
int joystick_index;
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_Joystick *joystick = NULL;
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonState *state;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-11-06 16:54:18 -07:00
|
|
|
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
|
|
|
if (state == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Enable standard application logging */
|
2015-11-25 13:39:28 -07:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (SDL_strcmp(argv[i], "--bind-touchpad") == 0) {
|
|
|
|
bind_touchpad = SDL_TRUE;
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
2023-07-06 13:35:22 -06:00
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
static const char *options[] = { "[--bind-touchpad]", NULL };
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
2023-07-09 21:42:00 -06:00
|
|
|
return 1;
|
2023-07-06 13:35:22 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2023-03-16 17:25:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Initialize SDL (Note: video is required to start event loop) */
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2023-07-09 21:42:00 -06:00
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
/* Create a window to display joystick axis position */
|
2023-03-05 15:44:38 -07:00
|
|
|
window = SDL_CreateWindow("Game Controller Map", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
2020-04-18 22:41:37 -06:00
|
|
|
if (window == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:26:49 -07:00
|
|
|
screen = SDL_CreateRenderer(window, NULL, 0);
|
2021-08-03 02:25:54 -06:00
|
|
|
if (screen == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-12-30 00:07:30 -07:00
|
|
|
while (!done && !HasJoysticks()) {
|
2020-04-18 22:41:37 -06:00
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&event) > 0) {
|
|
|
|
switch (event.type) {
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_KEY_DOWN:
|
2023-02-03 14:08:42 -07:00
|
|
|
if (event.key.keysym.sym != SDLK_ESCAPE) {
|
2020-04-18 22:41:37 -06:00
|
|
|
break;
|
|
|
|
}
|
Add and use `SDL_FALLTHROUGH` for fallthroughs
Case fallthrough warnings can be suppressed using the __fallthrough__
compiler attribute. Unfortunately, not all compilers have this
attribute, or even have __has_attribute to check if they have the
__fallthrough__ attribute. [[fallthrough]] is also available in C++17
and the next C2x, but not everyone uses C++17 or C2x.
So define the SDL_FALLTHROUGH macro to deal with those problems - if we
are using C++17 or C2x, it expands to [[fallthrough]]; else if the
compiler has __has_attribute and has the __fallthrough__ attribute, then
it expands to __attribute__((__fallthrough__)); else it expands to an
empty statement, with a /* fallthrough */ comment (it's a do {} while
(0) statement, because users of this macro need to use a semicolon,
because [[fallthrough]] and __attribute__((__fallthrough__)) require a
semicolon).
Clang before Clang 10 and GCC before GCC 7 have problems with using
__attribute__ as a sole statement and warn about a "declaration not
declaring anything", so fall back to using the /* fallthrough */ comment
if we are using those older compiler versions.
Applications using SDL are also free to use this macro (because it is
defined in begin_code.h).
All existing /* fallthrough */ comments have been replaced with this
macro. Some of them were unnecessary because they were the last case in
a switch; using SDL_FALLTHROUGH in those cases would result in a compile
error on compilers that support __fallthrough__, for having a
__attribute__((__fallthrough__)) statement that didn't immediately
precede a case label.
2021-09-27 15:38:12 -06:00
|
|
|
SDL_FALLTHROUGH;
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_QUIT:
|
2020-04-18 22:41:37 -06:00
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 02:25:54 -06:00
|
|
|
SDL_RenderPresent(screen);
|
2020-04-18 22:41:37 -06:00
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Print information about the joysticks */
|
2022-12-27 19:10:06 -07:00
|
|
|
joysticks = SDL_GetJoysticks(&num_joysticks);
|
|
|
|
if (joysticks) {
|
|
|
|
SDL_Log("There are %d joysticks attached\n", num_joysticks);
|
|
|
|
for (i = 0; joysticks[i]; ++i) {
|
|
|
|
SDL_JoystickID instance_id = joysticks[i];
|
|
|
|
|
|
|
|
name = SDL_GetJoystickInstanceName(instance_id);
|
|
|
|
SDL_Log("Joystick %" SDL_PRIu32 ": %s\n", instance_id, name ? name : "Unknown Joystick");
|
|
|
|
joystick = SDL_OpenJoystick(instance_id);
|
|
|
|
if (joystick == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenJoystick(%" SDL_PRIu32 ") failed: %s\n", instance_id,
|
|
|
|
SDL_GetError());
|
|
|
|
} else {
|
|
|
|
char guid[64];
|
|
|
|
SDL_GetJoystickGUIDString(SDL_GetJoystickGUID(joystick),
|
|
|
|
guid, sizeof(guid));
|
|
|
|
SDL_Log(" axes: %d\n", SDL_GetNumJoystickAxes(joystick));
|
|
|
|
SDL_Log(" hats: %d\n", SDL_GetNumJoystickHats(joystick));
|
|
|
|
SDL_Log(" buttons: %d\n", SDL_GetNumJoystickButtons(joystick));
|
|
|
|
SDL_Log("instance id: %" SDL_PRIu32 "\n", instance_id);
|
|
|
|
SDL_Log(" guid: %s\n", guid);
|
|
|
|
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick));
|
|
|
|
SDL_CloseJoystick(joystick);
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 02:58:42 -06:00
|
|
|
joystick_index = 0;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (argv[i] && *argv[i] != '-') {
|
|
|
|
joystick_index = SDL_atoi(argv[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 19:10:06 -07:00
|
|
|
if (joysticks && joystick_index < num_joysticks) {
|
|
|
|
joystick = SDL_OpenJoystick(joysticks[joystick_index]);
|
2022-12-30 00:07:30 -07:00
|
|
|
if (joystick == NULL) {
|
|
|
|
SDL_Log("Couldn't open joystick %d: %s\n", joystick_index, SDL_GetError());
|
|
|
|
}
|
2022-12-27 19:10:06 -07:00
|
|
|
}
|
2022-12-30 00:07:30 -07:00
|
|
|
if (joystick != NULL) {
|
2020-04-18 22:41:37 -06:00
|
|
|
WatchJoystick(joystick);
|
2022-12-27 06:50:46 -07:00
|
|
|
SDL_CloseJoystick(joystick);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2020-04-18 22:41:37 -06:00
|
|
|
|
2022-12-27 19:10:06 -07:00
|
|
|
SDL_free(joysticks);
|
|
|
|
|
2020-04-18 22:41:37 -06:00
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
return 0;
|
|
|
|
}
|