mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-06-12 01:15:39 +00:00
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.
This commit is contained in:
parent
264e9ccf8e
commit
b2f6615124
4 changed files with 50 additions and 22 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@ typedef int socklen_t;
|
|||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h> /* Required for ioctl on Unix-like systems including Haiku */
|
||||
#ifdef __sun
|
||||
#include <sys/filio.h> /* 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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 <winsock2.h>
|
||||
|
|
@ -27,6 +27,13 @@
|
|||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#endif
|
||||
#else
|
||||
/* Unix-like systems including Haiku */
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#include "SDL_internal.h"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue