157 lines
4.2 KiB
C
157 lines
4.2 KiB
C
#include "cell.h"
|
|
#include <SDL3/SDL.h>
|
|
|
|
// Sensor type enum to string
|
|
static const char *sensor_type_to_string(SDL_SensorType type) {
|
|
switch (type) {
|
|
case SDL_SENSOR_ACCEL: return "accel";
|
|
case SDL_SENSOR_GYRO: return "gyro";
|
|
case SDL_SENSOR_ACCEL_L: return "accel_l";
|
|
case SDL_SENSOR_GYRO_L: return "gyro_l";
|
|
case SDL_SENSOR_ACCEL_R: return "accel_r";
|
|
case SDL_SENSOR_GYRO_R: return "gyro_r";
|
|
case SDL_SENSOR_UNKNOWN: return "unknown";
|
|
default: return "invalid";
|
|
}
|
|
}
|
|
|
|
// SDL_Sensor class
|
|
void SDL_Sensor_free(JSRuntime *rt, SDL_Sensor *sensor) {
|
|
if (sensor) SDL_CloseSensor(sensor);
|
|
}
|
|
|
|
QJSCLASS(SDL_Sensor,)
|
|
|
|
// SDL_GetSensors() -> array of sensor IDs
|
|
JSC_CCALL(sensor_get_sensors,
|
|
int count = 0;
|
|
SDL_SensorID *sensors = SDL_GetSensors(&count);
|
|
if (!sensors) return JS_NewArray(js);
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
for (int i = 0; i < count; i++) {
|
|
JS_SetPropertyUint32(js, arr, i, JS_NewUint32(js, sensors[i]));
|
|
}
|
|
SDL_free(sensors);
|
|
return arr;
|
|
)
|
|
|
|
// SDL_GetSensorNameForID(id) -> string
|
|
JSC_CCALL(sensor_get_name_for_id,
|
|
uint32_t id;
|
|
JS_ToUint32(js, &id, argv[0]);
|
|
const char *name = SDL_GetSensorNameForID(id);
|
|
return name ? JS_NewString(js, name) : JS_NULL;
|
|
)
|
|
|
|
// SDL_GetSensorTypeForID(id) -> string
|
|
JSC_CCALL(sensor_get_type_for_id,
|
|
uint32_t id;
|
|
JS_ToUint32(js, &id, argv[0]);
|
|
SDL_SensorType type = SDL_GetSensorTypeForID(id);
|
|
return JS_NewString(js, sensor_type_to_string(type));
|
|
)
|
|
|
|
// SDL_GetSensorNonPortableTypeForID(id) -> number
|
|
JSC_CCALL(sensor_get_non_portable_type_for_id,
|
|
uint32_t id;
|
|
JS_ToUint32(js, &id, argv[0]);
|
|
return JS_NewInt32(js, SDL_GetSensorNonPortableTypeForID(id));
|
|
)
|
|
|
|
// SDL_OpenSensor(id) -> Sensor object
|
|
JSC_CCALL(sensor_open,
|
|
uint32_t id;
|
|
JS_ToUint32(js, &id, argv[0]);
|
|
SDL_Sensor *sensor = SDL_OpenSensor(id);
|
|
if (!sensor) return JS_NULL;
|
|
return SDL_Sensor2js(js, sensor);
|
|
)
|
|
|
|
// Sensor methods
|
|
JSC_CCALL(sensor_get_name,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
const char *name = SDL_GetSensorName(sensor);
|
|
return name ? JS_NewString(js, name) : JS_NULL;
|
|
)
|
|
|
|
JSC_CCALL(sensor_get_type,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
SDL_SensorType type = SDL_GetSensorType(sensor);
|
|
return JS_NewString(js, sensor_type_to_string(type));
|
|
)
|
|
|
|
JSC_CCALL(sensor_get_non_portable_type,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
return JS_NewInt32(js, SDL_GetSensorNonPortableType(sensor));
|
|
)
|
|
|
|
JSC_CCALL(sensor_get_id,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
return JS_NewUint32(js, SDL_GetSensorID(sensor));
|
|
)
|
|
|
|
JSC_CCALL(sensor_get_data,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
int num_values = 3;
|
|
if (argc > 0) JS_ToInt32(js, &num_values, argv[0]);
|
|
|
|
float *data = malloc(num_values * sizeof(float));
|
|
if (!data) return JS_ThrowOutOfMemory(js);
|
|
|
|
if (!SDL_GetSensorData(sensor, data, num_values)) {
|
|
free(data);
|
|
return JS_NULL;
|
|
}
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
for (int i = 0; i < num_values; i++) {
|
|
JS_SetPropertyUint32(js, arr, i, JS_NewFloat64(js, data[i]));
|
|
}
|
|
free(data);
|
|
return arr;
|
|
)
|
|
|
|
JSC_CCALL(sensor_close,
|
|
SDL_Sensor *sensor = js2SDL_Sensor(js, self);
|
|
SDL_CloseSensor(sensor);
|
|
return JS_NULL;
|
|
)
|
|
|
|
// SDL_UpdateSensors()
|
|
JSC_CCALL(sensor_update,
|
|
SDL_UpdateSensors();
|
|
return JS_NULL;
|
|
)
|
|
|
|
static const JSCFunctionListEntry js_SDL_Sensor_funcs[] = {
|
|
MIST_FUNC_DEF(sensor, get_name, 0),
|
|
MIST_FUNC_DEF(sensor, get_type, 0),
|
|
MIST_FUNC_DEF(sensor, get_non_portable_type, 0),
|
|
MIST_FUNC_DEF(sensor, get_id, 0),
|
|
MIST_FUNC_DEF(sensor, get_data, 1),
|
|
MIST_FUNC_DEF(sensor, close, 0),
|
|
};
|
|
|
|
static const JSCFunctionListEntry js_sensor_funcs[] = {
|
|
MIST_FUNC_DEF(sensor, get_sensors, 0),
|
|
MIST_FUNC_DEF(sensor, get_name_for_id, 1),
|
|
MIST_FUNC_DEF(sensor, get_type_for_id, 1),
|
|
MIST_FUNC_DEF(sensor, get_non_portable_type_for_id, 1),
|
|
MIST_FUNC_DEF(sensor, open, 1),
|
|
MIST_FUNC_DEF(sensor, update, 0),
|
|
};
|
|
|
|
CELL_USE_INIT(
|
|
SDL_Init(SDL_INIT_SENSOR);
|
|
QJSCLASSPREP_FUNCS(SDL_Sensor);
|
|
|
|
JSValue ret = JS_NewObject(js);
|
|
JS_SetPropertyFunctionList(js, ret, js_sensor_funcs, countof(js_sensor_funcs));
|
|
|
|
// Export standard gravity constant
|
|
JS_SetPropertyStr(js, ret, "STANDARD_GRAVITY", JS_NewFloat64(js, SDL_STANDARD_GRAVITY));
|
|
|
|
return ret;
|
|
)
|