SDL_sensor.c 13 KB

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