diff --git a/workspace/all/common/api.c b/workspace/all/common/api.c index e107225da5..6a9eef8186 100644 --- a/workspace/all/common/api.c +++ b/workspace/all/common/api.c @@ -3957,6 +3957,18 @@ static struct VIB_Context int queued_strength; int strength; } vib = {0}; +// User-facing "Rumble strength" setting, applied to every request right before it +// reaches the platform. vib.strength stays in core units (0..0xFFFF) so +// VIB_getStrength() and the change detection above are unaffected. +static int VIB_applyScale(int strength) +{ + int percent = CFG_getRumbleStrength(); + if (percent >= 100) + return strength; + if (percent <= 0) + return 0; + return (int)(((long long)strength * percent) / 100); +} static void *VIB_thread(void *arg) { #define DEFER_FRAMES 3 @@ -3974,7 +3986,7 @@ static void *VIB_thread(void *arg) vib.strength = vib.queued_strength; defer = 0; - PLAT_setRumble(vib.strength); + PLAT_setRumble(VIB_applyScale(vib.strength)); } } return 0; @@ -3993,6 +4005,11 @@ void VIB_quit(void) VIB_setStrength(0); pthread_cancel(vib.pt); pthread_join(vib.pt, NULL); + // the queued 0 above is usually lost (the thread is cancelled inside its + // SDL_Delay or still deferring the drop), so stop the motor ourselves + vib.strength = vib.queued_strength = 0; + PLAT_setRumble(0); + vib.initialized = 0; } void VIB_setStrength(int strength) { @@ -4291,6 +4308,10 @@ void PWR_disablePowerOff(void) } void PWR_powerOff(int reboot) { + // the core is blocked in PWR_update, so its last rumble request would stay + // queued and the motor would run through the power-off screen + VIB_setStrength(0); + if (pwr.can_poweroff) { @@ -4341,6 +4362,9 @@ void PWR_powerOff(int reboot) static void PWR_enterSleep(void) { + // a core may be mid-rumble when the lid closes; without haptics the sleep + // pulse below never runs, and the motor would stay on through suspend + VIB_setStrength(0); #if defined(SND_CLOSE_ON_SLEEP) && SND_CLOSE_ON_SLEEP // On H700, fully close the audio device before sleeping: a PCM left open // across suspend-to-RAM ends up in a state SDL takes ~10s to close on diff --git a/workspace/all/common/config.c b/workspace/all/common/config.c index aea4cb4967..cb8411b001 100644 --- a/workspace/all/common/config.c +++ b/workspace/all/common/config.c @@ -73,6 +73,7 @@ void CFG_defaults(NextUISettings *cfg) .keepAwakeWhenUSB = CFG_DEFAULT_KEEPAWAKEWHENUSB, .haptics = CFG_DEFAULT_HAPTICS, + .rumbleStrength = CFG_DEFAULT_RUMBLESTRENGTH, .romsUseFolderBackground = CFG_DEFAULT_ROMSUSEFOLDERBACKGROUND, .saveFormat = CFG_DEFAULT_SAVEFORMAT, .stateFormat = CFG_DEFAULT_STATEFORMAT, @@ -321,6 +322,11 @@ void CFG_init(FontLoad_callback_t cb, ColorSet_callback_t ccb) CFG_setHaptics((bool)temp_value); continue; } + if (sscanf(line, "rumbleStrength=%i", &temp_value) == 1) + { + CFG_setRumbleStrength(temp_value); + continue; + } if (sscanf(line, "romfolderbg=%i", &temp_value) == 1) { CFG_setRomsUseFolderBackground((bool)temp_value); @@ -892,6 +898,17 @@ void CFG_setHaptics(bool enable) CFG_sync(); } +int CFG_getRumbleStrength(void) +{ + return settings.rumbleStrength; +} + +void CFG_setRumbleStrength(int percent) +{ + settings.rumbleStrength = clamp(percent, 0, 100); + CFG_sync(); +} + int CFG_getSaveFormat(void) { return settings.saveFormat; @@ -1458,6 +1475,10 @@ void CFG_get(const char *key, char *value) { sprintf(value, "%i", CFG_getMuteLEDs()); } + else if (strcmp(key, "rumbleStrength") == 0) + { + sprintf(value, "%i", CFG_getRumbleStrength()); + } else if (strcmp(key, "fn1action") == 0) { sprintf(value, "%s", CFG_getFnAction(0)); @@ -1650,6 +1671,7 @@ void CFG_sync(void) fprintf(file, "keepAwakeWhenUSB=%i\n", settings.keepAwakeWhenUSB); fprintf(file, "switcherscale=%i\n", settings.gameSwitcherScaling); fprintf(file, "haptics=%i\n", settings.haptics); + fprintf(file, "rumbleStrength=%i\n", settings.rumbleStrength); fprintf(file, "romfolderbg=%i\n", settings.romsUseFolderBackground); fprintf(file, "saveFormat=%i\n", settings.saveFormat); fprintf(file, "stateFormat=%i\n", settings.stateFormat); @@ -1725,6 +1747,7 @@ void CFG_print(void) printf("\t\"keepAwakeWhenUSB\": %i,\n", settings.keepAwakeWhenUSB); printf("\t\"switcherscale\": %i,\n", settings.gameSwitcherScaling); printf("\t\"haptics\": %i,\n", settings.haptics); + printf("\t\"rumbleStrength\": %i,\n", settings.rumbleStrength); printf("\t\"romfolderbg\": %i,\n", settings.romsUseFolderBackground); printf("\t\"saveFormat\": %i,\n", settings.saveFormat); printf("\t\"stateFormat\": %i,\n", settings.stateFormat); diff --git a/workspace/all/common/config.h b/workspace/all/common/config.h index bad3f5fc00..df8024d00c 100644 --- a/workspace/all/common/config.h +++ b/workspace/all/common/config.h @@ -161,6 +161,7 @@ typedef struct // Haptic bool haptics; + int rumbleStrength; // percent, 0 = off, 100 = full // Networking bool ntp; @@ -232,6 +233,7 @@ typedef struct #define CFG_DEFAULT_POWEROFFPROTECTION true #define CFG_DEFAULT_KEEPAWAKEWHENUSB false #define CFG_DEFAULT_HAPTICS false +#define CFG_DEFAULT_RUMBLESTRENGTH 100 #define CFG_DEFAULT_ROMSUSEFOLDERBACKGROUND true #define CFG_DEFAULT_SAVEFORMAT SAVE_FORMAT_SAV #define CFG_DEFAULT_STATEFORMAT STATE_FORMAT_SAV @@ -370,6 +372,9 @@ void CFG_setGameSwitcherScaling(int enumValue); // Enable/disable haptics. bool CFG_getHaptics(void); void CFG_setHaptics(bool enable); +// Scale applied to every rumble/haptic request, in percent (0 = off, 100 = full). +int CFG_getRumbleStrength(void); +void CFG_setRumbleStrength(int percent); // Save format to use for libretro cores // 0 - .sav // 1 - .srm (compressed rzip) diff --git a/workspace/all/settings/settings.cpp b/workspace/all/settings/settings.cpp index f9f740470e..3ab6ea03f5 100644 --- a/workspace/all/settings/settings.cpp +++ b/workspace/all/settings/settings.cpp @@ -156,6 +156,10 @@ static const std::vector transition_mode_labels = {"Off", "Snappy", static const std::vector curtain_opacity_values = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; static const std::vector curtain_opacity_labels = {"Off", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"}; +// Rumble strength options (percent, 0 = off) +static const std::vector rumble_strength_values = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; +static const std::vector rumble_strength_labels = {"Off", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"}; + // Input prompt style options static const std::vector input_prompt_style_values = {(int)INPUT_STYLE_TEXT, (int)INPUT_STYLE_ABXY, (int)INPUT_STYLE_CARDINALS, (int)INPUT_STYLE_SHAPES}; static const std::vector input_prompt_style_labels = {"Text", "ABXY", "Cardinals", "Shapes"}; @@ -657,6 +661,11 @@ int main(int argc, char *argv[]) { return CFG_getHaptics(); }, [](const std::any &value) { CFG_setHaptics(std::any_cast(value)); }, []() { CFG_setHaptics(CFG_DEFAULT_HAPTICS);}}, + new MenuItem{ListItemType::Generic, "Rumble strength", "Strength of game rumble and haptic pulses (Off disables rumble)", rumble_strength_values, rumble_strength_labels, []() -> std::any + { // snap to the 10% grid so a hand-edited value still matches a list entry + return (CFG_getRumbleStrength() + 5) / 10 * 10; }, [](const std::any &value) + { CFG_setRumbleStrength(std::any_cast(value)); }, + []() { CFG_setRumbleStrength(CFG_DEFAULT_RUMBLESTRENGTH);}}, new MenuItem{ListItemType::Generic, "Default view", "The initial view to show on boot", {(int)SCREEN_GAMELIST, (int)SCREEN_GAMESWITCHER, (int)SCREEN_QUICKMENU}, {"Content List","Game Switcher","Quick Menu"},