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