diff --git a/CMakeLists.txt b/CMakeLists.txt index 79999826e2..2eb60a2421 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1378,19 +1378,29 @@ if(SDL_JOYSTICK) # DSU (DualShock UDP) client support if(SDL_DSU_JOYSTICK) - set(SDL_JOYSTICK_DSU 1) - sdl_glob_sources( - "${SDL3_SOURCE_DIR}/src/joystick/dsu/*.c" - "${SDL3_SOURCE_DIR}/src/joystick/dsu/*.h" - ) - - # DSU requires network libraries - if(WIN32) - list(APPEND SDL3_EXTRA_LIBS ws2_32) - elseif(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU) - # Unix systems typically have sockets built-in - elseif(HAIKU) - list(APPEND SDL3_EXTRA_LIBS network) + # Disable DSU for platforms that don't support UDP sockets + if(EMSCRIPTEN) + message(STATUS "DSU joystick disabled on Emscripten (no UDP socket support)") + set(SDL_DSU_JOYSTICK OFF) + else() + set(SDL_JOYSTICK_DSU 1) + sdl_glob_sources( + "${SDL3_SOURCE_DIR}/src/joystick/dsu/*.c" + "${SDL3_SOURCE_DIR}/src/joystick/dsu/*.h" + ) + + # DSU requires network libraries + if(WIN32) + sdl_link_dependency(dsu LIBS ws2_32) + elseif(HAIKU) + sdl_link_dependency(dsu LIBS network) + elseif(APPLE) + # macOS/iOS have sockets built-in, no extra linking needed + elseif(ANDROID) + # Android has sockets built-in, no extra linking needed + elseif(UNIX) + # Other Unix systems typically have sockets built-in + endif() endif() endif() endif() diff --git a/src/joystick/dsu/SDL_dsujoystick.c b/src/joystick/dsu/SDL_dsujoystick.c index ff7e1f451a..da4e0eb312 100644 --- a/src/joystick/dsu/SDL_dsujoystick.c +++ b/src/joystick/dsu/SDL_dsujoystick.c @@ -61,6 +61,10 @@ typedef int socklen_t; #include #include #include + #include /* Required for ioctl on Unix-like systems including Haiku */ + #ifdef __sun + #include /* FIONBIO on Solaris */ + #endif #define closesocket close #endif @@ -121,8 +125,15 @@ dsu_socket_t DSU_CreateSocket(Uint16 port) #ifdef _WIN32 ioctlsocket(sock, FIONBIO, &mode); #else - flags = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, flags | O_NONBLOCK); + /* Use ioctl with FIONBIO for better compatibility across Unix-like systems */ + #ifdef FIONBIO + int mode_unix = 1; + ioctl(sock, FIONBIO, &mode_unix); + #else + /* Fallback to fcntl if FIONBIO is not available */ + flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags | O_NONBLOCK); + #endif #endif /* Bind to client port if specified */ @@ -499,9 +510,9 @@ int SDLCALL DSU_ReceiverThread(void *data) case DSU_MSG_DATA: /* Controller data */ if (received >= (int)sizeof(DSU_ControllerData)) { - DSU_ControllerData *data = (DSU_ControllerData *)buffer; - SDL_Log("DSU: Data packet received for slot %d\n", data->info.slot); - DSU_ProcessControllerData(ctx, data); + DSU_ControllerData *packet = (DSU_ControllerData *)buffer; + SDL_Log("DSU: Data packet received for slot %d\n", packet->info.slot); + DSU_ProcessControllerData(ctx, packet); } break; diff --git a/src/joystick/dsu/SDL_dsujoystick_c.h b/src/joystick/dsu/SDL_dsujoystick_c.h index abb84bb914..a1f30ecab7 100644 --- a/src/joystick/dsu/SDL_dsujoystick_c.h +++ b/src/joystick/dsu/SDL_dsujoystick_c.h @@ -122,9 +122,9 @@ extern DSU_Context *s_dsu_ctx; extern "C" { #endif -static SDL_FORCE_INLINE Uint16 DSU_htons(Uint16 x) { return SDL_Swap16BE(x); } -static SDL_FORCE_INLINE Uint32 DSU_htonl(Uint32 x) { return SDL_Swap32BE(x); } -static SDL_FORCE_INLINE Uint32 DSU_ipv4_addr(const char *ip) + SDL_FORCE_INLINE Uint16 DSU_htons(Uint16 x) { return SDL_Swap16BE(x); } + SDL_FORCE_INLINE Uint32 DSU_htonl(Uint32 x) { return SDL_Swap32BE(x); } + SDL_FORCE_INLINE Uint32 DSU_ipv4_addr(const char *ip) { unsigned int a, b, c, d; if (SDL_sscanf(ip, "%u.%u.%u.%u", &a, &b, &c, &d) == 4) { diff --git a/src/joystick/dsu/SDL_dsujoystick_driver.c b/src/joystick/dsu/SDL_dsujoystick_driver.c index b35af5428a..9573fdb567 100644 --- a/src/joystick/dsu/SDL_dsujoystick_driver.c +++ b/src/joystick/dsu/SDL_dsujoystick_driver.c @@ -19,7 +19,7 @@ 3. This notice may not be removed or altered from any source distribution. */ -/* Include Windows socket headers before SDL to avoid macro conflicts */ +/* Include socket headers before SDL to avoid macro conflicts */ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -27,6 +27,13 @@ #ifdef _MSC_VER #pragma comment(lib, "ws2_32.lib") #endif +#else +/* Unix-like systems including Haiku */ +#include +#include +#include +#include +#include #endif #include "SDL_internal.h"