Skip to content

Commit d9c6545

Browse files
committed
Fixes controller rumble
1 parent 446a30b commit d9c6545

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

src/gamecontroller.c

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "util.h"
2121

2222
static SDL_Gamepad *controller = NULL;
23-
static SDL_Haptic *haptic = NULL;
2423
static Uint8 controllerMode = GAMEPAD_TYPE_NONE;
2524

2625
void
@@ -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

6954
void
7055
gamecontroller_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
}

src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ handle_events(void)
797797
} else if (event.type == SDL_EVENT_GAMEPAD_REMOVED) {
798798
debug("Gamepad removed");
799799
gamecontroller_set(NULL);
800+
skillbar_set_controller_mode(GAMEPAD_TYPE_NONE);
801+
tooltip_manager_set_controller_mode(GAMEPAD_TYPE_NONE);
800802
}
801803

802804
input_handle_event(&input, &event, &device_type);

0 commit comments

Comments
 (0)