Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions android-project/app/src/main/java/org/libsdl/app/SDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,28 @@ public class SDL {
public static final int SDL_INIT_EVERYTHING = SDL_INIT_AUDIO | SDL_INIT_VIDEO |
SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD | SDL_INIT_SENSOR | SDL_INIT_CAMERA;

// SDLControllerManager backs all three of these, so it is set up when any are present.
private static final int SDL_INIT_CONTROLLER = SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_HAPTIC;

private static int mInitializedSubsystems = SDL_INIT_EVERYTHING;

static public void setupJNI() {
setupJNI(SDL_INIT_EVERYTHING);
}

// Mask must match the native build's SDL_*_DISABLED flags: dropping SDL_INIT_AUDIO when the lib was built with audio stalls checkJNIReady() and SDL_SetMainReady() never fires.
static public void setupJNI(int subsystems) {
mInitializedSubsystems = subsystems;

SDLActivity.nativeSetupJNI();

if ((subsystems & SDL_INIT_AUDIO) != 0) {
int compiled = SDLActivity.nativeGetCompiledSubsystems();
mInitializedSubsystems = subsystems & compiled;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mInitializedSubsystems = subsystems & compiled;
mInitializedSubsystems = (subsystems & compiled);


if ((compiled & SDL_INIT_AUDIO) != 0) {
SDLAudioManager.nativeSetupJNI();
}

SDLControllerManager.nativeSetupJNI();
if ((compiled & SDL_INIT_CONTROLLER) != 0) {
SDLControllerManager.nativeSetupJNI();
}
}

static public void initialize() {
Expand All @@ -54,7 +59,17 @@ static public void initialize(int subsystems) {
SDLAudioManager.initialize();
}

SDLControllerManager.initialize();
if ((subsystems & SDL_INIT_CONTROLLER) != 0) {
SDLControllerManager.initialize();
}
}

static boolean isSubsystemInitialized(int subsystem) {
return (mInitializedSubsystems & subsystem) != 0;
}

static boolean isControllerManagerReady() {
return isSubsystemInitialized(SDL_INIT_CONTROLLER);
}

// This function stores the current activity (SDL or not)
Expand Down
63 changes: 40 additions & 23 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,28 @@ public void onClick(DialogInterface dialog,int id) {
mSingleton = this;
SDL.setContext(this);

SDLControllerManager.initializeDeviceListener();
if (SDL.isControllerManagerReady()) {
SDLControllerManager.initializeDeviceListener();
}

mClipboardHandler = new SDLClipboardHandler();
if (SDL.isSubsystemInitialized(SDL.SDL_INIT_VIDEO)) {
mClipboardHandler = new SDLClipboardHandler();
}

mHIDDeviceManager = HIDDeviceManager.acquire(this);

// Set up the surface
mSurface = createSDLSurface(this);
if (SDL.isSubsystemInitialized(SDL.SDL_INIT_VIDEO)) {
mSurface = createSDLSurface(this);

mLayout = new RelativeLayout(this);
mLayout.addView(mSurface);
mLayout = new RelativeLayout(this);
mLayout.addView(mSurface);

// Get our current screen orientation and pass it down.
SDLActivity.nativeSetNaturalOrientation(SDLActivity.getNaturalOrientation());
mCurrentRotation = SDLActivity.getCurrentRotation();
SDLActivity.onNativeRotationChanged(mCurrentRotation);
// Get our current screen orientation and pass it down.
SDLActivity.nativeSetNaturalOrientation(SDLActivity.getNaturalOrientation());
mCurrentRotation = SDLActivity.getCurrentRotation();
SDLActivity.onNativeRotationChanged(mCurrentRotation);
}

try {
if (Build.VERSION.SDK_INT < 24 /* Android 7.0 (N) */) {
Expand All @@ -502,19 +508,22 @@ public void onClick(DialogInterface dialog,int id) {
break;
}

setContentView(mLayout);

setWindowStyle(false);
if (mLayout != null) {
setContentView(mLayout);
setWindowStyle(false);
}

getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);

// Get filename from "Open with" of another application
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
String filename = intent.getData().getPath();
if (filename != null) {
Log.v(TAG, "Got filename: " + filename);
SDLActivity.onNativeDropFile(filename);
if (SDL.isSubsystemInitialized(SDL.SDL_INIT_VIDEO)) {
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
String filename = intent.getData().getPath();
if (filename != null) {
Log.v(TAG, "Got filename: " + filename);
SDLActivity.onNativeDropFile(filename);
}
}
}
}
Expand Down Expand Up @@ -886,21 +895,27 @@ public static void handleNativeState() {

// Try a transition to resumed state
if (mNextNativeState == NativeState.RESUMED) {
if (mSurface.mIsSurfaceReady && (mHasFocus || mHasMultiWindow) && mIsResumedCalled) {
boolean readyToRun = (mSurface == null) ? mIsResumedCalled
: (mSurface.mIsSurfaceReady && (mHasFocus || mHasMultiWindow) && mIsResumedCalled);
if (readyToRun) {
if (mSDLThread == null) {
// This is the entry point to the C app.
// Start up the C app thread and enable sensor input for the first time
// FIXME: Why aren't we enabling sensor input at start?

mSDLThread = new Thread(new SDLMain(), "SDLThread");
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
if (mSurface != null) {
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
}
mSDLThread.start();

// No nativeResume(), don't signal Android_ResumeSem
} else {
nativeResume();
}
mSurface.handleResume();
if (mSurface != null) {
mSurface.handleResume();
}

mCurrentNativeState = mNextNativeState;
}
Expand Down Expand Up @@ -1058,7 +1073,8 @@ protected boolean sendCommand(int command, Object data) {
DisplayMetrics realMetrics = new DisplayMetrics();
display.getRealMetrics(realMetrics);

boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) &&
boolean bFullscreenLayout = (mSurface != null) &&
((realMetrics.widthPixels == mSurface.getWidth()) &&
(realMetrics.heightPixels == mSurface.getHeight()));

if ((Integer) data == 1) {
Expand Down Expand Up @@ -1102,6 +1118,7 @@ protected boolean sendCommand(int command, Object data) {
// C functions we call
public static native String nativeGetVersion();
public static native void nativeSetupJNI();
public static native int nativeGetCompiledSubsystems();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HTRamsey, @AntTheAlchemist, do we need to do something with proguard here?

public static native void nativeInitMainThread();
public static native void nativeCleanupMainThread();
public static native int nativeRunMain(String library, String function, Object arguments);
Expand Down Expand Up @@ -1553,7 +1570,7 @@ public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputC
// 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)) {
if (SDL.isControllerManagerReady() && SDLControllerManager.isDeviceSDLJoystick(device)) {
// Note that we process events with specific key codes here
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (SDLControllerManager.onNativePadDown(deviceId, keyCode, event.getScanCode())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void initialize() {
mJoystickHandler = new SDLJoystickHandler();
}

if (mHapticHandler == null) {
if (mHapticHandler == null && SDL.isSubsystemInitialized(SDL.SDL_INIT_HAPTIC)) {
if (Build.VERSION.SDK_INT >= 31 /* Android 12.0 (S) */) {
mHapticHandler = new SDLHapticHandler_API31();
} else if (Build.VERSION.SDK_INT >= 26 /* Android 8.0 (O) */) {
Expand Down Expand Up @@ -977,10 +977,12 @@ boolean setRelativeMouseEnabled(boolean enabled) {
}

if (!SDLActivity.isDeXMode() || Build.VERSION.SDK_INT >= 27 /* Android 8.1 (O_MR1) */) {
if (enabled) {
SDLActivity.getContentView().requestPointerCapture();
} else {
SDLActivity.getContentView().releasePointerCapture();
if (SDLActivity.getContentView() != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this into a temporary variable.

if (enabled) {
SDLActivity.getContentView().requestPointerCapture();
} else {
SDLActivity.getContentView().releasePointerCapture();
}
}
mRelativeModeEnabled = enabled;
return true;
Expand All @@ -997,7 +999,7 @@ void reclaimRelativeMouseModeIfNeeded() {
return;
}

if (mRelativeModeEnabled && !SDLActivity.isDeXMode()) {
if (mRelativeModeEnabled && !SDLActivity.isDeXMode() && SDLActivity.getContentView() != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please put this into a temporary variable.

SDLActivity.getContentView().requestPointerCapture();
}
}
Expand Down
Loading
Loading