changeset 1473:152a60c6787e nuklear_ui

Moved initial path logic out of menu so it can be shared with new UI
author Michael Pavone <pavone@retrodev.com>
date Tue, 21 Nov 2017 18:55:33 -0800
parents 2e6320d261ff
children c5c022c7aa54
files menu.c paths.c paths.h
diffstat 3 files changed, 111 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/menu.c	Thu Oct 19 03:21:24 2017 -0700
+++ b/menu.c	Tue Nov 21 18:55:33 2017 -0800
@@ -8,71 +8,15 @@
 #include "backend.h"
 #include "util.h"
 #include "gst.h"
+#include "paths.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 = 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();
-#else
-			menu->curpath = "$HOME";
-#endif
-		}
-		tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
-		vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
-		menu->curpath = replace_vars(menu->curpath, vars, 1);
-		tern_free(vars);
+		get_initial_browse_path(&menu->curpath);
 	}
 	return menu;
 }
@@ -150,42 +94,6 @@
 
 #define SAVE_INFO_BUFFER_SIZE (11*40)
 
-#ifdef __ANDROID__
-#include <SDL.h>
-#include <jni.h>
-char *get_external_storage_path()
-{
-	static char *ret;
-	if (ret) {
-		return ret;
-	}
-	JNIEnv *env = SDL_AndroidGetJNIEnv();
-	if ((*env)->PushLocalFrame(env, 8) < 0) {
-		return NULL;
-	}
-
-	jclass Environment = (*env)->FindClass(env, "android/os/Environment");
-	jmethodID getExternalStorageDirectory =
-		(*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;");
-	jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory);
-	if (!file) {
-		goto cleanup;
-	}
-
-	jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file),
-		"getAbsolutePath", "()Ljava/lang/String;");
-	jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath);
-
-	char const *tmp = (*env)->GetStringUTFChars(env, path, NULL);
-	ret = strdup(tmp);
-	(*env)->ReleaseStringUTFChars(env, path, tmp);
-
-cleanup:
-	(*env)->PopLocalFrame(env, NULL);
-	return ret;
-}
-#endif
-
 #ifdef _WIN32
 #define localtime_r(a,b) localtime(a)
 //windows inclues seem not to like certain single letter defines from m68k_internal.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/paths.c	Tue Nov 21 18:55:33 2017 -0800
@@ -0,0 +1,103 @@
+#include <string.h>
+#include <stdlib.h>
+#include "blastem.h"
+#include "util.h"
+
+static char **current_path;
+
+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(*current_path, 1, strlen(*current_path), f) != strlen(*current_path)) {
+			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);
+}
+
+#ifdef __ANDROID__
+#include <SDL.h>
+#include <jni.h>
+static char *get_external_storage_path()
+{
+	static char *ret;
+	if (ret) {
+		return ret;
+	}
+	JNIEnv *env = SDL_AndroidGetJNIEnv();
+	if ((*env)->PushLocalFrame(env, 8) < 0) {
+		return NULL;
+	}
+
+	jclass Environment = (*env)->FindClass(env, "android/os/Environment");
+	jmethodID getExternalStorageDirectory =
+		(*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;");
+	jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory);
+	if (!file) {
+		goto cleanup;
+	}
+
+	jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file),
+		"getAbsolutePath", "()Ljava/lang/String;");
+	jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath);
+
+	char const *tmp = (*env)->GetStringUTFChars(env, path, NULL);
+	ret = strdup(tmp);
+	(*env)->ReleaseStringUTFChars(env, path, tmp);
+
+cleanup:
+	(*env)->PopLocalFrame(env, NULL);
+	return ret;
+}
+#endif
+
+void get_initial_browse_path(char **dst)
+{
+	*dst = 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) {
+				*dst = malloc(pathsize + 1);
+				if (fread(*dst, 1, pathsize, f) != pathsize) {
+					warning("Error restoring saved file browser path");
+					free(*dst);
+					*dst = NULL;
+				} else {
+					(*dst)[pathsize] = 0;
+				}
+			}
+			fclose(f);
+		}
+		free(pathfname);
+		if (!current_path) {
+			atexit(persist_path);
+			current_path = dst;
+		}
+	}
+	if (!*dst) {
+		*dst = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
+	}
+	if (!*dst){
+#ifdef __ANDROID__
+		*dst = get_external_storage_path();
+#else
+		*dst = "$HOME";
+#endif
+	}
+	tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
+	vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
+	*dst = replace_vars(*dst, vars, 1);
+	tern_free(vars);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/paths.h	Tue Nov 21 18:55:33 2017 -0800
@@ -0,0 +1,6 @@
+#ifndef PATHS_H_
+#define PATHS_H_
+
+void get_initial_browse_path(char **dst);
+
+#endif //PATHS_H_
\ No newline at end of file