SDL  2.0
SDL_sensor.c File Reference
#include "../SDL_internal.h"
#include "SDL.h"
#include "SDL_atomic.h"
#include "SDL_events.h"
#include "SDL_syssensor.h"
#include "SDL_assert.h"
#include "../events/SDL_events_c.h"
+ Include dependency graph for SDL_sensor.c:

Go to the source code of this file.

Functions

static void SDL_LockSensors (void)
static void SDL_UnlockSensors (void)
int SDL_SensorInit (void)
int SDL_NumSensors (void)
 Count the number of sensors attached to the system right now.
SDL_SensorID SDL_GetNextSensorInstanceID ()
static SDL_bool SDL_GetDriverAndSensorIndex (int device_index, SDL_SensorDriver **driver, int *driver_index)
const char * SDL_SensorGetDeviceName (int device_index)
 Get the implementation dependent name of a sensor.
SDL_SensorType SDL_SensorGetDeviceType (int device_index)
 Get the type of a sensor.
SDL_SensorType SDL_SensorGetDeviceNonPortableType (int device_index)
 Get the platform dependent type of a sensor.
SDL_SensorID SDL_SensorGetDeviceInstanceID (int device_index)
 Get the instance ID of a sensor.
SDL_Sensor * SDL_SensorOpen (int device_index)
 Open a sensor for use.
SDL_Sensor * SDL_SensorFromInstanceID (SDL_SensorID instance_id)
static int SDL_PrivateSensorValid (SDL_Sensor *sensor)
const char * SDL_SensorGetName (SDL_Sensor *sensor)
 Get the implementation dependent name of a sensor.
SDL_SensorType SDL_SensorGetType (SDL_Sensor *sensor)
 Get the type of a sensor.
int SDL_SensorGetNonPortableType (SDL_Sensor *sensor)
 Get the platform dependent type of a sensor.
SDL_SensorID SDL_SensorGetInstanceID (SDL_Sensor *sensor)
 Get the instance ID of a sensor.
int SDL_SensorGetData (SDL_Sensor *sensor, float *data, int num_values)
void SDL_SensorClose (SDL_Sensor *sensor)
void SDL_SensorQuit (void)
int SDL_PrivateSensorUpdate (SDL_Sensor *sensor, float *data, int num_values)
void SDL_SensorUpdate (void)

Variables

static SDL_SensorDriverSDL_sensor_drivers []
static SDL_Sensor * SDL_sensors = NULL
static SDL_bool SDL_updating_sensor = SDL_FALSE
static SDL_mutexSDL_sensor_lock = NULL
static SDL_atomic_t SDL_next_sensor_instance_id

Function Documentation

static SDL_bool SDL_GetDriverAndSensorIndex ( int  device_index,
SDL_SensorDriver **  driver,
int *  driver_index 
)
static

Definition at line 122 of file SDL_sensor.c.

References SDL_SensorDriver::GetCount, i, SDL_arraysize, SDL_FALSE, SDL_SetError, and SDL_TRUE.

Referenced by SDL_SensorGetDeviceInstanceID(), SDL_SensorGetDeviceName(), SDL_SensorGetDeviceNonPortableType(), SDL_SensorGetDeviceType(), and SDL_SensorOpen().

{
int i, num_sensors, total_sensors = 0;
if (device_index >= 0) {
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
num_sensors = SDL_sensor_drivers[i]->GetCount();
if (device_index < num_sensors) {
*driver = SDL_sensor_drivers[i];
*driver_index = device_index;
return SDL_TRUE;
}
device_index -= num_sensors;
total_sensors += num_sensors;
}
}
SDL_SetError("There are %d sensors available", total_sensors);
return SDL_FALSE;
}
SDL_SensorID SDL_GetNextSensorInstanceID ( void  )

Definition at line 112 of file SDL_sensor.c.

References SDL_AtomicIncRef.

int SDL_NumSensors ( void  )

Count the number of sensors attached to the system right now.

Gyroscope sensor

The gyroscope returns the current rate of rotation in radians per second. The rotation is positive in the counter-clockwise direction. That is, an observer looking from a positive location on one of the axes would see positive rotation on that axis when it appeared to be rotating counter-clockwise.

values[0]: Angular speed around the x axis values[1]: Angular speed around the y axis values[2]: Angular speed around the z axis

For phones held in portrait mode, the axes are defined as follows: -X ... +X : left ... right -Y ... +Y : bottom ... top -Z ... +Z : farther ... closer

The axis data is not changed when the phone is rotated.

See Also
SDL_GetDisplayOrientation()

Definition at line 97 of file SDL_sensor.c.

References SDL_SensorDriver::GetCount, i, SDL_arraysize, SDL_LockSensors(), and SDL_UnlockSensors().

{
int i, total_sensors = 0;
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
total_sensors += SDL_sensor_drivers[i]->GetCount();
}
return total_sensors;
}
int SDL_PrivateSensorUpdate ( SDL_Sensor *  sensor,
float *  data,
int  num_values 
)

Definition at line 476 of file SDL_sensor.c.

References SDL_SensorEvent::data, SDL_arraysize, SDL_ENABLE, SDL_GetEventState, SDL_JOYAXISMOTION, SDL_memcpy, SDL_memset, SDL_min, SDL_PushEvent, SDL_SENSORUPDATE, and SDL_Event::sensor.

{
int posted;
/* Allow duplicate events, for things like steps and heartbeats */
/* Update internal sensor state */
num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
SDL_memcpy(sensor->data, data, num_values*sizeof(*data));
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
event.type = SDL_SENSORUPDATE;
event.sensor.which = sensor->instance_id;
num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
SDL_memcpy(event.sensor.data, data, num_values*sizeof(*data));
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */
return posted;
}
static int SDL_PrivateSensorValid ( SDL_Sensor *  sensor)
static

Definition at line 305 of file SDL_sensor.c.

References NULL, and SDL_SetError.

Referenced by SDL_SensorClose(), SDL_SensorGetData(), SDL_SensorGetInstanceID(), SDL_SensorGetName(), SDL_SensorGetNonPortableType(), and SDL_SensorGetType().

{
int valid;
if (sensor == NULL) {
SDL_SetError("Sensor hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
void SDL_SensorClose ( SDL_Sensor *  sensor)

Close a sensor previously opened with SDL_SensorOpen()

Definition at line 390 of file SDL_sensor.c.

References NULL, SDL_free, SDL_LockSensors(), SDL_PrivateSensorValid(), SDL_sensors, SDL_UnlockSensors(), and SDL_updating_sensor.

{
SDL_Sensor *sensorlist;
SDL_Sensor *sensorlistprev;
if (!SDL_PrivateSensorValid(sensor)) {
return;
}
/* First decrement ref count */
if (--sensor->ref_count > 0) {
return;
}
return;
}
sensor->driver->Close(sensor);
sensor->hwdata = NULL;
sensorlist = SDL_sensors;
sensorlistprev = NULL;
while (sensorlist) {
if (sensor == sensorlist) {
if (sensorlistprev) {
/* unlink this entry */
sensorlistprev->next = sensorlist->next;
} else {
SDL_sensors = sensor->next;
}
break;
}
sensorlistprev = sensorlist;
sensorlist = sensorlist->next;
}
SDL_free(sensor->name);
/* Free the data associated with this sensor */
SDL_free(sensor);
}
SDL_Sensor* SDL_SensorFromInstanceID ( SDL_SensorID  instance_id)

Return the SDL_Sensor associated with an instance id.

Definition at line 287 of file SDL_sensor.c.

References SDL_LockSensors(), SDL_sensors, and SDL_UnlockSensors().

{
SDL_Sensor *sensor;
for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
if (sensor->instance_id == instance_id) {
break;
}
}
return sensor;
}
int SDL_SensorGetData ( SDL_Sensor *  sensor,
float *  data,
int  num_values 
)

Get the current state of an opened sensor.

The number of values and interpretation of the data is sensor dependent.

Parameters
sensorThe sensor to query
dataA pointer filled with the current sensor state
num_valuesThe number of values to write to data
Returns
0 or -1 if an error occurred.

Definition at line 375 of file SDL_sensor.c.

References SDL_arraysize, SDL_memcpy, SDL_min, and SDL_PrivateSensorValid().

{
if (!SDL_PrivateSensorValid(sensor)) {
return -1;
}
num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
SDL_memcpy(data, sensor->data, num_values*sizeof(*data));
return 0;
}
SDL_SensorID SDL_SensorGetDeviceInstanceID ( int  device_index)

Get the instance ID of a sensor.

This can be called before any sensors are opened.

Returns
The sensor instance ID, or -1 if device_index is out of range.

Definition at line 193 of file SDL_sensor.c.

References SDL_SensorDriver::GetDeviceInstanceID, SDL_GetDriverAndSensorIndex(), SDL_LockSensors(), and SDL_UnlockSensors().

{
SDL_SensorID instance_id = -1;
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
instance_id = driver->GetDeviceInstanceID(device_index);
}
return instance_id;
}
const char* SDL_SensorGetDeviceName ( int  device_index)

Get the implementation dependent name of a sensor.

This can be called before any sensors are opened.

Returns
The sensor name, or NULL if device_index is out of range.

Definition at line 147 of file SDL_sensor.c.

References SDL_SensorDriver::GetDeviceName, NULL, SDL_GetDriverAndSensorIndex(), SDL_LockSensors(), and SDL_UnlockSensors().

{
const char *name = NULL;
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
name = driver->GetDeviceName(device_index);
}
/* FIXME: Really we should reference count this name so it doesn't go away after unlock */
return name;
}
SDL_SensorType SDL_SensorGetDeviceNonPortableType ( int  device_index)

Get the platform dependent type of a sensor.

This can be called before any sensors are opened.

Returns
The sensor platform dependent type, or -1 if device_index is out of range.

Definition at line 178 of file SDL_sensor.c.

References SDL_SensorDriver::GetDeviceNonPortableType, SDL_GetDriverAndSensorIndex(), SDL_LockSensors(), and SDL_UnlockSensors().

{
int type = -1;
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
type = driver->GetDeviceNonPortableType(device_index);
}
return type;
}
SDL_SensorType SDL_SensorGetDeviceType ( int  device_index)

Get the type of a sensor.

This can be called before any sensors are opened.

Returns
The sensor type, or SDL_SENSOR_INVALID if device_index is out of range.

Definition at line 163 of file SDL_sensor.c.

References SDL_SensorDriver::GetDeviceType, SDL_GetDriverAndSensorIndex(), SDL_LockSensors(), SDL_SENSOR_INVALID, and SDL_UnlockSensors().

{
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
type = driver->GetDeviceType(device_index);
}
return type;
}
SDL_SensorID SDL_SensorGetInstanceID ( SDL_Sensor *  sensor)

Get the instance ID of a sensor.

This can be called before any sensors are opened.

Returns
The sensor instance ID, or -1 if the sensor is NULL.

Definition at line 362 of file SDL_sensor.c.

References SDL_PrivateSensorValid().

{
if (!SDL_PrivateSensorValid(sensor)) {
return -1;
}
return sensor->instance_id;
}
const char* SDL_SensorGetName ( SDL_Sensor *  sensor)

Get the implementation dependent name of a sensor.

Returns
The sensor name, or NULL if the sensor is NULL.

Definition at line 323 of file SDL_sensor.c.

References NULL, and SDL_PrivateSensorValid().

{
if (!SDL_PrivateSensorValid(sensor)) {
return NULL;
}
return sensor->name;
}
int SDL_SensorGetNonPortableType ( SDL_Sensor *  sensor)

Get the platform dependent type of a sensor.

This can be called before any sensors are opened.

Returns
The sensor platform dependent type, or -1 if the sensor is NULL.

Definition at line 349 of file SDL_sensor.c.

References SDL_PrivateSensorValid().

{
if (!SDL_PrivateSensorValid(sensor)) {
return -1;
}
return sensor->non_portable_type;
}
SDL_SensorType SDL_SensorGetType ( SDL_Sensor *  sensor)

Get the type of a sensor.

This can be called before any sensors are opened.

Returns
The sensor type, or SDL_SENSOR_INVALID if the sensor is NULL.

Definition at line 336 of file SDL_sensor.c.

References SDL_PrivateSensorValid(), and SDL_SENSOR_INVALID.

{
if (!SDL_PrivateSensorValid(sensor)) {
}
return sensor->type;
}
int SDL_SensorInit ( void  )

Definition at line 69 of file SDL_sensor.c.

References i, SDL_arraysize, SDL_CreateMutex, SDL_INIT_EVENTS, and SDL_InitSubSystem.

Referenced by SDL_InitSubSystem().

{
int i, status;
/* Create the sensor list lock */
}
#if !SDL_EVENTS_DISABLED
return -1;
}
#endif /* !SDL_EVENTS_DISABLED */
status = -1;
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
if (SDL_sensor_drivers[i]->Init() >= 0) {
status = 0;
}
}
return status;
}
SDL_Sensor* SDL_SensorOpen ( int  device_index)

Open a sensor for use.

The index passed as an argument refers to the N'th sensor on the system.

Returns
A sensor identifier, or NULL if an error occurred.

Definition at line 215 of file SDL_sensor.c.

References SDL_SensorDriver::GetDeviceInstanceID, SDL_SensorDriver::GetDeviceName, SDL_SensorDriver::GetDeviceNonPortableType, SDL_SensorDriver::GetDeviceType, NULL, SDL_SensorDriver::Open, SDL_calloc, SDL_free, SDL_GetDriverAndSensorIndex(), SDL_LockSensors(), SDL_OutOfMemory, SDL_sensors, SDL_strdup, SDL_UnlockSensors(), and SDL_SensorDriver::Update.

{
SDL_SensorID instance_id;
SDL_Sensor *sensor;
SDL_Sensor *sensorlist;
const char *sensorname = NULL;
if (!SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
return NULL;
}
sensorlist = SDL_sensors;
/* If the sensor is already open, return it
* it is important that we have a single sensor * for each instance id
*/
instance_id = driver->GetDeviceInstanceID(device_index);
while (sensorlist) {
if (instance_id == sensorlist->instance_id) {
sensor = sensorlist;
++sensor->ref_count;
return sensor;
}
sensorlist = sensorlist->next;
}
/* Create and initialize the sensor */
sensor = (SDL_Sensor *) SDL_calloc(sizeof(*sensor), 1);
if (sensor == NULL) {
return NULL;
}
sensor->driver = driver;
sensor->instance_id = instance_id;
sensor->type = driver->GetDeviceType(device_index);
sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
if (driver->Open(sensor, device_index) < 0) {
SDL_free(sensor);
return NULL;
}
sensorname = driver->GetDeviceName(device_index);
if (sensorname) {
sensor->name = SDL_strdup(sensorname);
} else {
sensor->name = NULL;
}
/* Add sensor to list */
++sensor->ref_count;
/* Link the sensor in the list */
sensor->next = SDL_sensors;
SDL_sensors = sensor;
driver->Update(sensor);
return sensor;
}
void SDL_SensorQuit ( void  )

Definition at line 440 of file SDL_sensor.c.

References i, NULL, SDL_SensorDriver::Quit, SDL_arraysize, SDL_assert, SDL_DestroyMutex, SDL_INIT_EVENTS, SDL_LockSensors(), SDL_QuitSubSystem, SDL_SensorClose, SDL_sensors, SDL_UnlockSensors(), and SDL_updating_sensor.

Referenced by SDL_QuitSubSystem().

{
int i;
/* Make sure we're not getting called in the middle of updating sensors */
/* Stop the event polling */
while (SDL_sensors) {
SDL_sensors->ref_count = 1;
}
/* Quit the sensor setup */
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
}
#if !SDL_EVENTS_DISABLED
#endif
}
}
void SDL_SensorUpdate ( void  )

Update the current state of the open sensors.

This is called automatically by the event loop if sensor events are enabled.

This needs to be called from the thread that initialized the sensor subsystem.

Definition at line 503 of file SDL_sensor.c.

References SDL_SensorDriver::Detect, i, SDL_arraysize, SDL_FALSE, SDL_LockSensors(), SDL_SensorClose, SDL_sensors, SDL_TRUE, SDL_UnlockSensors(), and SDL_updating_sensor.

{
int i;
SDL_Sensor *sensor;
/* The sensors are already being updated */
return;
}
/* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
sensor->driver->Update(sensor);
}
/* If any sensors were closed while updating, free them here */
for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
if (sensor->ref_count <= 0) {
SDL_SensorClose(sensor);
}
}
/* this needs to happen AFTER walking the sensor list above, so that any
dangling hardware data from removed devices can be free'd
*/
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
}
}

Variable Documentation

SDL_atomic_t SDL_next_sensor_instance_id
static

Definition at line 49 of file SDL_sensor.c.

SDL_SensorDriver* SDL_sensor_drivers[]
static
Initial value:

Definition at line 35 of file SDL_sensor.c.

SDL_mutex* SDL_sensor_lock = NULL
static

Definition at line 48 of file SDL_sensor.c.

SDL_Sensor* SDL_sensors = NULL
static
SDL_bool SDL_updating_sensor = SDL_FALSE
static

Definition at line 47 of file SDL_sensor.c.

Referenced by SDL_SensorClose(), SDL_SensorQuit(), and SDL_SensorUpdate().