SDL_emscriptensensor.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #ifdef SDL_SENSOR_EMSCRIPTEN
  20. #include "../SDL_syssensor.h"
  21. #include "SDL_emscriptensensor.h"
  22. #include <emscripten/html5.h>
  23. #define EMSCRIPTEN_SENSOR_COUNT 2
  24. typedef struct
  25. {
  26. SDL_SensorType type;
  27. SDL_SensorID instance_id;
  28. float data[3];
  29. bool new_data;
  30. } SDL_EmscriptenSensor;
  31. static SDL_EmscriptenSensor SDL_sensors[EMSCRIPTEN_SENSOR_COUNT];
  32. static void SDL_EMSCRIPTEN_AccelerometerCallback(const EmscriptenDeviceMotionEvent *event)
  33. {
  34. double total_gravity;
  35. double gravity[3];
  36. // Convert from browser specific gravity constant to SDL_STANDARD_GRAVITY.
  37. total_gravity = 0.0;
  38. total_gravity += fabs(event->accelerationIncludingGravityX - event->accelerationX);
  39. total_gravity += fabs(event->accelerationIncludingGravityY - event->accelerationY);
  40. total_gravity += fabs(event->accelerationIncludingGravityZ - event->accelerationZ);
  41. gravity[0] = (event->accelerationIncludingGravityX - event->accelerationX) / total_gravity;
  42. gravity[1] = (event->accelerationIncludingGravityY - event->accelerationY) / total_gravity;
  43. gravity[2] = (event->accelerationIncludingGravityZ - event->accelerationZ) / total_gravity;
  44. SDL_sensors[0].data[0] = (float)(event->accelerationX + gravity[0] * SDL_STANDARD_GRAVITY);
  45. SDL_sensors[0].data[1] = (float)(event->accelerationY + gravity[1] * SDL_STANDARD_GRAVITY);
  46. SDL_sensors[0].data[2] = (float)(event->accelerationZ + gravity[2] * SDL_STANDARD_GRAVITY);
  47. SDL_sensors[0].new_data = true;
  48. }
  49. static void SDL_EMSCRIPTEN_GyroscopeCallback(const EmscriptenDeviceMotionEvent *event)
  50. {
  51. SDL_sensors[1].data[0] = (float)event->rotationRateAlpha * SDL_PI_F / 180.0f;
  52. SDL_sensors[1].data[1] = (float)event->rotationRateBeta * SDL_PI_F / 180.0f;
  53. SDL_sensors[1].data[2] = (float)event->rotationRateGamma * SDL_PI_F / 180.0f;
  54. SDL_sensors[1].new_data = true;
  55. }
  56. static EM_BOOL SDL_EMSCRIPTEN_SensorCallback(int event_type, const EmscriptenDeviceMotionEvent *event, void *user_data)
  57. {
  58. SDL_EMSCRIPTEN_AccelerometerCallback(event);
  59. SDL_EMSCRIPTEN_GyroscopeCallback(event);
  60. return true;
  61. }
  62. static bool SDL_EMSCRIPTEN_SensorInit(void)
  63. {
  64. emscripten_set_devicemotion_callback((void *)0, false, &SDL_EMSCRIPTEN_SensorCallback);
  65. SDL_sensors[0].type = SDL_SENSOR_ACCEL;
  66. SDL_sensors[0].instance_id = SDL_GetNextObjectID();
  67. SDL_sensors[0].new_data = false;
  68. SDL_sensors[1].type = SDL_SENSOR_GYRO;
  69. SDL_sensors[1].instance_id = SDL_GetNextObjectID();
  70. SDL_sensors[1].new_data = false;
  71. return true;
  72. }
  73. static int SDL_EMSCRIPTEN_SensorGetCount(void)
  74. {
  75. return EMSCRIPTEN_SENSOR_COUNT;
  76. }
  77. static void SDL_EMSCRIPTEN_SensorDetect(void)
  78. {
  79. }
  80. static const char *SDL_EMSCRIPTEN_SensorGetDeviceName(int device_index)
  81. {
  82. if (device_index < EMSCRIPTEN_SENSOR_COUNT) {
  83. switch (SDL_sensors[device_index].type) {
  84. case SDL_SENSOR_ACCEL:
  85. return "Accelerometer";
  86. case SDL_SENSOR_GYRO:
  87. return "Gyroscope";
  88. default:
  89. return "Unknown";
  90. }
  91. }
  92. return NULL;
  93. }
  94. static SDL_SensorType SDL_EMSCRIPTEN_SensorGetDeviceType(int device_index)
  95. {
  96. if (device_index < EMSCRIPTEN_SENSOR_COUNT) {
  97. return SDL_sensors[device_index].type;
  98. }
  99. return SDL_SENSOR_INVALID;
  100. }
  101. static int SDL_EMSCRIPTEN_SensorGetDeviceNonPortableType(int device_index)
  102. {
  103. if (device_index < EMSCRIPTEN_SENSOR_COUNT) {
  104. return SDL_sensors[device_index].type;
  105. }
  106. return -1;
  107. }
  108. static SDL_SensorID SDL_EMSCRIPTEN_SensorGetDeviceInstanceID(int device_index)
  109. {
  110. if (device_index < EMSCRIPTEN_SENSOR_COUNT) {
  111. return SDL_sensors[device_index].instance_id;
  112. }
  113. return -1;
  114. }
  115. static bool SDL_EMSCRIPTEN_SensorOpen(SDL_Sensor *sensor, int device_index)
  116. {
  117. return true;
  118. }
  119. static void SDL_EMSCRIPTEN_SensorUpdate(SDL_Sensor *sensor)
  120. {
  121. Uint64 timestamp;
  122. switch (sensor->type) {
  123. case SDL_SENSOR_ACCEL:
  124. if (SDL_sensors[0].new_data) {
  125. SDL_sensors[0].new_data = false;
  126. timestamp = SDL_GetTicksNS();
  127. SDL_SendSensorUpdate(timestamp, sensor, timestamp, SDL_sensors[0].data, SDL_arraysize(SDL_sensors[0].data));
  128. }
  129. break;
  130. case SDL_SENSOR_GYRO:
  131. if (SDL_sensors[1].new_data) {
  132. SDL_sensors[1].new_data = false;
  133. timestamp = SDL_GetTicksNS();
  134. SDL_SendSensorUpdate(timestamp, sensor, timestamp, SDL_sensors[1].data, SDL_arraysize(SDL_sensors[1].data));
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. static void SDL_EMSCRIPTEN_SensorClose(SDL_Sensor *sensor)
  142. {
  143. }
  144. static void SDL_EMSCRIPTEN_SensorQuit(void)
  145. {
  146. }
  147. SDL_SensorDriver SDL_EMSCRIPTEN_SensorDriver = {
  148. SDL_EMSCRIPTEN_SensorInit,
  149. SDL_EMSCRIPTEN_SensorGetCount,
  150. SDL_EMSCRIPTEN_SensorDetect,
  151. SDL_EMSCRIPTEN_SensorGetDeviceName,
  152. SDL_EMSCRIPTEN_SensorGetDeviceType,
  153. SDL_EMSCRIPTEN_SensorGetDeviceNonPortableType,
  154. SDL_EMSCRIPTEN_SensorGetDeviceInstanceID,
  155. SDL_EMSCRIPTEN_SensorOpen,
  156. SDL_EMSCRIPTEN_SensorUpdate,
  157. SDL_EMSCRIPTEN_SensorClose,
  158. SDL_EMSCRIPTEN_SensorQuit,
  159. };
  160. #endif // SDL_SENSOR_EMSCRIPTEN