1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Lets SDL use various sensors (such as the accelerometer in a phone).
//!
//! [`SDL_Init`] must have been called with the [`SDL_INIT_SENSOR`] flag. This
//! causes SDL to scan the system for sensors, and load appropriate drivers.

use crate::{c_char, c_float, c_int, c_void, stdinc::*};

/// SDL's opaque sensor type.
#[repr(transparent)]
pub struct SDL_Sensor(c_void);

/// This is a unique ID for a sensor for the time it is connected to the system.
///
/// It is never reused for the lifetime of the application.
///
/// The ID value starts at 0 and increments from there. The value -1 is an
/// invalid ID.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct SDL_SensorID(pub Sint32);

/// The different sensors defined by SDL
///
/// Additional sensors may be available, using platform-dependent semantics.
///
/// Hare are the additional Android sensors:
/// https://developer.android.com/reference/android/hardware/SensorEvent.html#values
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct SDL_SensorType(pub i32);

/// Returned for an invalid sensor
pub const SDL_SENSOR_INVALID: SDL_SensorType = SDL_SensorType(-1);
/// Unknown sensor type
pub const SDL_SENSOR_UNKNOWN: SDL_SensorType = SDL_SensorType(0);
/// Accelerometer
pub const SDL_SENSOR_ACCEL: SDL_SensorType = SDL_SensorType(1);
/// Gyroscope
pub const SDL_SENSOR_GYRO: SDL_SensorType = SDL_SensorType(2);

/// Accelerometer sensor
///
/// The accelerometer returns the current acceleration in SI meters per
/// second squared. This includes gravity, so a device at rest will have
/// an acceleration of `SDL_STANDARD_GRAVITY` straight down.
///
/// * values[0]: Acceleration on the x axis
/// * values[1]: Acceleration on the y axis
/// * values[2]: Acceleration on 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`]
pub const SDL_STANDARD_GRAVITY: c_float = 9.80665;

extern "C" {
  /// Count the number of sensors attached to the system right now.
  pub fn SDL_NumSensors() -> c_int;

  /// 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.
  pub fn SDL_SensorGetDeviceName(device_index: c_int) -> *const c_char;

  /// 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.
  pub fn SDL_SensorGetDeviceType(device_index: c_int) -> SDL_SensorType;

  /// 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.
  pub fn SDL_SensorGetDeviceNonPortableType(device_index: c_int) -> c_int;

  /// 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.
  pub fn SDL_SensorGetDeviceInstanceID(device_index: c_int) -> SDL_SensorID;

  /// 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.
  pub fn SDL_SensorOpen(device_index: c_int) -> *mut SDL_Sensor;

  /// Return the SDL_Sensor associated with an instance id.
  pub fn SDL_SensorFromInstanceID(instance_id: SDL_SensorID)
    -> *mut SDL_Sensor;

  /// Get the implementation dependent name of a sensor.
  ///
  /// **Returns:** The sensor name, or NULL if the sensor is NULL.
  pub fn SDL_SensorGetName(sensor: *mut SDL_Sensor) -> *const c_char;

  /// 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.
  pub fn SDL_SensorGetType(sensor: *mut SDL_Sensor) -> SDL_SensorType;

  /// 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.
  pub fn SDL_SensorGetNonPortableType(sensor: *mut SDL_Sensor) -> c_int;

  /// 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.
  pub fn SDL_SensorGetInstanceID(sensor: *mut SDL_Sensor) -> SDL_SensorID;

  /// Get the current state of an opened sensor.
  ///
  /// The number of values and interpretation of the data is sensor dependent.
  ///
  /// * `sensor` The sensor to query
  /// * `data` A pointer filled with the current sensor state
  /// * `num_values` The number of values to write to data
  ///
  /// **Returns:** 0 or -1 if an error occurred.
  pub fn SDL_SensorGetData(
    sensor: *mut SDL_Sensor, data: *mut c_float, num_values: c_int,
  ) -> c_int;

  /// Close a sensor previously opened with [`SDL_SensorOpen`]
  pub fn SDL_SensorClose(sensor: *mut SDL_Sensor);

  /// 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.
  pub fn SDL_SensorUpdate();
}