Skip to content

Commit b2f6615

Browse files
committed
Improve DSU joystick cross-platform socket support
Disabled DSU joystick on Emscripten due to lack of UDP socket support. Updated socket initialization to use ioctl with FIONBIO for better compatibility on Unix-like systems. Refactored header includes and comments for clarity and platform correctness. Fixed variable naming in DSU data packet handling and removed unnecessary 'static' from inline functions in header.
1 parent 264e9cc commit b2f6615

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

CMakeLists.txt

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,19 +1378,29 @@ if(SDL_JOYSTICK)
13781378

13791379
# DSU (DualShock UDP) client support
13801380
if(SDL_DSU_JOYSTICK)
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-
list(APPEND SDL3_EXTRA_LIBS ws2_32)
1390-
elseif(UNIX AND NOT APPLE AND NOT ANDROID AND NOT HAIKU)
1391-
# Unix systems typically have sockets built-in
1392-
elseif(HAIKU)
1393-
list(APPEND SDL3_EXTRA_LIBS network)
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()
13941404
endif()
13951405
endif()
13961406
endif()

src/joystick/dsu/SDL_dsujoystick.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ 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 */
65+
#ifdef __sun
66+
#include <sys/filio.h> /* FIONBIO on Solaris */
67+
#endif
6468
#define closesocket close
6569
#endif
6670

@@ -121,8 +125,15 @@ dsu_socket_t DSU_CreateSocket(Uint16 port)
121125
#ifdef _WIN32
122126
ioctlsocket(sock, FIONBIO, &mode);
123127
#else
124-
flags = fcntl(sock, F_GETFL, 0);
125-
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
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 */
134+
flags = fcntl(sock, F_GETFL, 0);
135+
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
136+
#endif
126137
#endif
127138

128139
/* Bind to client port if specified */
@@ -499,9 +510,9 @@ int SDLCALL DSU_ReceiverThread(void *data)
499510
case DSU_MSG_DATA:
500511
/* Controller data */
501512
if (received >= (int)sizeof(DSU_ControllerData)) {
502-
DSU_ControllerData *data = (DSU_ControllerData *)buffer;
503-
SDL_Log("DSU: Data packet received for slot %d\n", data->info.slot);
504-
DSU_ProcessControllerData(ctx, data);
513+
DSU_ControllerData *packet = (DSU_ControllerData *)buffer;
514+
SDL_Log("DSU: Data packet received for slot %d\n", packet->info.slot);
515+
DSU_ProcessControllerData(ctx, packet);
505516
}
506517
break;
507518

src/joystick/dsu/SDL_dsujoystick_c.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ extern DSU_Context *s_dsu_ctx;
122122
extern "C" {
123123
#endif
124124

125-
static SDL_FORCE_INLINE Uint16 DSU_htons(Uint16 x) { return SDL_Swap16BE(x); }
126-
static SDL_FORCE_INLINE Uint32 DSU_htonl(Uint32 x) { return SDL_Swap32BE(x); }
127-
static SDL_FORCE_INLINE Uint32 DSU_ipv4_addr(const char *ip)
125+
SDL_FORCE_INLINE Uint16 DSU_htons(Uint16 x) { return SDL_Swap16BE(x); }
126+
SDL_FORCE_INLINE Uint32 DSU_htonl(Uint32 x) { return SDL_Swap32BE(x); }
127+
SDL_FORCE_INLINE Uint32 DSU_ipv4_addr(const char *ip)
128128
{
129129
unsigned int a, b, c, d;
130130
if (SDL_sscanf(ip, "%u.%u.%u.%u", &a, &b, &c, &d) == 4) {

src/joystick/dsu/SDL_dsujoystick_driver.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
3. This notice may not be removed or altered from any source distribution.
2020
*/
2121

22-
/* Include Windows socket headers before SDL to avoid macro conflicts */
22+
/* Include socket headers before SDL to avoid macro conflicts */
2323
#ifdef _WIN32
2424
#define WIN32_LEAN_AND_MEAN
2525
#include <winsock2.h>
2626
#include <ws2tcpip.h>
2727
#ifdef _MSC_VER
2828
#pragma comment(lib, "ws2_32.lib")
2929
#endif
30+
#else
31+
/* Unix-like systems including Haiku */
32+
#include <sys/socket.h>
33+
#include <netinet/in.h>
34+
#include <arpa/inet.h>
35+
#include <unistd.h>
36+
#include <errno.h>
3037
#endif
3138

3239
#include "SDL_internal.h"

0 commit comments

Comments
 (0)