From 1adba1910436b823e169cb414708d25561f72408 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 26 Nov 2012 21:11:28 -0800 Subject: [PATCH] Fixed iOS joystick support for new API --- Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj | 8 +++ src/joystick/SDL_joystick.c | 6 +-- src/joystick/SDL_sysjoystick.h | 2 +- src/joystick/darwin/SDL_sysjoystick.c | 2 +- src/joystick/iphoneos/SDL_sysjoystick.m | 57 ++++++++++++++++++--- src/joystick/linux/SDL_sysjoystick.c | 19 ++++--- src/joystick/windows/SDL_dxjoystick.c | 2 +- 7 files changed, 76 insertions(+), 20 deletions(-) diff --git a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj index 261caf647..b9aa3385c 100755 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj @@ -98,6 +98,8 @@ 56ED04E3118A8EFD00A56AA6 /* SDL_syspower.m in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */; }; 93CB792313FC5E5200BD3E05 /* SDL_uikitviewcontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = 93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */; }; 93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */ = {isa = PBXBuildFile; fileRef = 93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */; }; + AA0AD06216647BBB00CE5896 /* SDL_gamecontroller.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0AD06116647BBB00CE5896 /* SDL_gamecontroller.c */; }; + AA0AD06516647BD400CE5896 /* SDL_gamecontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = AA0AD06416647BD400CE5896 /* SDL_gamecontroller.h */; }; AA126AD41617C5E7005ABC8F /* SDL_uikitmodes.h in Headers */ = {isa = PBXBuildFile; fileRef = AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */; }; AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */ = {isa = PBXBuildFile; fileRef = AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */; }; AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */ = {isa = PBXBuildFile; fileRef = AA628AD9159369E3005138DD /* SDL_rotate.c */; }; @@ -356,6 +358,8 @@ 56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SDL_syspower.m; path = ../../src/power/uikit/SDL_syspower.m; sourceTree = SOURCE_ROOT; }; 93CB792213FC5E5200BD3E05 /* SDL_uikitviewcontroller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitviewcontroller.h; sourceTree = ""; }; 93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitviewcontroller.m; sourceTree = ""; }; + AA0AD06116647BBB00CE5896 /* SDL_gamecontroller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_gamecontroller.c; sourceTree = ""; }; + AA0AD06416647BD400CE5896 /* SDL_gamecontroller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_gamecontroller.h; sourceTree = ""; }; AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitmodes.h; sourceTree = ""; }; AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitmodes.m; sourceTree = ""; }; AA628AD9159369E3005138DD /* SDL_rotate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_rotate.c; sourceTree = ""; }; @@ -830,6 +834,7 @@ isa = PBXGroup; children = ( FD689EFF0E26E5B600F90B21 /* iphoneos */, + AA0AD06116647BBB00CE5896 /* SDL_gamecontroller.c */, FD5F9D1E0E0E08B3008E885B /* SDL_joystick.c */, FD5F9D1F0E0E08B3008E885B /* SDL_joystick_c.h */, FD5F9D200E0E08B3008E885B /* SDL_sysjoystick.h */, @@ -914,6 +919,7 @@ AA7558701595D55500BBD41B /* SDL_endian.h */, AA7558711595D55500BBD41B /* SDL_error.h */, AA7558721595D55500BBD41B /* SDL_events.h */, + AA0AD06416647BD400CE5896 /* SDL_gamecontroller.h */, AA7558731595D55500BBD41B /* SDL_gesture.h */, AA7558741595D55500BBD41B /* SDL_haptic.h */, AA7558751595D55500BBD41B /* SDL_hints.h */, @@ -1259,6 +1265,7 @@ AA704DD6162AA90A0076D1C1 /* SDL_dropevents_c.h in Headers */, AA9FF9511637C6E5000DF050 /* SDL_messagebox.h in Headers */, AABCC3941640643D00AB8930 /* SDL_uikitmessagebox.h in Headers */, + AA0AD06516647BD400CE5896 /* SDL_gamecontroller.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1478,6 +1485,7 @@ AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */, AA704DD7162AA90A0076D1C1 /* SDL_dropevents.c in Sources */, AABCC3951640643D00AB8930 /* SDL_uikitmessagebox.m in Sources */, + AA0AD06216647BBB00CE5896 /* SDL_gamecontroller.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 2fc52ee28..1149795a0 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -661,13 +661,13 @@ SDL_PrivateJoystickNeedsPolling() } -/* return the guid for this index*/ +/* return the guid for this index */ JoystickGUID SDL_JoystickGetDeviceGUID( int device_index ) { - return SDL_SYS_PrivateJoystickGetDeviceID( device_index ); + return SDL_SYS_PrivateJoystickGetDeviceGUID( device_index ); } -/* return the guid for this openeded device*/ +/* return the guid for this opened device */ JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick) { return SDL_SYS_PrivateJoystickGetGUID( joystick ); diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h index 165a96a25..d67f916cb 100644 --- a/src/joystick/SDL_sysjoystick.h +++ b/src/joystick/SDL_sysjoystick.h @@ -107,7 +107,7 @@ extern int SDL_SYS_JoystickNeedsPolling(); /* Function to return the stable GUID for a plugged in device */ -extern JoystickGUID SDL_SYS_PrivateJoystickGetDeviceID( int device_index ); +extern JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ); /* Function to return the stable GUID for a opened joystick */ diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c index 05ee36aa2..d46712f0c 100644 --- a/src/joystick/darwin/SDL_sysjoystick.c +++ b/src/joystick/darwin/SDL_sysjoystick.c @@ -1089,7 +1089,7 @@ SDL_SYS_JoystickNeedsPolling() return s_bDeviceAdded; } -JoystickGUID SDL_SYS_PrivateJoystickGetDeviceID( int device_index ) +JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) { recDevice *device = gpDeviceList; int index; diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m index 9b3d29944..6a6b8037c 100755 --- a/src/joystick/iphoneos/SDL_sysjoystick.m +++ b/src/joystick/iphoneos/SDL_sysjoystick.m @@ -37,13 +37,12 @@ int SDL_SYS_JoystickInit(void) { - SDL_numjoysticks = 1; return (1); } /* Function to get the device-dependent name of a joystick */ const char * -SDL_SYS_JoystickName(int index) +SDL_SYS_JoystickNameForIndex(int index) { switch(index) { case 0: @@ -60,14 +59,13 @@ It returns 0, or -1 if there is an error. */ int -SDL_SYS_JoystickOpen(SDL_Joystick * joystick) +SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) { - if (joystick->index == 0) { + if (device_index == 0) { joystick->naxes = 3; joystick->nhats = 0; joystick->nballs = 0; joystick->nbuttons = 0; - joystick->name = accelerometerName; [[SDLUIAccelerationDelegate sharedDelegate] startup]; return 0; } @@ -107,7 +105,7 @@ void SDL_SYS_JoystickClose(SDL_Joystick * joystick) { - if (joystick->index == 0 && [[SDLUIAccelerationDelegate sharedDelegate] isRunning]) { + if ([[SDLUIAccelerationDelegate sharedDelegate] isRunning]) { [[SDLUIAccelerationDelegate sharedDelegate] shutdown]; } SDL_SetError("No joystick open with that index"); @@ -121,4 +119,51 @@ { return; } + +/* Function to perform the mapping from device index to the instance id for this index */ +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index) +{ + return index; +} + +/* Function to determine is this joystick is attached to the system right now */ +int SDL_SYS_JoystickAttached(SDL_Joystick *joystick) +{ + return 1; +} + +int SDL_SYS_NumJoysticks() +{ + return 1; +} + +int SDL_SYS_JoystickNeedsPolling() +{ + return 0; +} + +void SDL_SYS_JoystickDetect() +{ +} + +JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) +{ + JoystickGUID guid; + // the GUID is just the first 16 chars of the name for now + const char *name = SDL_SYS_JoystickNameForIndex( device_index ); + SDL_zero( guid ); + SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); + return guid; +} + +JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) +{ + JoystickGUID guid; + // the GUID is just the first 16 chars of the name for now + const char *name = joystick->name; + SDL_zero( guid ); + SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); + return guid; +} + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index 46e2aa5df..1485dc80d 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -513,7 +513,7 @@ SDL_SYS_JoystickNameForIndex(int index) { int fd; static char namebuf[128]; - char *name; + const char *name; SDL_logical_joydecl(int oindex = index); #ifndef NO_LOGICAL_JOYSTICKS @@ -1248,7 +1248,7 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index) /* Function to determine is this joystick is attached to the system right now */ int SDL_SYS_JoystickAttached(SDL_Joystick *joystick) { - return !joystick->closed; + return 1; } int SDL_SYS_NumJoysticks() @@ -1265,24 +1265,27 @@ void SDL_SYS_JoystickDetect() { } -JoystickGUID SDL_SYS_PrivateJoystickGetDeviceID( int device_index ) +JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) { - static JoystickGUID guid; + JoystickGUID guid; // the GUID is just the first 16 chars of the name for now const char *name = SDL_SYS_JoystickNameForIndex( device_index ); - SDL_memcpy( &guid, name, sizeof(guid) ); + SDL_zero( guid ); + SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); return guid; } JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) { - static JoystickGUID guid; + JoystickGUID guid; // the GUID is just the first 16 chars of the name for now - const char *name = SDL_SYS_JoystickNameForIndex( joystick->name ); - SDL_memcpy( &guid, name, sizeof(guid) ); + const char *name = joystick->name; + SDL_zero( guid ); + SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); return guid; } #endif /* SDL_JOYSTICK_LINUX */ + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/windows/SDL_dxjoystick.c b/src/joystick/windows/SDL_dxjoystick.c index b7258a74e..bcd66417f 100644 --- a/src/joystick/windows/SDL_dxjoystick.c +++ b/src/joystick/windows/SDL_dxjoystick.c @@ -1651,7 +1651,7 @@ int SDL_SYS_JoystickNeedsPolling() } /* return the stable device guid for this device index */ -JoystickGUID SDL_SYS_PrivateJoystickGetDeviceID( int device_index ) +JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) { JoyStick_DeviceData *device = SYS_Joystick; int index;