Skip to content

Commit

Permalink
OpenXR - Switching between 2D and XR (termux#676)
Browse files Browse the repository at this point in the history
Co-authored-by: Twaik Yont <[email protected]>
  • Loading branch information
lvonasek and twaik authored Aug 12, 2024
1 parent 2ec3758 commit d366d40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
16 changes: 11 additions & 5 deletions app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,17 @@ void onPreferencesChanged(String key) {
void onPreferencesChangedCallback() {
prefs.recheckStoringSecondaryDisplayPreferences();

if (oldXrMode != prefs.xrMode.get() && XrActivity.isSupported() &&
prefs.xrMode.get() != this instanceof XrActivity) {
/* Going back to 2d mode does not work */
// getBaseContext().startActivity(new Intent(this, MainActivity.class)
// .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
if (oldXrMode != prefs.xrMode.get() && XrActivity.isSupported()) {
getBaseContext().startActivity(new Intent(this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));

if (oldXrMode) {
// XR process and 2D preferences screen are two different processes.
// To close XR, it is needed to do it using a broadcast.
Intent intent = new Intent(XrActivity.ACTION_STOP_XR);
intent.setPackage(getPackageName());
getBaseContext().sendBroadcast(intent);
}
finish();
return;
}
Expand Down
36 changes: 19 additions & 17 deletions app/src/main/java/com/termux/x11/XrActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.display.DisplayManager;
import android.opengl.GLES11Ext;
import android.opengl.GLSurfaceView;
import android.os.Build;
Expand Down Expand Up @@ -44,6 +45,8 @@ public enum RenderParam {
CANVAS_DISTANCE, IMMERSIVE, PASSTHROUGH, SBS, VIEWPORT_WIDTH, VIEWPORT_HEIGHT,
}

public static final String ACTION_STOP_XR = "com.termux.x11.CmdEntryPoint.ACTION_STOP_XR";

private static boolean isDeviceDetectionFinished = false;
private static boolean isDeviceSupported = false;

Expand All @@ -67,6 +70,16 @@ protected void onCreate(Bundle savedInstanceState) {
view.setEGLContextClientVersion(2);
view.setRenderer(this);
frm.addView(view);

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_STOP_XR.equals(intent.getAction())) {
teardown();
System.exit(0);
}
}
}, new IntentFilter(ACTION_STOP_XR) {}, 0);
}

@Override
Expand Down Expand Up @@ -101,7 +114,7 @@ public static void openIntent(Activity context) {
Intent intent = new Intent(context, XrActivity.class);

// 1. Locate the main display ID and add that to the intent
final int mainDisplayId = getMainDisplay(context);
final int mainDisplayId = Display.DEFAULT_DISPLAY;
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(mainDisplayId);

// 2. Set the flags: start in a new task
Expand All @@ -115,17 +128,6 @@ public static void openIntent(Activity context) {
context.finish();
}

private static int getMainDisplay(Context context) {
final DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : displayManager.getDisplays()) {
if (display.getDisplayId() == Display.DEFAULT_DISPLAY) {
return display.getDisplayId();
}
}
return -1;
}

@Override
void clientConnectedStateChanged(boolean connected) {
if (!connected && (surface != null)) {
Expand Down Expand Up @@ -377,8 +379,8 @@ private void renderFrame(GL10 gl10) {
private native void teardown();
private native boolean beginFrame();
private native void finishFrame();
public native float[] getAxes();
public native boolean[] getButtons();
public native int getRenderParam(int param);
public native void setRenderParam(int param, int value);
private native float[] getAxes();
private native boolean[] getButtons();
private native int getRenderParam(int param);
private native void setRenderParam(int param, int value);
}

0 comments on commit d366d40

Please sign in to comment.