SDL_sensor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. /* This is the sensor API for Simple DirectMedia Layer */
  20. #include "SDL_syssensor.h"
  21. #if !SDL_EVENTS_DISABLED
  22. #include "../events/SDL_events_c.h"
  23. #endif
  24. static SDL_SensorDriver *SDL_sensor_drivers[] = {
  25. #ifdef SDL_SENSOR_ANDROID
  26. &SDL_ANDROID_SensorDriver,
  27. #endif
  28. #ifdef SDL_SENSOR_COREMOTION
  29. &SDL_COREMOTION_SensorDriver,
  30. #endif
  31. #ifdef SDL_SENSOR_WINDOWS
  32. &SDL_WINDOWS_SensorDriver,
  33. #endif
  34. #ifdef SDL_SENSOR_VITA
  35. &SDL_VITA_SensorDriver,
  36. #endif
  37. #ifdef SDL_SENSOR_N3DS
  38. &SDL_N3DS_SensorDriver,
  39. #endif
  40. #if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
  41. &SDL_DUMMY_SensorDriver
  42. #endif
  43. };
  44. static SDL_mutex *SDL_sensor_lock = NULL; /* This needs to support recursive locks */
  45. static SDL_Sensor *SDL_sensors SDL_GUARDED_BY(SDL_sensor_lock) = NULL;
  46. static SDL_atomic_t SDL_next_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
  47. static SDL_bool SDL_updating_sensor SDL_GUARDED_BY(SDL_sensor_lock) = SDL_FALSE;
  48. void SDL_LockSensors(void) SDL_ACQUIRE(SDL_sensor_lock)
  49. {
  50. SDL_LockMutex(SDL_sensor_lock);
  51. }
  52. void SDL_UnlockSensors(void) SDL_RELEASE(SDL_sensor_lock)
  53. {
  54. SDL_UnlockMutex(SDL_sensor_lock);
  55. }
  56. int SDL_SensorInit(void)
  57. {
  58. int i, status;
  59. /* Create the sensor list lock */
  60. if (SDL_sensor_lock == NULL) {
  61. SDL_sensor_lock = SDL_CreateMutex();
  62. }
  63. #if !SDL_EVENTS_DISABLED
  64. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  65. return -1;
  66. }
  67. #endif /* !SDL_EVENTS_DISABLED */
  68. status = -1;
  69. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  70. if (SDL_sensor_drivers[i]->Init() >= 0) {
  71. status = 0;
  72. }
  73. }
  74. return status;
  75. }
  76. /*
  77. * Count the number of sensors attached to the system
  78. */
  79. int SDL_GetNumSensors(void)
  80. {
  81. int i, total_sensors = 0;
  82. SDL_LockSensors();
  83. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  84. total_sensors += SDL_sensor_drivers[i]->GetCount();
  85. }
  86. SDL_UnlockSensors();
  87. return total_sensors;
  88. }
  89. /*
  90. * Return the next available sensor instance ID
  91. * This may be called by drivers from multiple threads, unprotected by any locks
  92. */
  93. SDL_SensorID SDL_GetNextSensorInstanceID()
  94. {
  95. return SDL_AtomicIncRef(&SDL_next_sensor_instance_id);
  96. }
  97. /*
  98. * Get the driver and device index for an API device index
  99. * This should be called while the sensor lock is held, to prevent another thread from updating the list
  100. */
  101. static SDL_bool SDL_GetDriverAndSensorIndex(int device_index, SDL_SensorDriver **driver, int *driver_index)
  102. {
  103. int i, num_sensors, total_sensors = 0;
  104. if (device_index >= 0) {
  105. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  106. num_sensors = SDL_sensor_drivers[i]->GetCount();
  107. if (device_index < num_sensors) {
  108. *driver = SDL_sensor_drivers[i];
  109. *driver_index = device_index;
  110. return SDL_TRUE;
  111. }
  112. device_index -= num_sensors;
  113. total_sensors += num_sensors;
  114. }
  115. }
  116. SDL_SetError("There are %d sensors available", total_sensors);
  117. return SDL_FALSE;
  118. }
  119. /*
  120. * Get the implementation dependent name of a sensor
  121. */
  122. const char *SDL_GetSensorDeviceName(int device_index)
  123. {
  124. SDL_SensorDriver *driver;
  125. const char *name = NULL;
  126. SDL_LockSensors();
  127. if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
  128. name = driver->GetDeviceName(device_index);
  129. }
  130. SDL_UnlockSensors();
  131. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  132. return name;
  133. }
  134. SDL_SensorType SDL_GetSensorDeviceType(int device_index)
  135. {
  136. SDL_SensorDriver *driver;
  137. SDL_SensorType type = SDL_SENSOR_INVALID;
  138. SDL_LockSensors();
  139. if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
  140. type = driver->GetDeviceType(device_index);
  141. }
  142. SDL_UnlockSensors();
  143. return type;
  144. }
  145. int SDL_GetSensorDeviceNonPortableType(int device_index)
  146. {
  147. SDL_SensorDriver *driver;
  148. int type = -1;
  149. SDL_LockSensors();
  150. if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
  151. type = driver->GetDeviceNonPortableType(device_index);
  152. }
  153. SDL_UnlockSensors();
  154. return type;
  155. }
  156. SDL_SensorID SDL_GetSensorDeviceInstanceID(int device_index)
  157. {
  158. SDL_SensorDriver *driver;
  159. SDL_SensorID instance_id = -1;
  160. SDL_LockSensors();
  161. if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
  162. instance_id = driver->GetDeviceInstanceID(device_index);
  163. }
  164. SDL_UnlockSensors();
  165. return instance_id;
  166. }
  167. /*
  168. * Open a sensor for use - the index passed as an argument refers to
  169. * the N'th sensor on the system. This index is the value which will
  170. * identify this sensor in future sensor events.
  171. *
  172. * This function returns a sensor identifier, or NULL if an error occurred.
  173. */
  174. SDL_Sensor *SDL_OpenSensor(int device_index)
  175. {
  176. SDL_SensorDriver *driver;
  177. SDL_SensorID instance_id;
  178. SDL_Sensor *sensor;
  179. SDL_Sensor *sensorlist;
  180. const char *sensorname = NULL;
  181. SDL_LockSensors();
  182. if (!SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
  183. SDL_UnlockSensors();
  184. return NULL;
  185. }
  186. sensorlist = SDL_sensors;
  187. /* If the sensor is already open, return it
  188. * it is important that we have a single sensor * for each instance id
  189. */
  190. instance_id = driver->GetDeviceInstanceID(device_index);
  191. while (sensorlist) {
  192. if (instance_id == sensorlist->instance_id) {
  193. sensor = sensorlist;
  194. ++sensor->ref_count;
  195. SDL_UnlockSensors();
  196. return sensor;
  197. }
  198. sensorlist = sensorlist->next;
  199. }
  200. /* Create and initialize the sensor */
  201. sensor = (SDL_Sensor *)SDL_calloc(sizeof(*sensor), 1);
  202. if (sensor == NULL) {
  203. SDL_OutOfMemory();
  204. SDL_UnlockSensors();
  205. return NULL;
  206. }
  207. sensor->driver = driver;
  208. sensor->instance_id = instance_id;
  209. sensor->type = driver->GetDeviceType(device_index);
  210. sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
  211. if (driver->Open(sensor, device_index) < 0) {
  212. SDL_free(sensor);
  213. SDL_UnlockSensors();
  214. return NULL;
  215. }
  216. sensorname = driver->GetDeviceName(device_index);
  217. if (sensorname) {
  218. sensor->name = SDL_strdup(sensorname);
  219. } else {
  220. sensor->name = NULL;
  221. }
  222. /* Add sensor to list */
  223. ++sensor->ref_count;
  224. /* Link the sensor in the list */
  225. sensor->next = SDL_sensors;
  226. SDL_sensors = sensor;
  227. SDL_UnlockSensors();
  228. driver->Update(sensor);
  229. return sensor;
  230. }
  231. /*
  232. * Find the SDL_Sensor that owns this instance id
  233. */
  234. SDL_Sensor *SDL_GetSensorFromInstanceID(SDL_SensorID instance_id)
  235. {
  236. SDL_Sensor *sensor;
  237. SDL_LockSensors();
  238. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  239. if (sensor->instance_id == instance_id) {
  240. break;
  241. }
  242. }
  243. SDL_UnlockSensors();
  244. return sensor;
  245. }
  246. /*
  247. * Checks to make sure the sensor is valid.
  248. */
  249. static int SDL_PrivateSensorValid(SDL_Sensor *sensor)
  250. {
  251. int valid;
  252. if (sensor == NULL) {
  253. SDL_SetError("Sensor hasn't been opened yet");
  254. valid = 0;
  255. } else {
  256. valid = 1;
  257. }
  258. return valid;
  259. }
  260. /*
  261. * Get the friendly name of this sensor
  262. */
  263. const char *SDL_GetSensorName(SDL_Sensor *sensor)
  264. {
  265. if (!SDL_PrivateSensorValid(sensor)) {
  266. return NULL;
  267. }
  268. return sensor->name;
  269. }
  270. /*
  271. * Get the type of this sensor
  272. */
  273. SDL_SensorType SDL_GetSensorType(SDL_Sensor *sensor)
  274. {
  275. if (!SDL_PrivateSensorValid(sensor)) {
  276. return SDL_SENSOR_INVALID;
  277. }
  278. return sensor->type;
  279. }
  280. /*
  281. * Get the platform dependent type of this sensor
  282. */
  283. int SDL_GetSensorNonPortableType(SDL_Sensor *sensor)
  284. {
  285. if (!SDL_PrivateSensorValid(sensor)) {
  286. return -1;
  287. }
  288. return sensor->non_portable_type;
  289. }
  290. /*
  291. * Get the instance id for this opened sensor
  292. */
  293. SDL_SensorID SDL_GetSensorInstanceID(SDL_Sensor *sensor)
  294. {
  295. if (!SDL_PrivateSensorValid(sensor)) {
  296. return -1;
  297. }
  298. return sensor->instance_id;
  299. }
  300. /*
  301. * Get the current state of this sensor
  302. */
  303. int SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values)
  304. {
  305. if (!SDL_PrivateSensorValid(sensor)) {
  306. return -1;
  307. }
  308. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  309. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  310. return 0;
  311. }
  312. /*
  313. * Close a sensor previously opened with SDL_OpenSensor()
  314. */
  315. void SDL_CloseSensor(SDL_Sensor *sensor)
  316. {
  317. SDL_Sensor *sensorlist;
  318. SDL_Sensor *sensorlistprev;
  319. if (!SDL_PrivateSensorValid(sensor)) {
  320. return;
  321. }
  322. SDL_LockSensors();
  323. /* First decrement ref count */
  324. if (--sensor->ref_count > 0) {
  325. SDL_UnlockSensors();
  326. return;
  327. }
  328. if (SDL_updating_sensor) {
  329. SDL_UnlockSensors();
  330. return;
  331. }
  332. sensor->driver->Close(sensor);
  333. sensor->hwdata = NULL;
  334. sensorlist = SDL_sensors;
  335. sensorlistprev = NULL;
  336. while (sensorlist) {
  337. if (sensor == sensorlist) {
  338. if (sensorlistprev) {
  339. /* unlink this entry */
  340. sensorlistprev->next = sensorlist->next;
  341. } else {
  342. SDL_sensors = sensor->next;
  343. }
  344. break;
  345. }
  346. sensorlistprev = sensorlist;
  347. sensorlist = sensorlist->next;
  348. }
  349. SDL_free(sensor->name);
  350. /* Free the data associated with this sensor */
  351. SDL_free(sensor);
  352. SDL_UnlockSensors();
  353. }
  354. void SDL_SensorQuit(void)
  355. {
  356. int i;
  357. SDL_LockSensors();
  358. /* Make sure we're not getting called in the middle of updating sensors */
  359. SDL_assert(!SDL_updating_sensor);
  360. /* Stop the event polling */
  361. while (SDL_sensors) {
  362. SDL_sensors->ref_count = 1;
  363. SDL_CloseSensor(SDL_sensors);
  364. }
  365. /* Quit the sensor setup */
  366. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  367. SDL_sensor_drivers[i]->Quit();
  368. }
  369. SDL_UnlockSensors();
  370. #if !SDL_EVENTS_DISABLED
  371. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  372. #endif
  373. if (SDL_sensor_lock) {
  374. SDL_DestroyMutex(SDL_sensor_lock);
  375. SDL_sensor_lock = NULL;
  376. }
  377. }
  378. /* These are global for SDL_syssensor.c and SDL_events.c */
  379. int SDL_PrivateSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values)
  380. {
  381. int posted;
  382. /* Allow duplicate events, for things like steps and heartbeats */
  383. /* Update internal sensor state */
  384. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  385. SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
  386. /* Post the event, if desired */
  387. posted = 0;
  388. #if !SDL_EVENTS_DISABLED
  389. if (SDL_GetEventState(SDL_SENSORUPDATE) == SDL_ENABLE) {
  390. SDL_Event event;
  391. event.type = SDL_SENSORUPDATE;
  392. event.common.timestamp = timestamp;
  393. event.sensor.which = sensor->instance_id;
  394. num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
  395. SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
  396. SDL_memcpy(event.sensor.data, data, num_values * sizeof(*data));
  397. event.sensor.sensor_timestamp = sensor_timestamp;
  398. posted = SDL_PushEvent(&event) == 1;
  399. }
  400. #endif /* !SDL_EVENTS_DISABLED */
  401. return posted;
  402. }
  403. void SDL_UpdateSensors(void)
  404. {
  405. int i;
  406. SDL_Sensor *sensor, *next;
  407. if (!SDL_WasInit(SDL_INIT_SENSOR)) {
  408. return;
  409. }
  410. SDL_LockSensors();
  411. if (SDL_updating_sensor) {
  412. /* The sensors are already being updated */
  413. SDL_UnlockSensors();
  414. return;
  415. }
  416. SDL_updating_sensor = SDL_TRUE;
  417. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  418. sensor->driver->Update(sensor);
  419. }
  420. SDL_updating_sensor = SDL_FALSE;
  421. /* If any sensors were closed while updating, free them here */
  422. for (sensor = SDL_sensors; sensor; sensor = next) {
  423. next = sensor->next;
  424. if (sensor->ref_count <= 0) {
  425. SDL_CloseSensor(sensor);
  426. }
  427. }
  428. /* this needs to happen AFTER walking the sensor list above, so that any
  429. dangling hardware data from removed devices can be free'd
  430. */
  431. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  432. SDL_sensor_drivers[i]->Detect();
  433. }
  434. SDL_UnlockSensors();
  435. }