SDL_sensor.c 13 KB

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