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 "../events/SDL_events_c.h"
+ Include dependency graph for SDL_sensor.c:

Go to the source code of this file.

Functions

void SDL_LockSensors (void)
 
void SDL_UnlockSensors (void)
 
int SDL_SensorInit (void)
 
int SDL_NumSensors (void)
 Count the number of sensors attached to the system right now. More...
 
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. More...
 
SDL_SensorType SDL_SensorGetDeviceType (int device_index)
 Get the type of a sensor. More...
 
int SDL_SensorGetDeviceNonPortableType (int device_index)
 Get the platform dependent type of a sensor. More...
 
SDL_SensorID SDL_SensorGetDeviceInstanceID (int device_index)
 Get the instance ID of a sensor. More...
 
SDL_Sensor * SDL_SensorOpen (int device_index)
 Open a sensor for use. More...
 
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. More...
 
SDL_SensorType SDL_SensorGetType (SDL_Sensor *sensor)
 Get the type of a sensor. More...
 
int SDL_SensorGetNonPortableType (SDL_Sensor *sensor)
 Get the platform dependent type of a sensor. More...
 
SDL_SensorID SDL_SensorGetInstanceID (SDL_Sensor *sensor)
 Get the instance ID of a sensor. More...
 
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

◆ SDL_GetDriverAndSensorIndex()

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

Definition at line 124 of file SDL_sensor.c.

125 {
126  int i, num_sensors, total_sensors = 0;
127 
128  if (device_index >= 0) {
129  for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
130  num_sensors = SDL_sensor_drivers[i]->GetCount();
131  if (device_index < num_sensors) {
132  *driver = SDL_sensor_drivers[i];
133  *driver_index = device_index;
134  return SDL_TRUE;
135  }
136  device_index -= num_sensors;
137  total_sensors += num_sensors;
138  }
139  }
140 
141  SDL_SetError("There are %d sensors available", total_sensors);
142  return SDL_FALSE;
143 }
#define SDL_SetError
static SDL_SensorDriver * SDL_sensor_drivers[]
Definition: SDL_sensor.c:34
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:121
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:50
int(* GetCount)(void)
Definition: SDL_syssensor.h:60

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

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

◆ SDL_GetNextSensorInstanceID()

SDL_SensorID SDL_GetNextSensorInstanceID ( void  )

Definition at line 114 of file SDL_sensor.c.

115 {
117 }
#define SDL_AtomicIncRef(a)
Increment an atomic variable used as a reference count.
Definition: SDL_atomic.h:252
static SDL_atomic_t SDL_next_sensor_instance_id
Definition: SDL_sensor.c:51

References SDL_AtomicIncRef, and SDL_next_sensor_instance_id.

◆ SDL_LockSensors()

void SDL_LockSensors ( void  )

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 (pitch) values[1]: Angular speed around the y axis (yaw) values[2]: Angular speed around the z axis (roll)

For phones held in portrait mode and game controllers held in front of you, 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 or controller is rotated.

See also
SDL_GetDisplayOrientation() Locking for multi-threaded access to the sensor hidapi API

If you are using the sensor API or handling events from multiple threads you should use these locking functions to protect access to the sensors.

In particular, you are guaranteed that the sensor list won't change, so the API functions that take a sensor index will be valid, and sensor events will not be delivered.

Definition at line 54 of file SDL_sensor.c.

55 {
56  if (SDL_sensor_lock) {
58  }
59 }
#define SDL_LockMutex
static SDL_mutex * SDL_sensor_lock
Definition: SDL_sensor.c:50

References SDL_LockMutex, and SDL_sensor_lock.

Referenced by SDL_NumSensors(), SDL_SensorClose(), SDL_SensorFromInstanceID(), SDL_SensorGetDeviceInstanceID(), SDL_SensorGetDeviceName(), SDL_SensorGetDeviceNonPortableType(), SDL_SensorGetDeviceType(), SDL_SensorOpen(), SDL_SensorQuit(), and SDL_SensorUpdate().

◆ SDL_NumSensors()

int SDL_NumSensors ( void  )

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

Definition at line 99 of file SDL_sensor.c.

100 {
101  int i, total_sensors = 0;
102  SDL_LockSensors();
103  for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
104  total_sensors += SDL_sensor_drivers[i]->GetCount();
105  }
107  return total_sensors;
108 }
void SDL_UnlockSensors(void)
Definition: SDL_sensor.c:62
void SDL_LockSensors(void)
Definition: SDL_sensor.c:54

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

◆ SDL_PrivateSensorUpdate()

int SDL_PrivateSensorUpdate ( SDL_Sensor *  sensor,
float *  data,
int  num_values 
)

Definition at line 478 of file SDL_sensor.c.

479 {
480  int posted;
481 
482  /* Allow duplicate events, for things like steps and heartbeats */
483 
484  /* Update internal sensor state */
485  num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
486  SDL_memcpy(sensor->data, data, num_values*sizeof(*data));
487 
488  /* Post the event, if desired */
489  posted = 0;
490 #if !SDL_EVENTS_DISABLED
493  event.type = SDL_SENSORUPDATE;
494  event.sensor.which = sensor->instance_id;
495  num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
496  SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
497  SDL_memcpy(event.sensor.data, data, num_values*sizeof(*data));
498  posted = SDL_PushEvent(&event) == 1;
499  }
500 #endif /* !SDL_EVENTS_DISABLED */
501  return posted;
502 }
#define SDL_memset
#define SDL_PushEvent
#define SDL_memcpy
@ SDL_SENSORUPDATE
Definition: SDL_events.h:157
#define SDL_GetEventState(type)
Definition: SDL_events.h:808
#define SDL_ENABLE
Definition: SDL_events.h:795
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
struct _cl_event * event
#define SDL_min(x, y)
Definition: SDL_stdinc.h:412
General event structure.
Definition: SDL_events.h:592

References SDL_arraysize, SDL_ENABLE, SDL_GetEventState, SDL_memcpy, SDL_memset, SDL_min, SDL_PushEvent, and SDL_SENSORUPDATE.

◆ SDL_PrivateSensorValid()

static int SDL_PrivateSensorValid ( SDL_Sensor *  sensor)
static

Definition at line 307 of file SDL_sensor.c.

308 {
309  int valid;
310 
311  if (sensor == NULL) {
312  SDL_SetError("Sensor hasn't been opened yet");
313  valid = 0;
314  } else {
315  valid = 1;
316  }
317 
318  return valid;
319 }
#define NULL
Definition: begin_code.h:163

References NULL, and SDL_SetError.

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

◆ SDL_SensorClose()

void SDL_SensorClose ( SDL_Sensor *  sensor)

Close a sensor previously opened with SDL_SensorOpen()

Definition at line 392 of file SDL_sensor.c.

393 {
394  SDL_Sensor *sensorlist;
395  SDL_Sensor *sensorlistprev;
396 
397  if (!SDL_PrivateSensorValid(sensor)) {
398  return;
399  }
400 
401  SDL_LockSensors();
402 
403  /* First decrement ref count */
404  if (--sensor->ref_count > 0) {
406  return;
407  }
408 
409  if (SDL_updating_sensor) {
411  return;
412  }
413 
414  sensor->driver->Close(sensor);
415  sensor->hwdata = NULL;
416 
417  sensorlist = SDL_sensors;
418  sensorlistprev = NULL;
419  while (sensorlist) {
420  if (sensor == sensorlist) {
421  if (sensorlistprev) {
422  /* unlink this entry */
423  sensorlistprev->next = sensorlist->next;
424  } else {
425  SDL_sensors = sensor->next;
426  }
427  break;
428  }
429  sensorlistprev = sensorlist;
430  sensorlist = sensorlist->next;
431  }
432 
433  SDL_free(sensor->name);
434 
435  /* Free the data associated with this sensor */
436  SDL_free(sensor);
437 
439 }
#define SDL_free
static SDL_Sensor * SDL_sensors
Definition: SDL_sensor.c:48
static SDL_bool SDL_updating_sensor
Definition: SDL_sensor.c:49
static int SDL_PrivateSensorValid(SDL_Sensor *sensor)
Definition: SDL_sensor.c:307

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

Referenced by SDL_SensorQuit(), and SDL_SensorUpdate().

◆ SDL_SensorFromInstanceID()

SDL_Sensor* SDL_SensorFromInstanceID ( SDL_SensorID  instance_id)

Return the SDL_Sensor associated with an instance id.

Definition at line 289 of file SDL_sensor.c.

290 {
291  SDL_Sensor *sensor;
292 
293  SDL_LockSensors();
294  for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
295  if (sensor->instance_id == instance_id) {
296  break;
297  }
298  }
300  return sensor;
301 }

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

◆ SDL_SensorGetData()

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 377 of file SDL_sensor.c.

378 {
379  if (!SDL_PrivateSensorValid(sensor)) {
380  return -1;
381  }
382 
383  num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
384  SDL_memcpy(data, sensor->data, num_values*sizeof(*data));
385  return 0;
386 }

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

◆ SDL_SensorGetDeviceInstanceID()

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 195 of file SDL_sensor.c.

196 {
197  SDL_SensorDriver *driver;
198  SDL_SensorID instance_id = -1;
199 
200  SDL_LockSensors();
201  if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
202  instance_id = driver->GetDeviceInstanceID(device_index);
203  }
205 
206  return instance_id;
207 }
static SDL_bool SDL_GetDriverAndSensorIndex(int device_index, SDL_SensorDriver **driver, int *driver_index)
Definition: SDL_sensor.c:124
Sint32 SDL_SensorID
Definition: SDL_sensor.h:60
SDL_SensorID(* GetDeviceInstanceID)(int device_index)
Definition: SDL_syssensor.h:75

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

◆ SDL_SensorGetDeviceName()

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 149 of file SDL_sensor.c.

150 {
151  SDL_SensorDriver *driver;
152  const char *name = NULL;
153 
154  SDL_LockSensors();
155  if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
156  name = driver->GetDeviceName(device_index);
157  }
159 
160  /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
161  return name;
162 }
GLuint const GLchar * name
const char *(* GetDeviceName)(int device_index)
Definition: SDL_syssensor.h:66

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

◆ SDL_SensorGetDeviceNonPortableType()

int 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 180 of file SDL_sensor.c.

181 {
182  SDL_SensorDriver *driver;
183  int type = -1;
184 
185  SDL_LockSensors();
186  if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
187  type = driver->GetDeviceNonPortableType(device_index);
188  }
190 
191  return type;
192 }
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
int(* GetDeviceNonPortableType)(int device_index)
Definition: SDL_syssensor.h:72

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

◆ SDL_SensorGetDeviceType()

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 165 of file SDL_sensor.c.

166 {
167  SDL_SensorDriver *driver;
169 
170  SDL_LockSensors();
171  if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
172  type = driver->GetDeviceType(device_index);
173  }
175 
176  return type;
177 }
SDL_SensorType
Definition: SDL_sensor.h:70
@ SDL_SENSOR_INVALID
Definition: SDL_sensor.h:71
SDL_SensorType(* GetDeviceType)(int device_index)
Definition: SDL_syssensor.h:69

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

◆ SDL_SensorGetInstanceID()

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 364 of file SDL_sensor.c.

365 {
366  if (!SDL_PrivateSensorValid(sensor)) {
367  return -1;
368  }
369 
370  return sensor->instance_id;
371 }

References SDL_PrivateSensorValid().

◆ SDL_SensorGetName()

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 325 of file SDL_sensor.c.

326 {
327  if (!SDL_PrivateSensorValid(sensor)) {
328  return NULL;
329  }
330 
331  return sensor->name;
332 }

References NULL, and SDL_PrivateSensorValid().

◆ SDL_SensorGetNonPortableType()

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 351 of file SDL_sensor.c.

352 {
353  if (!SDL_PrivateSensorValid(sensor)) {
354  return -1;
355  }
356 
357  return sensor->non_portable_type;
358 }

References SDL_PrivateSensorValid().

◆ SDL_SensorGetType()

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 338 of file SDL_sensor.c.

339 {
340  if (!SDL_PrivateSensorValid(sensor)) {
341  return SDL_SENSOR_INVALID;
342  }
343 
344  return sensor->type;
345 }

References SDL_PrivateSensorValid(), and SDL_SENSOR_INVALID.

◆ SDL_SensorInit()

int SDL_SensorInit ( void  )

Definition at line 71 of file SDL_sensor.c.

72 {
73  int i, status;
74 
75  /* Create the sensor list lock */
76  if (!SDL_sensor_lock) {
78  }
79 
80 #if !SDL_EVENTS_DISABLED
82  return -1;
83  }
84 #endif /* !SDL_EVENTS_DISABLED */
85 
86  status = -1;
87  for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
88  if (SDL_sensor_drivers[i]->Init() >= 0) {
89  status = 0;
90  }
91  }
92  return status;
93 }
#define SDL_INIT_EVENTS
Definition: SDL.h:86
#define SDL_InitSubSystem
#define SDL_CreateMutex

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

Referenced by SDL_InitSubSystem().

◆ SDL_SensorOpen()

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 217 of file SDL_sensor.c.

218 {
219  SDL_SensorDriver *driver;
220  SDL_SensorID instance_id;
221  SDL_Sensor *sensor;
222  SDL_Sensor *sensorlist;
223  const char *sensorname = NULL;
224 
225  SDL_LockSensors();
226 
227  if (!SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
229  return NULL;
230  }
231 
232  sensorlist = SDL_sensors;
233  /* If the sensor is already open, return it
234  * it is important that we have a single sensor * for each instance id
235  */
236  instance_id = driver->GetDeviceInstanceID(device_index);
237  while (sensorlist) {
238  if (instance_id == sensorlist->instance_id) {
239  sensor = sensorlist;
240  ++sensor->ref_count;
242  return sensor;
243  }
244  sensorlist = sensorlist->next;
245  }
246 
247  /* Create and initialize the sensor */
248  sensor = (SDL_Sensor *) SDL_calloc(sizeof(*sensor), 1);
249  if (sensor == NULL) {
250  SDL_OutOfMemory();
252  return NULL;
253  }
254  sensor->driver = driver;
255  sensor->instance_id = instance_id;
256  sensor->type = driver->GetDeviceType(device_index);
257  sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
258 
259  if (driver->Open(sensor, device_index) < 0) {
260  SDL_free(sensor);
262  return NULL;
263  }
264 
265  sensorname = driver->GetDeviceName(device_index);
266  if (sensorname) {
267  sensor->name = SDL_strdup(sensorname);
268  } else {
269  sensor->name = NULL;
270  }
271 
272  /* Add sensor to list */
273  ++sensor->ref_count;
274  /* Link the sensor in the list */
275  sensor->next = SDL_sensors;
276  SDL_sensors = sensor;
277 
279 
280  driver->Update(sensor);
281 
282  return sensor;
283 }
#define SDL_strdup
#define SDL_calloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
void(* Update)(SDL_Sensor *sensor)
Definition: SDL_syssensor.h:88
int(* Open)(SDL_Sensor *sensor, int device_index)
Definition: SDL_syssensor.h:81

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_SensorQuit()

void SDL_SensorQuit ( void  )

Definition at line 442 of file SDL_sensor.c.

443 {
444  int i;
445 
446  /* Make sure we're not getting called in the middle of updating sensors */
448 
449  SDL_LockSensors();
450 
451  /* Stop the event polling */
452  while (SDL_sensors) {
453  SDL_sensors->ref_count = 1;
455  }
456 
457  /* Quit the sensor setup */
458  for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
460  }
461 
463 
464 #if !SDL_EVENTS_DISABLED
466 #endif
467 
468  if (SDL_sensor_lock) {
471  }
472 }
#define SDL_assert(condition)
Definition: SDL_assert.h:171
#define SDL_DestroyMutex
#define SDL_QuitSubSystem
void SDL_SensorClose(SDL_Sensor *sensor)
Definition: SDL_sensor.c:392
void(* Quit)(void)
Definition: SDL_syssensor.h:94

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

Referenced by SDL_QuitSubSystem().

◆ SDL_SensorUpdate()

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 505 of file SDL_sensor.c.

506 {
507  int i;
508  SDL_Sensor *sensor, *next;
509 
511  return;
512  }
513 
514  SDL_LockSensors();
515 
516  if (SDL_updating_sensor) {
517  /* The sensors are already being updated */
519  return;
520  }
521 
523 
524  /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
526 
527  for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
528  sensor->driver->Update(sensor);
529  }
530 
531  SDL_LockSensors();
532 
534 
535  /* If any sensors were closed while updating, free them here */
536  for (sensor = SDL_sensors; sensor; sensor = next) {
537  next = sensor->next;
538  if (sensor->ref_count <= 0) {
539  SDL_SensorClose(sensor);
540  }
541  }
542 
543  /* this needs to happen AFTER walking the sensor list above, so that any
544  dangling hardware data from removed devices can be free'd
545  */
546  for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
548  }
549 
551 }
#define SDL_INIT_SENSOR
Definition: SDL.h:87
#define SDL_WasInit
void(* Detect)(void)
Definition: SDL_syssensor.h:63

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

◆ SDL_UnlockSensors()

Variable Documentation

◆ SDL_next_sensor_instance_id

SDL_atomic_t SDL_next_sensor_instance_id
static

Definition at line 51 of file SDL_sensor.c.

Referenced by SDL_GetNextSensorInstanceID().

◆ SDL_sensor_drivers

SDL_SensorDriver* SDL_sensor_drivers[]
static
Initial value:
= {
}
SDL_SensorDriver SDL_DUMMY_SensorDriver

Definition at line 34 of file SDL_sensor.c.

Referenced by SDL_GetDriverAndSensorIndex(), SDL_NumSensors(), SDL_SensorInit(), SDL_SensorQuit(), and SDL_SensorUpdate().

◆ SDL_sensor_lock

SDL_mutex* SDL_sensor_lock = NULL
static

Definition at line 50 of file SDL_sensor.c.

Referenced by SDL_LockSensors(), SDL_SensorInit(), SDL_SensorQuit(), and SDL_UnlockSensors().

◆ SDL_sensors

SDL_Sensor* SDL_sensors = NULL
static

◆ SDL_updating_sensor

SDL_bool SDL_updating_sensor = SDL_FALSE
static

Definition at line 49 of file SDL_sensor.c.

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