From cdb0f7b06b53e3bf7c45de1e07eee3b36e1162d2 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sat, 12 Dec 2015 11:42:33 -0800 Subject: [PATCH] Shunt disk-state-change information back to the Java/Android menu system --- .../org/deadc0de/apple2ix/Apple2Activity.java | 21 +++++++++++++++++-- .../deadc0de/apple2ix/Apple2Preferences.java | 14 +++++++++++++ Android/jni/jnihooks.c | 18 +++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java index 76a30756..342f1a39 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java @@ -35,6 +35,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.deadc0de.apple2ix.basic.BuildConfig; import org.deadc0de.apple2ix.basic.R; +import org.json.JSONException; +import org.json.JSONObject; public class Apple2Activity extends Activity { @@ -97,7 +99,7 @@ public class Apple2Activity extends Activity { private native void nativeSaveState(String path); - private native void nativeLoadState(String path); + private native String nativeLoadState(String path); public native void nativeEmulationResume(); @@ -621,7 +623,22 @@ public class Apple2Activity extends Activity { }).setNeutralButton(R.string.restore, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Apple2Activity.this.nativeLoadState(quickSavePath); + // loading state can change the disk inserted ... reflect that in + String jsonData = Apple2Activity.this.nativeLoadState(quickSavePath); + try { + JSONObject map = new JSONObject(jsonData); + String diskPath1 = map.getString("disk1"); + boolean readOnly1 = map.getBoolean("readOnly1"); + Apple2Preferences.CURRENT_DISK_A.setPath(Apple2Activity.this, diskPath1); + Apple2Preferences.CURRENT_DISK_A_RO.saveBoolean(Apple2Activity.this, readOnly1); + + String diskPath2 = map.getString("disk2"); + boolean readOnly2 = map.getBoolean("readOnly2"); + Apple2Preferences.CURRENT_DISK_B.setPath(Apple2Activity.this, diskPath2); + Apple2Preferences.CURRENT_DISK_B_RO.saveBoolean(Apple2Activity.this, readOnly2); + } catch (JSONException je) { + Log.v(TAG, "OOPS : "+je); + } Apple2Activity.this.mMainMenu.dismiss(); } }).setNegativeButton(R.string.cancel, null).create(); diff --git a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java index 8ea8af62..7e9cc157 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java @@ -99,6 +99,11 @@ public enum Apple2Preferences { activity.getPreferences(Context.MODE_PRIVATE).edit().putString(toString(), str).apply(); load(activity); } + + @Override + public void setPath(Apple2Activity activity, String str) { + activity.getPreferences(Context.MODE_PRIVATE).edit().putString(toString(), str).apply(); + } }, CURRENT_DISK_A_RO { @Override @@ -133,6 +138,11 @@ public enum Apple2Preferences { activity.getPreferences(Context.MODE_PRIVATE).edit().putString(toString(), str).apply(); load(activity); } + + @Override + public void setPath(Apple2Activity activity, String str) { + activity.getPreferences(Context.MODE_PRIVATE).edit().putString(toString(), str).apply(); + } }, CURRENT_DISK_B_RO { @Override @@ -895,6 +905,10 @@ public enum Apple2Preferences { load(activity); } + public void setPath(Apple2Activity activity, String path) { + /* ... */ + } + // accessors public boolean booleanValue(Apple2Activity activity) { diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index 319b9238..a6ac8722 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -359,7 +359,7 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeSaveState(JNIEnv *env, jobj (*env)->ReleaseStringUTFChars(env, jPath, path); } -void Java_org_deadc0de_apple2ix_Apple2Activity_nativeLoadState(JNIEnv *env, jobject obj, jstring jPath) { +jstring Java_org_deadc0de_apple2ix_Apple2Activity_nativeLoadState(JNIEnv *env, jobject obj, jstring jPath) { const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); assert(cpu_isPaused() && "considered dangerous to save state CPU thread is running"); @@ -370,6 +370,22 @@ void Java_org_deadc0de_apple2ix_Apple2Activity_nativeLoadState(JNIEnv *env, jobj } (*env)->ReleaseStringUTFChars(env, jPath, path); + + // restoring state may cause a change in disk paths, so we need to notify the Java/Android menu system of the change + // (normally we drive state from the Java/menu side...) + char *disk1 = disk6.disk[0].file_name; + bool readOnly1 = disk6.disk[0].is_protected; + char *disk2 = disk6.disk[1].file_name; + bool readOnly2 = disk6.disk[1].is_protected; + char *str = NULL; + jstring jstr = NULL; + asprintf(&str, "{ disk1 = \"%s\"; readOnly1 = %s; disk2 = \"%s\"; readOnly2 = %s }", (disk1 ?: ""), readOnly1 ? "true" : "false", (disk2 ?: ""), readOnly2 ? "true" : "false"); + if (str) { + jstr = (*env)->NewStringUTF(env, str); + FREE(str); + } + + return jstr; } // ----------------------------------------------------------------------------