src/joystick/SDL_gamecontroller.c
changeset 8042 7863121cb7d5
parent 8009 c185ac7fb771
child 8046 e7c89b29932a
equal deleted inserted replaced
8041:afd62b3fda31 8042:7863121cb7d5
    31 #if !SDL_EVENTS_DISABLED
    31 #if !SDL_EVENTS_DISABLED
    32 #include "../events/SDL_events_c.h"
    32 #include "../events/SDL_events_c.h"
    33 #endif
    33 #endif
    34 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
    34 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
    35 
    35 
       
    36 #define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
    36 
    37 
    37 /* a list of currently opened game controllers */
    38 /* a list of currently opened game controllers */
    38 static SDL_GameController *SDL_gamecontrollers = NULL;
    39 static SDL_GameController *SDL_gamecontrollers = NULL;
    39 
    40 
    40 /* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
    41 /* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
   655 
   656 
   656 /*
   657 /*
   657  * Add or update an entry into the Mappings Database
   658  * Add or update an entry into the Mappings Database
   658  */
   659  */
   659 int
   660 int
       
   661 SDL_GameControllerAddMappingsFromFile( const char* mapDB )
       
   662 {
       
   663     const char *platform = SDL_GetPlatform();
       
   664     SDL_RWops *rw;
       
   665     int controllers = 0;
       
   666     char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
       
   667     size_t db_size, platform_len;
       
   668     
       
   669     rw = SDL_RWFromFile(mapDB, "rb");
       
   670     if (rw == NULL) {
       
   671         return SDL_SetError("Could not open %s", mapDB);
       
   672     }
       
   673     db_size = SDL_RWsize(rw);
       
   674     
       
   675     buf = (char *) SDL_malloc(db_size + 1);
       
   676     if (buf == NULL) {
       
   677         SDL_RWclose(rw);
       
   678         return SDL_SetError("Could allocate space to not read DB into memory");
       
   679     }
       
   680     
       
   681     if (SDL_RWread(rw, buf, db_size, 1) != 1) {
       
   682         SDL_RWclose(rw);
       
   683         SDL_free(buf);
       
   684         return SDL_SetError("Could not read DB");
       
   685     }
       
   686     SDL_RWclose(rw);
       
   687     
       
   688     buf[db_size] = '\0';
       
   689     line = buf;
       
   690     
       
   691     while (line < buf + db_size) {
       
   692         line_end = SDL_strchr( line, '\n' );
       
   693         if (line_end != NULL) {
       
   694             *line_end = '\0';
       
   695         }
       
   696         else {
       
   697             line_end = buf + db_size;
       
   698         }
       
   699         
       
   700         /* Extract and verify the platform */
       
   701         tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
       
   702         if ( tmp != NULL ) {
       
   703             tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
       
   704             comma = SDL_strchr(tmp, ',');
       
   705             if (comma != NULL) {
       
   706                 platform_len = comma - tmp + 1;
       
   707                 if (platform_len + 1 < SDL_arraysize(line_platform)) {
       
   708                     SDL_strlcpy(line_platform, tmp, platform_len);
       
   709                     if(SDL_strncasecmp(line_platform, platform, platform_len) == 0
       
   710                         && SDL_GameControllerAddMapping(line) > 0) {
       
   711                         controllers++;
       
   712                     }
       
   713                 }
       
   714             }
       
   715         }
       
   716         
       
   717         line = line_end + 1;
       
   718     }
       
   719 
       
   720     SDL_free(buf);
       
   721     return controllers;
       
   722 }
       
   723 
       
   724 /*
       
   725  * Add or update an entry into the Mappings Database
       
   726  */
       
   727 int
   660 SDL_GameControllerAddMapping( const char *mappingString )
   728 SDL_GameControllerAddMapping( const char *mappingString )
   661 {
   729 {
   662     char *pchGUID;
   730     char *pchGUID;
   663     char *pchName;
   731     char *pchName;
   664     char *pchMapping;
   732     char *pchMapping;