Add SDL2 to android

This commit is contained in:
Benau 2020-04-21 15:28:02 +08:00
parent 5eb7b023b3
commit bae2262bd9
9 changed files with 191 additions and 218 deletions

4
.gitignore vendored
View File

@ -64,6 +64,8 @@ android/libs
android/obj
android/res
android/.gradle
android/src/main/java/HID*
android/src/main/java/SDL*
android-*
*.apk
*.keystore
@ -77,6 +79,6 @@ lib/openal
lib/openssl
lib/fribidi
lib/harfbuzz
lib/sdl2
.DS_Store
supertuxkart/

View File

@ -176,7 +176,6 @@ LOCAL_MODULE := irrlicht
LOCAL_PATH := .
LOCAL_CPP_FEATURES += rtti
LOCAL_SRC_FILES := $(wildcard ../lib/irrlicht/source/Irrlicht/*.cpp) \
$(wildcard ../lib/irrlicht/source/Irrlicht/Android/*.cpp) \
../lib/irrlicht/source/Irrlicht/stk_android_native_app_glue.c
LOCAL_CFLAGS := -I../lib/irrlicht/source/Irrlicht/ \
-I../lib/irrlicht/include/ \
@ -184,12 +183,58 @@ LOCAL_CFLAGS := -I../lib/irrlicht/source/Irrlicht/ \
-Iobj/jpeglib/ \
-Iobj/libpng/ \
-Iobj/zlib/ \
-I../lib/sdl2/include/ \
-DANDROID_PACKAGE_CALLBACK_NAME=$(PACKAGE_CALLBACK_NAME)
LOCAL_CPPFLAGS := -std=gnu++0x
LOCAL_STATIC_LIBRARIES := jpeglib png zlib
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
# SDL2
LOCAL_MODULE := SDL2
LOCAL_PATH := .
LOCAL_CPP_FEATURES += rtti
LOCAL_SRC_FILES := $(wildcard ../lib/sdl2/src/*.c) \
$(wildcard ../lib/sdl2/src/audio/*.c) \
$(wildcard ../lib/sdl2/src/audio/android/*.c) \
$(wildcard ../lib/sdl2/src/audio/dummy/*.c) \
$(wildcard ../lib/sdl2/src/audio/openslES/*.c) \
$(wildcard ../lib/sdl2/src/core/android/*.c) \
$(wildcard ../lib/sdl2/src/cpuinfo/*.c) \
$(wildcard ../lib/sdl2/src/dynapi/*.c) \
$(wildcard ../lib/sdl2/src/events/*.c) \
$(wildcard ../lib/sdl2/src/file/*.c) \
$(wildcard ../lib/sdl2/src/haptic/*.c) \
$(wildcard ../lib/sdl2/src/haptic/android/*.c) \
$(wildcard ../lib/sdl2/src/joystick/*.c) \
$(wildcard ../lib/sdl2/src/joystick/android/*.c) \
$(wildcard ../lib/sdl2/src/joystick/hidapi/*.c) \
$(wildcard ../lib/sdl2/src/joystick/virtual/*.c) \
$(wildcard ../lib/sdl2/src/loadso/dlopen/*.c) \
$(wildcard ../lib/sdl2/src/power/*.c) \
$(wildcard ../lib/sdl2/src/power/android/*.c) \
$(wildcard ../lib/sdl2/src/filesystem/android/*.c) \
$(wildcard ../lib/sdl2/src/sensor/*.c) \
$(wildcard ../lib/sdl2/src/sensor/android/*.c) \
$(wildcard ../lib/sdl2/src/render/*.c) \
$(wildcard ../lib/sdl2/src/render/*/*.c) \
$(wildcard ../lib/sdl2/src/stdlib/*.c) \
$(wildcard ../lib/sdl2/src/thread/*.c) \
$(wildcard ../lib/sdl2/src/thread/pthread/*.c) \
$(wildcard ../lib/sdl2/src/timer/*.c) \
$(wildcard ../lib/sdl2/src/timer/unix/*.c) \
$(wildcard ../lib/sdl2/src/video/*.c) \
$(wildcard ../lib/sdl2/src/video/android/*.c) \
$(wildcard ../lib/sdl2/src/video/yuv2rgb/*.c) \
../lib/sdl2/src/atomic/SDL_atomic.c.arm \
../lib/sdl2/src/atomic/SDL_spinlock.c.arm \
../lib/sdl2/src/hidapi/android/hid.cpp
LOCAL_CFLAGS := -I../lib/sdl2/include/ -DGL_GLEXT_PROTOTYPES
LOCAL_CPPFLAGS := -std=gnu++0x
LOCAL_STATIC_LIBRARIES := cpufeatures
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
# STK
LOCAL_MODULE := main
@ -198,7 +243,7 @@ LOCAL_CPP_FEATURES += rtti exceptions
LOCAL_SRC_FILES := $(wildcard ../src/*.cpp) \
$(wildcard ../src/*/*.cpp) \
$(wildcard ../src/*/*/*.cpp)
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv3 -lOpenSLES
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lGLESv3 -lOpenSLES -ldl
LOCAL_CFLAGS := -I../lib/angelscript/include \
-I../lib/bullet/src \
-I../lib/libraqm \
@ -208,6 +253,7 @@ LOCAL_CFLAGS := -I../lib/angelscript/include \
-I../lib/irrlicht/source/Irrlicht \
-I../lib/graphics_utils \
-I../lib/mcpp \
-I../lib/sdl2/include \
-I../src \
-Iobj/curl/include \
-Iobj/fribidi/include \
@ -229,9 +275,10 @@ LOCAL_CFLAGS := -I../lib/angelscript/include \
-DANDROID_PACKAGE_CALLBACK_NAME=$(PACKAGE_CALLBACK_NAME)
LOCAL_CPPFLAGS := -std=gnu++0x
LOCAL_STATIC_LIBRARIES := irrlicht bullet enet ifaddrs angelscript mcpp \
LOCAL_STATIC_LIBRARIES := irrlicht bullet enet ifaddrs angelscript mcpp SDL2 \
vorbisfile vorbis ogg openal curl libssl libcrypto \
c++_static raqm fribidi harfbuzz freetype graphics_utils
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
$(call import-module, android/cpufeatures)

View File

@ -38,8 +38,12 @@
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.software.input_methods" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.gamepad" android:required="false"/>
<uses-feature android:name="android.hardware.gamepad" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>

View File

@ -559,6 +559,20 @@ sed -i "s/versionName=\".*\"/versionName=\"$PROJECT_VERSION\"/g" \
sed -i "s/versionCode=\".*\"/versionCode=\"$PROJECT_CODE\"/g" \
"$DIRNAME/AndroidManifest.xml"
cp -f "$DIRNAME/../lib/sdl2/android-project/app/src/main/java/org/libsdl/app/HIDDevice.java" \
"$DIRNAME/src/main/java/"
cp -f "$DIRNAME/../lib/sdl2/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java" \
"$DIRNAME/src/main/java/"
cp -f "$DIRNAME/../lib/sdl2/android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java" \
"$DIRNAME/src/main/java/"
cp -f "$DIRNAME/../lib/sdl2/android-project/app/src/main/java/org/libsdl/app/HIDDeviceUSB.java" \
"$DIRNAME/src/main/java/"
cp -f "$DIRNAME/../lib/sdl2/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java" \
"$DIRNAME/src/main/java/"
sed -i "s/import org.supertuxkart.*.SuperTuxKartActivity;/import $PACKAGE_NAME.SuperTuxKartActivity;/g" \
"$DIRNAME/src/main/java/SDLControllerManager.java"
cp "banner.png" "$DIRNAME/res/drawable/banner.png"
cp "$APP_ICON" "$DIRNAME/res/drawable/icon.png"
convert -scale 48x48 "$APP_ICON" "$DIRNAME/res/drawable-mdpi/icon.png"

View File

@ -2,6 +2,9 @@ package org.supertuxkart.stk_dbg;
import org.supertuxkart.stk_dbg.STKEditText;
import org.libsdl.app.SDLControllerManager;
import org.libsdl.app.HIDDeviceManager;
import android.app.NativeActivity;
import android.content.ActivityNotFoundException;
import android.content.Context;
@ -16,6 +19,8 @@ import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.View;
import android.widget.FrameLayout;
@ -30,10 +35,14 @@ import org.minidns.record.TXT;
public class SuperTuxKartActivity extends NativeActivity
{
private STKEditText m_stk_edittext;
private static Context m_context;
private static HIDDeviceManager m_hid_device_manager;
// ------------------------------------------------------------------------
private native void saveKeyboardHeight(int height);
// ------------------------------------------------------------------------
private native void startSTK();
// ------------------------------------------------------------------------
private void hideKeyboardNative(final boolean clear_text)
{
if (m_stk_edittext == null)
@ -95,6 +104,7 @@ public class SuperTuxKartActivity extends NativeActivity
{
super.onCreate(instance);
System.loadLibrary("main");
m_context = this;
m_stk_edittext = null;
final View root = getWindow().getDecorView().findViewById(
@ -124,6 +134,12 @@ public class SuperTuxKartActivity extends NativeActivity
hideNavBar(decor_view);
}
});
SDLControllerManager.nativeSetupJNI();
SDLControllerManager.initialize();
m_hid_device_manager = HIDDeviceManager.acquire(this);
// We only start stk main thread after JNI_OnLoad (for initializing the
// java environment)
startSTK();
}
// ------------------------------------------------------------------------
@Override
@ -131,7 +147,29 @@ public class SuperTuxKartActivity extends NativeActivity
{
super.onPause();
hideKeyboardNative(false/*clear_text*/);
if (m_hid_device_manager != null)
m_hid_device_manager.setFrozen(true);
}
// ------------------------------------------------------------------------
@Override
public void onResume()
{
super.onResume();
if (m_hid_device_manager != null)
m_hid_device_manager.setFrozen(false);
}
// ------------------------------------------------------------------------
@Override
protected void onDestroy()
{
if (m_hid_device_manager != null)
{
HIDDeviceManager.release(m_hid_device_manager);
m_hid_device_manager = null;
}
super.onDestroy();
}
// ------------------------------------------------------------------------
@Override
public void onWindowFocusChanged(boolean has_focus)
@ -144,6 +182,31 @@ public class SuperTuxKartActivity extends NativeActivity
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
int device_id = event.getDeviceId();
int source = event.getSource();
// Dispatch the different events depending on where they come from
// Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
// So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
//
// Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
// SOURCE_JOYSTICK, while its key events arrive from the keyboard source
// So, retrieve the device itself and check all of its sources
if (SDLControllerManager.isDeviceSDLJoystick(device_id))
{
// Note that we process events with specific key codes here
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
if (SDLControllerManager.onNativePadDown(device_id, event.getKeyCode()) == 0)
return true;
}
else if (event.getAction() == KeyEvent.ACTION_UP)
{
if (SDLControllerManager.onNativePadUp(device_id, event.getKeyCode()) == 0)
return true;
}
}
// Called when user change cursor / select all text in native android
// keyboard
boolean ret = super.dispatchKeyEvent(event);
@ -152,6 +215,14 @@ public class SuperTuxKartActivity extends NativeActivity
return ret;
}
// ------------------------------------------------------------------------
@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev)
{
if (SDLControllerManager.useGenericMotionListener(ev))
return true;
return super.dispatchGenericMotionEvent(ev);
}
// ------------------------------------------------------------------------
public void showKeyboard(final int type)
{
final Context context = this;
@ -272,4 +343,6 @@ public class SuperTuxKartActivity extends NativeActivity
return getResources().getConfiguration()
.keyboard == Configuration.KEYBOARD_QWERTY;
}
// ------------------------------------------------------------------------
public static Context getContext() { return m_context; }
}

View File

@ -18,6 +18,8 @@
#include "MobileCursorControl.h"
#include "../../../../src/utils/utf8/unchecked.h"
#include <SDL.h>
// Call when android keyboard is opened or close, and save its height for
// moving screen
std::atomic<int> g_keyboard_height(0);
@ -99,22 +101,7 @@ CIrrDeviceAndroid::CIrrDeviceAndroid(const SIrrlichtCreationParameters& param)
if (Android != NULL)
{
jint status = Android->activity->vm->GetEnv((void**)&m_jni_env, JNI_VERSION_1_6);
if (status == JNI_EDETACHED)
{
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = "NativeThread";
args.group = NULL;
status = Android->activity->vm->AttachCurrentThread(&m_jni_env, &args);
}
if (status != JNI_OK)
{
os::Printer::log("Failed to attach jni thread.", ELL_DEBUG);
return;
}
m_jni_env = (JNIEnv*)SDL_AndroidGetJNIEnv();
Android->userData = this;
Android->onAppCmd = handleAndroidCommand;
Android->onAppCmdDirect = NULL;
@ -178,7 +165,6 @@ CIrrDeviceAndroid::~CIrrDeviceAndroid()
Android->userData = NULL;
Android->onAppCmd = NULL;
Android->onInputEvent = NULL;
Android->activity->vm->DetachCurrentThread();
}
}
@ -529,35 +515,31 @@ s32 CIrrDeviceAndroid::handleInput(android_app* app, AInputEvent* androidEvent)
{
CIrrDeviceAndroid* device = (CIrrDeviceAndroid*)app->userData;
assert(device != NULL);
s32 status = 0;
// SDL will handle it
int32_t source = AInputEvent_getSource(androidEvent);
int32_t type = AInputEvent_getType(androidEvent);
if (source == AINPUT_SOURCE_GAMEPAD ||
source == AINPUT_SOURCE_JOYSTICK ||
source == AINPUT_SOURCE_DPAD)
return 0;
s32 status = 0;
int32_t type = AInputEvent_getType(androidEvent);
switch (type)
{
status = device->handleGamepad(androidEvent);
case AINPUT_EVENT_TYPE_MOTION:
{
status = device->handleTouch(androidEvent);
break;
}
else
case AINPUT_EVENT_TYPE_KEY:
{
switch (type)
{
case AINPUT_EVENT_TYPE_MOTION:
{
status = device->handleTouch(androidEvent);
break;
}
case AINPUT_EVENT_TYPE_KEY:
{
status = device->handleKeyboard(androidEvent);
break;
}
default:
break;
}
status = device->handleKeyboard(androidEvent);
break;
}
default:
break;
}
return status;
@ -747,144 +729,6 @@ s32 CIrrDeviceAndroid::handleKeyboard(AInputEvent* androidEvent)
return status;
}
s32 CIrrDeviceAndroid::handleGamepad(AInputEvent* androidEvent)
{
s32 status = 0;
int32_t type = AInputEvent_getType(androidEvent);
switch (type)
{
case AINPUT_EVENT_TYPE_MOTION:
{
float axis_x = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_HAT_X, 0);
if (axis_x == 0)
{
axis_x = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_X, 0);
}
if (axis_x == 0)
{
axis_x = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_Z, 0);
}
float axis_y = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_HAT_Y, 0);
if (axis_y == 0)
{
axis_y = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_Y, 0);
}
if (axis_y == 0)
{
axis_y = AMotionEvent_getAxisValue(androidEvent,
AMOTION_EVENT_AXIS_RZ, 0);
}
SEvent event;
event.EventType = EET_KEY_INPUT_EVENT;
event.KeyInput.Char = 0;
event.KeyInput.Shift = false;
event.KeyInput.Control = false;
event.KeyInput.SystemKeyCode = 0;
float deadzone = 0.3f;
axis_x = axis_x > deadzone || axis_x < -deadzone ? axis_x : 0;
axis_y = axis_y > deadzone || axis_y < -deadzone ? axis_y : 0;
if (axis_x != GamepadAxisX)
{
if (GamepadAxisX != 0)
{
event.KeyInput.PressedDown = false;
event.KeyInput.Key = GamepadAxisX < 0 ? IRR_KEY_BUTTON_LEFT
: IRR_KEY_BUTTON_RIGHT;
postEventFromUser(event);
}
if (axis_x != 0)
{
event.KeyInput.PressedDown = true;
event.KeyInput.Key = axis_x < 0 ? IRR_KEY_BUTTON_LEFT
: IRR_KEY_BUTTON_RIGHT;
postEventFromUser(event);
}
GamepadAxisX = axis_x;
}
if (axis_y != GamepadAxisY)
{
if (GamepadAxisY != 0)
{
event.KeyInput.PressedDown = false;
event.KeyInput.Key = GamepadAxisY < 0 ? IRR_KEY_BUTTON_UP
: IRR_KEY_BUTTON_DOWN;
postEventFromUser(event);
}
if (axis_y != 0)
{
event.KeyInput.PressedDown = true;
event.KeyInput.Key = axis_y < 0 ? IRR_KEY_BUTTON_UP
: IRR_KEY_BUTTON_DOWN;
postEventFromUser(event);
}
GamepadAxisY = axis_y;
}
status = 1;
break;
}
case AINPUT_EVENT_TYPE_KEY:
{
bool ignore = false;
int32_t keyCode = AKeyEvent_getKeyCode(androidEvent);
int32_t keyAction = AKeyEvent_getAction(androidEvent);
int32_t keyRepeat = AKeyEvent_getRepeatCount(androidEvent);
int32_t scanCode = AKeyEvent_getScanCode(androidEvent);
if (keyRepeat == 0)
{
bool ignore_event = false;
SEvent event;
event.EventType = EET_KEY_INPUT_EVENT;
event.KeyInput.Char = 0;
event.KeyInput.PressedDown = (keyAction == AKEY_EVENT_ACTION_DOWN);
event.KeyInput.Shift = false;
event.KeyInput.Control = false;
event.KeyInput.SystemKeyCode = (u32)keyCode;
event.KeyInput.Key = KeyMap[keyCode];
if (event.KeyInput.Key == 0)
{
event.KeyInput.Key = (EKEY_CODE)((int)IRR_KEY_CODES_COUNT + scanCode);
}
postEventFromUser(event);
}
status = 1;
break;
}
default:
break;
}
return status;
}
video::SExposedVideoData& CIrrDeviceAndroid::getExposedVideoData()
{
return ExposedVideoData;

View File

@ -146,7 +146,6 @@ namespace irr
s32 handleTouch(AInputEvent* androidEvent);
s32 handleKeyboard(AInputEvent* androidEvent);
s32 handleGamepad(AInputEvent* androidEvent);
};
} // end namespace irr

View File

@ -240,7 +240,7 @@ static void* android_app_entry(void* param) {
// --------------------------------------------------------------------
// Native activity interaction (called from main thread)
// --------------------------------------------------------------------
struct android_app* g_android_app;
static struct android_app* android_app_create(ANativeActivity* activity,
void* savedState, size_t savedStateSize) {
struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
@ -263,19 +263,7 @@ static struct android_app* android_app_create(ANativeActivity* activity,
}
android_app->msgread = msgpipe[0];
android_app->msgwrite = msgpipe[1];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
// Wait for thread to start.
pthread_mutex_lock(&android_app->mutex);
while (!android_app->running) {
pthread_cond_wait(&android_app->cond, &android_app->mutex);
}
pthread_mutex_unlock(&android_app->mutex);
g_android_app = android_app;
return android_app;
}
@ -463,3 +451,25 @@ void ANativeActivity_onCreate(ANativeActivity* activity,
activity->instance = android_app_create(activity, savedState, savedStateSize);
}
#define MAKE_ANDROID_START_STK_CALLBACK(x) JNIEXPORT void JNICALL Java_ ## x##_SuperTuxKartActivity_startSTK(JNIEnv* env, jobject this_obj)
#define ANDROID_START_STK_CALLBACK(PKG_NAME) MAKE_ANDROID_START_STK_CALLBACK(PKG_NAME)
ANDROID_START_STK_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
{
// This will be called after ANativeActivity_onCreate and loadLibrary
// (so SDL java environment is ready in JNI_OnLoad)
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&g_android_app->thread, &attr, android_app_entry, g_android_app);
pthread_attr_destroy(&attr);
// Wait for thread to start.
pthread_mutex_lock(&g_android_app->mutex);
while (!g_android_app->running)
{
pthread_cond_wait(&g_android_app->cond, &g_android_app->mutex);
}
pthread_mutex_unlock(&g_android_app->mutex);
}

View File

@ -23,7 +23,6 @@
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "input/gamepad_android_config.hpp"
#include "input/gamepad_device.hpp"
#include "input/keyboard_device.hpp"
#include "input/multitouch_device.hpp"
@ -83,24 +82,6 @@ bool DeviceManager::initialize()
created = true;
}
#ifdef ANDROID
bool has_gamepad_android_config = false;
for (unsigned int i = 0; i < m_keyboard_configs.size(); i++)
{
if (m_keyboard_configs[i].isGamePadAndroid())
{
has_gamepad_android_config = true;
}
}
if (!has_gamepad_android_config)
{
m_keyboard_configs.push_back(new GamepadAndroidConfig());
created = true;
}
#endif
const int keyboard_amount = m_keyboard_configs.size();
for (int n = 0; n < keyboard_amount; n++)
@ -528,8 +509,7 @@ bool DeviceManager::load()
config->getName().c_str());
continue;
}
if (config->getName() == "keyboard" ||
config->getName() == "gamepad_android")
if (config->getName() == "keyboard")
{
KeyboardConfig *kc = static_cast<KeyboardConfig*>(device_config);
m_keyboard_configs.push_back(kc);