ref: 4127efb8f8e8bce379036fd1a0c7b993126bd474
parent: f2b2c071a21d679af93ca38db0a25df978c05895
author: Gabriel Ravier <[email protected]>
date: Sat Apr 11 19:29:49 EDT 2020
Backends/SDL2/Controller: Start improving error handling Signed-off-by: Gabriel Ravier <[email protected]>
--- a/src/Backends/SDL2/Controller.cpp
+++ b/src/Backends/SDL2/Controller.cpp
@@ -7,6 +7,7 @@
#include "SDL.h"
#include "../../WindowsWrapper.h"
+#include "../Misc.h"
#define DEADZONE 10000;
@@ -16,7 +17,11 @@
BOOL ControllerBackend_Init(void)
{
- SDL_InitSubSystem(SDL_INIT_JOYSTICK);
+ if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
+ {
+ Backend_PrintError("Couldn't initialise joystack SDL2 subsystem : %s", SDL_GetError());
+ return FALSE;
+ }
return TRUE;
}
@@ -41,7 +46,12 @@
// Handle directional inputs
const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
+ if (!joystick_x)
+ Backend_PrintError("Failed to get current state of X axis control on joystick : %s", SDL_GetError());
+
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
+ if (!joystick_y)
+ Backend_PrintError("Failed to get current state of Y axis control on joystick : %s", SDL_GetError());
status->bLeft = joystick_x < axis_neutrals[0] - DEADZONE;
status->bRight = joystick_x > axis_neutrals[0] + DEADZONE;
@@ -50,8 +60,16 @@
// Handle button inputs
int total_buttons = SDL_JoystickNumButtons(joystick);
+ if (total_buttons < 0)
+ Backend_PrintError("Failed to get number of buttons on joystick : %s", SDL_GetError());
+
int total_axes = SDL_JoystickNumAxes(joystick);
+ if (total_axes < 0)
+ Backend_PrintError("Failed to get number of general axis controls on joystick : %s", SDL_GetError());
+
int total_hats = SDL_JoystickNumHats(joystick);
+ if (total_hats < 0)
+ Backend_PrintError("Failed to get number of POV hats on joystick : %s", SDL_GetError());
unsigned int buttons_done = 0;
@@ -81,7 +99,7 @@
}
// Then the joystick hats
- for (int i = 0; i < total_axes; ++i)
+ for (int i = 0; i < total_hats; ++i)
{
Uint8 hat = SDL_JoystickGetHat(joystick, i);