SDL_sysjoystick.m File Reference
`#include "../../SDL_internal.h"`
`#include "SDL_sysjoystick_c.h"`
`#include "SDL_config_iphoneos.h"`
`#include "SDL_joystick.h"`
`#include "SDL_hints.h"`
`#include "SDL_stdinc.h"`
`#include "../SDL_sysjoystick.h"`
`#include "../SDL_joystick_c.h"`
`#include "../../events/SDL_events_c.h"`
`#import <CoreMotion/CoreMotion.h>`
Include dependency graph for SDL_sysjoystick.m:

Go to the source code of this file.

## Functions

static SDL_JoystickDeviceItem * | GetDeviceForIndex (int device_index)
static void | SDL_SYS_AddMFIJoystickDevice (SDL_JoystickDeviceItem *device, GCController *controller)
static void | SDL_SYS_AddJoystickDevice (GCController *controller, SDL_bool accelerometer)
static SDL_JoystickDeviceItem * | SDL_SYS_RemoveJoystickDevice (SDL_JoystickDeviceItem *device)
int | SDL_SYS_JoystickInit (void)
int | SDL_SYS_NumJoysticks ()
void | SDL_SYS_JoystickDetect ()
const char * | SDL_SYS_JoystickNameForDeviceIndex (int device_index)
SDL_JoystickID | SDL_SYS_GetInstanceIdOfDeviceIndex (int device_index)
int | SDL_SYS_JoystickOpen (SDL_Joystick *joystick, int device_index)
SDL_bool | SDL_SYS_JoystickAttached (SDL_Joystick *joystick)
static void | SDL_SYS_AccelerometerUpdate (SDL_Joystick *joystick)
static void | SDL_SYS_MFIJoystickUpdate (SDL_Joystick *joystick)
void | SDL_SYS_JoystickUpdate (SDL_Joystick *joystick)
void | SDL_SYS_JoystickClose (SDL_Joystick *joystick)
void | SDL_SYS_JoystickQuit (void)
SDL_JoystickGUID | SDL_SYS_JoystickGetDeviceGUID (int device_index)
SDL_JoystickGUID | SDL_SYS_JoystickGetGUID (SDL_Joystick *joystick)

## Variables

static const char * | accelerometerName = "iOS Accelerometer"
static CMMotionManager * | motionManager = nil
static SDL_JoystickDeviceItem * | deviceList = NULL
static int | numjoysticks = 0
static SDL_JoystickID | instancecounter = 0

## Function Documentation

static SDL_JoystickDeviceItem* GetDeviceForIndex | ( | int | device_index | ) |

static

Definition at line 57 of file SDL_sysjoystick.m.

References deviceList, i, recDevice::next, and NULL.

Referenced by SDL_SYS_GetInstanceIdOfDeviceIndex(), SDL_SYS_JoystickGetDeviceGUID(), SDL_SYS_JoystickNameForDeviceIndex(), and SDL_SYS_JoystickOpen().

{
SDL_JoystickDeviceItem *device = deviceList;
int i = 0;
while (i < device_index) {
if (device == NULL) {
return NULL;
}
device = device->next;
i++;
}
return device;
}
deviceList
static SDL_JoystickDeviceItem * deviceList
Definition: SDL_sysjoystick.m:51
joystick_hwdata::next
struct joystick_hwdata * next
Definition: SDL_sysjoystick_c.h:47
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
NULL
#define NULL
Definition: begin_code.h:143

static void SDL_SYS_AccelerometerUpdate | ( | SDL_Joystick * | joystick | ) |

static

Definition at line 388 of file SDL_sysjoystick.m.

References motionManager, SDL_HAT_CENTERED, SDL_HAT_DOWN, SDL_HAT_LEFT, SDL_HAT_RIGHT, SDL_HAT_UP, SDL_IPHONE_MAX_GFORCE, SDL_max, SDL_min, and SDL_PrivateJoystickAxis().

Referenced by SDL_SYS_JoystickUpdate().

{
const float maxgforce = SDL_IPHONE_MAX_GFORCE;
const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel;
@autoreleasepool {
if (!motionManager.isAccelerometerActive) {
return;
}
accel = motionManager.accelerometerData.acceleration;
}
/*
Convert accelerometer data from floating point to Sint16, which is what
the joystick system expects.
To do the conversion, the data is first clamped onto the interval
[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
by MAX_SINT16 so that it is mapped to the full range of an Sint16.
You can customize the clamped range of this function by modifying the
SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
Once converted to Sint16, the accelerometer data no longer has coherent
units. You can convert the data back to units of g-force by multiplying
it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
*/
/* clamp the data */
accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
/* pass in data mapped to range of SInt16 */
SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
}
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:347
SDL_max
#define SDL_max(x, y)
Definition: SDL_stdinc.h:348
SDL_PrivateJoystickAxis
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:501
SDL_IPHONE_MAX_GFORCE
#define SDL_IPHONE_MAX_GFORCE
Definition: SDL_config_iphoneos.h:157
motionManager
static CMMotionManager * motionManager
Definition: SDL_sysjoystick.m:49

static void SDL_SYS_AddJoystickDevice | ( | GCController * | controller,
|  | SDL_bool | accelerometer
| ) |  |

static

Definition at line 127 of file SDL_sysjoystick.m.

References recDevice::accelerometer, accelerometerName, recDevice::controller, SDL_JoystickGUID::data, deviceList, recDevice::guid, recDevice::instance_id, instancecounter, recDevice::name, recDevice::naxes, recDevice::nbuttons, recDevice::next, recDevice::nhats, NULL, numjoysticks, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_GetEventState, SDL_JOYDEVICEADDED, SDL_malloc, SDL_memcpy, SDL_min, SDL_PushEvent, SDL_strdup, SDL_strlen, SDL_SYS_AddMFIJoystickDevice(), SDL_zerop, and SDL_Event::type.

Referenced by SDL_SYS_JoystickInit().

{
SDL_JoystickDeviceItem *device = deviceList;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
while (device != NULL) {
if (device->controller == controller) {
return;
}
device = device->next;
}
device = (SDL_JoystickDeviceItem *) SDL_malloc(sizeof(SDL_JoystickDeviceItem));
if (device == NULL) {
return;
}
SDL_zerop(device);
device->accelerometer = accelerometer;
device->instance_id = instancecounter++;
if (accelerometer) {
device->name = SDL_strdup(accelerometerName);
device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
device->nhats = 0;
device->nbuttons = 0;
/* Use the accelerometer name as a GUID. */
SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
} else if (controller) {
SDL_SYS_AddMFIJoystickDevice(device, controller);
}
if (deviceList == NULL) {
deviceList = device;
} else {
SDL_JoystickDeviceItem *lastdevice = deviceList;
while (lastdevice->next != NULL) {
lastdevice = lastdevice->next;
}
lastdevice->next = device;
}
++numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = numjoysticks - 1;
if ((SDL_EventOK == NULL) ||
(*SDL_EventOK)(SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
joystick_hwdata::nbuttons
int nbuttons
Definition: SDL_sysjoystick_c.h:44
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:347
joystick_hwdata::guid
SDL_JoystickGUID guid
Definition: SDL_sysjoystick_c.h:65
deviceList
static SDL_JoystickDeviceItem * deviceList
Definition: SDL_sysjoystick.m:51
SDL_JOYDEVICEADDED
Definition: SDL_events.h:113
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
accelerometerName
static const char * accelerometerName
Definition: SDL_sysjoystick.m:48
joystick_hwdata::next
struct joystick_hwdata * next
Definition: SDL_sysjoystick_c.h:47
SDL_JoystickGUID::data
Uint8 data[16]
Definition: SDL_joystick.h:69
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:358
joystick_hwdata::naxes
int naxes
Definition: SDL_sysjoystick_c.h:43
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::accelerometer
SDL_bool accelerometer
Definition: SDL_sysjoystick_c.h:33
joystick_hwdata::instance_id
int instance_id
Definition: SDL_sysjoystick_c.h:64
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:393
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_EventOK
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
SDL_SYS_AddMFIJoystickDevice
static void SDL_SYS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
Definition: SDL_sysjoystick.m:74
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_EventOKParam
void * SDL_EventOKParam
Definition: SDL_events.c:41
NULL
#define NULL
Definition: begin_code.h:143
instancecounter
static SDL_JoystickID instancecounter
Definition: SDL_sysjoystick.m:54
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:399
SDL_strdup
#define SDL_strdup
Definition: SDL_dynapi_overrides.h:403
joystick_hwdata::name
char * name
Definition: SDL_sysjoystick_c.h:38
SDL_JoystickGUID
Definition: SDL_joystick.h:68
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:53
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:380
joystick_hwdata::nhats
int nhats
Definition: SDL_sysjoystick_c.h:45
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523

static void SDL_SYS_AddMFIJoystickDevice | ( | SDL_JoystickDeviceItem * | device,
|  | GCController * | controller
| ) |  |

static

Definition at line 74 of file SDL_sysjoystick.m.

References recDevice::controller, SDL_JoystickGUID::data, recDevice::guid, recDevice::name, recDevice::naxes, recDevice::nbuttons, recDevice::nhats, NULL, and SDL_strdup.

Referenced by SDL_SYS_AddJoystickDevice().

{
#ifdef SDL_JOYSTICK_MFI
const char *name = NULL;
/* Explicitly retain the controller because SDL_JoystickDeviceItem is a
* struct, and ARC doesn't work with structs. */
device->controller = (__bridge GCController *) CFBridgingRetain(controller);
if (controller.vendorName) {
name = controller.vendorName.UTF8String;
}
if (!name) {
name = "MFi Gamepad";
}
device->name = SDL_strdup(name);
device->guid.data[0] = 'M';
device->guid.data[1] = 'F';
device->guid.data[2] = 'i';
device->guid.data[3] = 'G';
device->guid.data[4] = 'a';
device->guid.data[5] = 'm';
device->guid.data[6] = 'e';
device->guid.data[7] = 'p';
device->guid.data[8] = 'a';
device->guid.data[9] = 'd';
if (controller.extendedGamepad) {
device->guid.data[10] = 1;
} else if (controller.gamepad) {
device->guid.data[10] = 2;
}
if (controller.extendedGamepad) {
device->naxes = 6; /* 2 thumbsticks and 2 triggers */
device->nhats = 1; /* d-pad */
device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
} else if (controller.gamepad) {
device->naxes = 0; /* no traditional analog inputs */
device->nhats = 1; /* d-pad */
device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
}
/* TODO: Handle micro profiles on tvOS. */
/* This will be set when the first button press of the controller is
* detected. */
controller.playerIndex = -1;
#endif
}
joystick_hwdata::nbuttons
int nbuttons
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::guid
SDL_JoystickGUID guid
Definition: SDL_sysjoystick_c.h:65
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_JoystickGUID::data
Uint8 data[16]
Definition: SDL_joystick.h:69
joystick_hwdata::naxes
int naxes
Definition: SDL_sysjoystick_c.h:43
NULL
#define NULL
Definition: begin_code.h:143
SDL_strdup
#define SDL_strdup
Definition: SDL_dynapi_overrides.h:403
joystick_hwdata::name
char * name
Definition: SDL_sysjoystick_c.h:38
joystick_hwdata::nhats
int nhats
Definition: SDL_sysjoystick_c.h:45
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35

SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex | ( | int | device_index | ) |

Definition at line 327 of file SDL_sysjoystick.m.

References GetDeviceForIndex(), and recDevice::instance_id.

Referenced by SDL_GameControllerOpen(), and SDL_JoystickOpen().

{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->instance_id : 0;
}
GetDeviceForIndex
static SDL_JoystickDeviceItem * GetDeviceForIndex(int device_index)
Definition: SDL_sysjoystick.m:57
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::instance_id
int instance_id
Definition: SDL_sysjoystick_c.h:64

SDL_bool SDL_SYS_JoystickAttached | ( | SDL_Joystick * | joystick | ) |

Definition at line 382 of file SDL_sysjoystick.m.

References NULL.

Referenced by SDL_JoystickGetAttached().

{
return joystick->hwdata != NULL;
}
NULL
#define NULL
Definition: begin_code.h:143

void SDL_SYS_JoystickClose | ( | SDL_Joystick * | joystick | ) |

Definition at line 586 of file SDL_sysjoystick.m.

References recDevice::accelerometer, recDevice::controller, recDevice::joystick, and NULL.

Referenced by SDL_JoystickClose().

{
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
device->joystick = NULL;
@autoreleasepool {
if (device->accelerometer) {
[motionManager stopAccelerometerUpdates];
} else if (device->controller) {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
controller.controllerPausedHandler = nil;
controller.playerIndex = -1;
#endif
}
}
}
joystick_hwdata::joystick
SDL_Joystick * joystick
Definition: SDL_sysjoystick_c.h:39
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::accelerometer
SDL_bool accelerometer
Definition: SDL_sysjoystick_c.h:33
NULL
#define NULL
Definition: begin_code.h:143
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35

void SDL_SYS_JoystickDetect | ( |  | ) |

Definition at line 314 of file SDL_sysjoystick.m.

Referenced by SDL_JoystickUpdate().

{
}

SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID | ( | int | device_index | ) |

Definition at line 639 of file SDL_sysjoystick.m.

References GetDeviceForIndex(), recDevice::guid, and SDL_zero.

Referenced by SDL_JoystickGetDeviceGUID().

{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
SDL_JoystickGUID guid;
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
return guid;
}
joystick_hwdata::guid
SDL_JoystickGUID guid
Definition: SDL_sysjoystick_c.h:65
GetDeviceForIndex
static SDL_JoystickDeviceItem * GetDeviceForIndex(int device_index)
Definition: SDL_sysjoystick.m:57
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:357
SDL_JoystickGUID
Definition: SDL_joystick.h:68

SDL_JoystickGUID SDL_SYS_JoystickGetGUID | ( | SDL_Joystick * | joystick | ) |

Definition at line 652 of file SDL_sysjoystick.m.

References recDevice::guid, and SDL_zero.

Referenced by SDL_JoystickGetGUID().

{
SDL_JoystickGUID guid;
if (joystick->hwdata) {
guid = joystick->hwdata->guid;
} else {
SDL_zero(guid);
}
return guid;
}
SDL_zero
#define SDL_zero(x)
Definition: SDL_stdinc.h:357
SDL_JoystickGUID
Definition: SDL_joystick.h:68

int SDL_SYS_JoystickInit | ( | void |  | ) |

Definition at line 260 of file SDL_sysjoystick.m.

References recDevice::controller, sort_controllers::controllers, numjoysticks, SDL_atoi, SDL_FALSE, SDL_GetHint, SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_SYS_AddJoystickDevice(), and SDL_TRUE.

Referenced by SDL_JoystickInit().

{
@autoreleasepool {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
if (!hint || SDL_atoi(hint)) {
/* Default behavior, accelerometer as joystick */
SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
}
#ifdef SDL_JOYSTICK_MFI
/* GameController.framework was added in iOS 7. */
if (![GCController class]) {
return numjoysticks;
}
for (GCController *controller in [GCController controllers]) {
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
}
connectObserver = [center addObserverForName:GCControllerDidConnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
}];
disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_JoystickDeviceItem *device = deviceList;
while (device != NULL) {
if (device->controller == controller) {
SDL_SYS_RemoveJoystickDevice(device);
break;
}
device = device->next;
}
}];
#endif /* SDL_JOYSTICK_MFI */
}
return numjoysticks;
}
SDL_GetHint
#define SDL_GetHint
Definition: SDL_dynapi_overrides.h:197
SDL_HINT_ACCELEROMETER_AS_JOYSTICK
#define SDL_HINT_ACCELEROMETER_AS_JOYSTICK
A variable controlling whether the Android / iOS built-in accelerometer should be listed as a joystic...
Definition: SDL_hints.h:289
sort_controllers.controllers
list controllers
Definition: sort_controllers.py:12
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:416
SDL_FALSE
Definition: SDL_stdinc.h:128
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:53
in
GLuint in
Definition: SDL_opengl_glext.h:7940
SDL_SYS_AddJoystickDevice
static void SDL_SYS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
Definition: SDL_sysjoystick.m:127

const char* SDL_SYS_JoystickNameForDeviceIndex | ( | int | device_index | ) |

Definition at line 320 of file SDL_sysjoystick.m.

References GetDeviceForIndex(), and recDevice::name.

Referenced by SDL_JoystickNameForIndex(), and SDL_JoystickOpen().

{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->name : "Unknown";
}
GetDeviceForIndex
static SDL_JoystickDeviceItem * GetDeviceForIndex(int device_index)
Definition: SDL_sysjoystick.m:57
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::name
char * name
Definition: SDL_sysjoystick_c.h:38

int SDL_SYS_JoystickOpen | ( | SDL_Joystick * | joystick,
|  | int | device_index
| ) |  |

Definition at line 339 of file SDL_sysjoystick.m.

References recDevice::accelerometer, recDevice::controller, GetDeviceForIndex(), recDevice::instance_id, recDevice::joystick, motionManager, recDevice::naxes, recDevice::nbuttons, recDevice::nhats, NULL, and SDL_SetError.

Referenced by SDL_JoystickOpen().

{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device == NULL) {
return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
}
joystick->hwdata = device;
joystick->instance_id = device->instance_id;
joystick->naxes = device->naxes;
joystick->nhats = device->nhats;
joystick->nbuttons = device->nbuttons;
joystick->nballs = 0;
device->joystick = joystick;
@autoreleasepool {
if (device->accelerometer) {
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
/* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
} else {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
controller.controllerPausedHandler = ^(GCController *c) {
if (joystick->hwdata) {
++joystick->hwdata->num_pause_presses;
}
};
#endif /* SDL_JOYSTICK_MFI */
}
}
return 0;
}
joystick_hwdata::nbuttons
int nbuttons
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::joystick
SDL_Joystick * joystick
Definition: SDL_sysjoystick_c.h:39
GetDeviceForIndex
static SDL_JoystickDeviceItem * GetDeviceForIndex(int device_index)
Definition: SDL_sysjoystick.m:57
joystick_hwdata::naxes
int naxes
Definition: SDL_sysjoystick_c.h:43
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::accelerometer
SDL_bool accelerometer
Definition: SDL_sysjoystick_c.h:33
joystick_hwdata::instance_id
int instance_id
Definition: SDL_sysjoystick_c.h:64
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
NULL
#define NULL
Definition: begin_code.h:143
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36
joystick_hwdata::nhats
int nhats
Definition: SDL_sysjoystick_c.h:45
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35
motionManager
static CMMotionManager * motionManager
Definition: SDL_sysjoystick.m:49

void SDL_SYS_JoystickQuit | ( | void |  | ) |

Definition at line 611 of file SDL_sysjoystick.m.

References motionManager, NULL, numjoysticks, and SDL_SYS_RemoveJoystickDevice().

Referenced by SDL_JoystickQuit().

{
@autoreleasepool {
#ifdef SDL_JOYSTICK_MFI
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
if (connectObserver) {
[center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
connectObserver = nil;
}
if (disconnectObserver) {
[center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
disconnectObserver = nil;
}
#endif /* SDL_JOYSTICK_MFI */
while (deviceList != NULL) {
SDL_SYS_RemoveJoystickDevice(deviceList);
}
motionManager = nil;
}
numjoysticks = 0;
}
deviceList
static SDL_JoystickDeviceItem * deviceList
Definition: SDL_sysjoystick.m:51
NULL
#define NULL
Definition: begin_code.h:143
SDL_SYS_RemoveJoystickDevice
static SDL_JoystickDeviceItem * SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
Definition: SDL_sysjoystick.m:189
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:53
motionManager
static CMMotionManager * motionManager
Definition: SDL_sysjoystick.m:49

void SDL_SYS_JoystickUpdate | ( | SDL_Joystick * | joystick | ) |

Definition at line 569 of file SDL_sysjoystick.m.

References recDevice::accelerometer, recDevice::controller, NULL, SDL_SYS_AccelerometerUpdate(), and SDL_SYS_MFIJoystickUpdate().

Referenced by SDL_GameControllerOpen(), SDL_JoystickOpen(), and SDL_JoystickUpdate().

{
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
if (device->accelerometer) {
SDL_SYS_AccelerometerUpdate(joystick);
} else if (device->controller) {
SDL_SYS_MFIJoystickUpdate(joystick);
}
}
SDL_SYS_MFIJoystickUpdate
static void SDL_SYS_MFIJoystickUpdate(SDL_Joystick *joystick)
Definition: SDL_sysjoystick.m:456
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
joystick_hwdata::accelerometer
SDL_bool accelerometer
Definition: SDL_sysjoystick_c.h:33
NULL
#define NULL
Definition: begin_code.h:143
SDL_SYS_AccelerometerUpdate
static void SDL_SYS_AccelerometerUpdate(SDL_Joystick *joystick)
Definition: SDL_sysjoystick.m:388
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35

static void SDL_SYS_MFIJoystickUpdate | ( | SDL_Joystick * | joystick | ) |

static

Definition at line 456 of file SDL_sysjoystick.m.

References recDevice::axes, recDevice::buttons, recDevice::controller, sort_controllers::controllers, i, SDL_arraysize, SDL_HAT_CENTERED, SDL_PRESSED, SDL_PrivateJoystickAxis(), SDL_PrivateJoystickButton(), SDL_PrivateJoystickHat(), and SDL_RELEASED.

Referenced by SDL_SYS_JoystickUpdate().

{
#ifdef SDL_JOYSTICK_MFI
@autoreleasepool {
GCController *controller = joystick->hwdata->controller;
Uint8 hatstate = SDL_HAT_CENTERED;
int i;
int updateplayerindex = 0;
if (controller.extendedGamepad) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
/* Axis order matches the XInput Windows mappings. */
Sint16 axes[] = {
(Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
(Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
};
/* Button order matches the XInput Windows mappings. */
Uint8 buttons[] = {
gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
gamepad.leftShoulder.isPressed,
gamepad.rightShoulder.isPressed,
};
hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
for (i = 0; i < SDL_arraysize(axes); i++) {
/* The triggers (axes 2 and 5) are resting at -32768 but SDL
* initializes its values to 0. We only want to make sure the
* player index is up to date if the user actually moves an axis. */
if ((i != 2 && i != 5) || axes[i] != -32768) {
updateplayerindex |= (joystick->axes[i] != axes[i]);
}
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
for (i = 0; i < SDL_arraysize(buttons); i++) {
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
} else if (controller.gamepad) {
GCGamepad *gamepad = controller.gamepad;
/* Button order matches the XInput Windows mappings. */
Uint8 buttons[] = {
gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
gamepad.leftShoulder.isPressed,
gamepad.rightShoulder.isPressed,
};
hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
for (i = 0; i < SDL_arraysize(buttons); i++) {
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
}
/* TODO: Handle micro profiles on tvOS. */
if (joystick->nhats > 0) {
updateplayerindex |= (joystick->hats[0] != hatstate);
SDL_PrivateJoystickHat(joystick, 0, hatstate);
}
for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
/* The pause button is always last. */
Uint8 pausebutton = joystick->nbuttons - 1;
SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
updateplayerindex = YES;
}
joystick->hwdata->num_pause_presses = 0;
if (updateplayerindex && controller.playerIndex == -1) {
BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
/* Find the player index of all other connected controllers. */
for (GCController *c in [GCController controllers]) {
if (c != controller && c.playerIndex >= 0) {
usedPlayerIndexSlots[c.playerIndex] = YES;
}
}
/* Set this controller's player index to the first unused index.
* FIXME: This logic isn't great... but SDL doesn't expose this
* concept in its external API, so we don't have much to go on. */
for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
if (!usedPlayerIndexSlots[i]) {
controller.playerIndex = i;
break;
}
}
}
}
#endif
}
SDL_PrivateJoystickHat
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
Definition: SDL_joystick.c:542
SDL_PrivateJoystickButton
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:618
SDL_PrivateJoystickAxis
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:501
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
sort_controllers.controllers
list controllers
Definition: sort_controllers.py:12
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_HAT_CENTERED
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
in
GLuint in
Definition: SDL_opengl_glext.h:7940
Sint16
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:143

int SDL_SYS_NumJoysticks | ( |  | ) |

Definition at line 309 of file SDL_sysjoystick.m.

References numjoysticks.

Referenced by SDL_NumJoysticks().

{
return numjoysticks;
}
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:53

static SDL_JoystickDeviceItem* SDL_SYS_RemoveJoystickDevice | ( | SDL_JoystickDeviceItem * | device | ) |

static

Definition at line 189 of file SDL_sysjoystick.m.

References recDevice::controller, deviceList, recDevice::instance_id, recDevice::item, recDevice::joystick, recDevice::name, recDevice::next, NULL, numjoysticks, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_free(), SDL_GetEventState, SDL_JOYDEVICEREMOVED, SDL_PushEvent, and SDL_Event::type.

Referenced by SDL_SYS_JoystickQuit().

{
SDL_JoystickDeviceItem *prev = NULL;
SDL_JoystickDeviceItem *next = NULL;
SDL_JoystickDeviceItem *item = deviceList;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
if (device == NULL) {
return NULL;
}
next = device->next;
while (item != NULL) {
if (item == device) {
break;
}
prev = item;
item = item->next;
}
/* Unlink the device item from the device list. */
if (prev) {
prev->next = device->next;
} else if (device == deviceList) {
deviceList = device->next;
}
if (device->joystick) {
device->joystick->hwdata = NULL;
}
#ifdef SDL_JOYSTICK_MFI
@autoreleasepool {
if (device->controller) {
/* The controller was explicitly retained in the struct, so it
* should be explicitly released before freeing the struct. */
GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
controller.controllerPausedHandler = nil;
device->controller = nil;
}
}
#endif /* SDL_JOYSTICK_MFI */
--numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device->instance_id;
if ((SDL_EventOK == NULL) ||
(*SDL_EventOK)(SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_free(device->name);
SDL_free(device);
return next;
}
deviceList
static SDL_JoystickDeviceItem * deviceList
Definition: SDL_sysjoystick.m:51
joystick_hwdata::joystick
SDL_Joystick * joystick
Definition: SDL_sysjoystick_c.h:39
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
joystick_hwdata::next
struct joystick_hwdata * next
Definition: SDL_sysjoystick_c.h:47
joystick_hwdata
Definition: SDL_sysjoystick_c.h:44
SDL_JOYDEVICEREMOVED
Definition: SDL_events.h:114
joystick_hwdata::instance_id
int instance_id
Definition: SDL_sysjoystick_c.h:64
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_free
void SDL_free(void *mem)
SDL_EventOK
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_EventOKParam
void * SDL_EventOKParam
Definition: SDL_events.c:41
NULL
#define NULL
Definition: begin_code.h:143
joystick_hwdata::name
char * name
Definition: SDL_sysjoystick_c.h:38
numjoysticks
static int numjoysticks
Definition: SDL_sysjoystick.m:53
SDL_Event
General event structure.
Definition: SDL_events.h:521
joystick_hwdata::controller
GCController __unsafe_unretained * controller
Definition: SDL_sysjoystick_c.h:35
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523

## Variable Documentation

const char* accelerometerName = "iOS Accelerometer"

static

Definition at line 48 of file SDL_sysjoystick.m.

Referenced by SDL_SYS_AddJoystickDevice().

SDL_JoystickDeviceItem* deviceList = NULL

static

Definition at line 51 of file SDL_sysjoystick.m.

Referenced by GetDeviceForIndex(), SDL_SYS_AddJoystickDevice(), and SDL_SYS_RemoveJoystickDevice().

SDL_JoystickID instancecounter = 0

static

Definition at line 54 of file SDL_sysjoystick.m.

Referenced by SDL_SYS_AddJoystickDevice().

CMMotionManager* motionManager = nil

static

Definition at line 49 of file SDL_sysjoystick.m.

Referenced by SDL_SYS_AccelerometerUpdate(), SDL_SYS_JoystickOpen(), and SDL_SYS_JoystickQuit().

int numjoysticks = 0

static

Definition at line 53 of file SDL_sysjoystick.m.

Referenced by SDL_SYS_AddJoystickDevice(), SDL_SYS_JoystickInit(), SDL_SYS_JoystickQuit(), SDL_SYS_NumJoysticks(), and SDL_SYS_RemoveJoystickDevice().