SDL_sensor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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_last_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
  47. void SDL_LockSensors(void) SDL_ACQUIRE(SDL_sensor_lock)
  48. {
  49. SDL_LockMutex(SDL_sensor_lock);
  50. }
  51. void SDL_UnlockSensors(void) SDL_RELEASE(SDL_sensor_lock)
  52. {
  53. SDL_UnlockMutex(SDL_sensor_lock);
  54. }
  55. int SDL_InitSensors(void)
  56. {
  57. int i, status;
  58. /* Create the sensor list lock */
  59. if (SDL_sensor_lock == NULL) {
  60. SDL_sensor_lock = SDL_CreateMutex();
  61. }
  62. #if !SDL_EVENTS_DISABLED
  63. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  64. return -1;
  65. }
  66. #endif /* !SDL_EVENTS_DISABLED */
  67. status = -1;
  68. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  69. if (SDL_sensor_drivers[i]->Init() >= 0) {
  70. status = 0;
  71. }
  72. }
  73. return status;
  74. }
  75. SDL_bool SDL_SensorsOpened(void)
  76. {
  77. SDL_bool opened;
  78. SDL_LockSensors();
  79. {
  80. if (SDL_sensors != NULL) {
  81. opened = SDL_TRUE;
  82. } else {
  83. opened = SDL_FALSE;
  84. }
  85. }
  86. SDL_UnlockSensors();
  87. return opened;
  88. }
  89. SDL_SensorID *SDL_GetSensors(int *count)
  90. {
  91. int i, num_sensors, device_index;
  92. int sensor_index = 0, total_sensors = 0;
  93. SDL_SensorID *sensors;
  94. SDL_LockSensors();
  95. {
  96. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  97. total_sensors += SDL_sensor_drivers[i]->GetCount();
  98. }
  99. if (count) {
  100. *count = total_sensors;
  101. }
  102. sensors = (SDL_SensorID *)SDL_malloc((total_sensors + 1) * sizeof(*sensors));
  103. if (sensors) {
  104. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  105. num_sensors = SDL_sensor_drivers[i]->GetCount();
  106. for (device_index = 0; device_index < num_sensors; ++device_index) {
  107. SDL_assert(sensor_index < total_sensors);
  108. sensors[sensor_index] = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  109. SDL_assert(sensors[sensor_index] > 0);
  110. ++sensor_index;
  111. }
  112. }
  113. SDL_assert(sensor_index == total_sensors);
  114. sensors[sensor_index] = 0;
  115. } else {
  116. SDL_OutOfMemory();
  117. }
  118. }
  119. SDL_UnlockSensors();
  120. return sensors;
  121. }
  122. /*
  123. * Return the next available sensor instance ID
  124. * This may be called by drivers from multiple threads, unprotected by any locks
  125. */
  126. SDL_SensorID SDL_GetNextSensorInstanceID()
  127. {
  128. return SDL_AtomicIncRef(&SDL_last_sensor_instance_id) + 1;
  129. }
  130. /*
  131. * Get the driver and device index for a sensor instance ID
  132. * This should be called while the sensor lock is held, to prevent another thread from updating the list
  133. */
  134. static SDL_bool SDL_GetDriverAndSensorIndex(SDL_SensorID instance_id, SDL_SensorDriver **driver, int *driver_index)
  135. {
  136. int i, num_sensors, device_index;
  137. if (instance_id > 0) {
  138. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  139. num_sensors = SDL_sensor_drivers[i]->GetCount();
  140. for (device_index = 0; device_index < num_sensors; ++device_index) {
  141. SDL_SensorID sensor_id = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
  142. if (sensor_id == instance_id) {
  143. *driver = SDL_sensor_drivers[i];
  144. *driver_index = device_index;
  145. return SDL_TRUE;
  146. }
  147. }
  148. }
  149. }
  150. SDL_SetError("Sensor %" SDL_PRIs32 " not found", instance_id);
  151. return SDL_FALSE;
  152. }
  153. /*
  154. * Get the implementation dependent name of a sensor
  155. */
  156. const char *SDL_GetSensorInstanceName(SDL_SensorID instance_id)
  157. {
  158. SDL_SensorDriver *driver;
  159. int device_index;
  160. const char *name = NULL;
  161. SDL_LockSensors();
  162. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  163. name = driver->GetDeviceName(device_index);
  164. }
  165. SDL_UnlockSensors();
  166. /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
  167. return name;
  168. }
  169. SDL_SensorType SDL_GetSensorInstanceType(SDL_SensorID instance_id)
  170. {
  171. SDL_SensorDriver *driver;
  172. int device_index;
  173. SDL_SensorType type = SDL_SENSOR_INVALID;
  174. SDL_LockSensors();
  175. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  176. type = driver->GetDeviceType(device_index);
  177. }
  178. SDL_UnlockSensors();
  179. return type;
  180. }
  181. int SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id)
  182. {
  183. SDL_SensorDriver *driver;
  184. int device_index;
  185. int type = -1;
  186. SDL_LockSensors();
  187. if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  188. type = driver->GetDeviceNonPortableType(device_index);
  189. }
  190. SDL_UnlockSensors();
  191. return type;
  192. }
  193. /*
  194. * Open a sensor for use - the index passed as an argument refers to
  195. * the N'th sensor on the system. This index is the value which will
  196. * identify this sensor in future sensor events.
  197. *
  198. * This function returns a sensor identifier, or NULL if an error occurred.
  199. */
  200. SDL_Sensor *SDL_OpenSensor(SDL_SensorID instance_id)
  201. {
  202. SDL_SensorDriver *driver;
  203. int device_index;
  204. SDL_Sensor *sensor;
  205. SDL_Sensor *sensorlist;
  206. const char *sensorname = NULL;
  207. SDL_LockSensors();
  208. if (!SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
  209. SDL_UnlockSensors();
  210. return NULL;
  211. }
  212. sensorlist = SDL_sensors;
  213. /* If the sensor is already open, return it
  214. * it is important that we have a single sensor * for each instance id
  215. */
  216. while (sensorlist) {
  217. if (instance_id == sensorlist->instance_id) {
  218. sensor = sensorlist;
  219. ++sensor->ref_count;
  220. SDL_UnlockSensors();
  221. return sensor;
  222. }
  223. sensorlist = sensorlist->next;
  224. }
  225. /* Create and initialize the sensor */
  226. sensor = (SDL_Sensor *)SDL_calloc(sizeof(*sensor), 1);
  227. if (sensor == NULL) {
  228. SDL_OutOfMemory();
  229. SDL_UnlockSensors();
  230. return NULL;
  231. }
  232. sensor->driver = driver;
  233. sensor->instance_id = instance_id;
  234. sensor->type = driver->GetDeviceType(device_index);
  235. sensor->non_portable_type = driver->GetDeviceNonPortableType(device_index);
  236. if (driver->Open(sensor, device_index) < 0) {
  237. SDL_free(sensor);
  238. SDL_UnlockSensors();
  239. return NULL;
  240. }
  241. sensorname = driver->GetDeviceName(device_index);
  242. if (sensorname) {
  243. sensor->name = SDL_strdup(sensorname);
  244. } else {
  245. sensor->name = NULL;
  246. }
  247. /* Add sensor to list */
  248. ++sensor->ref_count;
  249. /* Link the sensor in the list */
  250. sensor->next = SDL_sensors;
  251. SDL_sensors = sensor;
  252. driver->Update(sensor);
  253. SDL_UnlockSensors();
  254. return sensor;
  255. }
  256. /*
  257. * Find the SDL_Sensor that owns this instance id
  258. */
  259. SDL_Sensor *SDL_GetSensorFromInstanceID(SDL_SensorID instance_id)
  260. {
  261. SDL_Sensor *sensor;
  262. SDL_LockSensors();
  263. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  264. if (sensor->instance_id == instance_id) {
  265. break;
  266. }
  267. }
  268. SDL_UnlockSensors();
  269. return sensor;
  270. }
  271. /*
  272. * Checks to make sure the sensor is valid.
  273. */
  274. static int SDL_IsSensorValid(SDL_Sensor *sensor)
  275. {
  276. int valid;
  277. if (sensor == NULL) {
  278. SDL_SetError("Sensor hasn't been opened yet");
  279. valid = 0;
  280. } else {
  281. valid = 1;
  282. }
  283. return valid;
  284. }
  285. /*
  286. * Get the friendly name of this sensor
  287. */
  288. const char *SDL_GetSensorName(SDL_Sensor *sensor)
  289. {
  290. if (!SDL_IsSensorValid(sensor)) {
  291. return NULL;
  292. }
  293. return sensor->name;
  294. }
  295. /*
  296. * Get the type of this sensor
  297. */
  298. SDL_SensorType SDL_GetSensorType(SDL_Sensor *sensor)
  299. {
  300. if (!SDL_IsSensorValid(sensor)) {
  301. return SDL_SENSOR_INVALID;
  302. }
  303. return sensor->type;
  304. }
  305. /*
  306. * Get the platform dependent type of this sensor
  307. */
  308. int SDL_GetSensorNonPortableType(SDL_Sensor *sensor)
  309. {
  310. if (!SDL_IsSensorValid(sensor)) {
  311. return -1;
  312. }
  313. return sensor->non_portable_type;
  314. }
  315. /*
  316. * Get the instance id for this opened sensor
  317. */
  318. SDL_SensorID SDL_GetSensorInstanceID(SDL_Sensor *sensor)
  319. {
  320. if (!SDL_IsSensorValid(sensor)) {
  321. return -1;
  322. }
  323. return sensor->instance_id;
  324. }
  325. /*
  326. * Get the current state of this sensor
  327. */
  328. int SDL_GetSensorData(SDL_Sensor *sensor, float *data, int num_values)
  329. {
  330. if (!SDL_IsSensorValid(sensor)) {
  331. return -1;
  332. }
  333. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  334. SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
  335. return 0;
  336. }
  337. /*
  338. * Close a sensor previously opened with SDL_OpenSensor()
  339. */
  340. void SDL_CloseSensor(SDL_Sensor *sensor)
  341. {
  342. SDL_Sensor *sensorlist;
  343. SDL_Sensor *sensorlistprev;
  344. if (!SDL_IsSensorValid(sensor)) {
  345. return;
  346. }
  347. SDL_LockSensors();
  348. /* First decrement ref count */
  349. if (--sensor->ref_count > 0) {
  350. SDL_UnlockSensors();
  351. return;
  352. }
  353. sensor->driver->Close(sensor);
  354. sensor->hwdata = NULL;
  355. sensorlist = SDL_sensors;
  356. sensorlistprev = NULL;
  357. while (sensorlist) {
  358. if (sensor == sensorlist) {
  359. if (sensorlistprev) {
  360. /* unlink this entry */
  361. sensorlistprev->next = sensorlist->next;
  362. } else {
  363. SDL_sensors = sensor->next;
  364. }
  365. break;
  366. }
  367. sensorlistprev = sensorlist;
  368. sensorlist = sensorlist->next;
  369. }
  370. SDL_free(sensor->name);
  371. /* Free the data associated with this sensor */
  372. SDL_free(sensor);
  373. SDL_UnlockSensors();
  374. }
  375. void SDL_QuitSensors(void)
  376. {
  377. int i;
  378. SDL_LockSensors();
  379. /* Stop the event polling */
  380. while (SDL_sensors) {
  381. SDL_sensors->ref_count = 1;
  382. SDL_CloseSensor(SDL_sensors);
  383. }
  384. /* Quit the sensor setup */
  385. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  386. SDL_sensor_drivers[i]->Quit();
  387. }
  388. SDL_UnlockSensors();
  389. #if !SDL_EVENTS_DISABLED
  390. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  391. #endif
  392. if (SDL_sensor_lock) {
  393. SDL_DestroyMutex(SDL_sensor_lock);
  394. SDL_sensor_lock = NULL;
  395. }
  396. }
  397. /* These are global for SDL_syssensor.c and SDL_events.c */
  398. int SDL_SendSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values)
  399. {
  400. int posted;
  401. /* Allow duplicate events, for things like steps and heartbeats */
  402. /* Update internal sensor state */
  403. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  404. SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
  405. /* Post the event, if desired */
  406. posted = 0;
  407. #if !SDL_EVENTS_DISABLED
  408. if (SDL_EventEnabled(SDL_SENSORUPDATE)) {
  409. SDL_Event event;
  410. event.type = SDL_SENSORUPDATE;
  411. event.common.timestamp = timestamp;
  412. event.sensor.which = sensor->instance_id;
  413. num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
  414. SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
  415. SDL_memcpy(event.sensor.data, data, num_values * sizeof(*data));
  416. event.sensor.sensor_timestamp = sensor_timestamp;
  417. posted = SDL_PushEvent(&event) == 1;
  418. }
  419. #endif /* !SDL_EVENTS_DISABLED */
  420. return posted;
  421. }
  422. void SDL_UpdateSensors(void)
  423. {
  424. int i;
  425. SDL_Sensor *sensor;
  426. if (!SDL_WasInit(SDL_INIT_SENSOR)) {
  427. return;
  428. }
  429. SDL_LockSensors();
  430. for (sensor = SDL_sensors; sensor; sensor = sensor->next) {
  431. sensor->driver->Update(sensor);
  432. }
  433. /* this needs to happen AFTER walking the sensor list above, so that any
  434. dangling hardware data from removed devices can be free'd
  435. */
  436. for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
  437. SDL_sensor_drivers[i]->Detect();
  438. }
  439. SDL_UnlockSensors();
  440. }