2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #ifdef SDL_JOYSTICK_MACOS
26 /* SDL stuff -- "SDL_sysjoystick.c"
27 MacOS joystick functions by Frederick Reitberger
29 The code that follows is meant for SDL. Use at your own risk.
32 #include <InputSprocket.h>
34 #include "SDL_joystick.h"
35 #include "../SDL_sysjoystick.h"
36 #include "../SDL_joystick_c.h"
39 /* The max number of joysticks we will detect */
40 #define MAX_JOYSTICKS 16
41 /* Limit ourselves to 32 elements per device */
42 #define kMaxReferences 32
44 #define ISpSymmetricAxisToFloat(axis) ((((float) axis) - kISpAxisMiddle) / (kISpAxisMaximum-kISpAxisMiddle))
45 #define ISpAsymmetricAxisToFloat(axis) (((float) axis) / (kISpAxisMaximum))
48 static ISpDeviceReference SYS_Joysticks[MAX_JOYSTICKS];
49 static ISpElementListReference SYS_Elements[MAX_JOYSTICKS];
50 static ISpDeviceDefinition SYS_DevDef[MAX_JOYSTICKS];
52 struct joystick_hwdata
56 ISpElementReference refs[kMaxReferences];
57 /* gonna need some sort of mapping info */
61 /* Function to scan the system for joysticks.
62 * Joystick 0 should be the system default joystick.
63 * This function should return the number of available joysticks, or -1
64 * on an unrecoverable fatal error.
66 int SDL_SYS_JoystickInit(void)
68 static ISpDeviceClass classes[4] = {
69 kISpDeviceClass_Joystick,
70 #if kISpDeviceClass_Gamepad
71 kISpDeviceClass_Gamepad,
73 kISpDeviceClass_Wheel,
78 UInt32 count, numJoysticks;
80 if ( (Ptr)0 == (Ptr)ISpStartup ) {
81 SDL_SetError("InputSprocket not installed");
82 return -1; // InputSprocket not installed
85 if( (Ptr)0 == (Ptr)ISpGetVersion ) {
86 SDL_SetError("InputSprocket not version 1.1 or newer");
87 return -1; // old version of ISp (not at least 1.1)
92 /* Get all the joysticks */
94 for ( i=0; classes[i]; ++i ) {
96 err = ISpDevices_ExtractByClass(
98 MAX_JOYSTICKS-numJoysticks,
100 &SYS_Joysticks[numJoysticks]);
101 numJoysticks += count;
104 for(i = 0; i < numJoysticks; i++)
106 ISpDevice_GetDefinition(
107 SYS_Joysticks[i], sizeof(ISpDeviceDefinition),
110 err = ISpElementList_New(
112 &SYS_Elements[i], 0);
119 ISpDevice_GetElementList(
124 ISpDevices_Deactivate(numJoysticks, SYS_Joysticks);
129 /* Function to get the device-dependent name of a joystick */
130 const char *SDL_SYS_JoystickName(int index)
132 static char name[64];
135 /* convert pascal string to c-string */
136 len = SYS_DevDef[index].deviceName[0];
137 if ( len >= sizeof(name) ) {
138 len = (sizeof(name) - 1);
140 SDL_memcpy(name, &SYS_DevDef[index].deviceName[1], len);
146 /* Function to open a joystick for use.
147 The joystick to open is specified by the index field of the joystick.
148 This should fill the nbuttons and naxes fields of the joystick structure.
149 It returns 0, or -1 if there is an error.
151 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
154 UInt32 count, gotCount, count2;
155 long numAxis, numButtons, numHats, numBalls;
157 count = kMaxReferences;
159 numAxis = numButtons = numHats = numBalls = 0;
161 index = joystick->index;
163 /* allocate memory for system specific hardware data */
164 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
165 if (joystick->hwdata == NULL)
170 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
171 SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name));
172 joystick->name = joystick->hwdata->name;
174 ISpElementList_ExtractByKind(
176 kISpElementKind_Axis,
179 joystick->hwdata->refs);
185 ISpElementList_ExtractByKind(
187 kISpElementKind_DPad,
190 &(joystick->hwdata->refs[count2]));
196 ISpElementList_ExtractByKind(
198 kISpElementKind_Button,
201 &(joystick->hwdata->refs[count2]));
203 numButtons = gotCount;
207 joystick->naxes = numAxis;
208 joystick->nhats = numHats;
209 joystick->nballs = numBalls;
210 joystick->nbuttons = numButtons;
214 &SYS_Joysticks[index]);
219 /* Function to update the state of a joystick - called as a device poll.
220 * This function shouldn't update the joystick structure directly,
221 * but instead should call SDL_PrivateJoystick*() to deliver events
222 * and update joystick device state.
224 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
232 for(i = 0, j = 0; i < joystick->naxes; i++, j++)
236 ISpElement_GetSimpleState(
237 joystick->hwdata->refs[j],
239 value = (ISpSymmetricAxisToFloat(a)* 32767.0);
240 if ( value != joystick->axes[i] ) {
241 SDL_PrivateJoystickAxis(joystick, i, value);
245 for(i = 0; i < joystick->nhats; i++, j++)
249 ISpElement_GetSimpleState(
250 joystick->hwdata->refs[j],
254 pos = SDL_HAT_CENTERED;
260 pos = SDL_HAT_LEFTUP;
266 pos = SDL_HAT_RIGHTUP;
271 case kISpPadDownRight:
272 pos = SDL_HAT_RIGHTDOWN;
277 case kISpPadDownLeft:
278 pos = SDL_HAT_LEFTDOWN;
281 if ( pos != joystick->hats[i] ) {
282 SDL_PrivateJoystickHat(joystick, i, pos);
286 for(i = 0; i < joystick->nballs; i++, j++)
288 /* ignore balls right now */
291 for(i = 0; i < joystick->nbuttons; i++, j++)
293 ISpElement_GetSimpleState(
294 joystick->hwdata->refs[j],
296 if ( d != joystick->buttons[i] ) {
297 SDL_PrivateJoystickButton(joystick, i, d);
302 /* Function to close a joystick after use */
303 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
307 index = joystick->index;
309 ISpDevices_Deactivate(
311 &SYS_Joysticks[index]);
314 /* Function to perform any system-specific joystick related cleanup */
315 void SDL_SYS_JoystickQuit(void)
320 #endif /* SDL_JOYSTICK_MACOS */