Skip to content

Commit

Permalink
GameSwitcher: Seamless State Management (#1754)
Browse files Browse the repository at this point in the history
With this feature, you can enjoy the seamless game transitions provided
by GameSwitcher even if you have disabled the "auto load state" option
in RetroArch. This means you can switch between games effortlessly
without being forced to enable the auto load state feature in RetroArch.

## Usage

- Disable the "auto load state" feature in RetroArch if you prefer not
to use it.
- The auto save is not loaded when launching a game from the game list,
search, or favorites.
- The auto save will still be loaded when you launch from GameSwitcher.
- Note: Quick switching will not create an auto save state.
  • Loading branch information
Aemiii91 authored Jan 18, 2025
1 parent 3af9ba0 commit 9fa52cc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/common/system/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ typedef struct Rect {
int h;
} rect_t;

void display_reset()
void display_clear(void)
{
if (g_display.fb_addr) {
memset(g_display.fb_addr, 0, g_display.fb_size);
}
}

void display_reset(void)
{
if (fb_fd < 0)
fb_fd = open("/dev/fb0", O_RDWR);
Expand All @@ -87,7 +94,7 @@ void display_getRenderResolution()
//
// Get physical screen resolution
//
void display_getResolution()
void display_getResolution(void)
{
FILE *file = fopen("/tmp/screen_resolution", "r");
if (file == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions src/common/system/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ void resumeGame(int index)

file_put_sync(fp, CMD_TO_RUN_PATH, "%s", LaunchCommand);

temp_flag_set("force_auto_load_state", true);

sync();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/system/system_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// [onion] Check retroarch running & savestate_auto_save in retroarch.cfg is
// true
//
int check_autosave(void)
bool check_autosave(void)
{
char value[STR_MAX];
file_parseKeyValue(RETROARCH_CONFIG, "savestate_auto_save", value, '=', 0);
Expand Down
7 changes: 6 additions & 1 deletion src/keymon/keymon.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ void cpuClockHotkey(int adjust)
}
}

static void signal_refresh(int sig)
{
display_getRenderResolution();
}

//
// Main
//
Expand All @@ -417,7 +422,7 @@ int main(void)
// Initialize
signal(SIGTERM, quit);
signal(SIGSEGV, quit);
signal(SIGUSR1, display_getRenderResolution);
signal(SIGUSR1, signal_refresh);
log_setName("keymon");

getDeviceModel();
Expand Down
15 changes: 10 additions & 5 deletions src/keymon/menuButtonAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ void action_MainUI_resumeGame(void)

static void _saveAndQuitRetroArch(bool quickSwitch)
{
enableSavingMessage();
retroarch_pause();
screenshot_system();
displaySavingMessage();
if (check_autosave()) {
enableSavingMessage();
retroarch_pause();
screenshot_system();
displaySavingMessage();
}
else {
display_clear();
}
if (quickSwitch)
set_quickSwitch();
terminate_retroarch();
Expand All @@ -187,8 +192,8 @@ void action_RA_gameSwitcher(void)
if (exists("/mnt/SDCARD/.tmp_update/.runGameSwitcher"))
return;
set_gameSwitcher();
system("(gameSwitcher --overlay && touch /tmp/state_changed) &");
retroarch_pause();
system("(gameSwitcher --overlay && touch /tmp/state_changed) &");
system_state_update();
}

Expand Down
52 changes: 38 additions & 14 deletions static/build/.tmp_update/runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ launch_game() {
romcfgpath=""
retroarch_core=""
full_resolution_path=""
launch_script=""

start_audioserver
save_settings
Expand All @@ -316,9 +317,9 @@ launch_game() {
rompath=$(echo "$cmd" | awk '{ st = index($0,"\" \""); print substr($0,st+3,length($0)-st-3)}')

if echo "$rompath" | grep -q ":"; then
launch=$(echo "$rompath" | awk '{split($0,a,":"); print a[1]}')
launch_script=$(echo "$rompath" | awk '{split($0,a,":"); print a[1]}')
rompath=$(echo "$rompath" | awk '{split($0,a,":"); print a[2]}')
echo "LD_PRELOAD=/mnt/SDCARD/miyoo/app/../lib/libpadsp.so \"$launch\" \"$rompath\"" > $sysdir/cmd_to_run.sh
echo "LD_PRELOAD=/mnt/SDCARD/miyoo/app/../lib/libpadsp.so \"$launch_script\" \"$rompath\"" > $sysdir/cmd_to_run.sh
fi

orig_path="$rompath"
Expand All @@ -342,9 +343,13 @@ launch_game() {

full_resolution_path="$(get_full_resolution_path)"

if [ -z "$launch_script" ]; then
launch_script=$(echo "$cmd" | awk -F'"' '{print $2}')
fi

if [ $is_game -eq 1 ]; then
if [ -f "$romcfgpath" ]; then
override_game_core "$romcfgpath"
if [ -f "$launch_script" ] && cat "$launch_script" | grep -q '.retroarch/cores'; then
override_game_core "$romcfgpath" "$launch_script"
fi

# Handle dollar sign
Expand All @@ -367,6 +372,7 @@ launch_game() {

# Prevent quick switch loop
rm -f /tmp/quick_switch 2> /dev/null
rm -f /tmp/force_auto_load_state 2> /dev/null

log "----- COMMAND:"
log "$(cat $sysdir/cmd_to_run.sh)"
Expand Down Expand Up @@ -440,15 +446,31 @@ EOM

override_game_core() {
romcfgpath="$1"
romcfg=$(cat "$romcfgpath")
retroarch_core=$(get_info_value "$romcfg" core)
corepath=".retroarch/cores/$retroarch_core.so"
launch_path="$2"

if [ -f "$romcfgpath" ]; then
romcfg=$(cat "$romcfgpath")
retroarch_core=$(get_info_value "$romcfg" core)
fi

log "per game core: $retroarch_core" >> $sysdir/logs/game_list_options.log
if grep -q "default_core=" "$launch_path"; then
default_core="$(cat "$launch_path" | grep "default_core=" | head -1 | awk '{split($0,a,"="); print a[2]}' | xargs)_libretro"
else
default_core=$(cat "$launch_path" | grep ".retroarch/cores/" | awk '{st = index($0,".retroarch/cores/"); s = substr($0,st+17); st2 = index(s,".so"); print substr(s,0,st2-1)}' | xargs)
fi

if [ "$retroarch_core" == "" ]; then
retroarch_core="$default_core"
fi

corepath=".retroarch/cores/$retroarch_core.so"

if [ -f "/mnt/SDCARD/RetroArch/$corepath" ] && echo "$cmd" | grep -qv "retroarch/cores"; then # Do not override game core when launching from GS
if echo "$cmd" | grep -q "$sysdir/reset.cfg"; then
echo "LD_PRELOAD=$miyoodir/lib/libpadsp.so ./retroarch -v --appendconfig \"$sysdir/reset.cfg\" -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
if echo "$cmd" | grep -q "/tmp/reset.cfg"; then
echo "LD_PRELOAD=$miyoodir/lib/libpadsp.so ./retroarch -v --appendconfig \"/tmp/reset.cfg\" -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
elif [ -f /tmp/force_auto_load_state ]; then
echo -e "savestate_auto_load = \"true\"\nconfig_save_on_exit = \"false\"\n" > /tmp/auto_load_state.cfg
echo "LD_PRELOAD=$miyoodir/lib/libpadsp.so ./retroarch -v --appendconfig \"/tmp/auto_load_state.cfg\" -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
else
echo "LD_PRELOAD=$miyoodir/lib/libpadsp.so ./retroarch -v -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
fi
Expand All @@ -468,13 +490,15 @@ launch_game_postprocess() {

# TIMER END + SHUTDOWN CHECK
if [ $is_game -eq 1 ]; then
if echo "$cmd" | grep -q "$sysdir/reset.cfg"; then
echo "$cmd" | sed 's/ --appendconfig \"\/mnt\/SDCARD\/.tmp_update\/reset.cfg\"//g' > $sysdir/cmd_to_run.sh
fi

cd $sysdir
playActivity stop "$rompath"

if echo "$cmd" | grep -q "/tmp/reset.cfg"; then
echo "$cmd" | sed 's/ --appendconfig \"\/tmp\/reset.cfg\"//g' > $sysdir/cmd_to_run.sh
elif echo "$cmd" | grep -q "/tmp/auto_load_state.cfg"; then
echo "$cmd" | sed 's/ --appendconfig \"\/tmp\/auto_load_state.cfg\"//g' > $sysdir/cmd_to_run.sh
fi

if [ -f /tmp/.lowBat ]; then
bootScreen lowBat
sleep 3
Expand Down
4 changes: 2 additions & 2 deletions static/build/.tmp_update/script/game_list_options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ get_json_value() {

reset_game() {
echo ":: reset_game $*"
echo -e "savestate_auto_load = \"false\"\nconfig_save_on_exit = \"false\"\n" > $sysdir/reset.cfg
echo "LD_PRELOAD=/mnt/SDCARD/miyoo/lib/libpadsp.so ./retroarch -v --appendconfig \"$sysdir/reset.cfg\" -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
echo -e "savestate_auto_load = \"false\"\nconfig_save_on_exit = \"false\"\n" > /tmp/reset.cfg
echo "LD_PRELOAD=/mnt/SDCARD/miyoo/lib/libpadsp.so ./retroarch -v --appendconfig \"/tmp/reset.cfg\" -L \"$corepath\" \"$rompath\"" > $sysdir/cmd_to_run.sh
add_game_to_recent_list
exit 0
}
Expand Down

0 comments on commit 9fa52cc

Please sign in to comment.