[ion] Update the Java SDL code

This commit is contained in:
Romain Goyet
2019-10-17 11:30:28 +02:00
committed by LeaNumworks
parent a34ecd2769
commit 585760bd7b
4 changed files with 156 additions and 61 deletions

View File

@@ -411,7 +411,7 @@ public class HIDDeviceManager {
if (mIsChromebook) {
mHandler = new Handler(Looper.getMainLooper());
mLastBluetoothDevices = new ArrayList<>();
mLastBluetoothDevices = new ArrayList<BluetoothDevice>();
// final HIDDeviceManager finalThis = this;
// mHandler.postDelayed(new Runnable() {
@@ -439,8 +439,8 @@ public class HIDDeviceManager {
return;
}
ArrayList<BluetoothDevice> disconnected = new ArrayList<>();
ArrayList<BluetoothDevice> connected = new ArrayList<>();
ArrayList<BluetoothDevice> disconnected = new ArrayList<BluetoothDevice>();
ArrayList<BluetoothDevice> connected = new ArrayList<BluetoothDevice>();
List<BluetoothDevice> currentConnected = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);

View File

@@ -38,6 +38,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
private static final String TAG = "SDL";
public static boolean mIsResumedCalled, mHasFocus;
public static final boolean mHasMultiWindow = (Build.VERSION.SDK_INT >= 24);
// Cursor types
private static final int SDL_SYSTEM_CURSOR_NONE = -1;
@@ -73,10 +74,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
/** If shared libraries (e.g. SDL or the native application) could not be loaded. */
public static boolean mBrokenLibraries;
// If we want to separate mouse and touch events.
// This is only toggled in native code when a hint is set!
public static boolean mSeparateMouseAndTouch;
// Main components
protected static SDLActivity mSingleton;
protected static SDLSurface mSurface;
@@ -257,7 +254,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
// Get our current screen orientation and pass it down.
mCurrentOrientation = SDLActivity.getCurrentOrientation();
// FIXME: with only one activity, SDL Thread is not yet started and this onNativeOrientationChanged() is ignored
// Only record current orientation
SDLActivity.onNativeOrientationChanged(mCurrentOrientation);
setContentView(mLayout);
@@ -277,16 +274,12 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
}
}
// Events
@Override
protected void onPause() {
Log.v(TAG, "onPause()");
super.onPause();
protected void pauseNativeThread() {
mNextNativeState = NativeState.PAUSED;
mIsResumedCalled = false;
if (SDLActivity.mBrokenLibraries) {
return;
return;
}
if (mHIDDeviceManager != null) {
@@ -296,10 +289,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
SDLActivity.handleNativeState();
}
@Override
protected void onResume() {
Log.v(TAG, "onResume()");
super.onResume();
protected void resumeNativeThread() {
mNextNativeState = NativeState.RESUMED;
mIsResumedCalled = true;
@@ -314,6 +304,43 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
SDLActivity.handleNativeState();
}
// Events
@Override
protected void onPause() {
Log.v(TAG, "onPause()");
super.onPause();
if (!mHasMultiWindow) {
pauseNativeThread();
}
}
@Override
protected void onResume() {
Log.v(TAG, "onResume()");
super.onResume();
if (!mHasMultiWindow) {
resumeNativeThread();
}
}
@Override
protected void onStop() {
Log.v(TAG, "onStop()");
super.onStop();
if (mHasMultiWindow) {
pauseNativeThread();
}
}
@Override
protected void onStart() {
Log.v(TAG, "onStart()");
super.onStart();
if (mHasMultiWindow) {
resumeNativeThread();
}
}
public static int getCurrentOrientation() {
final Context context = SDLActivity.getContext();
final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
@@ -350,15 +377,21 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
return;
}
SDLActivity.mHasFocus = hasFocus;
mHasFocus = hasFocus;
if (hasFocus) {
mNextNativeState = NativeState.RESUMED;
SDLActivity.getMotionListener().reclaimRelativeMouseModeIfNeeded();
} else {
mNextNativeState = NativeState.PAUSED;
}
SDLActivity.handleNativeState();
SDLActivity.handleNativeState();
nativeFocusChanged(true);
} else {
nativeFocusChanged(false);
if (!mHasMultiWindow) {
mNextNativeState = NativeState.PAUSED;
SDLActivity.handleNativeState();
}
}
}
@Override
@@ -617,7 +650,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
}
case COMMAND_CHANGE_SURFACEVIEW_FORMAT:
{
int format = ((int)msg.obj);
int format = (Integer) msg.obj;
int pf;
if (SDLActivity.mSurface == null) {
@@ -722,11 +755,13 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
public static native void nativeQuit();
public static native void nativePause();
public static native void nativeResume();
public static native void nativeFocusChanged(boolean hasFocus);
public static native void onNativeDropFile(String filename);
public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate);
public static native void onNativeResize();
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native boolean onNativeSoftReturnKey();
public static native void onNativeKeyboardFocusLost();
public static native void onNativeMouse(int button, int action, float x, float y, boolean relative);
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
@@ -810,6 +845,45 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
}
}
/**
* This method is called by SDL using JNI.
*/
public static void minimizeWindow() {
if (mSingleton == null) {
return;
}
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mSingleton.startActivity(startMain);
}
/**
* This method is called by SDL using JNI.
*/
public static boolean shouldMinimizeOnFocusLoss() {
/*
if (Build.VERSION.SDK_INT >= 24) {
if (mSingleton == null) {
return true;
}
if (mSingleton.isInMultiWindowMode()) {
return false;
}
if (mSingleton.isInPictureInPictureMode()) {
return false;
}
}
return true;
*/
return false;
}
/**
* This method is called by SDL using JNI.
*/
@@ -994,6 +1068,14 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
this.y = y;
this.w = w;
this.h = h;
/* Minimum size of 1 pixel, so it takes focus. */
if (this.w <= 0) {
this.w = 1;
}
if (this.h + HEIGHT_PADDING <= 0) {
this.h = 1 - HEIGHT_PADDING;
}
}
@Override
@@ -1656,6 +1738,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate());
SDLActivity.onNativeResize();
// Prevent a screen distortion glitch,
// for instance when the device is in Landscape and a Portrait App is resumed.
boolean skip = false;
int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation();
@@ -1685,6 +1769,16 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
}
// Don't skip in MultiWindow.
if (skip) {
if (Build.VERSION.SDK_INT >= 24) {
if (SDLActivity.mSingleton.isInMultiWindowMode()) {
Log.v("SDL", "Don't skip in Multi-Window");
skip = false;
}
}
}
if (skip) {
Log.v("SDL", "Skip .. Surface is not ready.");
mIsSurfaceReady = false;
@@ -1704,6 +1798,10 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Key events
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
int deviceId = 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
@@ -1711,20 +1809,27 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// 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(event.getDeviceId())) {
if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
// Note that we process events with specific key codes here
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (SDLControllerManager.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
return true;
}
} else if (event.getAction() == KeyEvent.ACTION_UP) {
if (SDLControllerManager.onNativePadUp(event.getDeviceId(), keyCode) == 0) {
if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
return true;
}
}
}
if ((event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
if (source == InputDevice.SOURCE_UNKNOWN) {
InputDevice device = InputDevice.getDevice(deviceId);
if (device != null) {
source = device.getSources();
}
}
if ((source & InputDevice.SOURCE_KEYBOARD) != 0) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
//Log.v("SDL", "key down: " + keyCode);
if (SDLActivity.isTextInputEvent(event)) {
@@ -1740,7 +1845,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
}
}
if ((event.getSource() & InputDevice.SOURCE_MOUSE) != 0) {
if ((source & InputDevice.SOURCE_MOUSE) != 0) {
// on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
// they are ignored here because sending them as mouse input to SDL is messy
if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {
@@ -1772,8 +1877,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// 12290 = Samsung DeX mode desktop mouse
// 12290 = 0x3002 = 0x2002 | 0x1002 = SOURCE_MOUSE | SOURCE_TOUCHSCREEN
// 0x2 = SOURCE_CLASS_POINTER
if ((event.getSource() == InputDevice.SOURCE_MOUSE || event.getSource() == (InputDevice.SOURCE_MOUSE | InputDevice.SOURCE_TOUCHSCREEN))
&& SDLActivity.mSeparateMouseAndTouch) {
if (event.getSource() == InputDevice.SOURCE_MOUSE || event.getSource() == (InputDevice.SOURCE_MOUSE | InputDevice.SOURCE_TOUCHSCREEN)) {
try {
mouseButton = (Integer) event.getClass().getMethod("getButtonState").invoke(event);
} catch(Exception e) {
@@ -1889,8 +1993,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
newOrientation = SDLActivity.SDL_ORIENTATION_LANDSCAPE_FLIPPED;
break;
case Surface.ROTATION_180:
x = -event.values[1];
y = -event.values[0];
x = -event.values[0];
y = -event.values[1];
newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT_FLIPPED;
break;
default:
@@ -2045,14 +2149,8 @@ class SDLInputConnection extends BaseInputConnection {
*/
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
String imeHide = SDLActivity.nativeGetHint("SDL_RETURN_KEY_HIDES_IME");
if ((imeHide != null) && imeHide.equals("1")) {
Context c = SDL.getContext();
if (c instanceof SDLActivity) {
SDLActivity activity = (SDLActivity)c;
activity.sendCommand(SDLActivity.COMMAND_TEXTEDIT_HIDE, null);
return true;
}
if (SDLActivity.onNativeSoftReturnKey()) {
return true;
}
}
@@ -2065,6 +2163,11 @@ class SDLInputConnection extends BaseInputConnection {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\n') {
if (SDLActivity.onNativeSoftReturnKey()) {
return true;
}
}
nativeGenerateScancodeForUnichar(c);
}

View File

@@ -555,9 +555,6 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
return SDLControllerManager.handleJoystickMotionEvent(event);
case InputDevice.SOURCE_MOUSE:
if (!SDLActivity.mSeparateMouseAndTouch) {
break;
}
action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_SCROLL:
@@ -624,14 +621,12 @@ class SDLGenericMotionListener_API24 extends SDLGenericMotionListener_API12 {
// Handle relative mouse mode
if (mRelativeModeEnabled) {
if (event.getSource() == InputDevice.SOURCE_MOUSE) {
if (SDLActivity.mSeparateMouseAndTouch) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_HOVER_MOVE) {
float x = event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
float y = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
SDLActivity.onNativeMouse(0, action, x, y, true);
return true;
}
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_HOVER_MOVE) {
float x = event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
float y = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
SDLActivity.onNativeMouse(0, action, x, y, true);
return true;
}
}
}
@@ -696,10 +691,6 @@ class SDLGenericMotionListener_API26 extends SDLGenericMotionListener_API24 {
case InputDevice.SOURCE_MOUSE:
// DeX desktop mouse cursor is a separate non-standard input type.
case InputDevice.SOURCE_MOUSE | InputDevice.SOURCE_TOUCHSCREEN:
if (!SDLActivity.mSeparateMouseAndTouch) {
break;
}
action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_SCROLL:
@@ -720,9 +711,6 @@ class SDLGenericMotionListener_API26 extends SDLGenericMotionListener_API24 {
break;
case InputDevice.SOURCE_MOUSE_RELATIVE:
if (!SDLActivity.mSeparateMouseAndTouch) {
break;
}
action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_SCROLL:

View File

@@ -1,2 +1,6 @@
The sdl directory contains a snapshot of the SDL source code
at revision 13105:db9d0d0b7ebc
The sdl directory contains a snapshot of the SDL source code at revision
13105:db9d0d0b7ebc
Note that parts of SDL source code are also copied into the path
ion/src/simulator/android/src/java because Gradle expects Java code to live
there...