From c5f80591a0f8cbc2a6a3ab30b28754a16042c6f3 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Tue, 21 Nov 2017 18:55:33 -0800 Subject: Moved initial path logic out of menu so it can be shared with new UI --HG-- branch : nuklear_ui --- paths.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 paths.c (limited to 'paths.c') diff --git a/paths.c b/paths.c new file mode 100644 index 0000000..c3c4ad9 --- /dev/null +++ b/paths.c @@ -0,0 +1,103 @@ +#include +#include +#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 +#include +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); +} -- cgit v1.2.3 From 2900092e4e06be77108ed6b8a66c7aa673d0b494 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Sat, 25 Nov 2017 13:57:38 -0800 Subject: Fix directory navigation in ROM file chooser in Nuklear UI --HG-- branch : nuklear_ui --- paths.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'paths.c') diff --git a/paths.c b/paths.c index c3c4ad9..9f7afbd 100644 --- a/paths.c +++ b/paths.c @@ -101,3 +101,43 @@ void get_initial_browse_path(char **dst) *dst = replace_vars(*dst, vars, 1); tern_free(vars); } + +char *path_append(char *base, char *suffix) +{ + if (!strcmp(suffix, "..")) { +#ifdef _WIN32 + //handle transition from root of a drive to virtual root + if (base[1] == ':' && !base[2]) { + return strdup(PATH_SEP) + } +#endif + size_t len = strlen(base); + while (len > 0) { + --len; + if (is_path_sep(base[len])) { + if (!len) { + //special handling for / + len++; + } + char *ret = malloc(len+1); + memcpy(ret, base, len); + ret[len] = 0; + return ret; + } + } + return strdup(PATH_SEP); + } else { +#ifdef _WIN32 + if (base[0] == PATH_SEP[0] && !base[1]) { + //handle transition from virtual root to root of a drive + return strdup(suffix); + } +#endif + if (is_path_sep(base[strlen(base) - 1])) { + return alloc_concat(base, suffix); + } else { + char const *pieces[] = {base, PATH_SEP, suffix}; + return alloc_concat_m(3, pieces); + } + } +} -- cgit v1.2.3 From 0b409a1a9ccc9d7d566a0a867eec4f6eb5a1f5e1 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Wed, 29 Nov 2017 08:41:37 -0800 Subject: Added code to persist config back to a file --HG-- branch : nuklear_ui --- paths.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'paths.c') diff --git a/paths.c b/paths.c index 9f7afbd..6f1cd5b 100644 --- a/paths.c +++ b/paths.c @@ -102,7 +102,7 @@ void get_initial_browse_path(char **dst) tern_free(vars); } -char *path_append(char *base, char *suffix) +char *path_append(const char *base, const char *suffix) { if (!strcmp(suffix, "..")) { #ifdef _WIN32 -- cgit v1.2.3 From a956d5e1f82e98097602e289ac8f02374ea3b5e7 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Wed, 7 Feb 2018 00:07:12 -0800 Subject: Fix Windows build, added Windows default_font_path implementation --HG-- branch : nuklear_ui --- paths.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'paths.c') diff --git a/paths.c b/paths.c index 6f1cd5b..49a3c5c 100644 --- a/paths.c +++ b/paths.c @@ -108,7 +108,7 @@ char *path_append(const char *base, const char *suffix) #ifdef _WIN32 //handle transition from root of a drive to virtual root if (base[1] == ':' && !base[2]) { - return strdup(PATH_SEP) + return strdup(PATH_SEP); } #endif size_t len = strlen(base); -- cgit v1.2.3