diff --git a/android/app-android.cpp b/android/app-android.cpp index 9fb4009f0d..9bc0009a77 100644 --- a/android/app-android.cpp +++ b/android/app-android.cpp @@ -53,6 +53,8 @@ static uint32_t pad_buttons_async_set; static uint32_t pad_buttons_async_clear; static float left_joystick_x_async; static float left_joystick_y_async; +static float right_joystick_x_async; +static float right_joystick_y_async; static uint32_t pad_buttons_down; @@ -148,6 +150,11 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_init pad_buttons_async_set = 0; pad_buttons_async_clear = 0; + left_joystick_x_async = 0; + left_joystick_y_async = 0; + right_joystick_x_async = 0; + right_joystick_y_async = 0; + std::string apkPath = GetJavaString(env, japkpath); ILOG("NativeApp::Init: APK path: %s", apkPath.c_str()); VFSRegister("", new ZipAssetReader(apkPath.c_str(), "assets/")); @@ -178,6 +185,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_init use_native_audio = juseNativeAudio; if (use_native_audio) { + ILOG("Using OpenSL audio!"); AndroidAudio_Init(&NativeMix, library_path); } ILOG("NativeApp.init() -- end"); @@ -195,6 +203,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_pause(JNIEnv *, jclas if (use_native_audio) { AndroidAudio_Pause(); } + ILOG("NativeApp.pause() - paused audio"); } extern "C" void Java_com_henrikrydgard_libnative_NativeApp_shutdown(JNIEnv *, jclass) { @@ -294,6 +303,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeRenderer_displayRender(JN } } +// This path is not used if OpenSL ES is available. extern "C" jint Java_com_henrikrydgard_libnative_NativeApp_audioRender(JNIEnv* env, jclass clazz, jshortArray array) { // Too spammy // ILOG("NativeApp.audioRender"); @@ -319,8 +329,7 @@ extern "C" jint Java_com_henrikrydgard_libnative_NativeApp_audioRender(JNIEnv* e extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch (JNIEnv *, jclass, float x, float y, int code, int pointerId) { - ELOG("Touch Enter %i", pointerId); - lock_guard guard(input_state.lock); + // ELOG("Touch Enter %i", pointerId); float scaledX = (int)(x * dp_xscale); // why the (int) cast? float scaledY = (int)(y * dp_yscale); @@ -338,6 +347,10 @@ extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch } else { touch.flags = TOUCH_MOVE; } + NativeTouch(touch); + + + lock_guard guard(input_state.lock); if (pointerId >= MAX_POINTERS) { ELOG("Too many pointers: %i", pointerId); @@ -346,16 +359,16 @@ extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch input_state.pointer_x[pointerId] = scaledX; input_state.pointer_y[pointerId] = scaledY; input_state.mouse_valid = true; - NativeTouch(touch); - ELOG("Touch Exit %i", pointerId); + + // ELOG("Touch Exit %i", pointerId); } -static void AsyncDown(int padbutton) { +static inline void AsyncDown(int padbutton) { pad_buttons_async_set |= padbutton; pad_buttons_async_clear &= ~padbutton; } -static void AsyncUp(int padbutton) { +static inline void AsyncUp(int padbutton) { pad_buttons_async_set &= ~padbutton; pad_buttons_async_clear |= padbutton; } @@ -415,9 +428,19 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_keyUp(JNIEnv *, jclas } extern "C" void Java_com_henrikrydgard_libnative_NativeApp_joystickEvent( - JNIEnv *env, jclass, jfloat x, jfloat y) { - left_joystick_x_async = x; - left_joystick_y_async = y; + JNIEnv *env, jclass, jint stick, jfloat x, jfloat y) { + if (stick == 0) { + left_joystick_x_async = x; + left_joystick_y_async = y; + } else if (stick == 1) { + right_joystick_x_async = x; + right_joystick_y_async = y; + } +} + +extern "C" void Java_com_henrikrydgard_libnative_NativeApp_mouseWheelEvent( + JNIEnv *env, jclass, jint stick, jfloat x, jfloat y) { + // TODO } extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_accelerometer(JNIEnv *, jclass, float x, float y, float z) { diff --git a/android/native-audio-so.cpp b/android/native-audio-so.cpp index 0fba729dc9..d399103c53 100644 --- a/android/native-audio-so.cpp +++ b/android/native-audio-so.cpp @@ -136,6 +136,13 @@ extern "C" bool OpenSLWrap_Init(AndroidAudioCallback cb) { // shut down the native audio system extern "C" void OpenSLWrap_Shutdown() { + SLresult result; + ILOG("OpenSLWrap_Shutdown - stopping playback"); + result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED); + assert(SL_RESULT_SUCCESS == result); + + ILOG("OpenSLWrap_Shutdown - deleting player object"); + if (bqPlayerObject != NULL) { (*bqPlayerObject)->Destroy(bqPlayerObject); bqPlayerObject = NULL; @@ -144,13 +151,21 @@ extern "C" void OpenSLWrap_Shutdown() { bqPlayerMuteSolo = NULL; bqPlayerVolume = NULL; } + + ILOG("OpenSLWrap_Shutdown - deleting mix object"); + if (outputMixObject != NULL) { (*outputMixObject)->Destroy(outputMixObject); outputMixObject = NULL; } + + ILOG("OpenSLWrap_Shutdown - deleting engine object"); + if (engineObject != NULL) { (*engineObject)->Destroy(engineObject); engineObject = NULL; engineEngine = NULL; } -} + ILOG("OpenSLWrap_Shutdown - finished"); +} + diff --git a/android/native_audio.cpp b/android/native_audio.cpp index 871a2ca67a..cb92e1ee3f 100644 --- a/android/native_audio.cpp +++ b/android/native_audio.cpp @@ -5,54 +5,84 @@ #include "android/native_audio.h" #include "android/native-audio-so.h" -static void *so; -static AndroidAudioCallback s_cb; -static OpenSLWrap_Init_T init_func; -static OpenSLWrap_Shutdown_T shutdown_func; -static bool playing; +struct AudioState { + void *so; + AndroidAudioCallback s_cb; + OpenSLWrap_Init_T init_func; + OpenSLWrap_Shutdown_T shutdown_func; + bool playing; +}; + +static AudioState *state = 0; bool AndroidAudio_Init(AndroidAudioCallback cb, std::string libraryDir) { - s_cb = cb; - playing = false; + if (state != 0) { + ELOG("Audio state already exists"); + return false; + } + ILOG("Loading native audio library..."); std::string so_filename = libraryDir + "/libnative_audio.so"; - so = dlopen(so_filename.c_str(), RTLD_LAZY); - if (!so) { + void *the_so = dlopen(so_filename.c_str(), RTLD_LAZY); + if (!the_so) { ELOG("Failed to find native audio library: %i: %s ", errno, dlerror()); return false; } - init_func = (OpenSLWrap_Init_T)dlsym(so, "OpenSLWrap_Init"); - shutdown_func = (OpenSLWrap_Shutdown_T)dlsym(so, "OpenSLWrap_Shutdown"); + + state = new AudioState(); + state->so = the_so; + state->s_cb = cb; + state->playing = false; + state->init_func = (OpenSLWrap_Init_T)dlsym(state->so, "OpenSLWrap_Init"); + state->shutdown_func = (OpenSLWrap_Shutdown_T)dlsym(state->so, "OpenSLWrap_Shutdown"); + ILOG("OpenSLWrap init_func: %p shutdown_func: %p", (void *)state->init_func, (void *)state->shutdown_func); + return true; } bool AndroidAudio_Resume() { - if (!playing) { + if (!state) { + ELOG("Audio was shutdown, cannot resume!"); + return false; + } + if (!state->playing) { ILOG("Calling OpenSLWrap_Init_T..."); - bool init_retval = init_func(s_cb); + bool init_retval = state->init_func(state->s_cb); ILOG("Returned from OpenSLWrap_Init_T"); - playing = true; + state->playing = true; return init_retval; } return false; } bool AndroidAudio_Pause() { - if (playing) { + if (!state) { + ELOG("Audio was shutdown, cannot pause!"); + return false; + } + if (state->playing) { ILOG("Calling OpenSLWrap_Shutdown_T..."); - shutdown_func(); - playing = false; + state->shutdown_func(); + ILOG("Returned from OpenSLWrap_Shutdown_T ..."); + state->playing = false; return true; } return false; } void AndroidAudio_Shutdown() { - if (playing) { + if (!state) { + ELOG("Audio already shutdown!"); + return; + } + if (state->playing) { ELOG("Should not shut down when playing! Something is wrong!"); } - ILOG("Returned from OpenSLWrap_Shutdown_T"); - dlclose(so); - so = 0; + ILOG("dlclose"); + dlclose(state->so); + ILOG("Returned from dlclose"); + state->so = 0; + delete state; + state = 0; ILOG("OpenSLWrap completely unloaded."); } diff --git a/android/src/com/henrikrydgard/libnative/NativeActivity.java b/android/src/com/henrikrydgard/libnative/NativeActivity.java index 2f2edb526d..29dd063bef 100644 --- a/android/src/com/henrikrydgard/libnative/NativeActivity.java +++ b/android/src/com/henrikrydgard/libnative/NativeActivity.java @@ -207,8 +207,11 @@ public class NativeActivity extends Activity { if (audioPlayer != null) { audioPlayer.stop(); } + Log.i(TAG, "nativeapp pause"); NativeApp.pause(); + Log.i(TAG, "gl pause"); mGLSurfaceView.onPause(); + Log.i(TAG, "onPause returning"); } @Override @@ -297,9 +300,19 @@ public class NativeActivity extends Activity { if (event.getSource() == InputDevice.SOURCE_JOYSTICK) { float leftJoystickX = event.getAxisValue(MotionEvent.AXIS_X) * 1f; float leftJoystickY = event.getAxisValue(MotionEvent.AXIS_Y) * -1f; - NativeApp.joystickEvent(leftJoystickX, leftJoystickY); + NativeApp.joystickEvent(0, leftJoystickX, leftJoystickY); } - return false; + if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { + switch (event.getAction()) { + case MotionEvent.ACTION_HOVER_MOVE: + // process the mouse hover movement... + return true; + case MotionEvent.ACTION_SCROLL: + NativeApp.mouseWheelEvent(event.getX(), event.getY()); + return true; + } + } + return super.onGenericMotionEvent(event); } @SuppressLint("NewApi") diff --git a/android/src/com/henrikrydgard/libnative/NativeApp.java b/android/src/com/henrikrydgard/libnative/NativeApp.java index 79fa414ff5..8b7f0e86e3 100644 --- a/android/src/com/henrikrydgard/libnative/NativeApp.java +++ b/android/src/com/henrikrydgard/libnative/NativeApp.java @@ -18,7 +18,8 @@ public class NativeApp { public static native void keyUp(int key); - public static native void joystickEvent(float x, float y); + public static native void joystickEvent(int stick, float x, float y); + public static native void mouseWheelEvent(float x, float y); // will only be called between init() and shutdown() public static native int audioRender(short[] buffer); diff --git a/file/zip_read.cpp b/file/zip_read.cpp index 21b45c193d..02add6f0f8 100644 --- a/file/zip_read.cpp +++ b/file/zip_read.cpp @@ -96,7 +96,7 @@ bool ZipAssetReader::GetFileListing(const char *path, std::vector *lis const char* name = zip_get_name(zip_file_, i, 0); if (!name) continue; - ILOG("Comparing %s %s %i", name, path, pathlen); + // ILOG("Comparing %s %s %i", name, path, pathlen); if (!memcmp(name, path, pathlen)) { // The prefix is right. Let's see if this is a file or path. char *slashPos = strchr(name + pathlen + 1, '/'); diff --git a/ui/view.h b/ui/view.h index 02b017342c..9bee17ace5 100644 --- a/ui/view.h +++ b/ui/view.h @@ -171,14 +171,19 @@ class Event { public: Event() : triggered_(false) {} - void Add(std::function func); - // Call this from input thread or whatever, it doesn't matter void Trigger(EventParams &e); // Call this from UI thread void Update(); + template + void Handle(T *thiz, EventReturn (T::* theCallback)(EventParams &e)) { + Add(std::bind(theCallback, thiz, placeholder::_1)); + } + private: + void Add(std::function func); + recursive_mutex mutex_; std::vector handlers_; bool triggered_; @@ -421,7 +426,7 @@ class CheckBox : public ClickableItem { public: CheckBox(bool *toggle, const std::string &text, const std::string &smallText = "", LayoutParams *layoutParams = 0) : ClickableItem(layoutParams), text_(text), smallText_(smallText) { - OnClick.Add(std::bind(&CheckBox::OnClicked, this, placeholder::_1)); + OnClick.Handle(this, &CheckBox::OnClicked); } virtual void Draw(UIContext &dc);