Skip to content

Commit 08814d1

Browse files
committed
Improve DSU joystick socket portability and linking
Updated CMakeLists.txt to only enable DSU joystick on non-Emscripten targets and use sdl_link_dependency for required network libraries. Enhanced DSU socket creation for better non-blocking support and portability, including FIONBIO and ioctl handling. Added missing socket-related includes for non-Windows platforms in SDL_dsujoystick_driver.c.
1 parent b2f6615 commit 08814d1

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

CMakeLists.txt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,32 +1375,20 @@ if(SDL_JOYSTICK)
13751375
"${SDL3_SOURCE_DIR}/src/joystick/virtual/*.h"
13761376
)
13771377
endif()
1378-
1378+
13791379
# DSU (DualShock UDP) client support
1380-
if(SDL_DSU_JOYSTICK)
1381-
# Disable DSU for platforms that don't support UDP sockets
1382-
if(EMSCRIPTEN)
1383-
message(STATUS "DSU joystick disabled on Emscripten (no UDP socket support)")
1384-
set(SDL_DSU_JOYSTICK OFF)
1385-
else()
1386-
set(SDL_JOYSTICK_DSU 1)
1387-
sdl_glob_sources(
1388-
"${SDL3_SOURCE_DIR}/src/joystick/dsu/*.c"
1389-
"${SDL3_SOURCE_DIR}/src/joystick/dsu/*.h"
1390-
)
1391-
1392-
# DSU requires network libraries
1393-
if(WIN32)
1394-
sdl_link_dependency(dsu LIBS ws2_32)
1395-
elseif(HAIKU)
1396-
sdl_link_dependency(dsu LIBS network)
1397-
elseif(APPLE)
1398-
# macOS/iOS have sockets built-in, no extra linking needed
1399-
elseif(ANDROID)
1400-
# Android has sockets built-in, no extra linking needed
1401-
elseif(UNIX)
1402-
# Other Unix systems typically have sockets built-in
1403-
endif()
1380+
if(SDL_DSU_JOYSTICK AND NOT EMSCRIPTEN)
1381+
set(SDL_JOYSTICK_DSU 1)
1382+
sdl_glob_sources(
1383+
"${SDL3_SOURCE_DIR}/src/joystick/dsu/*.c"
1384+
"${SDL3_SOURCE_DIR}/src/joystick/dsu/*.h"
1385+
)
1386+
1387+
# DSU requires network libraries
1388+
if(WIN32)
1389+
sdl_link_dependency(dsu LIBS ws2_32)
1390+
elseif(HAIKU)
1391+
sdl_link_dependency(dsu LIBS network)
14041392
endif()
14051393
endif()
14061394
endif()

src/joystick/dsu/SDL_dsujoystick.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ typedef int socklen_t;
6161
#include <fcntl.h>
6262
#include <unistd.h>
6363
#include <errno.h>
64-
#include <sys/ioctl.h> /* Required for ioctl on Unix-like systems including Haiku */
64+
#include <sys/ioctl.h>
6565
#ifdef __sun
66-
#include <sys/filio.h> /* FIONBIO on Solaris */
66+
#include <sys/filio.h>
6767
#endif
6868
#define closesocket close
6969
#endif
@@ -111,6 +111,9 @@ dsu_socket_t DSU_CreateSocket(Uint16 port)
111111
u_long mode = 1;
112112
#else
113113
int flags;
114+
#if defined(FIONBIO)
115+
int nonblock = 1;
116+
#endif
114117
#endif
115118

116119
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -125,15 +128,19 @@ dsu_socket_t DSU_CreateSocket(Uint16 port)
125128
#ifdef _WIN32
126129
ioctlsocket(sock, FIONBIO, &mode);
127130
#else
128-
/* Use ioctl with FIONBIO for better compatibility across Unix-like systems */
129-
#ifdef FIONBIO
130-
int mode_unix = 1;
131-
ioctl(sock, FIONBIO, &mode_unix);
132-
#else
133-
/* Fallback to fcntl if FIONBIO is not available */
131+
#if defined(FIONBIO)
132+
if (ioctl(sock, FIONBIO, &nonblock) < 0) {
134133
flags = fcntl(sock, F_GETFL, 0);
134+
if (flags != -1) {
135+
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
136+
}
137+
}
138+
#else
139+
flags = fcntl(sock, F_GETFL, 0);
140+
if (flags != -1) {
135141
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
136-
#endif
142+
}
143+
#endif
137144
#endif
138145

139146
/* Bind to client port if specified */

0 commit comments

Comments
 (0)