comparison paths.c @ 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
children 77a401044935
comparison
equal deleted inserted replaced
1471:2e6320d261ff 1473:152a60c6787e
1 #include <string.h>
2 #include <stdlib.h>
3 #include "blastem.h"
4 #include "util.h"
5
6 static char **current_path;
7
8 static void persist_path(void)
9 {
10 char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
11 char *pathfname = alloc_concat_m(3, parts);
12 FILE *f = fopen(pathfname, "wb");
13 if (f) {
14 if (fwrite(*current_path, 1, strlen(*current_path), f) != strlen(*current_path)) {
15 warning("Failed to save menu path");
16 }
17 fclose(f);
18 } else {
19 warning("Failed to save menu path: Could not open %s for writing\n", pathfname);
20
21 }
22 free(pathfname);
23 }
24
25 #ifdef __ANDROID__
26 #include <SDL.h>
27 #include <jni.h>
28 static char *get_external_storage_path()
29 {
30 static char *ret;
31 if (ret) {
32 return ret;
33 }
34 JNIEnv *env = SDL_AndroidGetJNIEnv();
35 if ((*env)->PushLocalFrame(env, 8) < 0) {
36 return NULL;
37 }
38
39 jclass Environment = (*env)->FindClass(env, "android/os/Environment");
40 jmethodID getExternalStorageDirectory =
41 (*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;");
42 jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory);
43 if (!file) {
44 goto cleanup;
45 }
46
47 jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file),
48 "getAbsolutePath", "()Ljava/lang/String;");
49 jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath);
50
51 char const *tmp = (*env)->GetStringUTFChars(env, path, NULL);
52 ret = strdup(tmp);
53 (*env)->ReleaseStringUTFChars(env, path, tmp);
54
55 cleanup:
56 (*env)->PopLocalFrame(env, NULL);
57 return ret;
58 }
59 #endif
60
61 void get_initial_browse_path(char **dst)
62 {
63 *dst = NULL;
64 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval;
65 if (!remember_path || !strcmp("on", remember_path)) {
66 char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
67 char *pathfname = alloc_concat_m(3, parts);
68 FILE *f = fopen(pathfname, "rb");
69 if (f) {
70 long pathsize = file_size(f);
71 if (pathsize > 0) {
72 *dst = malloc(pathsize + 1);
73 if (fread(*dst, 1, pathsize, f) != pathsize) {
74 warning("Error restoring saved file browser path");
75 free(*dst);
76 *dst = NULL;
77 } else {
78 (*dst)[pathsize] = 0;
79 }
80 }
81 fclose(f);
82 }
83 free(pathfname);
84 if (!current_path) {
85 atexit(persist_path);
86 current_path = dst;
87 }
88 }
89 if (!*dst) {
90 *dst = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
91 }
92 if (!*dst){
93 #ifdef __ANDROID__
94 *dst = get_external_storage_path();
95 #else
96 *dst = "$HOME";
97 #endif
98 }
99 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
100 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
101 *dst = replace_vars(*dst, vars, 1);
102 tern_free(vars);
103 }