changeset 1434:b9dbc823c014

Added a config file option for remembering the last path in the menu
author Michael Pavone <pavone@retrodev.com>
date Wed, 16 Aug 2017 20:45:51 -0700
parents c886c54d8cf1
children 178de1432291
files default.cfg menu.c
diffstat 2 files changed, 50 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/default.cfg	Wed Aug 16 20:06:28 2017 -0700
+++ b/default.cfg	Wed Aug 16 20:45:51 2017 -0700
@@ -210,6 +210,9 @@
 	#starting path for ROM browsing, accepts special variables $HOME, $EXEDIR
 	#and variables defined in the OS environment
 	initial_path $HOME
+	#if this is set to on, then the menu will remember the last path when visited
+	#if it's set to off, initial_path will always be used on startup
+	remember_path on
 	#path for storing internal screenshots, accepts the same variables as initial_path
 	screenshot_path $HOME
 	#see strftime for the format specifiers valid in screenshot_template
--- a/menu.c	Wed Aug 16 20:06:28 2017 -0700
+++ b/menu.c	Wed Aug 16 20:45:51 2017 -0700
@@ -10,12 +10,58 @@
 #include "gst.h"
 #include "m68k_internal.h" //needed for get_native_address_trans, should be eliminated once handling of PC is cleaned up
 
+static menu_context *persist_path_menu;
+static void persist_path(void)
+{
+	char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
+	char *pathfname = alloc_concat_m(3, parts);
+	FILE *f = fopen(pathfname, "wb");
+	if (f) {
+		if (fwrite(persist_path_menu->curpath, 1, strlen(persist_path_menu->curpath), f) != strlen(persist_path_menu->curpath)) {
+			warning("Failed to save menu path");
+		}
+		fclose(f);
+	} else {
+		warning("Failed to save menu path: Could not open %s for writing\n", pathfname);
+		
+	}
+	free(pathfname);
+}
+
 static menu_context *get_menu(genesis_context *gen)
 {
 	menu_context *menu = gen->extra;
 	if (!menu) {
 		gen->extra = menu = calloc(1, sizeof(menu_context));
-		menu->curpath = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
+		menu->curpath = NULL;
+		char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval;
+		if (!remember_path || !strcmp("on", remember_path)) {
+			char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
+			char *pathfname = alloc_concat_m(3, parts);
+			FILE *f = fopen(pathfname, "rb");
+			if (f) {
+				long pathsize = file_size(f);
+				if (pathsize > 0) {
+					menu->curpath = malloc(pathsize + 1);
+					if (fread(menu->curpath, 1, pathsize, f) != pathsize) {
+						warning("Error restoring saved menu path");
+						free(menu->curpath);
+						menu->curpath = NULL;
+					} else {
+						menu->curpath[pathsize] = 0;
+					}
+				}
+				fclose(f);
+			}
+			free(pathfname);
+			if (!persist_path_menu) {
+				atexit(persist_path);
+			}
+			persist_path_menu = menu;
+		}
+		if (!menu->curpath) {
+			menu->curpath = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
+		}
 		if (!menu->curpath){
 #ifdef __ANDROID__
 			menu->curpath = get_external_storage_path();