From 658bec620577014a4be89e00eaf52cd11e36c195 Mon Sep 17 00:00:00 2001 From: pelya Date: Fri, 10 Dec 2010 16:42:55 +0000 Subject: [PATCH] SDL keycodes now can be redefined by user --- build.sh | 2 +- project/java/Globals.java | 3 +- project/java/Keycodes.java | 2 + project/java/MainActivity.java | 4 +- project/java/Settings.java | 45 +++++++++++++------ .../fheroes2/AndroidAppSettings.cfg | 4 +- project/jni/application/src | 2 +- .../src/video/android/SDL_androidinput.c | 32 +++++++++---- 8 files changed, 63 insertions(+), 31 deletions(-) diff --git a/build.sh b/build.sh index ae7558add..9901e9b3c 100755 --- a/build.sh +++ b/build.sh @@ -15,5 +15,5 @@ if ( grep "package $AppFullName;" project/src/Globals.java > /dev/null && [ "`re touch project/src/Globals.java fi -cd project && env PATH=$NDKBUILDPATH nice -n10 ndk-build -j2 V=1 && ant `test -n "$1" && echo release || echo debug` && test -z "$1" && cd bin && adb install -r DemoActivity-debug.apk +cd project && env PATH=$NDKBUILDPATH nice -n10 ndk-build -j4 V=1 && ant `test -n "$1" && echo release || echo debug` && test -z "$1" && cd bin && adb install -r DemoActivity-debug.apk diff --git a/project/java/Globals.java b/project/java/Globals.java index d6460a079..001e37081 100644 --- a/project/java/Globals.java +++ b/project/java/Globals.java @@ -81,8 +81,7 @@ class Globals { public static boolean KeepAspectRatio = false; public static int ClickScreenPressure = 0; public static int ClickScreenTouchspotSize = 0; - public static Vector RemapHwKeycodeJava = new Vector(); - public static Vector RemapHwKeycodeSdl = new Vector(); + public static int RemapHwKeycode[] = new int[SDL_Keys.JAVA_KEYCODE_LAST]; } class LoadLibrary { diff --git a/project/java/Keycodes.java b/project/java/Keycodes.java index 43d8964a0..54cdbd7eb 100644 --- a/project/java/Keycodes.java +++ b/project/java/Keycodes.java @@ -495,6 +495,8 @@ class SDL_Keys { public static String [] names = null; public static Integer [] values = null; + + static final JAVA_KEYCODE_LAST = android.view.KeyEvent.KEYCODE_MUTE; // = 91 static { diff --git a/project/java/MainActivity.java b/project/java/MainActivity.java index 98ff21911..7491350e4 100644 --- a/project/java/MainActivity.java +++ b/project/java/MainActivity.java @@ -34,7 +34,7 @@ public class MainActivity extends Activity { // fullscreen mode requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); + WindowManager.LayoutParams.FLAG_FULLSCREEN); if(Globals.InhibitSuspend) getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); @@ -77,7 +77,7 @@ public class MainActivity extends Activity { if(mAudioThread == null) // Starting from background (should not happen) { - System.out.println("libSDL: Loading libraries"); + System.out.println("libSDL: Loading libraries"); mLoadLibraryStub = new LoadLibrary(); mAudioThread = new AudioThread(this); System.out.println("libSDL: Loading settings"); diff --git a/project/java/Settings.java b/project/java/Settings.java index 9b2f30f31..06ac81c73 100644 --- a/project/java/Settings.java +++ b/project/java/Settings.java @@ -59,6 +59,11 @@ class Settings out.writeBoolean(Globals.KeepAspectRatio); out.writeInt(Globals.MoveMouseWithJoystickSpeed); out.writeInt(Globals.MoveMouseWithJoystickAccel); + out.writeInt(SDL_Keys.JAVA_KEYCODE_LAST); + for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ ) + { + out.writeInt(RemapHwKeycode[i]); + } out.close(); settingsLoaded = true; @@ -75,6 +80,11 @@ class Settings return; } System.out.println("libSDL: Settings.Load(): enter"); + nativeInitKeymap(); + for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ ) + { + RemapHwKeycode[i] = nativeGetKeymapKey(i); + } try { ObjectInputStream settingsFile = new ObjectInputStream(new FileInputStream( p.getFilesDir().getAbsolutePath() + "/" + SettingsFileName )); Globals.DownloadToSdcard = settingsFile.readBoolean(); @@ -101,6 +111,12 @@ class Settings Globals.KeepAspectRatio = settingsFile.readBoolean(); Globals.MoveMouseWithJoystickSpeed = settingsFile.readInt(); Globals.MoveMouseWithJoystickAccel = settingsFile.readInt(); + if( settingsFile.readInt() != SDL_Keys.JAVA_KEYCODE_LAST ) + throw new IOException(); + for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ ) + { + RemapHwKeycode[i] = settingsFile.readInt(); + } settingsLoaded = true; @@ -845,27 +861,20 @@ class Settings public void onKeyEvent(final int keyCode) { p.keyRemapTool = null; - int keyIndex = -1; - for( int i = 0; i < Globals.RemapHwKeycodeJava.size(); i++ ) - { - if( Globals.RemapHwKeycodeJava.get(i) == keyCode ) - keyIndex = i; - } - if( keyIndex == -1 ) - { - keyIndex = Globals.RemapHwKeycodeJava.size(); - Globals.RemapHwKeycodeJava.add(keyCode); - Globals.RemapHwKeycodeSdl.add(0); // SDLK_UNKNOWN - } + int keyIndex = keyCode; + if( keyIndex < 0 ) + keyIndex = 0; + if( keyIndex > SDL_Keys.JAVA_KEYCODE_LAST ) + keyIndex = 0; final int KeyIndexFinal = keyIndex; AlertDialog.Builder builder = new AlertDialog.Builder(p); builder.setTitle(R.string.remap_hwkeys_select); - builder.setSingleChoiceItems(SDL_Keys.names, Globals.RemapHwKeycodeSdl.get(keyIndex), new DialogInterface.OnClickListener() + builder.setSingleChoiceItems(SDL_Keys.names, Globals.RemapHwKeycode[keyIndex], new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { - Globals.RemapHwKeycodeSdl.set(KeyIndexFinal, item); + Globals.RemapHwKeycode[KeyIndexFinal] = item; dialog.dismiss(); showConfigMainMenu(p); @@ -910,6 +919,11 @@ class Settings Globals.AppNeedsTextInput ? 1 : 0 ); } SetupTouchscreenKeyboardGraphics(p); + for( int i = 0; i < SDL_Keys.JAVA_KEYCODE_LAST; i++ ) + { + nativeSetKeymapKey(i, RemapHwKeycode[i]); + } + String lang = new String(Locale.getDefault().getLanguage()); if( Locale.getDefault().getCountry().length() > 0 ) lang = lang + "_" + Locale.getDefault().getCountry(); @@ -960,6 +974,9 @@ class Settings private static native void nativeSetTouchscreenKeyboardUsed(); private static native void nativeSetupScreenKeyboard(int size, int theme, int nbuttons, int nbuttonsAutoFire, int showArrows, int showTextInput); private static native void nativeSetupScreenKeyboardButtons(byte[] img); + private static native void nativeInitKeymap(); + private static native int nativeGetKeymapKey(int key); + private static native void nativeSetKeymapKey(int javakey, int key); public static native void nativeSetEnv(final String name, final String value); } diff --git a/project/jni/application/fheroes2/AndroidAppSettings.cfg b/project/jni/application/fheroes2/AndroidAppSettings.cfg index 942cac1d0..93f2a575b 100644 --- a/project/jni/application/fheroes2/AndroidAppSettings.cfg +++ b/project/jni/application/fheroes2/AndroidAppSettings.cfg @@ -21,8 +21,8 @@ RedefinedKeys="LCTRL m t h e" AppTouchscreenKeyboardKeysAmount=0 AppTouchscreenKeyboardKeysAmountAutoFire=0 MultiABI=n -AppVersionCode=211405 -AppVersionName="2114.05" +AppVersionCode=212506 +AppVersionName="2125.06" CompiledLibraries="sdl_net sdl_mixer sdl_image sdl_ttf png intl" CustomBuildScript=n AppCflags='-finline-functions -O2 -DWITH_ZLIB -DWITH_MIXER -DWITH_XML -DWITH_IMAGE -DWITH_TTF' diff --git a/project/jni/application/src b/project/jni/application/src index ee1bae6e3..59d41f41e 120000 --- a/project/jni/application/src +++ b/project/jni/application/src @@ -1 +1 @@ -glxgears \ No newline at end of file +fheroes2 \ No newline at end of file diff --git a/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c b/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c index fbe8b9a6a..77ddd9825 100644 --- a/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c +++ b/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c @@ -1202,12 +1202,8 @@ void SDL_ANDROID_processMoveMouseWithKeyboard() SDL_ANDROID_MainThreadPushMouseMotion(moveMouseWithKbX, moveMouseWithKbY); }; - void ANDROID_InitOSKeymap() { - int i; - SDLKey * keymap = SDL_android_keymap; - #if (SDL_VERSION_ATLEAST(1,3,0)) SDLKey defaultKeymap[SDL_NUM_SCANCODES]; SDL_GetDefaultKeymap(defaultKeymap); @@ -1229,8 +1225,30 @@ void ANDROID_InitOSKeymap() touch.id = 0; SDL_AddTouch(&touch, "Android touch screen"); - #endif +} + +JNIEXPORT jint JNICALL +JAVA_EXPORT_NAME(Settings_nativeGetKeymapKey) ( JNIEnv* env, jobject thiz, jint code) +{ + if( code < 0 || code > KEYCODE_LAST ) + return SDL_KEY(UNKNOWN); + return SDL_android_keymap[code]; +} + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(Settings_nativeSetKeymapKey) ( JNIEnv* env, jobject thiz, jint javakey, jint key) +{ + if( javakey < 0 || javakey > KEYCODE_LAST ) + return; + SDL_android_keymap[javakey] = key; +} + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(Settings_nativeInitKeymap) ( JNIEnv* env, jobject thiz ) +{ + int i; + SDLKey * keymap = SDL_android_keymap; // TODO: keys are mapped rather randomly @@ -1241,16 +1259,12 @@ void ANDROID_InitOSKeymap() keymap[KEYCODE_BACK] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_5)); - // TODO: make this configurable keymap[KEYCODE_MENU] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_4)); keymap[KEYCODE_DPAD_CENTER] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_1)); keymap[KEYCODE_SEARCH] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_9)); keymap[KEYCODE_CALL] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_10)); - //keymap[KEYCODE_CALL] = SDL_KEY(RCTRL); - //keymap[KEYCODE_DPAD_CENTER] = SDL_KEY(LALT); - keymap[KEYCODE_VOLUME_UP] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_2)); keymap[KEYCODE_VOLUME_DOWN] = SDL_KEY(SDL_KEY_VAL(SDL_ANDROID_KEYCODE_3));