Adds SDL_GameControllerAddMappingsFromRW, updates controllermap SDL_GameControllerAddMappingsFromFile is now a convenience macro. controllermap can now skip bindings by pressing space or clicking/touching the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h
index 84d58d8..3d49125 100644
--- a/include/SDL_gamecontroller.h
+++ b/include/SDL_gamecontroller.h
@@ -109,12 +109,21 @@ typedef struct SDL_GameControllerButtonBind
*/
/**
- * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
+ * Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform()
* A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
*
+ * If \c freerw is non-zero, the stream will be closed after being read.
+ *
* \return number of mappings added, -1 on error
*/
-extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromFile( const char* mapDB );
+extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw );
+
+/**
+ * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
+ *
+ * Convenience macro.
+ */
+#define SDL_GameControllerAddMappingsFromFile(file) SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
/**
* Add or update an existing mapping configuration
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 1563f51..59c56fc 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -658,32 +658,37 @@ void SDL_PrivateGameControllerRefreshMapping( ControllerMapping_t *pControllerMa
* Add or update an entry into the Mappings Database
*/
int
-SDL_GameControllerAddMappingsFromFile( const char* mapDB )
+SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw )
{
const char *platform = SDL_GetPlatform();
- SDL_RWops *rw;
int controllers = 0;
char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
size_t db_size, platform_len;
- rw = SDL_RWFromFile(mapDB, "rb");
if (rw == NULL) {
- return SDL_SetError("Could not open %s", mapDB);
+ return SDL_SetError("Invalid RWops");
}
db_size = SDL_RWsize(rw);
buf = (char *) SDL_malloc(db_size + 1);
if (buf == NULL) {
- SDL_RWclose(rw);
+ if (freerw) {
+ SDL_RWclose(rw);
+ }
return SDL_SetError("Could allocate space to not read DB into memory");
}
if (SDL_RWread(rw, buf, db_size, 1) != 1) {
- SDL_RWclose(rw);
+ if (freerw) {
+ SDL_RWclose(rw);
+ }
SDL_free(buf);
return SDL_SetError("Could not read DB");
}
- SDL_RWclose(rw);
+
+ if (freerw) {
+ SDL_RWclose(rw);
+ }
buf[db_size] = '\0';
line = buf;
diff --git a/test/controllermap.c b/test/controllermap.c
index 2c943cf..dc943cb 100644
--- a/test/controllermap.c
+++ b/test/controllermap.c
@@ -144,7 +144,7 @@ WatchJoystick(SDL_Joystick * joystick)
};
/* Create a window to display joystick axis position */
- window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
+ window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
if (window == NULL) {
@@ -178,6 +178,7 @@ WatchJoystick(SDL_Joystick * joystick)
Press the buttons on your controller when indicated\n\
(Your controller may look different than the picture)\n\
If you want to correct a mistake, press backspace or the back button on your device\n\
+ To skip a button, press SPACE or click/touch the screen\n\
To exit, press ESC\n\
====================================================================================\n");
@@ -287,6 +288,12 @@ WatchJoystick(SDL_Joystick * joystick)
next=SDL_TRUE;
}
break;
+ case SDL_FINGERDOWN:
+ case SDL_MOUSEBUTTONDOWN:
+ /* Skip this step */
+ s++;
+ next=SDL_TRUE;
+ break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
/* Undo! */
@@ -297,12 +304,17 @@ WatchJoystick(SDL_Joystick * joystick)
}
break;
}
+ if (event.key.keysym.sym == SDLK_SPACE) {
+ /* Skip this step */
+ s++;
+ next=SDL_TRUE;
+ break;
+ }
+
if ((event.key.keysym.sym != SDLK_ESCAPE)) {
break;
}
/* Fall through to signal quit */
- case SDL_FINGERDOWN:
- case SDL_MOUSEBUTTONDOWN:
case SDL_QUIT:
done = SDL_TRUE;
break;