src/joystick/SDL_gamecontroller.c
changeset 6823 e9d312d26979
parent 6738 b408d5a406a3
child 6841 90bba8d8e42a
equal deleted inserted replaced
6822:18f6818ba281 6823:e9d312d26979
   516 /*
   516 /*
   517  * grab the name string from a mapping string
   517  * grab the name string from a mapping string
   518  */
   518  */
   519 char *SDL_PrivateGetControllerNameFromMappingString( const char *pMapping )
   519 char *SDL_PrivateGetControllerNameFromMappingString( const char *pMapping )
   520 {
   520 {
   521 	const char *pFirstComma = SDL_strchr( pMapping, ',' );
   521 	const char *pFirstComma, *pSecondComma;
   522 	const char *pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
   522     char *pchName;
   523 	if ( pFirstComma && pSecondComma )
   523 
   524 	{
   524     pFirstComma = SDL_strchr( pMapping, ',' );
   525 		char *pchName = SDL_malloc( pSecondComma - pFirstComma );
   525     if ( !pFirstComma )
   526 		if ( !pchName )
   526         return NULL;
   527 		{
   527 
   528 			SDL_OutOfMemory();
   528 	pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
   529 			return NULL;
   529     if ( !pSecondComma )
   530 		}
   530         return NULL;
   531 		SDL_memcpy( pchName, pFirstComma + 1, pSecondComma - pFirstComma );
   531 
   532 		pchName[ pSecondComma - pFirstComma - 1 ] = 0;
   532     pchName = SDL_malloc( pSecondComma - pFirstComma );
   533 		return pchName;
   533     if ( !pchName )
   534 	}
   534     {
   535 	return NULL;
   535         SDL_OutOfMemory();
       
   536         return NULL;
       
   537     }
       
   538     SDL_memcpy( pchName, pFirstComma + 1, pSecondComma - pFirstComma );
       
   539     pchName[ pSecondComma - pFirstComma - 1 ] = 0;
       
   540     return pchName;
   536 }
   541 }
   537 
   542 
   538 
   543 
   539 /*
   544 /*
   540  * grab the button mapping string from a mapping string
   545  * grab the button mapping string from a mapping string
   541  */
   546  */
   542 const char *SDL_PrivateGetControllerMappingFromMappingString( const char *pMapping )
   547 const char *SDL_PrivateGetControllerMappingFromMappingString( const char *pMapping )
   543 {
   548 {
   544 	const char *pFirstComma = SDL_strchr( pMapping, ',' );
   549 	const char *pFirstComma, *pSecondComma;
   545 	const char *pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
   550 
   546 	if ( pSecondComma )
   551     pFirstComma = SDL_strchr( pMapping, ',' );
   547 		return pSecondComma + 1; // mapping is everything after the 3rd comma, no need to malloc it
   552     if ( !pFirstComma )
   548 	else
   553         return NULL;
   549 		return NULL;
   554 
       
   555 	pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
       
   556     if ( !pSecondComma )
       
   557         return NULL;
       
   558 
       
   559     return pSecondComma + 1; /* mapping is everything after the 3rd comma, no need to malloc it */
   550 }
   560 }
   551 
   561 
   552 
   562 
   553 /*
   563 /*
   554  * Initialize the game controller system, mostly load our DB of controller config mappings
   564  * Initialize the game controller system, mostly load our DB of controller config mappings
   601 	{
   611 	{
   602 		const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
   612 		const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
   603 		if ( hint && hint[0] )
   613 		if ( hint && hint[0] )
   604 		{
   614 		{
   605 			int nchHints = SDL_strlen( hint );
   615 			int nchHints = SDL_strlen( hint );
   606 			char *pUserMappings = SDL_malloc( nchHints + 1 );
   616 			char *pUserMappings = SDL_malloc( nchHints + 1 ); /* FIXME: memory leak, but we can't free it in this function because pchMapping below points into this memory */
   607 			SDL_memcpy( pUserMappings, hint, nchHints );
   617 			SDL_memcpy( pUserMappings, hint, nchHints + 1 );
   608 			while ( pUserMappings )
   618 			while ( pUserMappings )
   609 			{
   619 			{
   610 				char *pchGUID;
   620 				char *pchGUID;
   611 				char *pchName;
   621 				char *pchName;
   612 				const char *pchMapping;
   622 				const char *pchMapping;