src/joystick/SDL_joystick.c
branchSDL-1.2
changeset 6047 54b2716f595d
parent 5858 b406b7d61494
child 6137 4720145f848b
equal deleted inserted replaced
5988:bf927e528813 6047:54b2716f595d
    35 #define SDL_Lock_EventThread()
    35 #define SDL_Lock_EventThread()
    36 #define SDL_Unlock_EventThread()
    36 #define SDL_Unlock_EventThread()
    37 #endif
    37 #endif
    38 
    38 
    39 Uint8 SDL_numjoysticks = 0;
    39 Uint8 SDL_numjoysticks = 0;
       
    40 int SDL_allocatedjoysticks = 0;
    40 SDL_Joystick **SDL_joysticks = NULL;
    41 SDL_Joystick **SDL_joysticks = NULL;
    41 
    42 
    42 int SDL_JoystickInit(void)
    43 int SDL_JoystickInit(void)
    43 {
    44 {
    44 	int arraylen;
    45 	int arraylen;
    45 	int status;
    46 	int status;
    46 
    47 
    47 	SDL_numjoysticks = 0;
    48 	SDL_numjoysticks = 0;
    48 	status = SDL_SYS_JoystickInit();
    49 	status = SDL_SYS_JoystickInit();
    49 	if ( status >= 0 ) {
    50 	if ( status >= 0 ) {
    50 		arraylen = (status+1)*sizeof(*SDL_joysticks);
    51 		SDL_allocatedjoysticks = status;
       
    52 		arraylen = (SDL_allocatedjoysticks+1)*sizeof(*SDL_joysticks);
    51 		SDL_joysticks = (SDL_Joystick **)SDL_malloc(arraylen);
    53 		SDL_joysticks = (SDL_Joystick **)SDL_malloc(arraylen);
    52 		if ( SDL_joysticks == NULL ) {
    54 		if ( SDL_joysticks == NULL ) {
    53 			SDL_numjoysticks = 0;
    55 			SDL_numjoysticks = 0;
       
    56 			SDL_allocatedjoysticks = 0;
    54 		} else {
    57 		} else {
    55 			SDL_memset(SDL_joysticks, 0, arraylen);
    58 			SDL_memset(SDL_joysticks, 0, arraylen);
    56 			SDL_numjoysticks = status;
    59 			SDL_numjoysticks = status;
    57 		}
    60 		}
    58 		status = 0;
    61 		status = 0;
   368 
   371 
   369 	/* Remove joystick from list */
   372 	/* Remove joystick from list */
   370 	for ( i=0; SDL_joysticks[i]; ++i ) {
   373 	for ( i=0; SDL_joysticks[i]; ++i ) {
   371 		if ( joystick == SDL_joysticks[i] ) {
   374 		if ( joystick == SDL_joysticks[i] ) {
   372 			SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i+1],
   375 			SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i+1],
   373 			       (SDL_numjoysticks-i)*sizeof(joystick));
   376 			       (SDL_allocatedjoysticks-i)*sizeof(joystick));
   374 			break;
   377 			break;
   375 		}
   378 		}
   376 	}
   379 	}
   377 
   380 
   378 	/* Let the event thread keep running */
   381 	/* Let the event thread keep running */
   417 	/* Quit the joystick setup */
   420 	/* Quit the joystick setup */
   418 	SDL_SYS_JoystickQuit();
   421 	SDL_SYS_JoystickQuit();
   419 	if ( SDL_joysticks ) {
   422 	if ( SDL_joysticks ) {
   420 		SDL_free(SDL_joysticks);
   423 		SDL_free(SDL_joysticks);
   421 		SDL_joysticks = NULL;
   424 		SDL_joysticks = NULL;
       
   425 		SDL_allocatedjoysticks = 0;
   422 	}
   426 	}
   423 }
   427 }
   424 
   428 
   425 
   429 
   426 /* These are global for SDL_sysjoystick.c and SDL_events.c */
   430 /* These are global for SDL_sysjoystick.c and SDL_events.c */