comparison paths.c @ 2681:c4256ce2c45a

Updated Android port using gradle toolchain and with basic Storage Access Framework support for Android 11+ support
author Michael Pavone <pavone@retrodev.com>
date Wed, 26 Mar 2025 01:20:55 -0700
parents 2972a8e16ed2
children
comparison
equal deleted inserted replaced
2680:e3394457427e 2681:c4256ce2c45a
8 #include <windows.h> 8 #include <windows.h>
9 #else 9 #else
10 #include <unistd.h> 10 #include <unistd.h>
11 #include <errno.h> 11 #include <errno.h>
12 #endif 12 #endif
13 #ifdef __ANDROID__
14 #include <SDL_system.h>
15 #include <jni.h>
16 #endif
13 17
14 static char **current_path; 18 static char **current_path;
15 19
16 static char *sticky_path_path(void) 20 static char *sticky_path_path(void)
17 { 21 {
37 } 41 }
38 free(pathfname); 42 free(pathfname);
39 } 43 }
40 44
41 #ifdef __ANDROID__ 45 #ifdef __ANDROID__
42 #include <SDL.h>
43 #include <jni.h>
44 static char *get_external_storage_path() 46 static char *get_external_storage_path()
45 { 47 {
46 static char *ret; 48 static char *ret;
47 if (ret) { 49 if (ret) {
48 return ret; 50 return ret;
72 (*env)->PopLocalFrame(env, NULL); 74 (*env)->PopLocalFrame(env, NULL);
73 return ret; 75 return ret;
74 } 76 }
75 #endif 77 #endif
76 78
77 void get_initial_browse_path(char **dst) 79 uint8_t get_initial_browse_path(char **dst)
78 { 80 {
79 char *base = NULL; 81 char *base = NULL;
82 #ifdef __ANDROID__
83 static const char activity_class_name[] = "com/retrodev/blastem/BlastEmActivity";
84 static const char get_rom_path_name[] = "getRomPath";
85 JNIEnv *env = SDL_AndroidGetJNIEnv();
86 jclass act_class = (*env)->FindClass(env, activity_class_name);
87 if (!act_class) {
88 fatal_error("Failed to find activity class %s\n", activity_class_name);
89 }
90 jmethodID meth = (*env)->GetMethodID(env, act_class, get_rom_path_name, "()Ljava/lang/String;");
91 if (!meth) {
92 fatal_error("Failed to find method %s\n", get_rom_path_name);
93 }
94 jobject activity = SDL_AndroidGetActivity();
95 jobject ret = (*env)->CallObjectMethod(env, activity, meth);
96 char *res = NULL;
97 if (ret) {
98 const char*utf = (*env)->GetStringUTFChars(env, (jstring)ret, NULL);
99 jsize len = (*env)->GetStringUTFLength(env, (jstring)ret);
100 res = calloc(len + 1, 1);
101 memcpy(res, utf, len);
102 debug_message("Got initial browse path: %s\n", res);
103 (*env)->ReleaseStringUTFChars(env, (jstring)ret, utf);
104 (*env)->DeleteLocalRef(env, ret);
105 }
106
107 (*env)->DeleteLocalRef(env, activity);
108 if (res) {
109 *dst = res;
110 return 1;
111 }
112 return 0;
113 #else
80 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval; 114 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval;
81 if (!remember_path || !strcmp("on", remember_path)) { 115 if (!remember_path || !strcmp("on", remember_path)) {
82 char *pathfname = sticky_path_path(); 116 char *pathfname = sticky_path_path();
83 FILE *f = fopen(pathfname, "rb"); 117 FILE *f = fopen(pathfname, "rb");
84 if (f) { 118 if (f) {
102 } 136 }
103 } 137 }
104 if (!base) { 138 if (!base) {
105 base = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval; 139 base = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
106 } 140 }
141 #endif
107 if (!base){ 142 if (!base){
108 #ifdef __ANDROID__ 143 #ifdef __ANDROID__
144
109 base = get_external_storage_path(); 145 base = get_external_storage_path();
110 #else 146 #else
111 base = "$HOME"; 147 base = "$HOME";
112 #endif 148 #endif
113 } 149 }
114 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir()); 150 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
115 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir()); 151 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
116 *dst = replace_vars(base, vars, 1); 152 *dst = replace_vars(base, vars, 1);
117 free(base); 153 free(base);
118 tern_free(vars); 154 tern_free(vars);
155 return 1;
119 } 156 }
120 157
121 char *path_append(const char *base, const char *suffix) 158 char *path_append(const char *base, const char *suffix)
122 { 159 {
123 if (!strcmp(suffix, "..")) { 160 if (!strcmp(suffix, "..")) {