Skip to content

Commit 57a5ebe

Browse files
Don't use a separate thread when polling for gamepad events on DRM platforms (#3641)
1 parent 8b5943d commit 57a5ebe

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

src/platforms/rcore_drm.c

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ typedef struct {
128128
int touchSlot; // Hold the touch slot number of the currently being sent multitouch block
129129

130130
// Gamepad data
131-
pthread_t gamepadThreadId; // Gamepad reading thread id
132131
int gamepadStreamFd[MAX_GAMEPADS]; // Gamepad device file descriptor
133132

134133
} PlatformData;
@@ -191,7 +190,7 @@ static void PollKeyboardEvents(void); // Process evdev keyboard events
191190
static void *EventThread(void *arg); // Input device events reading thread
192191

193192
static void InitGamepad(void); // Initialize raw gamepad input
194-
static void *GamepadThread(void *arg); // Mouse reading thread
193+
static void PollGamepadEvents(void); // Gamepad reading function
195194

196195
static int FindMatchingConnectorMode(const drmModeConnector *connector, const drmModeModeInfo *mode); // Search matching DRM mode in connector's mode list
197196
static int FindExactConnectorMode(const drmModeConnector *connector, uint width, uint height, uint fps, bool allowInterlaced); // Search exactly matching DRM connector mode in connector's list
@@ -575,14 +574,7 @@ void PollInputEvents(void)
575574
}
576575

577576
// Register gamepads buttons events
578-
for (int i = 0; i < MAX_GAMEPADS; i++)
579-
{
580-
if (CORE.Input.Gamepad.ready[i])
581-
{
582-
// Register previous gamepad states
583-
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
584-
}
585-
}
577+
PollGamepadEvents();
586578

587579
// Register previous touch states
588580
for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
@@ -598,9 +590,6 @@ void PollInputEvents(void)
598590
// stdin reading is still used for legacy purposes, it allows keyboard input trough SSH console
599591

600592
if (!platform.eventKeyboardMode) ProcessKeyboard();
601-
602-
// NOTE: Mouse input events polling is done asynchronously in another pthread - EventThread()
603-
// NOTE: Gamepad (Joystick) input events polling is done asynchronously in another pthread - GamepadThread()
604593
#endif
605594

606595
// Handle the mouse/touch/gestures events:
@@ -1228,8 +1217,6 @@ void ClosePlatform(void)
12281217
pthread_join(platform.eventWorker[i].threadId, NULL);
12291218
}
12301219
}
1231-
1232-
if (platform.gamepadThreadId) pthread_join(platform.gamepadThreadId, NULL);
12331220
}
12341221

12351222
// Initialize Keyboard system (using standard input)
@@ -1943,14 +1930,8 @@ static void InitGamepad(void)
19431930
{
19441931
CORE.Input.Gamepad.ready[i] = true;
19451932

1946-
// NOTE: Only create one thread
1947-
if (i == 0)
1948-
{
1949-
int error = pthread_create(&platform.gamepadThreadId, NULL, &GamepadThread, NULL);
1950-
1951-
if (error != 0) TRACELOG(LOG_WARNING, "RPI: Failed to create gamepad input event thread");
1952-
else TRACELOG(LOG_INFO, "RPI: Gamepad device initialized successfully");
1953-
}
1933+
// NOTE: Only show message for first gamepad
1934+
if (i == 0) TRACELOG(LOG_INFO, "RPI: Gamepad device initialized successfully");
19541935

19551936
ioctl(platform.gamepadStreamFd[i], JSIOCGNAME(64), &CORE.Input.Gamepad.name[i]);
19561937
ioctl(platform.gamepadStreamFd[i], JSIOCGAXES, &CORE.Input.Gamepad.axisCount[i]);
@@ -1959,7 +1940,7 @@ static void InitGamepad(void)
19591940
}
19601941

19611942
// Process Gamepad (/dev/input/js0)
1962-
static void *GamepadThread(void *arg)
1943+
static void PollGamepadEvents(void)
19631944
{
19641945
#define JS_EVENT_BUTTON 0x01 // Button pressed/released
19651946
#define JS_EVENT_AXIS 0x02 // Joystick axis moved
@@ -1975,18 +1956,21 @@ static void *GamepadThread(void *arg)
19751956
// Read gamepad event
19761957
struct js_event gamepadEvent = { 0 };
19771958

1978-
while (!CORE.Window.shouldClose)
1959+
for (int i = 0; i < MAX_GAMEPADS; i++)
19791960
{
1980-
for (int i = 0; i < MAX_GAMEPADS; i++)
1961+
if (CORE.Input.Gamepad.ready[i])
19811962
{
1982-
if (read(platform.gamepadStreamFd[i], &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event))
1963+
// Register previous gamepad states
1964+
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
1965+
1966+
while (read(platform.gamepadStreamFd[i], &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event))
19831967
{
19841968
gamepadEvent.type &= ~JS_EVENT_INIT; // Ignore synthetic events
19851969

19861970
// Process gamepad events by type
19871971
if (gamepadEvent.type == JS_EVENT_BUTTON)
19881972
{
1989-
//TRACELOG(LOG_WARNING, "RPI: Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
1973+
TRACELOG(LOG_DEBUG, "RPI: Gamepad %i button: %i, value: %i", i, gamepadEvent.number, gamepadEvent.value);
19901974

19911975
if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS)
19921976
{
@@ -1999,7 +1983,7 @@ static void *GamepadThread(void *arg)
19991983
}
20001984
else if (gamepadEvent.type == JS_EVENT_AXIS)
20011985
{
2002-
//TRACELOG(LOG_WARNING, "RPI: Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
1986+
TRACELOG(LOG_DEBUG, "RPI: Gamepad %i axis: %i, value: %i", i, gamepadEvent.number, gamepadEvent.value);
20031987

20041988
if (gamepadEvent.number < MAX_GAMEPAD_AXIS)
20051989
{
@@ -2008,11 +1992,8 @@ static void *GamepadThread(void *arg)
20081992
}
20091993
}
20101994
}
2011-
else WaitTime(0.001); // Sleep for 1 ms to avoid hogging CPU time
20121995
}
20131996
}
2014-
2015-
return NULL;
20161997
}
20171998

20181999
// Search matching DRM mode in connector's mode list

0 commit comments

Comments
 (0)