2020#include "util.h"
2121
2222static SDL_Gamepad * controller = NULL ;
23- static SDL_Haptic * haptic = NULL ;
2423static Uint8 controllerMode = GAMEPAD_TYPE_NONE ;
2524
2625void
@@ -32,50 +31,35 @@ gamecontroller_set(SDL_Gamepad *ctrler)
3231 SDL_CloseGamepad (controller );
3332 controller = NULL ;
3433 }
35- if (haptic ) {
36- SDL_CloseHaptic (haptic );
37- haptic = NULL ;
38- }
3934 controllerMode = GAMEPAD_TYPE_NONE ;
35+ SDL_SetGamepadEventsEnabled (false);
4036 return ;
4137 }
4238
4339 controller = ctrler ;
4440
4541 const char * ctrlName = SDL_GetGamepadName (controller );
4642 info ("Game controller connected: %s" , ctrlName );
43+ SDL_SetGamepadEventsEnabled (true);
4744
4845 // Try to determine if this is a PS3/4 controller
4946 if (ctrlName [0 ] == 'P' &&
5047 ctrlName [1 ] == 'S' &&
51- (ctrlName [2 ] == '4' || ctrlName [2 ] == '3' ))
48+ (ctrlName [2 ] == '4' || ctrlName [2 ] == '3' || ctrlName [ 2 ] == '5' ))
5249 controllerMode = GAMEPAD_TYPE_PS ;
5350 else
5451 controllerMode = GAMEPAD_TYPE_XB ;
55-
56- haptic = SDL_OpenHapticFromJoystick (SDL_GetGamepadJoystick (controller ));
57- if (haptic ) {
58- info ("Haptics are supported by controller: %s" , ctrlName );
59- if (SDL_InitHapticRumble (haptic )) {
60- info ("Haptics enabled for controller: %s" , ctrlName );
61- } else {
62- info ("Failed to enable haptics for: %s" , ctrlName );
63- }
64- } else {
65- info ("Haptics not supported by controller: %s" , ctrlName );
66- }
6752}
6853
6954void
7055gamecontroller_rumble (float intensity , Uint32 duration )
7156{
72- if (controllerMode == GAMEPAD_TYPE_NONE )
73- return ;
74-
75- if (!haptic )
57+ if (controllerMode == GAMEPAD_TYPE_NONE || controller == NULL )
7658 return ;
7759
78- if (SDL_PlayHapticRumble (haptic , intensity , duration ) != 0 )
60+ uint16_t power = (uint16_t )(intensity * 0xFFFF );
61+ debug ("Playing rumble: 0x%hX, %d" , power , duration );
62+ if (!SDL_RumbleGamepad (controller , power , power , duration ))
7963 error ("Failed to play rumble: %s" , SDL_GetError ());
8064}
8165
@@ -90,6 +74,4 @@ gamecontroller_close(void)
9074{
9175 if (controller )
9276 SDL_CloseGamepad (controller );
93- if (haptic )
94- SDL_CloseHaptic (haptic );
9577}
0 commit comments