From 0d658932b6464dfbac324eae733e10c79cee1f74 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Thu, 30 Jul 2015 23:00:00 -0700 Subject: [PATCH] Expose particular settings back to Android so we can persist the values --- .../org/deadc0de/apple2ix/Apple2Activity.java | 36 +++++++++++ .../apple2ix/Apple2InputSettingsMenu.java | 13 ++-- .../deadc0de/apple2ix/Apple2Preferences.java | 27 +++++++- Android/app/src/main/res/values/strings.xml | 4 +- Android/jni/jniprefs.c | 50 ++++++++++++--- src/interface.h | 15 +++-- src/video/gltouchmenu.c | 63 ++----------------- 7 files changed, 122 insertions(+), 86 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 199be143..ace2c3db 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Activity.java @@ -66,11 +66,16 @@ public class Apple2Activity extends Activity { public final static int NATIVE_TOUCH_HANDLED = (1 << 0); public final static int NATIVE_TOUCH_REQUEST_SHOW_MENU = (1 << 1); + public final static int NATIVE_TOUCH_KEY_TAP = (1 << 4); public final static int NATIVE_TOUCH_KBD = (1 << 5); public final static int NATIVE_TOUCH_JOY = (1 << 6); public final static int NATIVE_TOUCH_MENU = (1 << 7); + public final static int NATIVE_TOUCH_INPUT_DEVICE_CHANGED = (1 << 16); + public final static int NATIVE_TOUCH_CPU_SPEED_DEC = (1 << 17); + public final static int NATIVE_TOUCH_CPU_SPEED_INC = (1 << 18); + private native void nativeOnCreate(String dataDir, int sampleRate, int monoBufferSize, int stereoBufferSize); private native void nativeGraphicsInitialized(int width, int height); @@ -425,6 +430,37 @@ public class Apple2Activity extends Activity { } } + if ((nativeFlags & NATIVE_TOUCH_MENU) == 0) { + break; + } + + // handle menu-specific actions + + if ((nativeFlags & NATIVE_TOUCH_INPUT_DEVICE_CHANGED) != 0) { + if (Apple2Preferences.nativeIsTouchJoystickScreenOwner()) { + Apple2Preferences.CURRENT_TOUCH_DEVICE.saveTouchDevice(this, Apple2Preferences.TouchDevice.JOYSTICK); + } else { + Apple2Preferences.CURRENT_TOUCH_DEVICE.saveTouchDevice(this, Apple2Preferences.TouchDevice.KEYBOARD); + } + } + if ((nativeFlags & NATIVE_TOUCH_CPU_SPEED_DEC) != 0) { + int percentSpeed = Apple2Preferences.nativeGetCPUSpeed(); + if (percentSpeed > 100) { + percentSpeed -= 25; + } else { + percentSpeed -= 5; + } + Apple2Preferences.CPU_SPEED_PERCENT.saveInt(this, percentSpeed); + } + if ((nativeFlags & NATIVE_TOUCH_CPU_SPEED_INC) != 0) { + int percentSpeed = Apple2Preferences.nativeGetCPUSpeed(); + if (percentSpeed >= 100) { + percentSpeed += 25; + } else { + percentSpeed += 5; + } + Apple2Preferences.CPU_SPEED_PERCENT.saveInt(this, percentSpeed); + } } while (false); return super.onTouchEvent(event); diff --git a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2InputSettingsMenu.java b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2InputSettingsMenu.java index 1bdc4904..a4d9801c 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2InputSettingsMenu.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2InputSettingsMenu.java @@ -23,7 +23,6 @@ import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CheckedTextView; import android.widget.CompoundButton; -import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; @@ -149,15 +148,15 @@ public class Apple2InputSettingsMenu implements Apple2MenuView { return convertView; } }, - FIRST_INPUT { + CURRENT_INPUT { @Override public String getTitle(Apple2Activity activity) { - return activity.getResources().getString(R.string.input_configure_first_title); + return activity.getResources().getString(R.string.input_current); } @Override public String getSummary(Apple2Activity activity) { - return activity.getResources().getString(R.string.input_configure_first_summary); + return activity.getResources().getString(R.string.input_current_summary); } @Override @@ -169,7 +168,7 @@ public class Apple2InputSettingsMenu implements Apple2MenuView { @Override public void handleSelection(final Apple2Activity activity, final Apple2InputSettingsMenu settingsMenu, boolean isChecked) { - AlertDialog.Builder builder = new AlertDialog.Builder(activity).setIcon(R.drawable.ic_launcher).setCancelable(true).setTitle(R.string.input_configure_first_title); + AlertDialog.Builder builder = new AlertDialog.Builder(activity).setIcon(R.drawable.ic_launcher).setCancelable(true).setTitle(R.string.input_current); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -181,7 +180,7 @@ public class Apple2InputSettingsMenu implements Apple2MenuView { activity.getResources().getString(R.string.joystick), activity.getResources().getString(R.string.keyboard), }; - final int checkedPosition = Apple2Preferences.FIRST_TOUCH_DEVICE.intValue(activity) - 1; + final int checkedPosition = Apple2Preferences.CURRENT_TOUCH_DEVICE.intValue(activity) - 1; final ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.select_dialog_singlechoice, touch_choices) { @Override public View getView(int position, View convertView, ViewGroup parent) { @@ -195,7 +194,7 @@ public class Apple2InputSettingsMenu implements Apple2MenuView { builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int color) { - Apple2Preferences.FIRST_TOUCH_DEVICE.saveTouchDevice(activity, Apple2Preferences.TouchDevice.values()[color + 1]); + Apple2Preferences.CURRENT_TOUCH_DEVICE.saveTouchDevice(activity, Apple2Preferences.TouchDevice.values()[color + 1]); dialog.dismiss(); } }); 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 e49ad6af..4c2facd4 100644 --- a/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java +++ b/Android/app/src/main/java/org/deadc0de/apple2ix/Apple2Preferences.java @@ -28,6 +28,17 @@ public enum Apple2Preferences { activity.getPreferences(Context.MODE_PRIVATE).edit().putBoolean(toString(), true).apply(); } }, + CPU_SPEED_PERCENT { + @Override + public void load(Apple2Activity activity) { + nativeSetCPUSpeed(intValue(activity)); + } + + @Override + public int intValue(Apple2Activity activity) { + return activity.getPreferences(Context.MODE_PRIVATE).getInt(toString(), 100); + } + }, HIRES_COLOR { @Override public void load(Apple2Activity activity) { @@ -112,10 +123,10 @@ public enum Apple2Preferences { return activity.getPreferences(Context.MODE_PRIVATE).getFloat(toString(), defaultLatency); } }, - FIRST_TOUCH_DEVICE { + CURRENT_TOUCH_DEVICE { @Override public void load(Apple2Activity activity) { - nativeSetDefaultTouchDevice(intValue(activity)); + nativeSetCurrentTouchDevice(intValue(activity)); } @Override @@ -260,6 +271,8 @@ public enum Apple2Preferences { loadPreferences(activity); } + // native hooks + private static native void nativeSetColor(int color); private static native boolean nativeSetSpeakerEnabled(boolean enabled); @@ -272,9 +285,17 @@ public enum Apple2Preferences { private static native void nativeSetAudioLatency(float latencySecs); - private static native void nativeSetDefaultTouchDevice(int device); + private static native void nativeSetCurrentTouchDevice(int device); private static native void nativeSetTouchMenuEnabled(boolean enabled); private static native void nativeSetTouchMenuVisibility(float alpha); + + public static native boolean nativeIsTouchKeyboardScreenOwner(); + + public static native boolean nativeIsTouchJoystickScreenOwner(); + + public static native int nativeGetCPUSpeed(); + + public static native void nativeSetCPUSpeed(int percentSpeed); } diff --git a/Android/app/src/main/res/values/strings.xml b/Android/app/src/main/res/values/strings.xml index bd553dbd..9b7ea947 100644 --- a/Android/app/src/main/res/values/strings.xml +++ b/Android/app/src/main/res/values/strings.xml @@ -24,8 +24,8 @@ Joystick Configure input devices… Keyboard, joystick, etc - First touch device - Touch device enabled on launch + Current touch device + Choose current touch device Touch joystick Touch keyboard Configure joysticks… diff --git a/Android/jni/jniprefs.c b/Android/jni/jniprefs.c index cab74c6c..acc0ef7f 100644 --- a/Android/jni/jniprefs.c +++ b/Android/jni/jniprefs.c @@ -21,14 +21,6 @@ typedef enum AndroidTouchDevice_t { ANDROID_TOUCH_DEVICE_MAX, } AndroidTouchDevice_t; -void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeEnableTouchJoystick(JNIEnv *env, jclass cls, jboolean enabled) { - LOG("native enable touch joystick : %d", enabled); -} - -void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeEnableTiltJoystick(JNIEnv *env, jclass cls, jboolean enabled) { - LOG("native enable tilt joystick : %d", enabled); -} - void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetColor(JNIEnv *env, jclass cls, jint color) { LOG("native set hires color : %d", color); if (color < COLOR_NONE || color > COLOR_INTERP) { @@ -76,7 +68,7 @@ void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetMockingboardVolume(JN MB_SetVolumeZeroToTen(goesToTen); } -void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetDefaultTouchDevice(JNIEnv *env, jclass cls, jint touchDevice) { +void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetCurrentTouchDevice(JNIEnv *env, jclass cls, jint touchDevice) { LOG("native set default touch device : %d", touchDevice); assert(touchDevice >= 0 && touchDevice < ANDROID_TOUCH_DEVICE_MAX); switch (touchDevice) { @@ -106,3 +98,43 @@ void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetTouchMenuVisibility(J interface_setTouchMenuVisibility(alpha); } +jboolean Java_org_deadc0de_apple2ix_Apple2Preferences_nativeIsTouchKeyboardScreenOwner(JNIEnv *env, jclass cls) { + LOG("nativeIsTouchKeyboardScreenOwner() ..."); + return keydriver_ownsScreen(); +} + +jboolean Java_org_deadc0de_apple2ix_Apple2Preferences_nativeIsTouchJoystickScreenOwner(JNIEnv *env, jclass cls) { + LOG("nativeIsTouchJoystickScreenOwner() ..."); + return joydriver_ownsScreen(); +} + +jint Java_org_deadc0de_apple2ix_Apple2Preferences_nativeGetCPUSpeed(JNIEnv *env, jclass cls) { + LOG("nativeGetCPUSpeed() ..."); + return (jint)round(cpu_scale_factor * 100.0); +} + +void Java_org_deadc0de_apple2ix_Apple2Preferences_nativeSetCPUSpeed(JNIEnv *env, jclass cls, jint percentSpeed) { + LOG("nativeSetCPUSpeed() : %d%%", percentSpeed); + bool wasPaused = cpu_isPaused(); + + if (!wasPaused) { + cpu_pause(); + } + + cpu_scale_factor = percentSpeed/100.0; + if (cpu_scale_factor > CPU_SCALE_FASTEST) { + cpu_scale_factor = CPU_SCALE_FASTEST; + } + if (cpu_scale_factor < CPU_SCALE_SLOWEST) { + cpu_scale_factor = CPU_SCALE_SLOWEST; + } + + if (video_backend->animation_showCPUSpeed) { + video_backend->animation_showCPUSpeed(); + } + + if (!wasPaused) { + cpu_resume(); + } +} + diff --git a/src/interface.h b/src/interface.h index 2c7a7f9a..974a93e2 100644 --- a/src/interface.h +++ b/src/interface.h @@ -55,12 +55,15 @@ typedef enum interface_touch_event_t { } interface_touch_event_t; typedef enum interface_touch_event_flags { - TOUCH_FLAGS_HANDLED = (1<<0), - TOUCH_FLAGS_REQUEST_HOST_MENU = (1<<1), - TOUCH_FLAGS_KEY_TAP = (1<<4), - TOUCH_FLAGS_KBD = (1<<5), - TOUCH_FLAGS_JOY = (1<<6), - TOUCH_FLAGS_MENU = (1<<7), + TOUCH_FLAGS_HANDLED = (1<<0), + TOUCH_FLAGS_REQUEST_HOST_MENU = (1<<1), + TOUCH_FLAGS_KEY_TAP = (1<<4), + TOUCH_FLAGS_KBD = (1<<5), + TOUCH_FLAGS_JOY = (1<<6), + TOUCH_FLAGS_MENU = (1<<7), + TOUCH_FLAGS_INPUT_DEVICE_CHANGE = (1<<16), + TOUCH_FLAGS_CPU_SPEED_DEC = (1<<17), + TOUCH_FLAGS_CPU_SPEED_INC = (1<<18), } interface_touch_event_flags; // handle touch event diff --git a/src/video/gltouchmenu.c b/src/video/gltouchmenu.c index c2110b23..69379a51 100644 --- a/src/video/gltouchmenu.c +++ b/src/video/gltouchmenu.c @@ -186,63 +186,6 @@ static inline void _screen_to_menu(float x, float y, OUTPARM int *col, OUTPARM i //LOG("SCREEN TO MENU : menuX:%d menuXMax:%d menuW:%d keyW:%d ... scrn:(%f,%f)->kybd:(%d,%d)", touchport.topLeftX, touchport.topLeftXMax, touchport.width, keyW, x, y, *col, *row); } -static void _increase_cpu_speed(void) { - cpu_pause(); - - int percent_scale = (int)round(cpu_scale_factor * 100.0); - if (percent_scale >= 100) { - percent_scale += 25; - } else { - percent_scale += 5; - } - cpu_scale_factor = percent_scale/100.0; - - if (cpu_scale_factor > CPU_SCALE_FASTEST) { - cpu_scale_factor = CPU_SCALE_FASTEST; - } - - LOG("native set emulation percentage to %f", cpu_scale_factor); - - if (video_backend->animation_showCPUSpeed) { - video_backend->animation_showCPUSpeed(); - } - - timing_initialize(); - - cpu_resume(); -} - -void _decrease_cpu_speed(void) { - cpu_pause(); - - int percent_scale = (int)round(cpu_scale_factor * 100.0); - if (cpu_scale_factor == CPU_SCALE_FASTEST) { - cpu_scale_factor = CPU_SCALE_FASTEST0; - percent_scale = (int)round(cpu_scale_factor * 100); - } else { - if (percent_scale > 100) { - percent_scale -= 25; - } else { - percent_scale -= 5; - } - } - cpu_scale_factor = percent_scale/100.0; - - if (cpu_scale_factor < CPU_SCALE_SLOWEST) { - cpu_scale_factor = CPU_SCALE_SLOWEST; - } - - LOG("native set emulation percentage to %f", cpu_scale_factor); - - if (video_backend->animation_showCPUSpeed) { - video_backend->animation_showCPUSpeed(); - } - - timing_initialize(); - - cpu_resume(); -} - static inline bool _sprout_menu(float x, float y) { if (! (_is_point_on_left_menu(x, y) || _is_point_on_right_menu(x, y)) ) { @@ -303,12 +246,12 @@ static inline int64_t _tap_menu_item(float x, float y) { case MOUSETEXT_LEFT: LOG("decreasing cpu speed..."); - _decrease_cpu_speed(); + flags |= TOUCH_FLAGS_CPU_SPEED_DEC; break; case MOUSETEXT_RIGHT: LOG("increasing cpu speed..."); - _increase_cpu_speed(); + flags |= TOUCH_FLAGS_CPU_SPEED_INC; break; case MOUSETEXT_CHECKMARK: @@ -327,6 +270,7 @@ static inline int64_t _tap_menu_item(float x, float y) { if (video_backend->animation_showTouchJoystick) { video_backend->animation_showTouchJoystick(); } + flags |= TOUCH_FLAGS_INPUT_DEVICE_CHANGE; _hide_top_left(); break; @@ -340,6 +284,7 @@ static inline int64_t _tap_menu_item(float x, float y) { if (video_backend->animation_showTouchKeyboard) { video_backend->animation_showTouchKeyboard(); } + flags |= TOUCH_FLAGS_INPUT_DEVICE_CHANGE; _hide_top_left(); break;