SDL_sensor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "../events/SDL_events_c.h"
  22. #include "../joystick/SDL_gamepad_c.h"
  23. static SDL_SensorDriver *SDL_sensor_drivers[] = {
  24. #ifdef SDL_SENSOR_ANDROID
  25. &SDL_ANDROID_SensorDriver,
  26. #endif
  27. #ifdef SDL_SENSOR_COREMOTION
  28. &SDL_COREMOTION_SensorDriver,
  29. #endif
  30. #ifdef SDL_SENSOR_WINDOWS
  31. &SDL_WINDOWS_SensorDriver,
  32. #endif
  33. #ifdef SDL_SENSOR_VITA
  34. &SDL_VITA_SensorDriver,
  35. #endif
  36. #ifdef SDL_SENSOR_N3DS
  37. &SDL_N3DS_SensorDriver,
  38. #endif
  39. #if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
  40. &SDL_DUMMY_SensorDriver
  41. #endif
  42. };
  43. #ifndef SDL_THREAD_SAFETY_ANALYSIS
  44. static
  45. #endif
  46. SDL_Mutex *SDL_sensor_lock = NULL; /* This needs to support recursive locks */
  47. static SDL_AtomicInt SDL_sensor_lock_pending;
  48. static int SDL_sensors_locked;
  49. static SDL_bool SDL_sensors_initialized;
  50. static SDL_Sensor *SDL_sensors SDL_GUARDED_BY(SDL_sensor_lock) = NULL;
  51. #define CHECK_SENSOR_MAGIC(sensor, retval) \
  52. if (!SDL_ObjectValid(sensor, SDL_OBJECT_TYPE_SENSOR)) { \
  53. SDL_InvalidParamError("sensor"); \
  54. SDL_UnlockSensors(); \
  55. return retval; \
  56. }
  57. SDL_bool SDL_SensorsInitialized(void)
  58. {
  59. return SDL_sensors_initialized;
  60. }
  61. void SDL_LockSensors(void)
  62. {
  63. (void)SDL_AtomicIncRef(&SDL_sensor_lock_pending);
  64. SDL_LockMutex(SDL_sensor_lock);
  65. (void)SDL_AtomicDecRef(&SDL_sensor_lock_pending);
  66. ++SDL_sensors_locked;
  67. }
  68. void SDL_UnlockSensors(void)
  69. {
  70. SDL_bool last_unlock = SDL_FALSE;
  71. --SDL_sensors_locked;
  72. if (!SDL_sensors_initialized) {
  73. /* NOTE: There's a small window here where another thread could lock the mutex after we've checked for pending locks */
  74. if (!SDL_sensors_locked && SDL_AtomicGet(&SDL_sensor_lock_pending) == 0) {
  75. last_unlock = SDL_TRUE;
  76. }
  77. }
  78. /* The last unlock after sensors are uninitialized will cleanup the mutex,
  79. * allowing applications to lock sensors while reinitializing the system.
  80. */
  81. if (last_unlock) {
  82. SDL_Mutex *sensor_lock = SDL_sensor_lock;
  83. SDL_LockMutex(sensor_lock);
  84. {
  85. SDL_UnlockMutex(SDL_sensor_lock);
  86. SDL_sensor_lock = NULL;
  87. }
  88. SDL_UnlockMutex(sensor_lock);
  89. SDL_DestroyMutex(sensor_lock);
  90. } else {
  91. SDL_UnlockMutex(SDL_sensor_lock);
  92. }
  93. }
  94. SDL_bool SDL_SensorsLocked(void)
  95. {
  96. return (SDL_sensors_locked > 0);
  97. }
  98. void SDL_AssertSensorsLocked(void)
  99. {
  100. SDL_assert(SDL_SensorsLocked());
  101. }
  102. int SDL_InitSensors(void)
  103. {
  104. int i, status;
  105. /* Create the sensor list lock */
  106. if (SDL_sensor_lock == NULL) {
  107. SDL_sensor_lock = SDL_CreateMutex();
  108. }
  109. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  110. return -1;
  111. }
  112. SDL_LockSensors();
  113. SDL_sensors_initialized = SDL_TRUE;
  114. status = -1;
  115. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  116. if (SDL_sensor_drivers[i]->Init() >= 0) {
  117. status = 0;
  118. }
  119. }
  120. SDL_UnlockSensors();
  121. if (status < 0) {
  122. SDL_QuitSensors();
  123. }
  124. return status;
  125. }
  126. SDL_bool SDL_SensorsOpened(void)
  127. {
  128. SDL_bool opened;
  129. SDL_LockSensors();
  130. {
  131. if (SDL_sensors != NULL) {
  132. opened = SDL_TRUE;
  133. } else {
  134. opened = SDL_FALSE;
  135. }
  136. }
  137. SDL_UnlockSensors();
  138. return opened;
  139. }
  140. SDL_SensorID *SDL_GetSensors(int *count)
  141. {
  142. int i, num_sensors, device_index;
  143. int sensor_index = 0, total_sensors = 0;
  144. SDL_SensorID *sensors;
  145. SDL_LockSensors();
  146. {
  147. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  148. total_sensors += SDL_sensor_drivers[i]->GetCount();
  149. }
  150. sensors = (SDL_SensorID *)SDL_malloc((total_sensors + 1) * sizeof(*sensors));
  151. if (sensors) {
  152. if (count) {
  153. *count = total_sensors;
  154. }
  155. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  156. num_sensors = SDL_sensor_drivers[i]->GetCount();
  157. for (device_index = 0; device_index < num_sensors; ++device_index) {
  158. SDL_assert(sensor_index < total_sensors);
  159. sensors[sensor_index] = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  160. SDL_assert(sensors[sensor_index] > 0);
  161. ++sensor_index;
  162. }
  163. }
  164. SDL_assert(sensor_index == total_sensors);
  165. sensors[sensor_index] = 0;
  166. } else {
  167. if (count) {
  168. *count = 0;
  169. }
  170. }
  171. }
  172. SDL_UnlockSensors();
  173. return sensors;
  174. }
  175. /*
  176. * Get the driver and device index for a sensor instance ID
  177. * This should be called while the sensor lock is held, to prevent another thread from updating the list
  178. */
  179. static SDL_bool SDL_GetDriverAndSensorIndex(SDL_SensorID instance_id, SDL_SensorDriver **driver, int *driver_index)
  180. {
  181. int i, num_sensors, device_index;
  182. if (instance_id > 0) {
  183. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  184. num_sensors = SDL_sensor_drivers[i]->GetCount();
  185. for (device_index = 0; device_index < num_sensors; ++device_index) {
  186. SDL_SensorID sensor_id = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  187. if (sensor_id == instance_id) {
  188. *driver = SDL_sensor_drivers[i];
  189. *driver_index = device_index;
  190. return SDL_TRUE;
  191. }
  192. }
  193. }
  194. }
  195. SDL_SetError("Sensor %" SDL_PRIu32 " not found", instance_id);
  196. return SDL_FALSE;
  197. }
  198. /*
  199. * Get the implementation dependent name of a sensor
  200. */
  201. const char *SDL_GetSensorNameFromID(SDL_SensorID instance_id)
  202. {
  203. SDL_SensorDriver *driver;
  204. int device_index;
  205. const char *name = NULL;
  206. SDL_LockSensors();
  207. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  208. name = driver->GetDeviceName(device_index);
  209. }
  210. SDL_UnlockSensors();
  211. return name ? SDL_FreeLater(SDL_strdup(name)) : NULL;
  212. }
  213. SDL_SensorType SDL_GetSensorTypeFromID(SDL_SensorID instance_id)
  214. {
  215. SDL_SensorDriver *driver;
  216. int device_index;
  217. SDL_SensorType type = SDL_SENSOR_INVALID;
  218. SDL_LockSensors();
  219. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  220. type = driver->GetDeviceType(device_index);
  221. }
  222. SDL_UnlockSensors();
  223. return type;
  224. }
  225. int SDL_GetSensorNonPortableTypeFromID(SDL_SensorID instance_id)
  226. {
  227. SDL_SensorDriver *driver;
  228. int device_index;
  229. int type = -1;
  230. SDL_LockSensors();
  231. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  232. type = driver->GetDeviceNonPortableType(device_index);
  233. }
  234. SDL_UnlockSensors();
  235. return type;
  236. }
  237. /*
  238. * Open a sensor for use - the index passed as an argument refers to
  239. * the N'th sensor on the system. This index is the value which will
  240. * identify this sensor in future sensor events.
  241. *
  242. * This function returns a sensor identifier, or NULL if an error occurred.
  243. */
  244. SDL_Sensor *SDL_OpenSensor(SDL_SensorID instance_id)
  245. {
  246. SDL_SensorDriver *driver;
  247. int device_index;
  248. SDL_Sensor *sensor;
  249. SDL_Sensor *sensorlist;
  250. const char *sensorname = NULL;
  251. SDL_LockSensors();
  252. if (!SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  253. SDL_UnlockSensors();
  254. return NULL;
  255. }
  256. sensorlist = SDL_sensors;
  257. /* If the sensor is already open, return it
  258. * it is important that we have a single sensor * for each instance id
  259. */
  260. while (sensorlist) {
  261. if (instance_id == sensorlist->instance_id) {
  262. sensor = sensorlist;
  263. ++sensor->ref_count;
  264. SDL_UnlockSensors();
  265. return sensor;
  266. }
  267. sensorlist = sensorlist->next;
  268. }
  269. /* Create and initialize the sensor */
  270. sensor = (SDL_Sensor *)SDL_calloc(sizeof(*sensor), 1);
  271. if (!sensor) {
  272. SDL_UnlockSensors();
  273. return NULL;
  274. }
  275. SDL_SetObjectValid(sensor, SDL_OBJECT_TYPE_SENSOR, SDL_TRUE);
  276. sensor->driver = driver;
  277. sensor->instance_id = instance_id;
  278. sensor->type = driver->GetDeviceType(device_index);
  279. sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
  280. if (driver->Open(sensor, device_index) < 0) {
  281. SDL_SetObjectValid(sensor, SDL_OBJECT_TYPE_SENSOR, SDL_FALSE);
  282. SDL_free(sensor);
  283. SDL_UnlockSensors();
  284. return NULL;
  285. }
  286. sensorname = driver->GetDeviceName(device_index);
  287. if (sensorname) {
  288. sensor->name = SDL_strdup(sensorname);
  289. } else {
  290. sensor->name = NULL;
  291. }
  292. /* Add sensor to list */
  293. ++sensor->ref_count;
  294. /* Link the sensor in the list */
  295. sensor->next = SDL_sensors;
  296. SDL_sensors = sensor;
  297. driver->Update(sensor);
  298. SDL_UnlockSensors();
  299. return sensor;
  300. }
  301. /*
  302. * Find the SDL_Sensor that owns this instance id
  303. */
  304. SDL_Sensor *SDL_GetSensorFromID(SDL_SensorID instance_id)
  305. {
  306. SDL_Sensor *sensor;
  307. SDL_LockSensors();
  308. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  309. if (sensor->instance_id == instance_id) {
  310. break;
  311. }
  312. }
  313. SDL_UnlockSensors();
  314. return sensor;
  315. }
  316. /*
  317. * Get the properties associated with a sensor.
  318. */
  319. SDL_PropertiesID SDL_GetSensorProperties(SDL_Sensor *sensor)
  320. {
  321. SDL_PropertiesID retval;
  322. SDL_LockSensors();
  323. {
  324. CHECK_SENSOR_MAGIC(sensor, 0);
  325. if (sensor->props == 0) {
  326. sensor->props = SDL_CreateProperties();
  327. }
  328. retval = sensor->props;
  329. }
  330. SDL_UnlockSensors();
  331. return retval;
  332. }
  333. /*
  334. * Get the friendly name of this sensor
  335. */
  336. const char *SDL_GetSensorName(SDL_Sensor *sensor)
  337. {
  338. const char *retval;
  339. SDL_LockSensors();
  340. {
  341. CHECK_SENSOR_MAGIC(sensor, NULL);
  342. retval = sensor->name;
  343. }
  344. SDL_UnlockSensors();
  345. return retval;
  346. }
  347. /*
  348. * Get the type of this sensor
  349. */
  350. SDL_SensorType SDL_GetSensorType(SDL_Sensor *sensor)
  351. {
  352. SDL_SensorType retval;
  353. SDL_LockSensors();
  354. {
  355. CHECK_SENSOR_MAGIC(sensor, SDL_SENSOR_INVALID);
  356. retval = sensor->type;
  357. }
  358. SDL_UnlockSensors();
  359. return retval;
  360. }
  361. /*
  362. * Get the platform dependent type of this sensor
  363. */
  364. int SDL_GetSensorNonPortableType(SDL_Sensor *sensor)
  365. {
  366. int retval;
  367. SDL_LockSensors();
  368. {
  369. CHECK_SENSOR_MAGIC(sensor, -1);
  370. retval = sensor->non_portable_type;
  371. }
  372. SDL_UnlockSensors();
  373. return retval;
  374. }
  375. /*
  376. * Get the instance id for this opened sensor
  377. */
  378. SDL_SensorID SDL_GetSensorID(SDL_Sensor *sensor)
  379. {
  380. SDL_SensorID retval;
  381. SDL_LockSensors();
  382. {
  383. CHECK_SENSOR_MAGIC(sensor, 0);
  384. retval = sensor->instance_id;
  385. }
  386. SDL_UnlockSensors();
  387. return retval;
  388. }
  389. /*
  390. * Get the current state of this sensor
  391. */
  392. int SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values)
  393. {
  394. SDL_LockSensors();
  395. {
  396. CHECK_SENSOR_MAGIC(sensor, -1);
  397. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  398. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  399. }
  400. SDL_UnlockSensors();
  401. return 0;
  402. }
  403. /*
  404. * Close a sensor previously opened with SDL_OpenSensor()
  405. */
  406. void SDL_CloseSensor(SDL_Sensor *sensor)
  407. {
  408. SDL_Sensor *sensorlist;
  409. SDL_Sensor *sensorlistprev;
  410. SDL_LockSensors();
  411. {
  412. CHECK_SENSOR_MAGIC(sensor,);
  413. /* First decrement ref count */
  414. if (--sensor->ref_count > 0) {
  415. SDL_UnlockSensors();
  416. return;
  417. }
  418. SDL_DestroyProperties(sensor->props);
  419. sensor->driver->Close(sensor);
  420. sensor->hwdata = NULL;
  421. SDL_SetObjectValid(sensor, SDL_OBJECT_TYPE_SENSOR, SDL_FALSE);
  422. sensorlist = SDL_sensors;
  423. sensorlistprev = NULL;
  424. while (sensorlist) {
  425. if (sensor == sensorlist) {
  426. if (sensorlistprev) {
  427. /* unlink this entry */
  428. sensorlistprev->next = sensorlist->next;
  429. } else {
  430. SDL_sensors = sensor->next;
  431. }
  432. break;
  433. }
  434. sensorlistprev = sensorlist;
  435. sensorlist = sensorlist->next;
  436. }
  437. /* Free the data associated with this sensor */
  438. SDL_FreeLater(sensor->name); // this pointer gets handed to the app by SDL_GetSensorName().
  439. SDL_free(sensor);
  440. }
  441. SDL_UnlockSensors();
  442. }
  443. void SDL_QuitSensors(void)
  444. {
  445. int i;
  446. SDL_LockSensors();
  447. /* Stop the event polling */
  448. while (SDL_sensors) {
  449. SDL_sensors->ref_count = 1;
  450. SDL_CloseSensor(SDL_sensors);
  451. }
  452. /* Quit the sensor setup */
  453. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  454. SDL_sensor_drivers[i]->Quit();
  455. }
  456. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  457. SDL_sensors_initialized = SDL_FALSE;
  458. SDL_UnlockSensors();
  459. }
  460. /* These are global for SDL_syssensor.c and SDL_events.c */
  461. int SDL_SendSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values)
  462. {
  463. int posted;
  464. SDL_AssertSensorsLocked();
  465. /* Allow duplicate events, for things like steps and heartbeats */
  466. /* Update internal sensor state */
  467. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  468. SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
  469. /* Post the event, if desired */
  470. posted = 0;
  471. if (SDL_EventEnabled(SDL_EVENT_SENSOR_UPDATE)) {
  472. SDL_Event event;
  473. event.type = SDL_EVENT_SENSOR_UPDATE;
  474. event.common.timestamp = timestamp;
  475. event.sensor.which = sensor->instance_id;
  476. num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
  477. SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
  478. SDL_memcpy(event.sensor.data, data, num_values * sizeof(*data));
  479. event.sensor.sensor_timestamp = sensor_timestamp;
  480. posted = SDL_PushEvent(&event) == 1;
  481. }
  482. SDL_GamepadSensorWatcher(timestamp, sensor->instance_id, sensor_timestamp, data, num_values);
  483. return posted;
  484. }
  485. void SDL_UpdateSensor(SDL_Sensor *sensor)
  486. {
  487. SDL_LockSensors();
  488. {
  489. CHECK_SENSOR_MAGIC(sensor,);
  490. sensor->driver->Update(sensor);
  491. }
  492. SDL_UnlockSensors();
  493. }
  494. void SDL_UpdateSensors(void)
  495. {
  496. int i;
  497. SDL_Sensor *sensor;
  498. if (!SDL_WasInit(SDL_INIT_SENSOR)) {
  499. return;
  500. }
  501. SDL_LockSensors();
  502. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  503. sensor->driver->Update(sensor);
  504. }
  505. /* this needs to happen AFTER walking the sensor list above, so that any
  506. dangling hardware data from removed devices can be free'd
  507. */
  508. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  509. SDL_sensor_drivers[i]->Detect();
  510. }
  511. SDL_UnlockSensors();
  512. }