@@ -81,6 +81,11 @@ bool XBoxController::Initialize() {
81
81
SDL_GameControllerEventState (SDL_ENABLE);
82
82
m_pJoystick = candidateJoystick;
83
83
return true ;
84
+ } else if (num_axes == 7 && num_buttons == 15 && num_hats == 1 ) {
85
+ std::cout << " Connected to new XBox Series controller\n " ;
86
+ SDL_GameControllerEventState (SDL_ENABLE);
87
+ m_pJoystick = candidateJoystick;
88
+ return true ;
84
89
} else {
85
90
std::cout << " VendorID: " << SDL_GameControllerGetVendor (m_pJoystick)
86
91
<< " , ProductID: " << SDL_GameControllerGetProduct (m_pJoystick) << ' \n ' ;
@@ -101,15 +106,21 @@ void XBoxController::Deinitialize() {
101
106
}
102
107
103
108
std::optional<XBoxController::ControllerState> XBoxController::CurrentState () {
109
+ SDL_JoystickUpdate ();
110
+
104
111
// Try getting controller if it was lost
105
112
if (m_pJoystick == nullptr ) {
106
113
Initialize ();
107
114
}
108
115
109
116
if (m_pJoystick != nullptr ) {
117
+ if (!SDL_GameControllerGetAttached (m_pJoystick)) {
118
+ std::cout << " Game controller gone\n " ;
119
+ Deinitialize ();
120
+ return std::nullopt;
121
+ }
110
122
SDL_Event event;
111
123
while (SDL_PollEvent (&event)) {
112
- m_lastHeartbeat = std::chrono::steady_clock::now ();
113
124
switch (event.type ) {
114
125
case SDL_QUIT:
115
126
Deinitialize ();
@@ -121,6 +132,12 @@ std::optional<XBoxController::ControllerState> XBoxController::CurrentState() {
121
132
std::cout << " DEVICEADDED cdevice.which = " << event.cdevice .which << std::endl;
122
133
break ;
123
134
135
+ case SDL_CONTROLLERDEVICEREMOVED:
136
+ std::cout << " DEVICEREMOVED" << std::endl;
137
+ Deinitialize ();
138
+ return std::nullopt;
139
+ break ;
140
+
124
141
// If a controller button is pressed
125
142
case SDL_CONTROLLERBUTTONDOWN:
126
143
// Looking for the button that was pressed
@@ -260,10 +277,6 @@ std::optional<XBoxController::ControllerState> XBoxController::CurrentState() {
260
277
}
261
278
262
279
UpdateVibration ();
263
- if (std::chrono::steady_clock::now () - m_lastHeartbeat > std::chrono::milliseconds (2000 )) {
264
- printf (" Timeout!\n " );
265
- return std::nullopt;
266
- }
267
280
268
281
return m_latestState;
269
282
}
0 commit comments