SDL  2.0
testgamecontroller.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
+ Include dependency graph for testgamecontroller.c:

Go to the source code of this file.

Macros

#define SCREEN_WIDTH   512
 
#define SCREEN_HEIGHT   320
 

Functions

static void UpdateWindowTitle ()
 
static int FindController (SDL_JoystickID controller_id)
 
static void AddController (int device_index, SDL_bool verbose)
 
static void SetController (SDL_JoystickID controller)
 
static void DelController (SDL_JoystickID controller)
 
static SDL_TextureLoadTexture (SDL_Renderer *renderer, const char *file, SDL_bool transparent)
 
static Uint16 ConvertAxisToRumble (Sint16 axis)
 
void loop (void *arg)
 
int main (int argc, char *argv[])
 

Variables

struct {
   int   x
 
   int   y
 
button_positions []
 
struct {
   int   x
 
   int   y
 
   double   angle
 
axis_positions []
 
SDL_Windowwindow = NULL
 
SDL_Rendererscreen = NULL
 
SDL_bool retval = SDL_FALSE
 
SDL_bool done = SDL_FALSE
 
SDL_bool set_LED = SDL_FALSE
 
SDL_Texturebackground_front
 
SDL_Texturebackground_back
 
SDL_Texturebutton
 
SDL_Textureaxis
 
SDL_GameController * gamecontroller
 
SDL_GameController ** gamecontrollers
 
int num_controllers = 0
 

Macro Definition Documentation

◆ SCREEN_HEIGHT

#define SCREEN_HEIGHT   320

Definition at line 28 of file testgamecontroller.c.

◆ SCREEN_WIDTH

#define SCREEN_WIDTH   512

Definition at line 27 of file testgamecontroller.c.

Function Documentation

◆ AddController()

static void AddController ( int  device_index,
SDL_bool  verbose 
)
static

Definition at line 117 of file testgamecontroller.c.

118 {
119  SDL_JoystickID controller_id = SDL_JoystickGetDeviceInstanceID(device_index);
120  SDL_GameController *controller;
121  SDL_GameController **controllers;
122 
123  controller_id = SDL_JoystickGetDeviceInstanceID(device_index);
124  if (controller_id < 0) {
125  SDL_Log("Couldn't get controller ID: %s\n", SDL_GetError());
126  return;
127  }
128 
129  if (FindController(controller_id) >= 0) {
130  /* We already have this controller */
131  return;
132  }
133 
134  controller = SDL_GameControllerOpen(device_index);
135  if (!controller) {
136  SDL_Log("Couldn't open controller: %s\n", SDL_GetError());
137  return;
138  }
139 
140  controllers = (SDL_GameController **)SDL_realloc(gamecontrollers, (num_controllers + 1) * sizeof(*controllers));
141  if (!controllers) {
142  SDL_GameControllerClose(controller);
143  return;
144  }
145 
146  controllers[num_controllers++] = controller;
148  gamecontroller = controller;
149 
150  if (verbose) {
152  SDL_Log("Opened game controller %s\n", name);
153  }
154 
156  if (verbose) {
157  SDL_Log("Enabling accelerometer\n");
158  }
160  }
161 
163  if (verbose) {
164  SDL_Log("Enabling gyro\n");
165  }
167  }
168 
170 }
#define SDL_JoystickGetDeviceInstanceID
#define SDL_GameControllerHasSensor
#define SDL_GetError
#define SDL_GameControllerClose
#define SDL_GameControllerSetSensorEnabled
#define SDL_realloc
#define SDL_GameControllerName
#define SDL_GameControllerOpen
#define SDL_Log
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
GLuint const GLchar * name
@ SDL_SENSOR_GYRO
Definition: SDL_sensor.h:74
@ SDL_SENSOR_ACCEL
Definition: SDL_sensor.h:73
@ SDL_TRUE
Definition: SDL_stdinc.h:170
static int FindController(SDL_JoystickID controller_id)
SDL_GameController * gamecontroller
SDL_GameController ** gamecontrollers
int num_controllers
static void UpdateWindowTitle()

References sort_controllers::controllers, FindController(), gamecontroller, gamecontrollers, num_controllers, SDL_GameControllerClose, SDL_GameControllerHasSensor, SDL_GameControllerName, SDL_GameControllerOpen, SDL_GameControllerSetSensorEnabled, SDL_GetError, SDL_JoystickGetDeviceInstanceID, SDL_Log, SDL_realloc, SDL_SENSOR_ACCEL, SDL_SENSOR_GYRO, SDL_TRUE, and UpdateWindowTitle().

Referenced by loop(), and main().

◆ ConvertAxisToRumble()

static Uint16 ConvertAxisToRumble ( Sint16  axis)
static

Definition at line 237 of file testgamecontroller.c.

238 {
239  /* Only start rumbling if the axis is past the halfway point */
240  const Sint16 half_axis = (Sint16)SDL_ceil(SDL_JOYSTICK_AXIS_MAX / 2.0f);
241  if (axis > half_axis) {
242  return (Uint16)(axis - half_axis) * 4;
243  } else {
244  return 0;
245  }
246 }
#define SDL_ceil
#define SDL_JOYSTICK_AXIS_MAX
Definition: SDL_joystick.h:358
uint16_t Uint16
Definition: SDL_stdinc.h:197
int16_t Sint16
Definition: SDL_stdinc.h:191
SDL_Texture * axis

References axis, SDL_ceil, and SDL_JOYSTICK_AXIS_MAX.

Referenced by loop().

◆ DelController()

static void DelController ( SDL_JoystickID  controller)
static

Definition at line 186 of file testgamecontroller.c.

187 {
188  int i = FindController(controller);
189 
190  if (i < 0) {
191  return;
192  }
193 
195 
196  --num_controllers;
197  if (i < num_controllers) {
199  }
200 
201  if (num_controllers > 0) {
203  } else {
205  }
207 }
#define SDL_memcpy
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163

References FindController(), gamecontroller, gamecontrollers, i, NULL, num_controllers, SDL_GameControllerClose, SDL_memcpy, and UpdateWindowTitle().

Referenced by loop().

◆ FindController()

static int FindController ( SDL_JoystickID  controller_id)
static

Definition at line 105 of file testgamecontroller.c.

106 {
107  int i;
108 
109  for (i = 0; i < num_controllers; ++i) {
111  return i;
112  }
113  }
114  return -1;
115 }
#define SDL_GameControllerGetJoystick
#define SDL_JoystickInstanceID

References gamecontrollers, i, num_controllers, SDL_GameControllerGetJoystick, and SDL_JoystickInstanceID.

Referenced by AddController(), DelController(), and SetController().

◆ LoadTexture()

static SDL_Texture* LoadTexture ( SDL_Renderer renderer,
const char *  file,
SDL_bool  transparent 
)
static

Definition at line 210 of file testgamecontroller.c.

211 {
212  SDL_Surface *temp = NULL;
214 
215  temp = SDL_LoadBMP(file);
216  if (temp == NULL) {
217  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
218  } else {
219  /* Set transparent pixel as the pixel at (0,0) */
220  if (transparent) {
221  if (temp->format->BytesPerPixel == 1) {
222  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
223  }
224  }
225 
227  if (!texture) {
228  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
229  }
230  }
231  if (temp) {
232  SDL_FreeSurface(temp);
233  }
234  return texture;
235 }
#define SDL_SetColorKey
#define SDL_CreateTextureFromSurface
#define SDL_LogError
#define SDL_FreeSurface
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLenum GLenum GLuint texture
uint8_t Uint8
Definition: SDL_stdinc.h:185
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:203
Uint8 BytesPerPixel
Definition: SDL_pixels.h:329
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76
static SDL_Renderer * renderer

References SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, NULL, SDL_Surface::pixels, renderer, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, and SDL_TRUE.

Referenced by main().

◆ loop()

void loop ( void arg)

Definition at line 249 of file testgamecontroller.c.

250 {
252  int i;
253  SDL_bool showing_front = SDL_TRUE;
254 
255  while (SDL_PollEvent(&event)) {
256  switch (event.type) {
258  SDL_Log("Game controller device %d added.\n", (int) SDL_JoystickGetDeviceInstanceID(event.cdevice.which));
259  AddController(event.cdevice.which, SDL_TRUE);
260  break;
261 
263  SDL_Log("Game controller device %d removed.\n", (int) event.cdevice.which);
264  DelController(event.cdevice.which);
265  break;
266 
270  SDL_Log("Controller touchpad %d finger %d %s %.2f, %.2f, %.2f\n",
271  event.ctouchpad.touchpad,
272  event.ctouchpad.finger,
273  (event.type == SDL_CONTROLLERTOUCHPADDOWN ? "pressed at" :
274  (event.type == SDL_CONTROLLERTOUCHPADUP ? "released at" :
275  "moved to")),
276  event.ctouchpad.x,
277  event.ctouchpad.y,
278  event.ctouchpad.pressure);
279  break;
280 
282  SDL_Log("Controller sensor %s: %.2f, %.2f, %.2f\n",
283  event.csensor.sensor == SDL_SENSOR_ACCEL ? "accelerometer" :
284  event.csensor.sensor == SDL_SENSOR_GYRO ? "gyro" : "unknown",
285  event.csensor.data[0],
286  event.csensor.data[1],
287  event.csensor.data[2]);
288  break;
289 
291  if (event.caxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.caxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
292  SetController(event.caxis.which);
293  }
294  SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
295  break;
296 
299  if (event.type == SDL_CONTROLLERBUTTONDOWN) {
300  SetController(event.cbutton.which);
301  }
302  SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
303  break;
304 
305  case SDL_KEYDOWN:
306  if (event.key.keysym.sym != SDLK_ESCAPE) {
307  break;
308  }
309  /* Fall through to signal quit */
310  case SDL_QUIT:
311  done = SDL_TRUE;
312  break;
313  default:
314  break;
315  }
316  }
317 
318  if (gamecontroller) {
319  /* Show the back of the controller if the paddles are being held */
322  showing_front = SDL_FALSE;
323  break;
324  }
325  }
326  }
327 
328  /* blank screen, set up for drawing this frame. */
329  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
332 
333  if (gamecontroller) {
334  /* Update visual controller state */
335  for (i = 0; i < SDL_CONTROLLER_BUTTON_TOUCHPAD; ++i) {
337  SDL_bool on_front = (i < SDL_CONTROLLER_BUTTON_PADDLE1 || i > SDL_CONTROLLER_BUTTON_PADDLE4);
338  if (on_front == showing_front) {
339  const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
341  }
342  }
343  }
344 
345  if (showing_front) {
346  for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
347  const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
349  if (value < -deadzone) {
350  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
351  const double angle = axis_positions[i].angle;
353  } else if (value > deadzone) {
354  const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
355  const double angle = axis_positions[i].angle + 180.0;
357  }
358  }
359  }
360 
361  /* Update LED based on left thumbstick position */
362  {
365 
366  if (!set_LED) {
367  set_LED = (x < -8000 || x > 8000 || y > 8000);
368  }
369  if (set_LED) {
370  Uint8 r, g, b;
371 
372  if (x < 0) {
373  r = (Uint8)(((int)(~x) * 255) / 32767);
374  b = 0;
375  } else {
376  r = 0;
377  b = (Uint8)(((int)(x) * 255) / 32767);
378  }
379  if (y > 0) {
380  g = (Uint8)(((int)(y) * 255) / 32767);
381  } else {
382  g = 0;
383  }
384 
386  }
387  }
388 
389  /* Update rumble based on trigger state */
390  {
393  Uint16 low_frequency_rumble = ConvertAxisToRumble(left);
394  Uint16 high_frequency_rumble = ConvertAxisToRumble(right);
395  SDL_GameControllerRumble(gamecontroller, low_frequency_rumble, high_frequency_rumble, 250);
396  }
397 
398  /* Update trigger rumble based on thumbstick state */
399  {
402  Uint16 left_rumble = ConvertAxisToRumble(~left);
403  Uint16 right_rumble = ConvertAxisToRumble(~right);
404 
405  SDL_GameControllerRumbleTriggers(gamecontroller, left_rumble, right_rumble, 250);
406  }
407  }
408 
410 
411 #ifdef __EMSCRIPTEN__
412  if (done) {
413  emscripten_cancel_main_loop();
414  }
415 #endif
416 }
#define SDL_RenderPresent
#define SDL_PollEvent
#define SDL_SetRenderDrawColor
#define SDL_GameControllerRumble
#define SDL_RenderCopyEx
#define SDL_GameControllerGetStringForButton
#define SDL_RenderClear
#define SDL_GameControllerSetLED
#define SDL_GameControllerRumbleTriggers
#define SDL_RenderCopy
#define SDL_GameControllerGetStringForAxis
#define SDL_GameControllerGetButton
#define SDL_GameControllerGetAxis
@ SDL_CONTROLLERTOUCHPADDOWN
Definition: SDL_events.h:128
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_CONTROLLERTOUCHPADMOTION
Definition: SDL_events.h:129
@ SDL_CONTROLLERBUTTONUP
Definition: SDL_events.h:124
@ SDL_CONTROLLERDEVICEADDED
Definition: SDL_events.h:125
@ SDL_CONTROLLERBUTTONDOWN
Definition: SDL_events.h:123
@ SDL_CONTROLLERAXISMOTION
Definition: SDL_events.h:122
@ SDL_KEYDOWN
Definition: SDL_events.h:98
@ SDL_CONTROLLERDEVICEREMOVED
Definition: SDL_events.h:126
@ SDL_CONTROLLERTOUCHPADUP
Definition: SDL_events.h:130
@ SDL_CONTROLLERSENSORUPDATE
Definition: SDL_events.h:131
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_GameControllerAxis
@ SDL_CONTROLLER_AXIS_LEFTX
@ SDL_CONTROLLER_AXIS_TRIGGERRIGHT
@ SDL_CONTROLLER_AXIS_RIGHTY
@ SDL_CONTROLLER_AXIS_MAX
@ SDL_CONTROLLER_AXIS_TRIGGERLEFT
@ SDL_CONTROLLER_AXIS_LEFTY
SDL_GameControllerButton
@ SDL_CONTROLLER_BUTTON_PADDLE1
@ SDL_CONTROLLER_BUTTON_TOUCHPAD
@ SDL_CONTROLLER_BUTTON_PADDLE4
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLboolean GLboolean GLboolean b
struct _cl_event * event
GLint left
GLenum GLenum dst
GLboolean GLboolean g
GLfloat angle
GLsizei const GLfloat * value
GLdouble GLdouble right
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
@ SDL_FLIP_NONE
Definition: SDL_render.h:123
SDL_bool
Definition: SDL_stdinc.h:168
@ SDL_FALSE
Definition: SDL_stdinc.h:169
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
SDL_Texture * button
int y
static const struct @466 button_positions[]
SDL_Texture * background_front
SDL_Renderer * screen
static Uint16 ConvertAxisToRumble(Sint16 axis)
SDL_bool set_LED
int x
SDL_Texture * background_back
static void DelController(SDL_JoystickID controller)
static const struct @467 axis_positions[]
SDL_bool done
static void SetController(SDL_JoystickID controller)
static void AddController(int device_index, SDL_bool verbose)
General event structure.
Definition: SDL_events.h:592

References AddController(), axis, axis_positions, background_back, background_front, button, button_positions, ConvertAxisToRumble(), DelController(), done, gamecontroller, i, NULL, screen, SDL_ALPHA_OPAQUE, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY, SDL_CONTROLLER_AXIS_MAX, SDL_CONTROLLER_AXIS_RIGHTY, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_CONTROLLER_BUTTON_PADDLE1, SDL_CONTROLLER_BUTTON_PADDLE4, SDL_CONTROLLER_BUTTON_TOUCHPAD, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERSENSORUPDATE, SDL_CONTROLLERTOUCHPADDOWN, SDL_CONTROLLERTOUCHPADMOTION, SDL_CONTROLLERTOUCHPADUP, SDL_FALSE, SDL_FLIP_NONE, SDL_GameControllerGetAxis, SDL_GameControllerGetButton, SDL_GameControllerGetStringForAxis, SDL_GameControllerGetStringForButton, SDL_GameControllerRumble, SDL_GameControllerRumbleTriggers, SDL_GameControllerSetLED, SDL_JOYSTICK_AXIS_MAX, SDL_JoystickGetDeviceInstanceID, SDL_KEYDOWN, SDL_Log, SDL_PollEvent, SDL_PRESSED, SDL_QUIT, SDL_RenderClear, SDL_RenderCopy, SDL_RenderCopyEx, SDL_RenderPresent, SDL_SENSOR_ACCEL, SDL_SENSOR_GYRO, SDL_SetRenderDrawColor, SDL_TRUE, SDLK_ESCAPE, set_LED, SetController(), x, and y.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 419 of file testgamecontroller.c.

420 {
421  int i;
422  int controller_count = 0;
423  int controller_index = 0;
424  char guid[64];
425 
429 
430  /* Enable standard application logging */
432 
433  /* Initialize SDL (Note: video is required to start event loop) */
435  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
436  return 1;
437  }
438 
439  SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
440 
441  /* Print information about the mappings */
442  if (argv[1] && SDL_strcmp(argv[1], "--mappings") == 0) {
443  SDL_Log("Supported mappings:\n");
444  for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
446  if (mapping) {
447  SDL_Log("\t%s\n", mapping);
448  SDL_free(mapping);
449  }
450  }
451  SDL_Log("\n");
452  }
453 
454  /* Print information about the controller */
455  for (i = 0; i < SDL_NumJoysticks(); ++i) {
456  const char *name;
457  const char *description;
458 
460  guid, sizeof (guid));
461 
462  if (SDL_IsGameController(i)) {
463  controller_count++;
467  description = "XBox 360 Controller";
468  break;
470  description = "XBox One Controller";
471  break;
473  description = "PS3 Controller";
474  break;
476  description = "PS4 Controller";
477  break;
479  description = "Nintendo Switch Pro Controller";
480  break;
482  description = "Virtual Game Controller";
483  break;
484  default:
485  description = "Game Controller";
486  break;
487  }
489  } else {
491  description = "Joystick";
492  }
493  SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n",
494  description, i, name ? name : "Unknown", guid,
496  }
497  SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", controller_count, SDL_NumJoysticks());
498 
499  /* Create a window to display controller state */
500  window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
502  SCREEN_HEIGHT, 0);
503  if (window == NULL) {
504  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
505  return 2;
506  }
507 
508  screen = SDL_CreateRenderer(window, -1, 0);
509  if (screen == NULL) {
510  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
512  return 2;
513  }
514 
515  SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
518 
519  /* scale for platforms that don't give you the window size you asked for. */
521 
522  background_front = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
523  background_back = LoadTexture(screen, "controllermap_back.bmp", SDL_FALSE);
524  button = LoadTexture(screen, "button.bmp", SDL_TRUE);
525  axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
526 
527  if (!background_front || !background_back || !button || !axis) {
530  return 2;
531  }
532  SDL_SetTextureColorMod(button, 10, 255, 21);
533  SDL_SetTextureColorMod(axis, 10, 255, 21);
534 
535  /* !!! FIXME: */
536  /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
537 
538  if (argv[1] && *argv[1] != '-') {
539  controller_index = SDL_atoi(argv[1]);
540  }
541  if (controller_index < num_controllers) {
542  gamecontroller = gamecontrollers[controller_index];
543  } else {
545  }
547 
548  /* Loop, getting controller events! */
549 #ifdef __EMSCRIPTEN__
550  emscripten_set_main_loop_arg(loop, NULL, 0, 1);
551 #else
552  while (!done) {
553  loop(NULL);
554  }
555 #endif
556 
560 
561  return 0;
562 }
#define SDL_INIT_GAMECONTROLLER
Definition: SDL.h:85
#define SDL_INIT_JOYSTICK
Definition: SDL.h:83
#define SDL_INIT_VIDEO
Definition: SDL.h:82
#define SDL_GameControllerNumMappings
#define SDL_DestroyWindow
#define SDL_JoystickGetDeviceProduct
#define SDL_DestroyRenderer
#define SDL_JoystickGetDeviceVendor
#define SDL_JoystickGetDevicePlayerIndex
#define SDL_CreateWindow
#define SDL_JoystickNameForIndex
#define SDL_RenderSetLogicalSize
#define SDL_JoystickGetGUIDString
#define SDL_NumJoysticks
#define SDL_GameControllerTypeForIndex
#define SDL_CreateRenderer
#define SDL_GameControllerNameForIndex
#define SDL_JoystickGetDeviceGUID
#define SDL_LogSetPriority
#define SDL_free
#define SDL_strcmp
#define SDL_Init
#define SDL_atoi
#define SDL_SetTextureColorMod
#define SDL_GameControllerMappingForIndex
#define SDL_QuitSubSystem
#define SDL_SetHint
#define SDL_IsGameController
#define SDL_GameControllerAddMappingsFromFile(file)
@ SDL_CONTROLLER_TYPE_VIRTUAL
@ SDL_CONTROLLER_TYPE_XBOX360
@ SDL_CONTROLLER_TYPE_PS4
@ SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO
@ SDL_CONTROLLER_TYPE_PS3
@ SDL_CONTROLLER_TYPE_XBOXONE
#define SDL_HINT_ACCELEROMETER_AS_JOYSTICK
A variable controlling whether the Android / iOS built-in accelerometer should be listed as a joystic...
Definition: SDL_hints.h:454
#define SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE
A variable controlling whether extended input reports should be used for PS4 controllers when using t...
Definition: SDL_hints.h:631
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
Definition: SDL_hints.h:583
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
GLenum GLenum GLenum GLenum mapping
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SCREEN_WIDTH
static SDL_Texture * LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
#define SCREEN_HEIGHT
void loop(void *arg)

References AddController(), axis, background_back, background_front, button, done, gamecontroller, gamecontrollers, i, LoadTexture(), loop(), NULL, num_controllers, screen, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_ALPHA_OPAQUE, SDL_atoi, SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO, SDL_CONTROLLER_TYPE_PS3, SDL_CONTROLLER_TYPE_PS4, SDL_CONTROLLER_TYPE_VIRTUAL, SDL_CONTROLLER_TYPE_XBOX360, SDL_CONTROLLER_TYPE_XBOXONE, SDL_CreateRenderer, SDL_CreateWindow, SDL_DestroyRenderer, SDL_DestroyWindow, SDL_FALSE, SDL_free, SDL_GameControllerAddMappingsFromFile, SDL_GameControllerMappingForIndex, SDL_GameControllerNameForIndex, SDL_GameControllerNumMappings, SDL_GameControllerTypeForIndex, SDL_GetError, SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_Init, SDL_INIT_GAMECONTROLLER, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_IsGameController, SDL_JoystickGetDeviceGUID, SDL_JoystickGetDevicePlayerIndex, SDL_JoystickGetDeviceProduct, SDL_JoystickGetDeviceVendor, SDL_JoystickGetGUIDString, SDL_JoystickNameForIndex, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_NumJoysticks, SDL_QuitSubSystem, SDL_RenderClear, SDL_RenderPresent, SDL_RenderSetLogicalSize, SDL_SetHint, SDL_SetRenderDrawColor, SDL_SetTextureColorMod, SDL_strcmp, SDL_TRUE, SDL_WINDOWPOS_CENTERED, and UpdateWindowTitle().

◆ SetController()

static void SetController ( SDL_JoystickID  controller)
static

Definition at line 172 of file testgamecontroller.c.

173 {
174  int i = FindController(controller);
175 
176  if (i < 0) {
177  return;
178  }
179 
180  if (gamecontroller != gamecontrollers[i]) {
183  }
184 }

References FindController(), gamecontroller, gamecontrollers, i, and UpdateWindowTitle().

Referenced by loop().

◆ UpdateWindowTitle()

static void UpdateWindowTitle ( )
static

Definition at line 74 of file testgamecontroller.c.

75 {
76  if (!window) {
77  return;
78  }
79 
80  if (gamecontroller) {
82  const char *serial = SDL_GameControllerGetSerial(gamecontroller);
83  const char *basetitle = "Game Controller Test: ";
84  const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + (serial ? 3 + SDL_strlen(serial) : 0) + 1;
85  char *title = (char *)SDL_malloc(titlelen);
86 
87  retval = SDL_FALSE;
88  done = SDL_FALSE;
89 
90  if (title) {
91  SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
92  if (serial) {
93  SDL_strlcat(title, " (", titlelen);
94  SDL_strlcat(title, serial, titlelen);
95  SDL_strlcat(title, ")", titlelen);
96  }
97  SDL_SetWindowTitle(window, title);
98  SDL_free(title);
99  }
100  } else {
101  SDL_SetWindowTitle(window, "Waiting for controller...");
102  }
103 }
#define SDL_GameControllerGetSerial
#define SDL_strlcat
#define SDL_malloc
#define SDL_strlen
#define SDL_snprintf
#define SDL_SetWindowTitle
SDL_bool retval

References done, gamecontroller, retval, SDL_FALSE, SDL_free, SDL_GameControllerGetSerial, SDL_GameControllerName, SDL_malloc, SDL_SetWindowTitle, SDL_snprintf, SDL_strlcat, and SDL_strlen.

Referenced by AddController(), DelController(), main(), and SetController().

Variable Documentation

◆ angle

double angle

Definition at line 55 of file testgamecontroller.c.

◆ axis

◆ 

const { ... } axis_positions[]
Initial value:
= {
{74, 153, 270.0},
{74, 153, 0.0},
{306, 231, 270.0},
{306, 231, 0.0},
{91, -20, 0.0},
{375, -20, 0.0},
}

Referenced by loop().

◆ background_back

SDL_Texture * background_back

Definition at line 69 of file testgamecontroller.c.

Referenced by loop(), main(), and WatchJoystick().

◆ background_front

SDL_Texture* background_front

Definition at line 69 of file testgamecontroller.c.

Referenced by loop(), main(), and WatchJoystick().

◆ button

◆ 

const { ... } button_positions[]
Initial value:
= {
{387, 167},
{431, 132},
{342, 132},
{389, 101},
{174, 132},
{232, 128},
{289, 132},
{75, 154},
{305, 230},
{77, 40},
{396, 36},
{154, 188},
{154, 249},
{116, 217},
{186, 217},
{232, 174},
{132, 135},
{330, 135},
{132, 175},
{330, 175},
}

Referenced by loop().

◆ done

Definition at line 67 of file testgamecontroller.c.

Referenced by loop(), main(), and UpdateWindowTitle().

◆ gamecontroller

SDL_GameController* gamecontroller

◆ gamecontrollers

SDL_GameController** gamecontrollers

◆ num_controllers

int num_controllers = 0

Definition at line 72 of file testgamecontroller.c.

Referenced by AddController(), DelController(), FindController(), and main().

◆ retval

SDL_bool retval = SDL_FALSE

Definition at line 66 of file testgamecontroller.c.

Referenced by add_audio_device(), AllocateRenderCommand(), build_locales_from_csv_string(), FcitxCreateInputContext(), FlushRenderCommands(), get_sdlapi_entry(), IMA_ADPCM_DecodeBlockData(), main(), PrepQueueCmdDraw(), QueueCmdCopy(), QueueCmdCopyEx(), QueueCmdDrawLines(), QueueCmdDrawPoints(), QueueCmdFillRects(), QueueCmdSetClipRect(), QueueCmdSetDrawColor(), QueueCmdSetViewport(), render(), RenderDrawLinesWithRects(), RenderDrawLinesWithRectsF(), RenderDrawPointsWithRects(), RenderDrawPointsWithRectsF(), SDL_AtomicCAS(), SDL_AtomicCASPtr(), SDL_BuildAudioTypeCVTFromFloat(), SDL_BuildAudioTypeCVTToFloat(), SDL_CondBroadcast(), SDL_CondSignal(), SDL_CondWaitTimeout(), SDL_GetAudioDeviceName(), SDL_GetNumAudioDevices(), SDL_GetPowerInfo(), SDL_GetQueuedAudioSize(), SDL_GL_LoadLibrary(), SDL_GL_MakeCurrent(), SDL_GL_SetAttribute(), SDL_JoystickGetBall(), SDL_NewAudioStream(), SDL_RenderClear(), SDL_RenderCopyExF(), SDL_RenderCopyF(), SDL_RenderDrawLines(), SDL_RenderDrawLinesF(), SDL_RenderDrawPoints(), SDL_RenderDrawPointsF(), SDL_RenderFillRects(), SDL_RenderFillRectsF(), SDL_RenderSetClipRect(), SDL_RenderSetViewport(), SDL_ResampleAudioStream(), SDL_ReserveSpaceInDataQueue(), SDL_SemPost(), SDL_SemTryWait(), SDL_SemWait(), SDL_SemWaitTimeout(), SDL_SetWindowOpacity(), SDL_ShowMessageBox(), SDL_SIMDAlloc(), SDL_SIMDRealloc(), SDL_snprintf(), SDL_TryLockMutex(), SDL_utf8strlen(), SDL_vsscanf(), SDL_Vulkan_LoadLibrary(), SW_RenderCopyEx(), TestWaitTimeout(), and UpdateWindowTitle().

◆ screen

◆ set_LED

SDL_bool set_LED = SDL_FALSE

Definition at line 68 of file testgamecontroller.c.

Referenced by loop().

◆ window

Definition at line 64 of file testgamecontroller.c.

◆ x

int x

Definition at line 31 of file testgamecontroller.c.

Referenced by loop().

◆ y

int y

Definition at line 31 of file testgamecontroller.c.

Referenced by loop().