SDL_sensor.c 13 KB

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