Atlas - SDL_emscriptensensor.c
Home / ext / SDL / src / sensor / emscripten Lines: 1 | Size: 6107 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#ifdef SDL_SENSOR_EMSCRIPTEN 24 25#include "../SDL_syssensor.h" 26#include "SDL_emscriptensensor.h" 27#include <emscripten/html5.h> 28 29#define EMSCRIPTEN_SENSOR_COUNT 2 30 31typedef struct 32{ 33 SDL_SensorType type; 34 SDL_SensorID instance_id; 35 float data[3]; 36 bool new_data; 37} SDL_EmscriptenSensor; 38 39static SDL_EmscriptenSensor SDL_sensors[EMSCRIPTEN_SENSOR_COUNT]; 40 41static void SDL_EMSCRIPTEN_AccelerometerCallback(const EmscriptenDeviceMotionEvent *event) 42{ 43 double total_gravity; 44 double gravity[3]; 45 46 // Convert from browser specific gravity constant to SDL_STANDARD_GRAVITY. 47 total_gravity = 0.0; 48 total_gravity += SDL_fabs(event->accelerationIncludingGravityX - event->accelerationX); 49 total_gravity += SDL_fabs(event->accelerationIncludingGravityY - event->accelerationY); 50 total_gravity += SDL_fabs(event->accelerationIncludingGravityZ - event->accelerationZ); 51 52 gravity[0] = (event->accelerationIncludingGravityX - event->accelerationX) / total_gravity; 53 gravity[1] = (event->accelerationIncludingGravityY - event->accelerationY) / total_gravity; 54 gravity[2] = (event->accelerationIncludingGravityZ - event->accelerationZ) / total_gravity; 55 56 SDL_sensors[0].data[0] = (float)(event->accelerationX + gravity[0] * SDL_STANDARD_GRAVITY); 57 SDL_sensors[0].data[1] = (float)(event->accelerationY + gravity[1] * SDL_STANDARD_GRAVITY); 58 SDL_sensors[0].data[2] = (float)(event->accelerationZ + gravity[2] * SDL_STANDARD_GRAVITY); 59 SDL_sensors[0].new_data = true; 60} 61 62static void SDL_EMSCRIPTEN_GyroscopeCallback(const EmscriptenDeviceMotionEvent *event) 63{ 64 SDL_sensors[1].data[0] = (float)event->rotationRateAlpha * SDL_PI_F / 180.0f; 65 SDL_sensors[1].data[1] = (float)event->rotationRateBeta * SDL_PI_F / 180.0f; 66 SDL_sensors[1].data[2] = (float)event->rotationRateGamma * SDL_PI_F / 180.0f; 67 SDL_sensors[1].new_data = true; 68} 69 70static EM_BOOL SDL_EMSCRIPTEN_SensorCallback(int event_type, const EmscriptenDeviceMotionEvent *event, void *user_data) 71{ 72 SDL_EMSCRIPTEN_AccelerometerCallback(event); 73 SDL_EMSCRIPTEN_GyroscopeCallback(event); 74 75 return true; 76} 77 78static bool SDL_EMSCRIPTEN_SensorInit(void) 79{ 80 emscripten_set_devicemotion_callback((void *)0, false, &SDL_EMSCRIPTEN_SensorCallback); 81 82 SDL_sensors[0].type = SDL_SENSOR_ACCEL; 83 SDL_sensors[0].instance_id = SDL_GetNextObjectID(); 84 SDL_sensors[0].new_data = false; 85 SDL_sensors[1].type = SDL_SENSOR_GYRO; 86 SDL_sensors[1].instance_id = SDL_GetNextObjectID(); 87 SDL_sensors[1].new_data = false; 88 89 return true; 90} 91 92static int SDL_EMSCRIPTEN_SensorGetCount(void) 93{ 94 return EMSCRIPTEN_SENSOR_COUNT; 95} 96 97static void SDL_EMSCRIPTEN_SensorDetect(void) 98{ 99} 100 101static const char *SDL_EMSCRIPTEN_SensorGetDeviceName(int device_index) 102{ 103 if (device_index < EMSCRIPTEN_SENSOR_COUNT) { 104 switch (SDL_sensors[device_index].type) { 105 case SDL_SENSOR_ACCEL: 106 return "Accelerometer"; 107 case SDL_SENSOR_GYRO: 108 return "Gyroscope"; 109 default: 110 return "Unknown"; 111 } 112 } 113 114 return NULL; 115} 116 117static SDL_SensorType SDL_EMSCRIPTEN_SensorGetDeviceType(int device_index) 118{ 119 if (device_index < EMSCRIPTEN_SENSOR_COUNT) { 120 return SDL_sensors[device_index].type; 121 } 122 123 return SDL_SENSOR_INVALID; 124} 125 126static int SDL_EMSCRIPTEN_SensorGetDeviceNonPortableType(int device_index) 127{ 128 if (device_index < EMSCRIPTEN_SENSOR_COUNT) { 129 return SDL_sensors[device_index].type; 130 } 131 132 return -1; 133} 134 135static SDL_SensorID SDL_EMSCRIPTEN_SensorGetDeviceInstanceID(int device_index) 136{ 137 if (device_index < EMSCRIPTEN_SENSOR_COUNT) { 138 return SDL_sensors[device_index].instance_id; 139 } 140 141 return -1; 142} 143 144static bool SDL_EMSCRIPTEN_SensorOpen(SDL_Sensor *sensor, int device_index) 145{ 146 return true; 147} 148 149static void SDL_EMSCRIPTEN_SensorUpdate(SDL_Sensor *sensor) 150{ 151 Uint64 timestamp; 152 153 switch (sensor->type) { 154 case SDL_SENSOR_ACCEL: 155 if (SDL_sensors[0].new_data) { 156 SDL_sensors[0].new_data = false; 157 timestamp = SDL_GetTicksNS(); 158 SDL_SendSensorUpdate(timestamp, sensor, timestamp, SDL_sensors[0].data, SDL_arraysize(SDL_sensors[0].data)); 159 } 160 break; 161 case SDL_SENSOR_GYRO: 162 if (SDL_sensors[1].new_data) { 163 SDL_sensors[1].new_data = false; 164 timestamp = SDL_GetTicksNS(); 165 SDL_SendSensorUpdate(timestamp, sensor, timestamp, SDL_sensors[1].data, SDL_arraysize(SDL_sensors[1].data)); 166 } 167 break; 168 default: 169 break; 170 } 171} 172 173static void SDL_EMSCRIPTEN_SensorClose(SDL_Sensor *sensor) 174{ 175} 176 177static void SDL_EMSCRIPTEN_SensorQuit(void) 178{ 179} 180 181SDL_SensorDriver SDL_EMSCRIPTEN_SensorDriver = { 182 SDL_EMSCRIPTEN_SensorInit, 183 SDL_EMSCRIPTEN_SensorGetCount, 184 SDL_EMSCRIPTEN_SensorDetect, 185 SDL_EMSCRIPTEN_SensorGetDeviceName, 186 SDL_EMSCRIPTEN_SensorGetDeviceType, 187 SDL_EMSCRIPTEN_SensorGetDeviceNonPortableType, 188 SDL_EMSCRIPTEN_SensorGetDeviceInstanceID, 189 SDL_EMSCRIPTEN_SensorOpen, 190 SDL_EMSCRIPTEN_SensorUpdate, 191 SDL_EMSCRIPTEN_SensorClose, 192 SDL_EMSCRIPTEN_SensorQuit, 193}; 194 195#endif // SDL_SENSOR_EMSCRIPTEN 196[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.