comparison menu.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 e2bd03ed3190
children da1dce39e846
comparison
equal deleted inserted replaced
1471:2e6320d261ff 1473:152a60c6787e
6 #include "genesis.h" 6 #include "genesis.h"
7 #include "menu.h" 7 #include "menu.h"
8 #include "backend.h" 8 #include "backend.h"
9 #include "util.h" 9 #include "util.h"
10 #include "gst.h" 10 #include "gst.h"
11 #include "paths.h"
11 #include "m68k_internal.h" //needed for get_native_address_trans, should be eliminated once handling of PC is cleaned up 12 #include "m68k_internal.h" //needed for get_native_address_trans, should be eliminated once handling of PC is cleaned up
12
13 static menu_context *persist_path_menu;
14 static void persist_path(void)
15 {
16 char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
17 char *pathfname = alloc_concat_m(3, parts);
18 FILE *f = fopen(pathfname, "wb");
19 if (f) {
20 if (fwrite(persist_path_menu->curpath, 1, strlen(persist_path_menu->curpath), f) != strlen(persist_path_menu->curpath)) {
21 warning("Failed to save menu path");
22 }
23 fclose(f);
24 } else {
25 warning("Failed to save menu path: Could not open %s for writing\n", pathfname);
26
27 }
28 free(pathfname);
29 }
30 13
31 static menu_context *get_menu(genesis_context *gen) 14 static menu_context *get_menu(genesis_context *gen)
32 { 15 {
33 menu_context *menu = gen->extra; 16 menu_context *menu = gen->extra;
34 if (!menu) { 17 if (!menu) {
35 gen->extra = menu = calloc(1, sizeof(menu_context)); 18 gen->extra = menu = calloc(1, sizeof(menu_context));
36 menu->curpath = NULL; 19 get_initial_browse_path(&menu->curpath);
37 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval;
38 if (!remember_path || !strcmp("on", remember_path)) {
39 char const *parts[] = {get_userdata_dir(), PATH_SEP, "sticky_path"};
40 char *pathfname = alloc_concat_m(3, parts);
41 FILE *f = fopen(pathfname, "rb");
42 if (f) {
43 long pathsize = file_size(f);
44 if (pathsize > 0) {
45 menu->curpath = malloc(pathsize + 1);
46 if (fread(menu->curpath, 1, pathsize, f) != pathsize) {
47 warning("Error restoring saved menu path");
48 free(menu->curpath);
49 menu->curpath = NULL;
50 } else {
51 menu->curpath[pathsize] = 0;
52 }
53 }
54 fclose(f);
55 }
56 free(pathfname);
57 if (!persist_path_menu) {
58 atexit(persist_path);
59 }
60 persist_path_menu = menu;
61 }
62 if (!menu->curpath) {
63 menu->curpath = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
64 }
65 if (!menu->curpath){
66 #ifdef __ANDROID__
67 menu->curpath = get_external_storage_path();
68 #else
69 menu->curpath = "$HOME";
70 #endif
71 }
72 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
73 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
74 menu->curpath = replace_vars(menu->curpath, vars, 1);
75 tern_free(vars);
76 } 20 }
77 return menu; 21 return menu;
78 } 22 }
79 23
80 uint16_t menu_read_w(uint32_t address, void * vcontext) 24 uint16_t menu_read_w(uint32_t address, void * vcontext)
147 *dst = cur[1]; 91 *dst = cur[1];
148 } 92 }
149 } 93 }
150 94
151 #define SAVE_INFO_BUFFER_SIZE (11*40) 95 #define SAVE_INFO_BUFFER_SIZE (11*40)
152
153 #ifdef __ANDROID__
154 #include <SDL.h>
155 #include <jni.h>
156 char *get_external_storage_path()
157 {
158 static char *ret;
159 if (ret) {
160 return ret;
161 }
162 JNIEnv *env = SDL_AndroidGetJNIEnv();
163 if ((*env)->PushLocalFrame(env, 8) < 0) {
164 return NULL;
165 }
166
167 jclass Environment = (*env)->FindClass(env, "android/os/Environment");
168 jmethodID getExternalStorageDirectory =
169 (*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;");
170 jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory);
171 if (!file) {
172 goto cleanup;
173 }
174
175 jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file),
176 "getAbsolutePath", "()Ljava/lang/String;");
177 jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath);
178
179 char const *tmp = (*env)->GetStringUTFChars(env, path, NULL);
180 ret = strdup(tmp);
181 (*env)->ReleaseStringUTFChars(env, path, tmp);
182
183 cleanup:
184 (*env)->PopLocalFrame(env, NULL);
185 return ret;
186 }
187 #endif
188 96
189 #ifdef _WIN32 97 #ifdef _WIN32
190 #define localtime_r(a,b) localtime(a) 98 #define localtime_r(a,b) localtime(a)
191 //windows inclues seem not to like certain single letter defines from m68k_internal.h 99 //windows inclues seem not to like certain single letter defines from m68k_internal.h
192 //get rid of them here 100 //get rid of them here