| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Game controller mapping generator */ |
| 14 |
/* Gabriel Jacobo <[email protected]> */ |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "SDL.h" |
| 21 |
#include "testutils.h" |
| 22 |
|
| 23 |
#ifndef SDL_JOYSTICK_DISABLED |
| 24 |
|
| 25 |
/* Define this for verbose output while mapping controllers */ |
| 26 |
#define DEBUG_CONTROLLERMAP |
| 27 |
|
| 28 |
#define SCREEN_WIDTH 512 |
| 29 |
#define SCREEN_HEIGHT 320 |
| 30 |
|
| 31 |
enum marker_type |
| 32 |
{ |
| 33 |
MARKER_BUTTON, |
| 34 |
MARKER_AXIS, |
| 35 |
}; |
| 36 |
|
| 37 |
enum |
| 38 |
{ |
| 39 |
SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE, |
| 40 |
SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE, |
| 41 |
SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE, |
| 42 |
SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE, |
| 43 |
SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE, |
| 44 |
SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE, |
| 45 |
SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE, |
| 46 |
SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE, |
| 47 |
SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT, |
| 48 |
SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT, |
| 49 |
SDL_CONTROLLER_BINDING_AXIS_MAX, |
| 50 |
}; |
| 51 |
|
| 52 |
#define BINDING_COUNT (SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_MAX) |
| 53 |
|
| 54 |
static struct |
| 55 |
{ |
| 56 |
int x, y; |
| 57 |
double angle; |
| 58 |
enum marker_type marker; |
| 59 |
|
| 60 |
} s_arrBindingDisplay[] = { |
| 61 |
{ 387, 167, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_A */ |
| 62 |
{ 431, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_B */ |
| 63 |
{ 342, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_X */ |
| 64 |
{ 389, 101, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_Y */ |
| 65 |
{ 174, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_BACK */ |
| 66 |
{ 232, 128, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_GUIDE */ |
| 67 |
{ 289, 132, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_START */ |
| 68 |
{ 75, 154, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */ |
| 69 |
{ 305, 230, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */ |
| 70 |
{ 77, 40, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */ |
| 71 |
{ 396, 36, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */ |
| 72 |
{ 154, 188, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_UP */ |
| 73 |
{ 154, 249, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */ |
| 74 |
{ 116, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */ |
| 75 |
{ 186, 217, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */ |
| 76 |
{ 232, 174, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_MISC1 */ |
| 77 |
{ 132, 135, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_PADDLE1 */ |
| 78 |
{ 330, 135, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_PADDLE2 */ |
| 79 |
{ 132, 175, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_PADDLE3 */ |
| 80 |
{ 330, 175, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_PADDLE4 */ |
| 81 |
{ 0, 0, 0.0, MARKER_BUTTON }, /* SDL_CONTROLLER_BUTTON_TOUCHPAD */ |
| 82 |
{ 74, 153, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE */ |
| 83 |
{ 74, 153, 90.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE */ |
| 84 |
{ 74, 153, 0.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE */ |
| 85 |
{ 74, 153, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE */ |
| 86 |
{ 306, 231, 270.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE */ |
| 87 |
{ 306, 231, 90.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE */ |
| 88 |
{ 306, 231, 0.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE */ |
| 89 |
{ 306, 231, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE */ |
| 90 |
{ 91, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT */ |
| 91 |
{ 375, -20, 180.0, MARKER_AXIS }, /* SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT */ |
| 92 |
}; |
| 93 |
SDL_COMPILE_TIME_ASSERT(s_arrBindingDisplay, SDL_arraysize(s_arrBindingDisplay) == BINDING_COUNT); |
| 94 |
|
| 95 |
static int s_arrBindingOrder[] = { |
| 96 |
SDL_CONTROLLER_BUTTON_A, |
| 97 |
SDL_CONTROLLER_BUTTON_B, |
| 98 |
SDL_CONTROLLER_BUTTON_Y, |
| 99 |
SDL_CONTROLLER_BUTTON_X, |
| 100 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE, |
| 101 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE, |
| 102 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE, |
| 103 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE, |
| 104 |
SDL_CONTROLLER_BUTTON_LEFTSTICK, |
| 105 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE, |
| 106 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE, |
| 107 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE, |
| 108 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE, |
| 109 |
SDL_CONTROLLER_BUTTON_RIGHTSTICK, |
| 110 |
SDL_CONTROLLER_BUTTON_LEFTSHOULDER, |
| 111 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT, |
| 112 |
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, |
| 113 |
SDL_CONTROLLER_BUTTON_MAX + SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT, |
| 114 |
SDL_CONTROLLER_BUTTON_DPAD_UP, |
| 115 |
SDL_CONTROLLER_BUTTON_DPAD_RIGHT, |
| 116 |
SDL_CONTROLLER_BUTTON_DPAD_DOWN, |
| 117 |
SDL_CONTROLLER_BUTTON_DPAD_LEFT, |
| 118 |
SDL_CONTROLLER_BUTTON_BACK, |
| 119 |
SDL_CONTROLLER_BUTTON_GUIDE, |
| 120 |
SDL_CONTROLLER_BUTTON_START, |
| 121 |
SDL_CONTROLLER_BUTTON_MISC1, |
| 122 |
SDL_CONTROLLER_BUTTON_PADDLE1, |
| 123 |
SDL_CONTROLLER_BUTTON_PADDLE2, |
| 124 |
SDL_CONTROLLER_BUTTON_PADDLE3, |
| 125 |
SDL_CONTROLLER_BUTTON_PADDLE4, |
| 126 |
SDL_CONTROLLER_BUTTON_TOUCHPAD, |
| 127 |
}; |
| 128 |
SDL_COMPILE_TIME_ASSERT(s_arrBindingOrder, SDL_arraysize(s_arrBindingOrder) == BINDING_COUNT); |
| 129 |
|
| 130 |
typedef struct |
| 131 |
{ |
| 132 |
SDL_GameControllerBindType bindType; |
| 133 |
union |
| 134 |
{ |
| 135 |
int button; |
| 136 |
|
| 137 |
struct |
| 138 |
{ |
| 139 |
int axis; |
| 140 |
int axis_min; |
| 141 |
int axis_max; |
| 142 |
} axis; |
| 143 |
|
| 144 |
struct |
| 145 |
{ |
| 146 |
int hat; |
| 147 |
int hat_mask; |
| 148 |
} hat; |
| 149 |
|
| 150 |
} value; |
| 151 |
|
| 152 |
SDL_bool committed; |
| 153 |
|
| 154 |
} SDL_GameControllerExtendedBind; |
| 155 |
|
| 156 |
static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT]; |
| 157 |
|
| 158 |
typedef struct |
| 159 |
{ |
| 160 |
SDL_bool m_bMoving; |
| 161 |
int m_nLastValue; |
| 162 |
int m_nStartingValue; |
| 163 |
int m_nFarthestValue; |
| 164 |
} AxisState; |
| 165 |
|
| 166 |
static int s_nNumAxes; |
| 167 |
static AxisState *s_arrAxisState; |
| 168 |
|
| 169 |
static int s_iCurrentBinding; |
| 170 |
static Uint32 s_unPendingAdvanceTime; |
| 171 |
static SDL_bool s_bBindingComplete; |
| 172 |
|
| 173 |
static SDL_Window *window; |
| 174 |
static SDL_Renderer *screen; |
| 175 |
static SDL_bool done = SDL_FALSE; |
| 176 |
static SDL_bool bind_touchpad = SDL_FALSE; |
| 177 |
|
| 178 |
static int |
| 179 |
StandardizeAxisValue(int nValue) |
| 180 |
{ |
| 181 |
if (nValue > SDL_JOYSTICK_AXIS_MAX / 2) { |
| 182 |
return SDL_JOYSTICK_AXIS_MAX; |
| 183 |
} else if (nValue < SDL_JOYSTICK_AXIS_MIN / 2) { |
| 184 |
return SDL_JOYSTICK_AXIS_MIN; |
| 185 |
} else { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
static void |
| 191 |
SetCurrentBinding(int iBinding) |
| 192 |
{ |
| 193 |
int iIndex; |
| 194 |
SDL_GameControllerExtendedBind *pBinding; |
| 195 |
|
| 196 |
if (iBinding < 0) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
if (iBinding == BINDING_COUNT) { |
| 201 |
s_bBindingComplete = SDL_TRUE; |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (s_arrBindingOrder[iBinding] == -1) { |
| 206 |
SetCurrentBinding(iBinding + 1); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
if (s_arrBindingOrder[iBinding] == SDL_CONTROLLER_BUTTON_TOUCHPAD && |
| 211 |
!bind_touchpad) { |
| 212 |
SetCurrentBinding(iBinding + 1); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
s_iCurrentBinding = iBinding; |
| 217 |
|
| 218 |
pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]]; |
| 219 |
SDL_zerop(pBinding); |
| 220 |
|
| 221 |
for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) { |
| 222 |
s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue; |
| 223 |
} |
| 224 |
|
| 225 |
s_unPendingAdvanceTime = 0; |
| 226 |
} |
| 227 |
|
| 228 |
static SDL_bool |
| 229 |
BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB) |
| 230 |
{ |
| 231 |
if (pBindingA->bindType != pBindingB->bindType) { |
| 232 |
return SDL_FALSE; |
| 233 |
} |
| 234 |
switch (pBindingA->bindType) { |
| 235 |
case SDL_CONTROLLER_BINDTYPE_AXIS: |
| 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 |
} |
| 254 |
|
| 255 |
static void |
| 256 |
ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding) |
| 257 |
{ |
| 258 |
SDL_GameControllerExtendedBind *pCurrent; |
| 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 */ |
| 268 |
SetCurrentBinding(s_iCurrentBinding + 1); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
if (iIndex == SDL_CONTROLLER_BUTTON_B) { |
| 273 |
/* Go back to the previous binding */ |
| 274 |
SetCurrentBinding(s_iCurrentBinding - 1); |
| 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 |
case SDL_CONTROLLER_BINDTYPE_NONE: |
| 286 |
break; |
| 287 |
case SDL_CONTROLLER_BINDTYPE_BUTTON: |
| 288 |
SDL_Log("Configuring button binding for button %d\n", pBinding->value.button); |
| 289 |
break; |
| 290 |
case SDL_CONTROLLER_BINDTYPE_AXIS: |
| 291 |
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, |
| 292 |
pBinding->committed ? "true" : "false"); |
| 293 |
break; |
| 294 |
case SDL_CONTROLLER_BINDTYPE_HAT: |
| 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) { |
| 329 |
s_unPendingAdvanceTime = SDL_GetTicks(); |
| 330 |
} else { |
| 331 |
s_unPendingAdvanceTime = 0; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static SDL_bool |
| 336 |
BMergeAxisBindings(int iIndex) |
| 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; |
| 346 |
pBindingB->bindType = SDL_CONTROLLER_BINDTYPE_NONE; |
| 347 |
return SDL_TRUE; |
| 348 |
} |
| 349 |
} |
| 350 |
return SDL_FALSE; |
| 351 |
} |
| 352 |
|
| 353 |
static void |
| 354 |
WatchJoystick(SDL_Joystick *joystick) |
| 355 |
{ |
| 356 |
SDL_Texture *background_front, *background_back, *button, *axis, *marker = NULL; |
| 357 |
const char *name = NULL; |
| 358 |
SDL_Event event; |
| 359 |
SDL_Rect dst; |
| 360 |
Uint8 alpha = 200, alpha_step = -1; |
| 361 |
Uint32 alpha_ticks = 0; |
| 362 |
SDL_JoystickID nJoystickID; |
| 363 |
|
| 364 |
background_front = LoadTexture(screen, "controllermap.bmp", SDL_FALSE, NULL, NULL); |
| 365 |
background_back = LoadTexture(screen, "controllermap_back.bmp", SDL_FALSE, NULL, NULL); |
| 366 |
button = LoadTexture(screen, "button.bmp", SDL_TRUE, NULL, NULL); |
| 367 |
axis = LoadTexture(screen, "axis.bmp", SDL_TRUE, NULL, NULL); |
| 368 |
SDL_RaiseWindow(window); |
| 369 |
|
| 370 |
/* scale for platforms that don't give you the window size you asked for. */ |
| 371 |
SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT); |
| 372 |
|
| 373 |
/* Print info about the joystick we are watching */ |
| 374 |
name = SDL_JoystickName(joystick); |
| 375 |
SDL_Log("Watching joystick %" SDL_PRIs32 ": (%s)\n", SDL_JoystickInstanceID(joystick), |
| 376 |
name ? name : "Unknown Joystick"); |
| 377 |
SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", |
| 378 |
SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), |
| 379 |
SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick)); |
| 380 |
|
| 381 |
SDL_Log("\n\n\ |
| 382 |
====================================================================================\n\ |
| 383 |
Press the buttons on your controller when indicated\n\ |
| 384 |
(Your controller may look different than the picture)\n\ |
| 385 |
If you want to correct a mistake, press backspace or the back button on your device\n\ |
| 386 |
To skip a button, press SPACE or click/touch the screen\n\ |
| 387 |
To exit, press ESC\n\ |
| 388 |
====================================================================================\n"); |
| 389 |
|
| 390 |
nJoystickID = SDL_JoystickInstanceID(joystick); |
| 391 |
|
| 392 |
s_nNumAxes = SDL_JoystickNumAxes(joystick); |
| 393 |
s_arrAxisState = (AxisState *)SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState)); |
| 394 |
|
| 395 |
/* Skip any spurious events at start */ |
| 396 |
while (SDL_PollEvent(&event) > 0) { |
| 397 |
} |
| 398 |
|
| 399 |
/* Loop, getting joystick events! */ |
| 400 |
while (!done && !s_bBindingComplete) { |
| 401 |
int iElement = s_arrBindingOrder[s_iCurrentBinding]; |
| 402 |
|
| 403 |
switch (s_arrBindingDisplay[iElement].marker) { |
| 404 |
case MARKER_AXIS: |
| 405 |
marker = axis; |
| 406 |
break; |
| 407 |
case MARKER_BUTTON: |
| 408 |
marker = button; |
| 409 |
break; |
| 410 |
} |
| 411 |
|
| 412 |
dst.x = s_arrBindingDisplay[iElement].x; |
| 413 |
dst.y = s_arrBindingDisplay[iElement].y; |
| 414 |
SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h); |
| 415 |
|
| 416 |
if (SDL_GetTicks() - alpha_ticks > 5) { |
| 417 |
alpha_ticks = SDL_GetTicks(); |
| 418 |
alpha += alpha_step; |
| 419 |
if (alpha == 255) { |
| 420 |
alpha_step = -1; |
| 421 |
} |
| 422 |
if (alpha < 128) { |
| 423 |
alpha_step = 1; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
| 428 |
SDL_RenderClear(screen); |
| 429 |
if (s_arrBindingOrder[s_iCurrentBinding] >= SDL_CONTROLLER_BUTTON_PADDLE1 && |
| 430 |
s_arrBindingOrder[s_iCurrentBinding] <= SDL_CONTROLLER_BUTTON_PADDLE4) { |
| 431 |
SDL_RenderCopy(screen, background_back, NULL, NULL); |
| 432 |
} else { |
| 433 |
SDL_RenderCopy(screen, background_front, NULL, NULL); |
| 434 |
} |
| 435 |
SDL_SetTextureAlphaMod(marker, alpha); |
| 436 |
SDL_SetTextureColorMod(marker, 10, 255, 21); |
| 437 |
SDL_RenderCopyEx(screen, marker, NULL, &dst, s_arrBindingDisplay[iElement].angle, NULL, SDL_FLIP_NONE); |
| 438 |
SDL_RenderPresent(screen); |
| 439 |
|
| 440 |
while (SDL_PollEvent(&event) > 0) { |
| 441 |
switch (event.type) { |
| 442 |
case SDL_JOYDEVICEREMOVED: |
| 443 |
if (event.jaxis.which == nJoystickID) { |
| 444 |
done = SDL_TRUE; |
| 445 |
} |
| 446 |
break; |
| 447 |
case SDL_JOYAXISMOTION: |
| 448 |
if (event.jaxis.which == nJoystickID) { |
| 449 |
const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */ |
| 450 |
AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis]; |
| 451 |
int nValue = event.jaxis.value; |
| 452 |
int nCurrentDistance, nFarthestDistance; |
| 453 |
if (!pAxisState->m_bMoving) { |
| 454 |
Sint16 nInitialValue; |
| 455 |
pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, event.jaxis.axis, &nInitialValue); |
| 456 |
pAxisState->m_nLastValue = nValue; |
| 457 |
pAxisState->m_nStartingValue = nInitialValue; |
| 458 |
pAxisState->m_nFarthestValue = nInitialValue; |
| 459 |
} else if (SDL_abs(nValue - pAxisState->m_nLastValue) <= MAX_ALLOWED_JITTER) { |
| 460 |
break; |
| 461 |
} else { |
| 462 |
pAxisState->m_nLastValue = nValue; |
| 463 |
} |
| 464 |
nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue); |
| 465 |
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue); |
| 466 |
if (nCurrentDistance > nFarthestDistance) { |
| 467 |
pAxisState->m_nFarthestValue = nValue; |
| 468 |
nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue); |
| 469 |
} |
| 470 |
|
| 471 |
#ifdef DEBUG_CONTROLLERMAP |
| 472 |
SDL_Log("AXIS %d nValue %d nCurrentDistance %d nFarthestDistance %d\n", event.jaxis.axis, nValue, nCurrentDistance, nFarthestDistance); |
| 473 |
#endif |
| 474 |
if (nFarthestDistance >= 16000) { |
| 475 |
/* If we've gone out far enough and started to come back, let's bind this axis */ |
| 476 |
SDL_bool bCommitBinding = (nCurrentDistance <= 10000) ? SDL_TRUE : SDL_FALSE; |
| 477 |
SDL_GameControllerExtendedBind binding; |
| 478 |
SDL_zero(binding); |
| 479 |
binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS; |
| 480 |
binding.value.axis.axis = event.jaxis.axis; |
| 481 |
binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue); |
| 482 |
binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue); |
| 483 |
binding.committed = bCommitBinding; |
| 484 |
ConfigureBinding(&binding); |
| 485 |
} |
| 486 |
} |
| 487 |
break; |
| 488 |
case SDL_JOYHATMOTION: |
| 489 |
if (event.jhat.which == nJoystickID) { |
| 490 |
if (event.jhat.value != SDL_HAT_CENTERED) { |
| 491 |
SDL_GameControllerExtendedBind binding; |
| 492 |
|
| 493 |
#ifdef DEBUG_CONTROLLERMAP |
| 494 |
SDL_Log("HAT %d %d\n", event.jhat.hat, event.jhat.value); |
| 495 |
#endif |
| 496 |
SDL_zero(binding); |
| 497 |
binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT; |
| 498 |
binding.value.hat.hat = event.jhat.hat; |
| 499 |
binding.value.hat.hat_mask = event.jhat.value; |
| 500 |
binding.committed = SDL_TRUE; |
| 501 |
ConfigureBinding(&binding); |
| 502 |
} |
| 503 |
} |
| 504 |
break; |
| 505 |
case SDL_JOYBUTTONUP: |
| 506 |
if (event.jbutton.which == nJoystickID) { |
| 507 |
SDL_GameControllerExtendedBind binding; |
| 508 |
|
| 509 |
#ifdef DEBUG_CONTROLLERMAP |
| 510 |
SDL_Log("BUTTON %d\n", event.jbutton.button); |
| 511 |
#endif |
| 512 |
SDL_zero(binding); |
| 513 |
binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON; |
| 514 |
binding.value.button = event.jbutton.button; |
| 515 |
binding.committed = SDL_TRUE; |
| 516 |
ConfigureBinding(&binding); |
| 517 |
} |
| 518 |
break; |
| 519 |
case SDL_FINGERDOWN: |
| 520 |
case SDL_MOUSEBUTTONDOWN: |
| 521 |
/* Skip this step */ |
| 522 |
SetCurrentBinding(s_iCurrentBinding + 1); |
| 523 |
break; |
| 524 |
case SDL_KEYDOWN: |
| 525 |
if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) { |
| 526 |
SetCurrentBinding(s_iCurrentBinding - 1); |
| 527 |
break; |
| 528 |
} |
| 529 |
if (event.key.keysym.sym == SDLK_SPACE) { |
| 530 |
SetCurrentBinding(s_iCurrentBinding + 1); |
| 531 |
break; |
| 532 |
} |
| 533 |
|
| 534 |
if (event.key.keysym.sym != SDLK_ESCAPE) { |
| 535 |
break; |
| 536 |
} |
| 537 |
SDL_FALLTHROUGH; |
| 538 |
case SDL_QUIT: |
| 539 |
done = SDL_TRUE; |
| 540 |
break; |
| 541 |
default: |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
SDL_Delay(15); |
| 547 |
|
| 548 |
/* Wait 30 ms for joystick events to stop coming in, |
| 549 |
in case a controller sends multiple events for a single control (e.g. axis and button for trigger) |
| 550 |
*/ |
| 551 |
if (s_unPendingAdvanceTime && SDL_GetTicks() - s_unPendingAdvanceTime >= 30) { |
| 552 |
SetCurrentBinding(s_iCurrentBinding + 1); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
if (s_bBindingComplete) { |
| 557 |
SDL_JoystickGUID guid; |
| 558 |
Uint16 crc; |
| 559 |
char mapping[1024]; |
| 560 |
char trimmed_name[128]; |
| 561 |
char *spot; |
| 562 |
int iIndex; |
| 563 |
char pszElement[12]; |
| 564 |
|
| 565 |
SDL_strlcpy(trimmed_name, name, SDL_arraysize(trimmed_name)); |
| 566 |
while (SDL_isspace(trimmed_name[0])) { |
| 567 |
SDL_memmove(&trimmed_name[0], &trimmed_name[1], SDL_strlen(trimmed_name)); |
| 568 |
} |
| 569 |
while (trimmed_name[0] && SDL_isspace(trimmed_name[SDL_strlen(trimmed_name) - 1])) { |
| 570 |
trimmed_name[SDL_strlen(trimmed_name) - 1] = '\0'; |
| 571 |
} |
| 572 |
while ((spot = SDL_strchr(trimmed_name, ',')) != NULL) { |
| 573 |
SDL_memmove(spot, spot + 1, SDL_strlen(spot)); |
| 574 |
} |
| 575 |
|
| 576 |
/* Initialize mapping with GUID and name */ |
| 577 |
guid = SDL_JoystickGetGUID(joystick); |
| 578 |
SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc); |
| 579 |
if (crc) { |
| 580 |
/* Clear the CRC from the GUID for the mapping */ |
| 581 |
guid.data[2] = 0; |
| 582 |
guid.data[3] = 0; |
| 583 |
} |
| 584 |
SDL_JoystickGetGUIDString(guid, mapping, SDL_arraysize(mapping)); |
| 585 |
SDL_strlcat(mapping, ",", SDL_arraysize(mapping)); |
| 586 |
SDL_strlcat(mapping, trimmed_name, SDL_arraysize(mapping)); |
| 587 |
SDL_strlcat(mapping, ",", SDL_arraysize(mapping)); |
| 588 |
SDL_strlcat(mapping, "platform:", SDL_arraysize(mapping)); |
| 589 |
SDL_strlcat(mapping, SDL_GetPlatform(), SDL_arraysize(mapping)); |
| 590 |
SDL_strlcat(mapping, ",", SDL_arraysize(mapping)); |
| 591 |
if (crc) { |
| 592 |
char crc_string[5]; |
| 593 |
|
| 594 |
SDL_strlcat(mapping, "crc:", SDL_arraysize(mapping)); |
| 595 |
(void)SDL_snprintf(crc_string, sizeof(crc_string), "%.4x", crc); |
| 596 |
SDL_strlcat(mapping, crc_string, SDL_arraysize(mapping)); |
| 597 |
SDL_strlcat(mapping, ",", SDL_arraysize(mapping)); |
| 598 |
} |
| 599 |
|
| 600 |
for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) { |
| 601 |
SDL_GameControllerExtendedBind *pBinding = &s_arrBindings[iIndex]; |
| 602 |
if (pBinding->bindType == SDL_CONTROLLER_BINDTYPE_NONE) { |
| 603 |
continue; |
| 604 |
} |
| 605 |
|
| 606 |
if (iIndex < SDL_CONTROLLER_BUTTON_MAX) { |
| 607 |
SDL_GameControllerButton eButton = (SDL_GameControllerButton)iIndex; |
| 608 |
SDL_strlcat(mapping, SDL_GameControllerGetStringForButton(eButton), SDL_arraysize(mapping)); |
| 609 |
} else { |
| 610 |
const char *pszAxisName = NULL; |
| 611 |
switch (iIndex - SDL_CONTROLLER_BUTTON_MAX) { |
| 612 |
case SDL_CONTROLLER_BINDING_AXIS_LEFTX_NEGATIVE: |
| 613 |
if (!BMergeAxisBindings(iIndex)) { |
| 614 |
SDL_strlcat(mapping, "-", SDL_arraysize(mapping)); |
| 615 |
} |
| 616 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX); |
| 617 |
break; |
| 618 |
case SDL_CONTROLLER_BINDING_AXIS_LEFTX_POSITIVE: |
| 619 |
SDL_strlcat(mapping, "+", SDL_arraysize(mapping)); |
| 620 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTX); |
| 621 |
break; |
| 622 |
case SDL_CONTROLLER_BINDING_AXIS_LEFTY_NEGATIVE: |
| 623 |
if (!BMergeAxisBindings(iIndex)) { |
| 624 |
SDL_strlcat(mapping, "-", SDL_arraysize(mapping)); |
| 625 |
} |
| 626 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY); |
| 627 |
break; |
| 628 |
case SDL_CONTROLLER_BINDING_AXIS_LEFTY_POSITIVE: |
| 629 |
SDL_strlcat(mapping, "+", SDL_arraysize(mapping)); |
| 630 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_LEFTY); |
| 631 |
break; |
| 632 |
case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_NEGATIVE: |
| 633 |
if (!BMergeAxisBindings(iIndex)) { |
| 634 |
SDL_strlcat(mapping, "-", SDL_arraysize(mapping)); |
| 635 |
} |
| 636 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX); |
| 637 |
break; |
| 638 |
case SDL_CONTROLLER_BINDING_AXIS_RIGHTX_POSITIVE: |
| 639 |
SDL_strlcat(mapping, "+", SDL_arraysize(mapping)); |
| 640 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTX); |
| 641 |
break; |
| 642 |
case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_NEGATIVE: |
| 643 |
if (!BMergeAxisBindings(iIndex)) { |
| 644 |
SDL_strlcat(mapping, "-", SDL_arraysize(mapping)); |
| 645 |
} |
| 646 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY); |
| 647 |
break; |
| 648 |
case SDL_CONTROLLER_BINDING_AXIS_RIGHTY_POSITIVE: |
| 649 |
SDL_strlcat(mapping, "+", SDL_arraysize(mapping)); |
| 650 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_RIGHTY); |
| 651 |
break; |
| 652 |
case SDL_CONTROLLER_BINDING_AXIS_TRIGGERLEFT: |
| 653 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERLEFT); |
| 654 |
break; |
| 655 |
case SDL_CONTROLLER_BINDING_AXIS_TRIGGERRIGHT: |
| 656 |
pszAxisName = SDL_GameControllerGetStringForAxis(SDL_CONTROLLER_AXIS_TRIGGERRIGHT); |
| 657 |
break; |
| 658 |
} |
| 659 |
if (pszAxisName) { |
| 660 |
SDL_strlcat(mapping, pszAxisName, SDL_arraysize(mapping)); |
| 661 |
} |
| 662 |
} |
| 663 |
SDL_strlcat(mapping, ":", SDL_arraysize(mapping)); |
| 664 |
|
| 665 |
pszElement[0] = '\0'; |
| 666 |
switch (pBinding->bindType) { |
| 667 |
case SDL_CONTROLLER_BINDTYPE_BUTTON: |
| 668 |
(void)SDL_snprintf(pszElement, sizeof(pszElement), "b%d", pBinding->value.button); |
| 669 |
break; |
| 670 |
case SDL_CONTROLLER_BINDTYPE_AXIS: |
| 671 |
if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MIN) { |
| 672 |
/* The negative half axis */ |
| 673 |
(void)SDL_snprintf(pszElement, sizeof(pszElement), "-a%d", pBinding->value.axis.axis); |
| 674 |
} else if (pBinding->value.axis.axis_min == 0 && pBinding->value.axis.axis_max == SDL_JOYSTICK_AXIS_MAX) { |
| 675 |
/* The positive half axis */ |
| 676 |
(void)SDL_snprintf(pszElement, sizeof(pszElement), "+a%d", pBinding->value.axis.axis); |
| 677 |
} else { |
| 678 |
(void)SDL_snprintf(pszElement, sizeof(pszElement), "a%d", pBinding->value.axis.axis); |
| 679 |
if (pBinding->value.axis.axis_min > pBinding->value.axis.axis_max) { |
| 680 |
/* Invert the axis */ |
| 681 |
SDL_strlcat(pszElement, "~", SDL_arraysize(pszElement)); |
| 682 |
} |
| 683 |
} |
| 684 |
break; |
| 685 |
case SDL_CONTROLLER_BINDTYPE_HAT: |
| 686 |
(void)SDL_snprintf(pszElement, sizeof(pszElement), "h%d.%d", pBinding->value.hat.hat, pBinding->value.hat.hat_mask); |
| 687 |
break; |
| 688 |
default: |
| 689 |
SDL_assert(!"Unknown bind type"); |
| 690 |
break; |
| 691 |
} |
| 692 |
SDL_strlcat(mapping, pszElement, SDL_arraysize(mapping)); |
| 693 |
SDL_strlcat(mapping, ",", SDL_arraysize(mapping)); |
| 694 |
} |
| 695 |
|
| 696 |
SDL_Log("Mapping:\n\n%s\n\n", mapping); |
| 697 |
/* Print to stdout as well so the user can cat the output somewhere */ |
| 698 |
printf("%s\n", mapping); |
| 699 |
} |
| 700 |
|
| 701 |
SDL_free(s_arrAxisState); |
| 702 |
s_arrAxisState = NULL; |
| 703 |
|
| 704 |
SDL_DestroyRenderer(screen); |
| 705 |
} |
| 706 |
|
| 707 |
int main(int argc, char *argv[]) |
| 708 |
{ |
| 709 |
const char *name; |
| 710 |
int i; |
| 711 |
int joystick_index; |
| 712 |
SDL_Joystick *joystick; |
| 713 |
|
| 714 |
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); |
| 715 |
|
| 716 |
/* Enable standard application logging */ |
| 717 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 718 |
|
| 719 |
/* Initialize SDL (Note: video is required to start event loop) */ |
| 720 |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { |
| 721 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 722 |
exit(1); |
| 723 |
} |
| 724 |
|
| 725 |
if (argv[1] && SDL_strcmp(argv[1], "--bind-touchpad") == 0) { |
| 726 |
bind_touchpad = SDL_TRUE; |
| 727 |
} |
| 728 |
|
| 729 |
/* Create a window to display joystick axis position */ |
| 730 |
window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED, |
| 731 |
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, |
| 732 |
SCREEN_HEIGHT, 0); |
| 733 |
if (!window) { |
| 734 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError()); |
| 735 |
return 2; |
| 736 |
} |
| 737 |
|
| 738 |
screen = SDL_CreateRenderer(window, -1, 0); |
| 739 |
if (!screen) { |
| 740 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); |
| 741 |
return 2; |
| 742 |
} |
| 743 |
|
| 744 |
while (!done && SDL_NumJoysticks() == 0) { |
| 745 |
SDL_Event event; |
| 746 |
|
| 747 |
while (SDL_PollEvent(&event) > 0) { |
| 748 |
switch (event.type) { |
| 749 |
case SDL_KEYDOWN: |
| 750 |
if (event.key.keysym.sym != SDLK_ESCAPE) { |
| 751 |
break; |
| 752 |
} |
| 753 |
SDL_FALLTHROUGH; |
| 754 |
case SDL_QUIT: |
| 755 |
done = SDL_TRUE; |
| 756 |
break; |
| 757 |
default: |
| 758 |
break; |
| 759 |
} |
| 760 |
} |
| 761 |
SDL_RenderPresent(screen); |
| 762 |
} |
| 763 |
|
| 764 |
/* Print information about the joysticks */ |
| 765 |
SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks()); |
| 766 |
for (i = 0; i < SDL_NumJoysticks(); ++i) { |
| 767 |
name = SDL_JoystickNameForIndex(i); |
| 768 |
SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); |
| 769 |
joystick = SDL_JoystickOpen(i); |
| 770 |
if (!joystick) { |
| 771 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i, |
| 772 |
SDL_GetError()); |
| 773 |
} else { |
| 774 |
char guid[64]; |
| 775 |
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), |
| 776 |
guid, sizeof(guid)); |
| 777 |
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick)); |
| 778 |
SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick)); |
| 779 |
SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick)); |
| 780 |
SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); |
| 781 |
SDL_Log("instance id: %" SDL_PRIu32 "\n", SDL_JoystickInstanceID(joystick)); |
| 782 |
SDL_Log(" guid: %s\n", guid); |
| 783 |
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick)); |
| 784 |
SDL_JoystickClose(joystick); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
joystick_index = 0; |
| 789 |
for (i = 1; i < argc; ++i) { |
| 790 |
if (argv[i] && *argv[i] != '-') { |
| 791 |
joystick_index = SDL_atoi(argv[i]); |
| 792 |
break; |
| 793 |
} |
| 794 |
} |
| 795 |
joystick = SDL_JoystickOpen(joystick_index); |
| 796 |
if (!joystick) { |
| 797 |
SDL_Log("Couldn't open joystick %d: %s\n", joystick_index, SDL_GetError()); |
| 798 |
} else { |
| 799 |
WatchJoystick(joystick); |
| 800 |
SDL_JoystickClose(joystick); |
| 801 |
} |
| 802 |
|
| 803 |
SDL_DestroyWindow(window); |
| 804 |
|
| 805 |
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
#else |
| 811 |
|
| 812 |
int |
| 813 |
main(int argc, char *argv[]) |
| 814 |
{ |
| 815 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); |
| 816 |
return 1; |
| 817 |
} |
| 818 |
|
| 819 |
#endif |
| 820 |
|
| 821 |
/* vi: set ts=4 sw=4 expandtab: */ |