19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_JOYSTICK_MACOS
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  SDL stuff  --  "SDL_sysjoystick.c"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MacOS joystick functions by Frederick Reitberger
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    The code that follows is meant for SDL.  Use at your own risk.
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <InputSprocket.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_joystick.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysjoystick.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_joystick_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  The max number of joysticks we will detect  */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define     MAX_JOYSTICKS       16
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*  Limit ourselves to 32 elements per device  */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define     kMaxReferences      32
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define		ISpSymmetricAxisToFloat(axis)	((((float) axis) - kISpAxisMiddle) / (kISpAxisMaximum-kISpAxisMiddle))
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define		ISpAsymmetricAxisToFloat(axis)	(((float) axis) / (kISpAxisMaximum))
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic  ISpDeviceReference  SYS_Joysticks[MAX_JOYSTICKS];
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic  ISpElementListReference SYS_Elements[MAX_JOYSTICKS];
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic  ISpDeviceDefinition     SYS_DevDef[MAX_JOYSTICKS];
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct joystick_hwdata
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char name[64];
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*    Uint8   id;*/
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpElementReference refs[kMaxReferences];
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*  gonna need some sort of mapping info  */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to scan the system for joysticks.
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Joystick 0 should be the system default joystick.
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function should return the number of available joysticks, or -1
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * on an unrecoverable fatal error.
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickInit(void)
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static ISpDeviceClass classes[4] = {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpDeviceClass_Joystick,
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #if kISpDeviceClass_Gamepad
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpDeviceClass_Gamepad,
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    #endif
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpDeviceClass_Wheel,
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        0
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    };
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSErr   err;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     i;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    UInt32  count, numJoysticks;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (Ptr)0 == (Ptr)ISpStartup ) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("InputSprocket not installed");
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;  //  InputSprocket not installed
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if( (Ptr)0 == (Ptr)ISpGetVersion ) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("InputSprocket not version 1.1 or newer");
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;  //  old version of ISp (not at least 1.1)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpStartup();
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get all the joysticks */
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    numJoysticks = 0;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( i=0; classes[i]; ++i ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        count = 0;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        err = ISpDevices_ExtractByClass(
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            classes[i],
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            MAX_JOYSTICKS-numJoysticks,
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &count,
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &SYS_Joysticks[numJoysticks]);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        numJoysticks += count;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0; i < numJoysticks; i++)
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ISpDevice_GetDefinition(
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SYS_Joysticks[i], sizeof(ISpDeviceDefinition),
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &SYS_DevDef[i]);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        err = ISpElementList_New(
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            0, NULL,
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &SYS_Elements[i], 0);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (err) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_OutOfMemory();
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return -1;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ISpDevice_GetElementList(
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SYS_Joysticks[i],
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &SYS_Elements[i]);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpDevices_Deactivate(numJoysticks, SYS_Joysticks);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return numJoysticks;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to get the device-dependent name of a joystick */
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char *SDL_SYS_JoystickName(int index)
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static char name[64];
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int len;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*  convert pascal string to c-string  */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SYS_DevDef[index].deviceName[0];
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( len >= sizeof(name) ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        len = (sizeof(name) - 1);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memcpy(name, &SYS_DevDef[index].deviceName[1], len);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    name[len] = '\0';
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return name;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to open a joystick for use.
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The joystick to open is specified by the index field of the joystick.
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   This should fill the nbuttons and naxes fields of the joystick structure.
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   It returns 0, or -1 if there is an error.
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     index;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    UInt32  count, gotCount, count2;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    long    numAxis, numButtons, numHats, numBalls;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count = kMaxReferences;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count2 = 0;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    numAxis = numButtons = numHats = numBalls = 0;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    index = joystick->index;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* allocate memory for system specific hardware data */
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (joystick->hwdata == NULL)
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name));
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->name = joystick->hwdata->name;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpElementList_ExtractByKind(
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SYS_Elements[index],
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpElementKind_Axis,
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        count,
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &gotCount,
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        joystick->hwdata->refs);
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    numAxis = gotCount;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count -= gotCount;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count2 += gotCount;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpElementList_ExtractByKind(
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SYS_Elements[index],
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpElementKind_DPad,
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        count,
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &gotCount,
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &(joystick->hwdata->refs[count2]));
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    numHats = gotCount;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count -= gotCount;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count2 += gotCount;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpElementList_ExtractByKind(
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SYS_Elements[index],
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        kISpElementKind_Button,
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        count,
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &gotCount,
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &(joystick->hwdata->refs[count2]));
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    numButtons = gotCount;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count -= gotCount;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    count2 += gotCount;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->naxes = numAxis;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->nhats = numHats;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->nballs = numBalls;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    joystick->nbuttons = numButtons;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpDevices_Activate(
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        1,
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &SYS_Joysticks[index]);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to update the state of a joystick - called as a device poll.
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function shouldn't update the joystick structure directly,
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * but instead should call SDL_PrivateJoystick*() to deliver events
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * and update joystick device state.
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     i, j;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpAxisData     a;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpDPadData     b;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //ISpDeltaData    c;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpButtonData   d;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0, j = 0; i < joystick->naxes; i++, j++)
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Sint16 value;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ISpElement_GetSimpleState(
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            joystick->hwdata->refs[j],
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &a);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value = (ISpSymmetricAxisToFloat(a)* 32767.0);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( value != joystick->axes[i] ) {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateJoystickAxis(joystick, i, value);
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0; i < joystick->nhats; i++, j++)
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint8 pos;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ISpElement_GetSimpleState(
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            joystick->hwdata->refs[j],
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &b);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        switch(b) {
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadIdle:
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_CENTERED;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadLeft:
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_LEFT;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadUpLeft:
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_LEFTUP;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadUp:
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_UP;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadUpRight:
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_RIGHTUP;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadRight:
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_RIGHT;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadDownRight:
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_RIGHTDOWN;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadDown:
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_DOWN;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case kISpPadDownLeft:
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pos = SDL_HAT_LEFTDOWN;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( pos != joystick->hats[i] ) {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateJoystickHat(joystick, i, pos);
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0; i < joystick->nballs; i++, j++)
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /*  ignore balls right now  */
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0; i < joystick->nbuttons; i++, j++)
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ISpElement_GetSimpleState(
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            joystick->hwdata->refs[j],
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            &d);
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( d != joystick->buttons[i] ) {
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateJoystickButton(joystick, i, d);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to close a joystick after use */
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickClose(SDL_Joystick *joystick)
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int index;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    index = joystick->index;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpDevices_Deactivate(
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        1,
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        &SYS_Joysticks[index]);
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to perform any system-specific joystick related cleanup */
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickQuit(void)
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ISpShutdown();
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_JOYSTICK_MACOS */
321