Skip to content

Commit ab0d71d

Browse files
committed
Fix Joystick/GameController SDL polling loop
1 parent d27139b commit ab0d71d

4 files changed

Lines changed: 40 additions & 74 deletions

File tree

GameController.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,22 @@
33
/*static*/ std::mutex GameController::s_joyCntLck;
44
/*static*/ uint64_t GameController::s_joyCnt{0};
55

6-
bool GameController::IsConnected() const
6+
void GameController::Periodic()
77
{
8-
if (!_joy) {
9-
/* no game controller */
10-
return false;
11-
}
8+
SDL_Event event;
9+
while (SDL_PollEvent(&event)) {}
1210

1311
/* poll for game controller disconnects */
14-
SDL_Event event;
15-
while (SDL_PollEvent(&event)) {
16-
if (event.type == SDL_QUIT) {
17-
/* SDL shut down */
18-
return false;
19-
}
20-
else if (event.cdevice.type == SDL_CONTROLLERDEVICEREMOVED) {
21-
/* SDL game controller removed */
22-
return false;
23-
}
12+
if (!IsConnected()) {
13+
/* one of the game controllers disconnected, assume it was ours */
14+
Close();
2415
}
2516

26-
return true;
17+
/* game controller may have disconnected, make sure it's still valid */
18+
if (!_joy) {
19+
/* no game controller, initialize a new one */
20+
Init();
21+
}
2722
}
2823

2924
void GameController::ReportMissingGameController()
@@ -39,11 +34,6 @@ void GameController::ReportMissingGameController()
3934

4035
SDL_GameController *GameController::CreateGameController()
4136
{
42-
/* SDL seems somewhat fragile, shut it down and bring it up */
43-
SDL_Quit();
44-
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); // so Ctrl-C still works
45-
SDL_Init(SDL_INIT_GAMECONTROLLER);
46-
4737
/* poll for game controller */
4838
int res = SDL_NumJoysticks();
4939
if (res < 0) {

GameController.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ class GameController {
5151
/* first increment game controller count */
5252
{
5353
std::lock_guard lck{s_joyCntLck};
54-
++s_joyCnt;
54+
if (s_joyCnt++ == 0) {
55+
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); // so Ctrl-C still works
56+
SDL_Init(SDL_INIT_GAMECONTROLLER);
57+
}
5558
}
5659

5760
/* then initialize this game controller */
@@ -131,26 +134,16 @@ class GameController {
131134
/**
132135
* Returns whether this game controller is currently connected.
133136
*/
134-
bool IsConnected() const;
137+
bool IsConnected() const
138+
{
139+
return _joy && SDL_GameControllerGetAttached(_joy);
140+
}
135141

136142
/**
137143
* Periodically manages this game controller, handling disconnect
138144
* events in the process.
139145
*/
140-
void Periodic()
141-
{
142-
/* poll for game controller disconnects */
143-
if (!IsConnected()) {
144-
/* one of the game controllers disconnected, assume it was ours */
145-
Close();
146-
}
147-
148-
/* game controller may have disconnected, make sure it's still valid */
149-
if (!_joy) {
150-
/* no game controller, initialize a new one */
151-
Init();
152-
}
153-
}
146+
void Periodic();
154147

155148
private:
156149
static constexpr auto kErrorTimeMs = 3000;

Joystick.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,22 @@
33
/*static*/ std::mutex Joystick::s_joyCntLck;
44
/*static*/ uint64_t Joystick::s_joyCnt{0};
55

6-
bool Joystick::IsConnected() const
6+
void Joystick::Periodic()
77
{
8-
if (!_joy) {
9-
/* no joystick */
10-
return false;
11-
}
8+
SDL_Event event;
9+
while (SDL_PollEvent(&event)) {}
1210

1311
/* poll for joystick disconnects */
14-
SDL_Event event;
15-
while (SDL_PollEvent(&event)) {
16-
if (event.type == SDL_QUIT) {
17-
/* SDL shut down */
18-
return false;
19-
}
20-
else if (event.cdevice.type == SDL_JOYDEVICEREMOVED) {
21-
/* SDL joystick removed */
22-
return false;
23-
}
12+
if (!IsConnected()) {
13+
/* one of the joysticks disconnected, assume it was ours */
14+
Close();
2415
}
2516

26-
return true;
17+
/* joystick may have disconnected, make sure it's still valid */
18+
if (!_joy) {
19+
/* no joystick, initialize a new one */
20+
Init();
21+
}
2722
}
2823

2924
void Joystick::ReportMissingJoystick()
@@ -39,11 +34,6 @@ void Joystick::ReportMissingJoystick()
3934

4035
SDL_Joystick *Joystick::CreateJoystick()
4136
{
42-
/* SDL seems somewhat fragile, shut it down and bring it up */
43-
SDL_Quit();
44-
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); // so Ctrl-C still works
45-
SDL_Init(SDL_INIT_GAMECONTROLLER);
46-
4737
/* poll for joysticks */
4838
int res = SDL_NumJoysticks();
4939
if (res < 0) {

Joystick.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class Joystick {
5454
/* first increment joystick count */
5555
{
5656
std::lock_guard lck{s_joyCntLck};
57-
++s_joyCnt;
57+
if (s_joyCnt++ == 0) {
58+
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); // so Ctrl-C still works
59+
SDL_Init(SDL_INIT_GAMECONTROLLER);
60+
}
5861
}
5962

6063
/* then initialize this joystick */
@@ -170,26 +173,16 @@ class Joystick {
170173
/**
171174
* Returns whether this joystick is currently connected.
172175
*/
173-
bool IsConnected() const;
176+
bool IsConnected() const
177+
{
178+
return _joy && SDL_JoystickGetAttached(_joy);
179+
}
174180

175181
/**
176182
* Periodically manages this joystick, handling disconnect
177183
* events in the process.
178184
*/
179-
void Periodic()
180-
{
181-
/* poll for joystick disconnects */
182-
if (!IsConnected()) {
183-
/* one of the joysticks disconnected, assume it was ours */
184-
Close();
185-
}
186-
187-
/* joystick may have disconnected, make sure it's still valid */
188-
if (!_joy) {
189-
/* no joystick, initialize a new one */
190-
Init();
191-
}
192-
}
185+
void Periodic();
193186

194187
private:
195188
static constexpr auto kErrorTimeMs = 3000;

0 commit comments

Comments
 (0)