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

Go to the source code of this file.

Data Structures

struct  SDL_GameControllerExtendedBind
 
struct  AxisState
 

Macros

#define SCREEN_WIDTH   512
 
#define SCREEN_HEIGHT   320
 
#define MARKER_BUTTON   1
 
#define MARKER_AXIS   2
 
#define BINDING_COUNT   (SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_MAX)
 

Enumerations

enum  {
  SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE,
  SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE,
  SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE,
  SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE,
  SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE,
  SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE,
  SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE,
  SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE,
  SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT,
  SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT,
  SDL_CONTROLLER_BINDING_AXIS_MAX
}
 

Functions

SDL_TextureLoadTexture (SDL_Renderer *renderer, const char *file, SDL_bool transparent)
 
static int StandardizeAxisValue (int nValue)
 
static void SetCurrentBinding (int iBinding)
 
static SDL_bool BBindingContainsBinding (const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
 
static void ConfigureBinding (const SDL_GameControllerExtendedBind *pBinding)
 
static SDL_bool BMergeAxisBindings (int iIndex)
 
static void WatchJoystick (SDL_Joystick *joystick)
 
int main (int argc, char *argv[])
 

Variables

struct {
   int   x
 
   int   y
 
   double   angle
 
   int   marker
 
s_arrBindingDisplay [BINDING_COUNT]
 
static int s_arrBindingOrder [BINDING_COUNT]
 
static SDL_GameControllerExtendedBind s_arrBindings [BINDING_COUNT]
 
static int s_nNumAxes
 
static AxisStates_arrAxisState
 
static int s_iCurrentBinding
 
static Uint32 s_unPendingAdvanceTime
 
static SDL_bool s_bBindingComplete
 

Macro Definition Documentation

◆ BINDING_COUNT

Definition at line 50 of file controllermap.c.

Referenced by SetCurrentBinding().

◆ MARKER_AXIS

#define MARKER_AXIS   2

Definition at line 33 of file controllermap.c.

Referenced by WatchJoystick().

◆ MARKER_BUTTON

#define MARKER_BUTTON   1

Definition at line 32 of file controllermap.c.

Referenced by WatchJoystick().

◆ SCREEN_HEIGHT

#define SCREEN_HEIGHT   320

Definition at line 29 of file controllermap.c.

Referenced by WatchJoystick().

◆ SCREEN_WIDTH

#define SCREEN_WIDTH   512

Definition at line 28 of file controllermap.c.

Referenced by WatchJoystick().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE 
SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE 
SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE 
SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE 
SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE 
SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE 
SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE 
SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE 
SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT 
SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT 
SDL_CONTROLLER_BINDING_AXIS_MAX 

Definition at line 35 of file controllermap.c.

Function Documentation

◆ BBindingContainsBinding()

static SDL_bool BBindingContainsBinding ( const SDL_GameControllerExtendedBind pBindingA,
const SDL_GameControllerExtendedBind pBindingB 
)
static

Definition at line 227 of file controllermap.c.

References SDL_GameControllerExtendedBind::axis, SDL_GameControllerExtendedBind::bindType, SDL_GameControllerExtendedBind::committed, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_FALSE, SDL_max, SDL_memcmp, SDL_min, and SDL_GameControllerExtendedBind::value.

Referenced by ConfigureBinding().

228 {
229  if (pBindingA->bindType != pBindingB->bindType)
230  {
231  return SDL_FALSE;
232  }
233  switch (pBindingA->bindType)
234  {
236  if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
237  return SDL_FALSE;
238  }
239  if (!pBindingA->committed) {
240  return SDL_FALSE;
241  }
242  {
243  int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
244  int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
245  int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
246  int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
247  return (minA <= minB && maxA >= maxB);
248  }
249  /* Not reached */
250  default:
251  return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
252  }
253 }
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
union SDL_GameControllerExtendedBind::@48 value
#define SDL_max(x, y)
Definition: SDL_stdinc.h:407
#define SDL_memcmp
SDL_GameControllerBindType bindType

◆ BMergeAxisBindings()

static SDL_bool BMergeAxisBindings ( int  iIndex)
static

Definition at line 336 of file controllermap.c.

References SDL_GameControllerExtendedBind::axis, SDL_GameControllerExtendedBind::bindType, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_CONTROLLER_BINDTYPE_NONE, SDL_FALSE, SDL_TRUE, and SDL_GameControllerExtendedBind::value.

Referenced by WatchJoystick().

337 {
338  SDL_GameControllerExtendedBind *pBindingA = &s_arrBindings[iIndex];
339  SDL_GameControllerExtendedBind *pBindingB = &s_arrBindings[iIndex+1];
340  if (pBindingA->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
341  pBindingB->bindType == SDL_CONTROLLER_BINDTYPE_AXIS &&
342  pBindingA->value.axis.axis == pBindingB->value.axis.axis) {
343  if (pBindingA->value.axis.axis_min == pBindingB->value.axis.axis_min) {
344  pBindingA->value.axis.axis_min = pBindingA->value.axis.axis_max;
345  pBindingA->value.axis.axis_max = pBindingB->value.axis.axis_max;
347  return SDL_TRUE;
348  }
349  }
350  return SDL_FALSE;
351 }
union SDL_GameControllerExtendedBind::@48 value
SDL_GameControllerBindType bindType
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT]

◆ ConfigureBinding()

static void ConfigureBinding ( const SDL_GameControllerExtendedBind pBinding)
static

Definition at line 256 of file controllermap.c.

References SDL_GameControllerExtendedBind::axis, BBindingContainsBinding(), SDL_GameControllerExtendedBind::bindType, SDL_GameControllerExtendedBind::button, SDL_GameControllerExtendedBind::committed, SDL_GameControllerExtendedBind::hat, s_arrBindingOrder, s_iCurrentBinding, s_unPendingAdvanceTime, SDL_arraysize, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_CONTROLLER_BINDTYPE_BUTTON, SDL_CONTROLLER_BINDTYPE_HAT, SDL_CONTROLLER_BINDTYPE_NONE, SDL_CONTROLLER_BUTTON_A, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_BUTTON_MAX, SDL_GetTicks(), SDL_Log, SetCurrentBinding(), and SDL_GameControllerExtendedBind::value.

Referenced by WatchJoystick().

257 {
259  int iIndex;
260  int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
261 
262  /* Do we already have this binding? */
263  for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
264  pCurrent = &s_arrBindings[iIndex];
265  if (BBindingContainsBinding(pCurrent, pBinding)) {
266  if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
267  /* Skip to the next binding */
269  return;
270  }
271 
272  if (iIndex == SDL_CONTROLLER_BUTTON_B) {
273  /* Go back to the previous binding */
275  return;
276  }
277 
278  /* Already have this binding, ignore it */
279  return;
280  }
281  }
282 
283 #ifdef DEBUG_CONTROLLERMAP
284  switch ( pBinding->bindType )
285  {
287  break;
289  SDL_Log("Configuring button binding for button %d\n", pBinding->value.button);
290  break;
292  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");
293  break;
295  SDL_Log("Configuring hat binding for hat %d %d\n", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
296  break;
297  }
298 #endif /* DEBUG_CONTROLLERMAP */
299 
300  /* Should the new binding override the existing one? */
301  pCurrent = &s_arrBindings[iCurrentElement];
302  if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
303  SDL_bool bNativeDPad, bCurrentDPad;
304  SDL_bool bNativeAxis, bCurrentAxis;
305 
306  bNativeDPad = (iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_UP ||
307  iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_DOWN ||
308  iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
309  iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
310  bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
311  if (bNativeDPad && bCurrentDPad) {
312  /* We already have a binding of the type we want, ignore the new one */
313  return;
314  }
315 
316  bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
317  bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
318  if (bNativeAxis == bCurrentAxis &&
319  (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
320  pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
321  /* We already have a binding of the type we want, ignore the new one */
322  return;
323  }
324  }
325 
326  *pCurrent = *pBinding;
327 
328  if (pBinding->committed) {
330  } else {
332  }
333 }
static void SetCurrentBinding(int iBinding)
union SDL_GameControllerExtendedBind::@48 value
static int s_arrBindingOrder[BINDING_COUNT]
Definition: controllermap.c:86
static SDL_bool BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
#define SDL_Log
static Uint32 s_unPendingAdvanceTime
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
static int s_iCurrentBinding
SDL_GameControllerBindType bindType
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT]

◆ LoadTexture()

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

Definition at line 155 of file controllermap.c.

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

Referenced by WatchJoystick().

156 {
157  SDL_Surface *temp;
159 
160  /* Load the sprite image */
161  temp = SDL_LoadBMP(file);
162  if (temp == NULL) {
163  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
164  return NULL;
165  }
166 
167  /* Set transparent pixel as the pixel at (0,0) */
168  if (transparent) {
169  if (temp->format->palette) {
170  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
171  }
172  }
173 
174  /* Create textures from the image */
175  texture = SDL_CreateTextureFromSurface(renderer, temp);
176  if (!texture) {
177  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
178  SDL_FreeSurface(temp);
179  return NULL;
180  }
181  SDL_FreeSurface(temp);
182 
183  /* We're ready to roll. :) */
184  return texture;
185 }
#define SDL_GetError
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_LogError
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_SetColorKey
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:318

◆ main()

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

Definition at line 704 of file controllermap.c.

References device, i, NULL, SDL_GetError, SDL_Init, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_JoystickClose, SDL_JoystickGetGUID, SDL_JoystickGetGUIDString, SDL_JoystickGetProduct, SDL_JoystickGetVendor, SDL_JoystickInstanceID, SDL_JoystickNameForIndex, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_JoystickOpen, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_NumJoysticks, SDL_QuitSubSystem, and WatchJoystick().

705 {
706  const char *name;
707  int i;
708  SDL_Joystick *joystick;
709 
710  /* Enable standard application logging */
712 
713  /* Initialize SDL (Note: video is required to start event loop) */
715  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
716  exit(1);
717  }
718 
719  /* Print information about the joysticks */
720  SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
721  for (i = 0; i < SDL_NumJoysticks(); ++i) {
722  name = SDL_JoystickNameForIndex(i);
723  SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
724  joystick = SDL_JoystickOpen(i);
725  if (joystick == NULL) {
726  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
727  SDL_GetError());
728  } else {
729  char guid[64];
731  guid, sizeof (guid));
732  SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
733  SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
734  SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
735  SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
736  SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
737  SDL_Log(" guid: %s\n", guid);
738  SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
739  SDL_JoystickClose(joystick);
740  }
741  }
742 
743 #ifdef __ANDROID__
744  if (SDL_NumJoysticks() > 0) {
745 #else
746  if (argv[1]) {
747 #endif
748  int device;
749 #ifdef __ANDROID__
750  device = 0;
751 #else
752  device = atoi(argv[1]);
753 #endif
754  joystick = SDL_JoystickOpen(device);
755  if (joystick == NULL) {
756  SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
757  } else {
758  WatchJoystick(joystick);
759  SDL_JoystickClose(joystick);
760  }
761  }
762  else {
763  SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
764  }
766 
767  return 0;
768 }
#define SDL_GetError
#define SDL_JoystickClose
#define SDL_JoystickGetVendor
#define SDL_JoystickGetGUID
#define SDL_INIT_JOYSTICK
Definition: SDL.h:79
#define SDL_QuitSubSystem
#define SDL_JoystickNameForIndex
#define SDL_JoystickOpen
#define SDL_NumJoysticks
#define SDL_JoystickNumButtons
#define SDL_JoystickGetGUIDString
#define SDL_JoystickInstanceID
#define SDL_JoystickGetProduct
GLuint const GLchar * name
#define SDL_LogError
#define SDL_JoystickNumAxes
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_Log
static void WatchJoystick(SDL_Joystick *joystick)
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 SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
#define SDL_JoystickNumHats
#define SDL_JoystickNumBalls
#define SDL_Init
#define SDL_INIT_VIDEO
Definition: SDL.h:78

◆ SetCurrentBinding()

static void SetCurrentBinding ( int  iBinding)
static

Definition at line 200 of file controllermap.c.

References BINDING_COUNT, AxisState::m_nFarthestValue, AxisState::m_nStartingValue, s_arrBindingOrder, s_bBindingComplete, s_iCurrentBinding, s_nNumAxes, s_unPendingAdvanceTime, SDL_TRUE, and SDL_zerop.

Referenced by ConfigureBinding(), and WatchJoystick().

201 {
202  int iIndex;
204 
205  if (iBinding < 0) {
206  return;
207  }
208 
209  if (iBinding == BINDING_COUNT) {
211  return;
212  }
213 
214  s_iCurrentBinding = iBinding;
215 
217  SDL_zerop(pBinding);
218 
219  for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
221  }
222 
224 }
static SDL_bool s_bBindingComplete
#define BINDING_COUNT
Definition: controllermap.c:50
static AxisState * s_arrAxisState
static int s_arrBindingOrder[BINDING_COUNT]
Definition: controllermap.c:86
int m_nFarthestValue
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
static Uint32 s_unPendingAdvanceTime
static int s_iCurrentBinding
int m_nStartingValue
static int s_nNumAxes
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT]

◆ StandardizeAxisValue()

static int StandardizeAxisValue ( int  nValue)
static

Definition at line 188 of file controllermap.c.

References SDL_JOYSTICK_AXIS_MAX, and SDL_JOYSTICK_AXIS_MIN.

Referenced by WatchJoystick().

189 {
190  if (nValue > SDL_JOYSTICK_AXIS_MAX/2) {
191  return SDL_JOYSTICK_AXIS_MAX;
192  } else if (nValue < SDL_JOYSTICK_AXIS_MIN/2) {
193  return SDL_JOYSTICK_AXIS_MIN;
194  } else {
195  return 0;
196  }
197 }
#define SDL_JOYSTICK_AXIS_MIN
Definition: SDL_joystick.h:289
#define SDL_JOYSTICK_AXIS_MAX
Definition: SDL_joystick.h:288

◆ WatchJoystick()

static void WatchJoystick ( SDL_Joystick *  joystick)
static

Definition at line 354 of file controllermap.c.

References axis, SDL_GameControllerExtendedBind::axis, SDL_JoyAxisEvent::axis, background, SDL_GameControllerExtendedBind::bindType, BMergeAxisBindings(), button, SDL_GameControllerExtendedBind::button, SDL_JoyButtonEvent::button, SDL_GameControllerExtendedBind::committed, ConfigureBinding(), done, SDL_Rect::h, SDL_GameControllerExtendedBind::hat, SDL_JoyHatEvent::hat, SDL_Event::jaxis, SDL_Event::jbutton, SDL_Event::jhat, SDL_Event::key, SDL_KeyboardEvent::keysym, LoadTexture(), AxisState::m_bMoving, AxisState::m_nFarthestValue, AxisState::m_nStartingValue, marker, MARKER_AXIS, MARKER_BUTTON, NULL, s_arrBindingDisplay, s_arrBindingOrder, s_bBindingComplete, s_iCurrentBinding, s_nNumAxes, s_unPendingAdvanceTime, screen, SCREEN_HEIGHT, SCREEN_WIDTH, SDL_abs, SDL_ALPHA_OPAQUE, SDL_arraysize, SDL_assert, SDL_calloc, SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY, SDL_CONTROLLER_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_RIGHTY, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE, SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE, SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE, SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE, SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE, SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE, SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE, SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE, SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT, SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_CONTROLLER_BINDTYPE_BUTTON, SDL_CONTROLLER_BINDTYPE_HAT, SDL_CONTROLLER_BINDTYPE_NONE, SDL_CONTROLLER_BUTTON_MAX, SDL_CreateRenderer, SDL_CreateWindow, SDL_Delay, SDL_DestroyRenderer, SDL_DestroyWindow, SDL_FALSE, SDL_FINGERDOWN, SDL_FLIP_NONE, SDL_free, SDL_GameControllerGetStringForAxis, SDL_GameControllerGetStringForButton, SDL_GetError, SDL_GetPlatform, SDL_GetTicks(), SDL_HAT_CENTERED, SDL_isspace, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYBUTTONDOWN, SDL_JOYDEVICEREMOVED, SDL_JOYHATMOTION, SDL_JOYSTICK_AXIS_MAX, SDL_JOYSTICK_AXIS_MIN, SDL_JoystickGetAxisInitialState, SDL_JoystickGetGUID, SDL_JoystickGetGUIDString, SDL_JoystickInstanceID, SDL_JoystickName, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_KEYDOWN, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_memmove, SDL_MOUSEBUTTONDOWN, SDL_PollEvent, SDL_QueryTexture, SDL_QUIT, SDL_RaiseWindow, SDL_RenderClear, SDL_RenderCopy, SDL_RenderCopyEx, SDL_RenderPresent, SDL_RenderSetLogicalSize, SDL_SetRenderDrawColor, SDL_SetTextureAlphaMod, SDL_SetTextureColorMod, SDL_snprintf, SDL_strchr, SDL_strlcat, SDL_strlcpy, SDL_strlen, SDL_TRUE, SDL_WINDOWPOS_CENTERED, SDL_zero, SDLK_AC_BACK, SDLK_BACKSPACE, SDLK_ESCAPE, SDLK_SPACE, SetCurrentBinding(), StandardizeAxisValue(), SDL_Keysym::sym, SDL_Event::type, SDL_GameControllerExtendedBind::value, SDL_JoyHatEvent::value, SDL_Rect::w, SDL_JoyAxisEvent::which, SDL_JoyHatEvent::which, SDL_JoyButtonEvent::which, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

355 {
359  const char *name = NULL;
362  SDL_Rect dst;
363  Uint8 alpha=200, alpha_step = -1;
364  Uint32 alpha_ticks = 0;
365  SDL_JoystickID nJoystickID;
366  int iIndex;
367 
368  /* Create a window to display joystick axis position */
369  window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
371  SCREEN_HEIGHT, 0);
372  if (window == NULL) {
373  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
374  return;
375  }
376 
377  screen = SDL_CreateRenderer(window, -1, 0);
378  if (screen == NULL) {
379  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
380  SDL_DestroyWindow(window);
381  return;
382  }
383 
384  background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
385  button = LoadTexture(screen, "button.bmp", SDL_TRUE);
386  axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
387  SDL_RaiseWindow(window);
388 
389  /* scale for platforms that don't give you the window size you asked for. */
391 
392  /* Print info about the joystick we are watching */
393  name = SDL_JoystickName(joystick);
394  SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
395  name ? name : "Unknown Joystick");
396  SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
397  SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
398  SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
399 
400  SDL_Log("\n\n\
401  ====================================================================================\n\
402  Press the buttons on your controller when indicated\n\
403  (Your controller may look different than the picture)\n\
404  If you want to correct a mistake, press backspace or the back button on your device\n\
405  To skip a button, press SPACE or click/touch the screen\n\
406  To exit, press ESC\n\
407  ====================================================================================\n");
408 
409  nJoystickID = SDL_JoystickInstanceID(joystick);
410 
411  s_nNumAxes = SDL_JoystickNumAxes(joystick);
413  for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
414  AxisState *pAxisState = &s_arrAxisState[iIndex];
415  Sint16 nInitialValue;
416  pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
417  pAxisState->m_nStartingValue = nInitialValue;
418  pAxisState->m_nFarthestValue = nInitialValue;
419  }
420 
421  /* Loop, getting joystick events! */
422  while (!done && !s_bBindingComplete) {
423  int iElement = s_arrBindingOrder[s_iCurrentBinding];
424 
425  switch (s_arrBindingDisplay[iElement].marker) {
426  case MARKER_AXIS:
427  marker = axis;
428  break;
429  case MARKER_BUTTON:
430  marker = button;
431  break;
432  default:
433  break;
434  }
435 
436  dst.x = s_arrBindingDisplay[iElement].x;
437  dst.y = s_arrBindingDisplay[iElement].y;
438  SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
439 
440  if (SDL_GetTicks() - alpha_ticks > 5) {
441  alpha_ticks = SDL_GetTicks();
442  alpha += alpha_step;
443  if (alpha == 255) {
444  alpha_step = -1;
445  }
446  if (alpha < 128) {
447  alpha_step = 1;
448  }
449  }
450 
451  SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
452  SDL_RenderClear(screen);
453  SDL_RenderCopy(screen, background, NULL, NULL);
454  SDL_SetTextureAlphaMod(marker, alpha);
455  SDL_SetTextureColorMod(marker, 10, 255, 21);
456  SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE);
457  SDL_RenderPresent(screen);
458 
459  while (SDL_PollEvent(&event) > 0) {
460  switch (event.type) {
462  if (event.jaxis.which == nJoystickID) {
463  done = SDL_TRUE;
464  }
465  break;
466  case SDL_JOYAXISMOTION:
467  if (event.jaxis.which == nJoystickID) {
468  AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
469  int nValue = event.jaxis.value;
470  int nCurrentDistance, nFarthestDistance;
471  if (!pAxisState->m_bMoving) {
472  pAxisState->m_bMoving = SDL_TRUE;
473  pAxisState->m_nStartingValue = nValue;
474  pAxisState->m_nFarthestValue = nValue;
475  }
476  nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
477  nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
478  if (nCurrentDistance > nFarthestDistance) {
479  pAxisState->m_nFarthestValue = nValue;
480  nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
481  }
482 
483 #ifdef DEBUG_CONTROLLERMAP
484  SDL_Log("AXIS %d nValue %d nCurrentDistance %d nFarthestDistance %d\n", event.jaxis.axis, nValue, nCurrentDistance, nFarthestDistance);
485 #endif
486  if (nFarthestDistance >= 16000) {
487  /* If we've gone out far enough and started to come back, let's bind this axis */
488  SDL_bool bCommitBinding = (nCurrentDistance <= 10000) ? SDL_TRUE : SDL_FALSE;
490  SDL_zero(binding);
492  binding.value.axis.axis = event.jaxis.axis;
493  binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
494  binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
495  binding.committed = bCommitBinding;
496  ConfigureBinding(&binding);
497  }
498  }
499  break;
500  case SDL_JOYHATMOTION:
501  if (event.jhat.which == nJoystickID) {
502  if (event.jhat.value != SDL_HAT_CENTERED) {
504 
505 #ifdef DEBUG_CONTROLLERMAP
506  SDL_Log("HAT %d %d\n", event.jhat.hat, event.jhat.value);
507 #endif
508  SDL_zero(binding);
510  binding.value.hat.hat = event.jhat.hat;
511  binding.value.hat.hat_mask = event.jhat.value;
512  binding.committed = SDL_TRUE;
513  ConfigureBinding(&binding);
514  }
515  }
516  break;
517  case SDL_JOYBALLMOTION:
518  break;
519  case SDL_JOYBUTTONDOWN:
520  if (event.jbutton.which == nJoystickID) {
522 
523 #ifdef DEBUG_CONTROLLERMAP
524  SDL_Log("BUTTON %d\n", event.jbutton.button);
525 #endif
526  SDL_zero(binding);
528  binding.value.button = event.jbutton.button;
529  binding.committed = SDL_TRUE;
530  ConfigureBinding(&binding);
531  }
532  break;
533  case SDL_FINGERDOWN:
534  case SDL_MOUSEBUTTONDOWN:
535  /* Skip this step */
537  break;
538  case SDL_KEYDOWN:
539  if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
541  break;
542  }
543  if (event.key.keysym.sym == SDLK_SPACE) {
545  break;
546  }
547 
548  if ((event.key.keysym.sym != SDLK_ESCAPE)) {
549  break;
550  }
551  /* Fall through to signal quit */
552  case SDL_QUIT:
553  done = SDL_TRUE;
554  break;
555  default:
556  break;
557  }
558  }
559 
560  SDL_Delay(15);
561 
562  /* Wait 100 ms for joystick events to stop coming in,
563  in case a controller sends multiple events for a single control (e.g. axis and button for trigger)
564  */
567  }
568  }
569 
570  if (s_bBindingComplete) {
571  char mapping[1024];
572  char trimmed_name[128];
573  char *spot;
574  int iIndex;
575  char pszElement[12];
576 
577  SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name));
578  while (SDL_isspace(trimmed_name[0])) {
579  SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name));
580  }
581  while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) {
582  trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0';
583  }
584  while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) {
585  SDL_memmove(spot, spot + 1, SDL_strlen(spot));
586  }
587 
588  /* Initialize mapping with GUID and name */
589  SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), mapping, SDL_arraysize(mapping));
590  SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
591  SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping));
592  SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
593  SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping));
594  SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping));
595  SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
596 
597  for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
598  SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex];
599  if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) {
600  continue;
601  }
602 
603  if (iIndex < SDL_CONTROLLER_BUTTON_MAX) {
606  } else {
607  const char *pszAxisName;
608  switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) {
610  if (!BMergeAxisBindings(iIndex)) {
611  SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
612  }
614  break;
616  SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
618  break;
620  if (!BMergeAxisBindings(iIndex)) {
621  SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
622  }
624  break;
626  SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
628  break;
630  if (!BMergeAxisBindings(iIndex)) {
631  SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
632  }
634  break;
636  SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
638  break;
640  if (!BMergeAxisBindings(iIndex)) {
641  SDL_strlcat(mapping, "-", SDL_arraysize(mapping));
642  }
644  break;
646  SDL_strlcat(mapping, "+", SDL_arraysize(mapping));
648  break;
651  break;
654  break;
655  }
656  SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping));
657  }
658  SDL_strlcat(mapping, ":", SDL_arraysize(mapping));
659 
660  pszElement[0] = '\0';
661  switch (pBinding->bindType) {
663  SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button);
664  break;
666  if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) {
667  /* The negative half axis */
668  SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis);
669  } else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) {
670  /* The positive half axis */
671  SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis);
672  } else {
673  SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis);
674  if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) {
675  /* Invert the axis */
676  SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement));
677  }
678  }
679  break;
681  SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
682  break;
683  default:
684  SDL_assert(!"Unknown bind type");
685  break;
686  }
687  SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping));
688  SDL_strlcat(mapping, ",", SDL_arraysize(mapping));
689  }
690 
691  SDL_Log("Mapping:\n\n%s\n\n", mapping);
692  /* Print to stdout as well so the user can cat the output somewhere */
693  printf("%s\n", mapping);
694  }
695 
698 
699  SDL_DestroyRenderer(screen);
700  SDL_DestroyWindow(window);
701 }
static void ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
#define SDL_strlcpy
#define SDL_abs
SDL_JoystickID which
Definition: SDL_events.h:335
static int StandardizeAxisValue(int nValue)
#define SCREEN_HEIGHT
Definition: controllermap.c:29
static SDL_bool s_bBindingComplete
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
#define SDL_PollEvent
static void SetCurrentBinding(int iBinding)
#define SDL_GetError
SDL_Texture * button
static AxisState * s_arrAxisState
#define SDL_strlcat
#define SCREEN_WIDTH
Definition: controllermap.c:28
#define SDL_JoystickGetGUID
GLenum GLenum dst
SDL_JoyButtonEvent jbutton
Definition: SDL_events.h:539
union SDL_GameControllerExtendedBind::@48 value
#define SDL_GameControllerGetStringForButton
SDL_Texture * LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
SDL_JoystickID which
Definition: SDL_events.h:283
#define MARKER_AXIS
Definition: controllermap.c:33
static int s_arrBindingOrder[BINDING_COUNT]
Definition: controllermap.c:86
int m_nFarthestValue
int marker
Definition: controllermap.c:56
#define SDL_JoystickNumButtons
#define SDL_CreateWindow
SDL_Texture * axis
SDL_Texture * background
uint32_t Uint32
Definition: SDL_stdinc.h:181
SDL_GameControllerButton
#define SDL_JoystickGetGUIDString
#define SDL_JoystickInstanceID
SDL_bool m_bMoving
static struct @47 s_arrBindingDisplay[BINDING_COUNT]
GLfloat GLfloat GLfloat alpha
GLuint const GLchar * name
#define SDL_JoystickName
#define SDL_strchr
#define SDL_LogError
#define SDL_JoystickNumAxes
#define SDL_RenderCopy
#define SDL_JOYSTICK_AXIS_MIN
Definition: SDL_joystick.h:289
#define SDL_Log
#define SDL_JOYSTICK_AXIS_MAX
Definition: SDL_joystick.h:288
static Uint32 s_unPendingAdvanceTime
static SDL_bool BMergeAxisBindings(int iIndex)
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
#define SDL_GameControllerGetStringForAxis
#define SDL_JoystickGetAxisInitialState
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_JoyAxisEvent jaxis
Definition: SDL_events.h:536
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_free
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_RaiseWindow
int done
Definition: checkkeys.c:28
#define SDL_QueryTexture
static int s_iCurrentBinding
SDL_GameControllerBindType bindType
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
#define SDL_GetPlatform
int m_nStartingValue
#define SDL_SetTextureColorMod
#define SDL_memmove
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:199
#define SDL_isspace
int w
Definition: SDL_rect.h:67
static int s_nNumAxes
#define SDL_Delay
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_JoystickNumHats
#define SDL_calloc
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_KeyboardEvent key
Definition: SDL_events.h:530
#define SDL_strlen
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_JoystickNumBalls
#define SDL_RenderCopyEx
SDL_Keycode sym
Definition: SDL_keyboard.h:50
GLfloat angle
#define SDL_snprintf
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
GLenum GLenum GLenum GLenum mapping
General event structure.
Definition: SDL_events.h:525
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT]
#define SDL_SetRenderDrawColor
#define SDL_DestroyRenderer
SDL_JoyHatEvent jhat
Definition: SDL_events.h:538
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:316
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_Renderer * screen
#define SDL_DestroyWindow
#define MARKER_BUTTON
Definition: controllermap.c:32
SDL_JoystickID which
Definition: SDL_events.h:315
int y
Definition: SDL_rect.h:66
#define SDL_CreateRenderer
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
int16_t Sint16
Definition: SDL_stdinc.h:163
#define SDL_RenderPresent
#define SDL_SetTextureAlphaMod
Uint32 type
Definition: SDL_events.h:527

Variable Documentation

◆ angle

double angle

Definition at line 55 of file controllermap.c.

◆ marker

int marker

Definition at line 56 of file controllermap.c.

Referenced by WatchJoystick().

◆ s_arrAxisState

AxisState* s_arrAxisState
static

Definition at line 148 of file controllermap.c.

◆ s_arrBindingDisplay

struct { ... } s_arrBindingDisplay[BINDING_COUNT]
Initial value:
= {
{ 387, 167, 0.0, MARKER_BUTTON },
{ 431, 132, 0.0, MARKER_BUTTON },
{ 342, 132, 0.0, MARKER_BUTTON },
{ 389, 101, 0.0, MARKER_BUTTON },
{ 174, 132, 0.0, MARKER_BUTTON },
{ 233, 132, 0.0, MARKER_BUTTON },
{ 289, 132, 0.0, MARKER_BUTTON },
{ 75, 154, 0.0, MARKER_BUTTON },
{ 305, 230, 0.0, MARKER_BUTTON },
{ 77, 40, 0.0, MARKER_BUTTON },
{ 396, 36, 0.0, MARKER_BUTTON },
{ 154, 188, 0.0, MARKER_BUTTON },
{ 154, 249, 0.0, MARKER_BUTTON },
{ 116, 217, 0.0, MARKER_BUTTON },
{ 186, 217, 0.0, MARKER_BUTTON },
{ 74, 153, 270.0, MARKER_AXIS },
{ 74, 153, 90.0, MARKER_AXIS },
{ 74, 153, 0.0, MARKER_AXIS },
{ 74, 153, 180.0, MARKER_AXIS },
{ 306, 231, 270.0, MARKER_AXIS },
{ 306, 231, 90.0, MARKER_AXIS },
{ 306, 231, 0.0, MARKER_AXIS },
{ 306, 231, 180.0, MARKER_AXIS },
{ 91, -20, 180.0, MARKER_AXIS },
{ 375, -20, 180.0, MARKER_AXIS },
}
#define MARKER_AXIS
Definition: controllermap.c:33
#define MARKER_BUTTON
Definition: controllermap.c:32

Referenced by WatchJoystick().

◆ s_arrBindingOrder

int s_arrBindingOrder[BINDING_COUNT]
static
Initial value:
= {
}

Definition at line 86 of file controllermap.c.

Referenced by ConfigureBinding(), SetCurrentBinding(), and WatchJoystick().

◆ s_arrBindings

Definition at line 138 of file controllermap.c.

◆ s_bBindingComplete

SDL_bool s_bBindingComplete
static

Definition at line 152 of file controllermap.c.

Referenced by SetCurrentBinding(), and WatchJoystick().

◆ s_iCurrentBinding

int s_iCurrentBinding
static

Definition at line 150 of file controllermap.c.

Referenced by ConfigureBinding(), SetCurrentBinding(), and WatchJoystick().

◆ s_nNumAxes

int s_nNumAxes
static

Definition at line 147 of file controllermap.c.

Referenced by SetCurrentBinding(), and WatchJoystick().

◆ s_unPendingAdvanceTime

Uint32 s_unPendingAdvanceTime
static

Definition at line 151 of file controllermap.c.

Referenced by ConfigureBinding(), SetCurrentBinding(), and WatchJoystick().

◆ x

int x

Definition at line 54 of file controllermap.c.

◆ y

int y

Definition at line 54 of file controllermap.c.