2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Game controller mapping generator */
14 /* Gabriel Jacobo <gabomdq@gmail.com> */
22 #ifndef SDL_JOYSTICK_DISABLED
25 #define SCREEN_WIDTH 320
26 #define SCREEN_HEIGHT 480
28 #define SCREEN_WIDTH 512
29 #define SCREEN_HEIGHT 317
33 #define MAP_HEIGHT 317
35 #define MARKER_BUTTON 1
38 typedef struct MappingStep
44 int axis, button, hat, hat_value;
50 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
55 /* Load the sprite image */
56 temp = SDL_LoadBMP(file);
58 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
62 /* Set transparent pixel as the pixel at (0,0) */
64 if (temp->format->palette) {
65 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
67 switch (temp->format->BitsPerPixel) {
69 SDL_SetColorKey(temp, SDL_TRUE,
70 (*(Uint16 *) temp->pixels) & 0x00007FFF);
73 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
76 SDL_SetColorKey(temp, SDL_TRUE,
77 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
80 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
86 /* Create textures from the image */
87 texture = SDL_CreateTextureFromSurface(renderer, temp);
89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
90 SDL_FreeSurface(temp);
93 SDL_FreeSurface(temp);
95 /* We're ready to roll. :) */
100 WatchJoystick(SDL_Joystick * joystick)
102 SDL_Window *window = NULL;
103 SDL_Renderer *screen = NULL;
104 SDL_Texture *background, *button, *axis, *marker;
105 const char *name = NULL;
106 SDL_bool retval = SDL_FALSE;
107 SDL_bool done = SDL_FALSE, next=SDL_FALSE;
111 Uint8 alpha=200, alpha_step = -1;
112 Uint32 alpha_ticks = 0;
113 char mapping[4096], temp[4096];
114 MappingStep *step, *prev_step;
115 MappingStep steps[] = {
116 {342, 132, 0.0, MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
117 {387, 167, 0.0, MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
118 {431, 132, 0.0, MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
119 {389, 101, 0.0, MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
120 {174, 132, 0.0, MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
121 {233, 132, 0.0, MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
122 {289, 132, 0.0, MARKER_BUTTON, "start", -1, -1, -1, -1, ""},
123 {116, 217, 0.0, MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
124 {154, 249, 0.0, MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
125 {186, 217, 0.0, MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
126 {154, 188, 0.0, MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
127 {77, 40, 0.0, MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
128 {91, 0, 0.0, MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
129 {396, 36, 0.0, MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
130 {375, 0, 0.0, MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
131 {75, 154, 0.0, MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
132 {305, 230, 0.0, MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
133 {75, 154, 0.0, MARKER_AXIS, "leftx", -1, -1, -1, -1, ""},
134 {75, 154, 90.0, MARKER_AXIS, "lefty", -1, -1, -1, -1, ""},
135 {305, 230, 0.0, MARKER_AXIS, "rightx", -1, -1, -1, -1, ""},
136 {305, 230, 90.0, MARKER_AXIS, "righty", -1, -1, -1, -1, ""},
139 /* Create a window to display joystick axis position */
140 window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
141 SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
143 if (window == NULL) {
144 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
148 screen = SDL_CreateRenderer(window, -1, 0);
149 if (screen == NULL) {
150 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
151 SDL_DestroyWindow(window);
155 background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
156 button = LoadTexture(screen, "button.bmp", SDL_TRUE);
157 axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
158 SDL_RaiseWindow(window);
160 /* scale for platforms that don't give you the window size you asked for. */
161 SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
163 /* Print info about the joystick we are watching */
164 name = SDL_JoystickName(joystick);
165 SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
166 name ? name : "Unknown Joystick");
167 SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
168 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
169 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
172 ====================================================================================\n\
173 Press the buttons on your controller when indicated\n\
174 (Your controller may look different than the picture)\n\
175 If you want to correct a mistake, press backspace or the back button on your device\n\
176 To skip a button, press SPACE or click/touch the screen\n\
177 To exit, press ESC\n\
178 ====================================================================================\n");
180 /* Initialize mapping with GUID and name */
181 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
182 SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
183 temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
185 /* Loop, getting joystick events! */
186 for(s=0; s<SDL_arraysize(steps) && !done;) {
187 /* blank screen, set up for drawing this frame. */
189 SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
193 step->hat_value = -1;
195 switch(step->marker) {
208 SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
211 SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
213 while (!done && !next) {
214 if (SDL_GetTicks() - alpha_ticks > 5) {
215 alpha_ticks = SDL_GetTicks();
225 SDL_RenderClear(screen);
226 SDL_RenderCopy(screen, background, NULL, NULL);
227 SDL_SetTextureAlphaMod(marker, alpha);
228 SDL_SetTextureColorMod(marker, 10, 255, 21);
229 SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
230 SDL_RenderPresent(screen);
232 if (SDL_PollEvent(&event)) {
233 switch (event.type) {
234 case SDL_JOYAXISMOTION:
235 if ((event.jaxis.value > 20000 || event.jaxis.value < -20000) && event.jaxis.value != -32768) {
236 for (_s = 0; _s < s; _s++) {
237 if (steps[_s].axis == event.jaxis.axis) {
242 step->axis = event.jaxis.axis;
243 SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
244 SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
245 SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
252 case SDL_JOYHATMOTION:
253 if (event.jhat.value == SDL_HAT_CENTERED) {
254 break; /* ignore centering, we're probably just coming back to the center from the previous item we set. */
256 for (_s = 0; _s < s; _s++) {
257 if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
262 step->hat = event.jhat.hat;
263 step->hat_value = event.jhat.value;
264 SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
265 SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
266 SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
271 case SDL_JOYBALLMOTION:
273 case SDL_JOYBUTTONUP:
274 for (_s = 0; _s < s; _s++) {
275 if (steps[_s].button == event.jbutton.button) {
280 step->button = event.jbutton.button;
281 SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
282 SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
283 SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
289 case SDL_MOUSEBUTTONDOWN:
295 if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
298 prev_step = &steps[--s];
299 SDL_strlcpy(mapping, prev_step->mapping, SDL_arraysize(prev_step->mapping));
304 if (event.key.keysym.sym == SDLK_SPACE) {
311 if ((event.key.keysym.sym != SDLK_ESCAPE)) {
314 /* Fall through to signal quit */
326 if (s == SDL_arraysize(steps) ) {
327 SDL_Log("Mapping:\n\n%s\n\n", mapping);
328 /* Print to stdout as well so the user can cat the output somewhere */
329 printf("%s\n", mapping);
332 while(SDL_PollEvent(&event)) {};
334 SDL_DestroyRenderer(screen);
335 SDL_DestroyWindow(window);
340 main(int argc, char *argv[])
344 SDL_Joystick *joystick;
346 /* Enable standard application logging */
347 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
349 /* Initialize SDL (Note: video is required to start event loop) */
350 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
351 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
355 /* Print information about the joysticks */
356 SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
357 for (i = 0; i < SDL_NumJoysticks(); ++i) {
358 name = SDL_JoystickNameForIndex(i);
359 SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
360 joystick = SDL_JoystickOpen(i);
361 if (joystick == NULL) {
362 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
366 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
367 guid, sizeof (guid));
368 SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
369 SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
370 SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
371 SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
372 SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
373 SDL_Log(" guid: %s\n", guid);
374 SDL_JoystickClose(joystick);
379 if (SDL_NumJoysticks() > 0) {
383 SDL_bool reportederror = SDL_FALSE;
384 SDL_bool keepGoing = SDL_TRUE;
390 device = atoi(argv[1]);
392 joystick = SDL_JoystickOpen(device);
394 while ( keepGoing ) {
395 if (joystick == NULL) {
396 if ( !reportederror ) {
397 SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
398 keepGoing = SDL_FALSE;
399 reportederror = SDL_TRUE;
402 reportederror = SDL_FALSE;
403 keepGoing = WatchJoystick(joystick);
404 SDL_JoystickClose(joystick);
409 SDL_Log("Waiting for attach\n");
412 SDL_WaitEvent(&event);
413 if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
414 || (event.type == SDL_MOUSEBUTTONDOWN)) {
415 keepGoing = SDL_FALSE;
416 } else if (event.type == SDL_JOYDEVICEADDED) {
417 joystick = SDL_JoystickOpen(device);
424 SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
426 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
434 main(int argc, char *argv[])
436 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");