From 49b12139ec00397065dd27b3720aff5ce577f4bc Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sat, 20 Mar 2021 22:58:52 -0700 Subject: [PATCH] Libretro: Lazy-load sensors --- src/platform/libretro/libretro.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/platform/libretro/libretro.c b/src/platform/libretro/libretro.c index 427b5c6fd..f3f03843b 100644 --- a/src/platform/libretro/libretro.c +++ b/src/platform/libretro/libretro.c @@ -69,7 +69,8 @@ static int rumbleDown; static struct mRumble rumble; static struct GBALuminanceSource lux; static struct mRotationSource rotation; -static bool rotationEnabled; +static bool tiltEnabled; +static bool gyroEnabled; static int luxLevelIndex; static uint8_t luxLevel; static bool luxSensorEnabled; @@ -100,9 +101,12 @@ static void _initSensors(void) { sensorStateCallback = sensorInterface.set_sensor_state; if (sensorStateCallback && sensorGetCallback) { - if (sensorStateCallback(0, RETRO_SENSOR_ACCELEROMETER_ENABLE, EVENT_RATE) - && sensorStateCallback(0, RETRO_SENSOR_GYROSCOPE_ENABLE, EVENT_RATE)) { - rotationEnabled = true; + if (sensorStateCallback(0, RETRO_SENSOR_ACCELEROMETER_ENABLE, EVENT_RATE)) { + tiltEnabled = true; + } + + if (sensorStateCallback(0, RETRO_SENSOR_GYROSCOPE_ENABLE, EVENT_RATE)) { + gyroEnabled = true; } if (sensorStateCallback(0, RETRO_SENSOR_ILLUMINANCE_ENABLE, EVENT_RATE)) { @@ -318,7 +322,8 @@ void retro_init(void) { sensorGetCallback = 0; sensorStateCallback = 0; - rotationEnabled = false; + tiltEnabled = false; + gyroEnabled = false; rotation.sample = _updateRotation; rotation.readTiltX = _readTiltX; rotation.readTiltY = _readTiltY; @@ -363,7 +368,8 @@ void retro_deinit(void) { sensorStateCallback = NULL; } - rotationEnabled = false; + tiltEnabled = false; + gyroEnabled = false; luxSensorEnabled = false; sensorsInitDone = false; } @@ -374,7 +380,6 @@ void retro_run(void) { } uint16_t keys; - _initSensors(); inputPollCallback(); bool updated = false; @@ -1023,6 +1028,7 @@ static void _updateLux(struct GBALuminanceSource* lux) { } if (luxSensorUsed) { + _initSensors(); float fLux = luxSensorEnabled ? sensorGetCallback(0, RETRO_SENSOR_ILLUMINANCE) : 0.0f; luxLevel = cbrtf(fLux) * 8; } else { @@ -1124,9 +1130,12 @@ static void _updateRotation(struct mRotationSource* source) { tiltX = 0; tiltY = 0; gyroZ = 0; - if (rotationEnabled) { + _initSensors(); + if (tiltEnabled) { tiltX = sensorGetCallback(0, RETRO_SENSOR_ACCELEROMETER_X) * 3e8f; tiltY = sensorGetCallback(0, RETRO_SENSOR_ACCELEROMETER_Y) * -3e8f; + } + if (gyroEnabled) { gyroZ = sensorGetCallback(0, RETRO_SENSOR_GYROSCOPE_Z) * -1.1e9f; } }