diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index fbbd09886e..faa764565e 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -1661,15 +1661,24 @@ static void bind_desktop(struct wl_client *c, void *data, uint32_t ver, uint32_t static void output_release(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } static const struct wl_output_interface output_impl = { .release = output_release }; -static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { - struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); - if (!r) { wl_client_post_no_memory(c); return; } - wl_resource_set_implementation(r, &output_impl, NULL, NULL); - wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, - "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); +/* Every bound wl_output, so a resized output (a foldable opening) can send its new mode. */ +static struct wl_list g_outputs; +static void output_resource_destroy(struct wl_resource *r) { wl_list_remove(wl_resource_get_link(r)); } + +static void output_send_mode(struct wl_resource *r) { wl_output_send_mode(r, WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED, g_output_w > 0 ? g_output_w : 1920, g_output_h > 0 ? g_output_h : 1080, g_output_refresh_mhz > 0 ? g_output_refresh_mhz : 60000); +} + +static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { + struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); + if (!r) { wl_client_post_no_memory(c); return; } + wl_resource_set_implementation(r, &output_impl, NULL, output_resource_destroy); + wl_list_insert(&g_outputs, wl_resource_get_link(r)); + wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, + "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); + output_send_mode(r); if (ver >= WL_OUTPUT_NAME_SINCE_VERSION) { wl_output_send_name(r, "DroidDeck-1"); wl_output_send_description(r, "DroidDeck display"); @@ -3200,6 +3209,23 @@ static void key_event(uint32_t evdev, int pressed) { wl_display_flush_clients(g_display); } +/* The output changed size under a running session (a foldable opened or closed, with the + * session following the screen). Every client that has had its first configure is told the new + * size: gamescope's Wayland backend and the desktop's labwc both resize their output to it, and + * the next buffers they commit fill the new panel instead of being letterboxed onto it. */ +static void output_resized(void) { + struct surface *s; + struct wl_resource *r; + wl_resource_for_each(r, &g_outputs) { + output_send_mode(r); + if (wl_resource_get_version(r) >= 2) wl_output_send_done(r); + } + wl_list_for_each(s, &g_surfaces, link) + if (s->xdg_toplevel && s->toplevel_committed) send_toplevel_configure(s); + droiddeck_log("display", "output resized to %dx%d", g_output_w, g_output_h); + wl_display_flush_clients(g_display); +} + /* wl event-loop callback: drain queued input events written by the Android UI thread. */ static int on_input_readable(int fd, uint32_t mask, void *data) { struct input_msg m; @@ -3212,6 +3238,7 @@ static int on_input_readable(int fd, uint32_t mask, void *data) { case 5: on_vsync(((int64_t)m.p1 << 32) | (uint32_t)m.p2); break; case 6: pointer_delta(m.p1 / 256.0, m.p2 / 256.0); break; /* relative motion, 1/256 px */ case 7: deliver_touch(&m); break; + case 8: output_resized(); break; default: deliver_pointer(&m); break; } } @@ -3242,6 +3269,14 @@ void droiddeck_wayland_send_key(int evdev, int state) { (void)n; } +/* Called from JNI after g_output_w/h changed: the compositor thread re-sends the size. */ +void droiddeck_wayland_output_resized(void) { + if (g_input_pipe[1] < 0) return; + struct input_msg m = { 8, 0, 0, 0, 0 }; + ssize_t n = write(g_input_pipe[1], &m, sizeof(m)); + (void)n; +} + /* Called from JNI on every screen refresh (Choreographer). */ void droiddeck_wayland_vsync(int64_t frame_time_ns) { if (g_input_pipe[1] < 0) return; @@ -3439,6 +3474,7 @@ static struct wl_listener g_client_created = { .notify = on_client_created }; int droiddeck_wayland_run(void) { wl_list_init(&g_surfaces); wl_list_init(&g_toplevels); + wl_list_init(&g_outputs); wl_list_init(&g_constraints); wl_list_init(&g_relative_pointers); open_session_log(); diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index 3af9a8c254..4551c4806f 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -160,6 +160,11 @@ static const char *vk_result_name(VkResult r); static pthread_mutex_t g_req_lock = PTHREAD_MUTEX_INITIALIZER; static ANativeWindow *g_req_window; static int g_req_pending; +/* A detach was asked for since the compositor thread last looked. A rebind (null, then the same + * window) to rebuild the swapchain at a new size can land in full before the thread gets to it, + * and would otherwise read as the same window handed over again - nothing rebuilt, and the old + * size's buffers scaled onto the rotated or refolded panel. */ +static int g_req_detached; /* Scale mode + alignment (app values, see vk_present.h); written from any thread, read per frame. */ static volatile int g_mode = VKP_MODE_STRETCH, g_align = VKP_ALIGN_CENTER; @@ -252,6 +257,7 @@ void vk_present_set_window(ANativeWindow *window) { if (g_req_pending && g_req_window && g_req_window != window) superseded = g_req_window; g_req_window = window; g_req_pending = 1; + if (!window) g_req_detached = 1; pthread_mutex_unlock(&g_req_lock); if (superseded) ANativeWindow_release(superseded); } @@ -261,13 +267,20 @@ int vkp_apply_window_request(void) { pthread_mutex_lock(&g_req_lock); if (!g_req_pending) { pthread_mutex_unlock(&g_req_lock); return 0; } w = g_req_window; + const int detached = g_req_detached; g_req_window = NULL; g_req_pending = 0; + g_req_detached = 0; pthread_mutex_unlock(&g_req_lock); if (w == g_window) { /* The same window handed over again (ANativeWindow_fromSurface adds a reference each time). */ if (w) ANativeWindow_release(w); - return 0; + if (!w || !detached) return 0; + /* ... after a detach: a rebind for a resized surface. Rebuilt at its size on the next frame. */ + sc_layer_window_gone(); + destroy_swapchain(); + droiddeck_log("gpu", "screen surface rebound: rebuilding the swapchain at its new size"); + return 1; } sc_layer_window_gone(); /* the layer (if any) belongs to the old window */ destroy_swapchain(); /* recreated against the new window on the next frame */ @@ -1516,6 +1529,7 @@ static int render_impl(int scene_w, int scene_h, const struct vkp_draw *draws, i const struct vkp_hdr_frame *hf, int *how) { if (how) *how = 0; vkp_apply_window_request(); + check_surface_changed(); /* a rotation or a fold the rebind did not cover: rebuild at the new size */ if (g_dev_state == -2) return -1; if (dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; @@ -2099,6 +2113,7 @@ static int pass_begin_impl(int scene_w, int scene_h, const struct vkp_draw *draw const struct vkp_hdr_frame *hf, int *rw, int *rh) { if (g_pass.active) vkp_pass_abort(); if (g_dev_state == -2 || dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; + check_surface_changed(); /* as in render_impl: the effects pass maps onto the output's size */ if (!g_swapchain && swap_init() != 0) return -1; /* the mapping is in output pixels */ update_map(scene_w, scene_h); if (!g_map.valid) return -1; diff --git a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c index 4d11a54192..8ec4b96b64 100644 --- a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c +++ b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c @@ -30,6 +30,7 @@ extern int droiddeck_cursor_serial(void); extern void droiddeck_wayland_send_key(int evdev, int state); extern void droiddeck_wayland_send_scene_input(int type, int a, int b); extern void droiddeck_wayland_vsync(int64_t frame_time_ns); +extern void droiddeck_wayland_output_resized(void); extern volatile int g_fps_limit; extern volatile int g_hide_shell; extern volatile int g_zero_copy; @@ -401,6 +402,17 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetOutputSize(JNIEnv g_output_h = h > 0 ? h : 0; } +/* The output's size changed while the compositor runs (a foldable opening or closing with the + * session following the screen): store it and have the compositor thread tell the clients. */ +JNIEXPORT void JNICALL +Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeResizeOutput(JNIEnv *env, jclass clazz, jint w, jint h) { + (void)env; (void)clazz; + if (w <= 0 || h <= 0) return; + g_output_w = w; + g_output_h = h; + droiddeck_wayland_output_resized(); +} + /* Fullscreen mode + screen alignment (Container.FULLSCREEN_* / ALIGN_* values): how the scene is * fitted onto the output. Any thread, any time; the next frame uses it. */ JNIEXPORT void JNICALL diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 2e9be51934..d706b0622a 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -336,6 +336,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { ) } Log.i(TAG, "perf: " + PerfMode.apply(this, SessionState.fpsLimit)) + if (!SessionState.running) SessionState.followScreen = followChoice() + applyFollowOrientation(resources.configuration) goFullscreen() // The device's volume keys change the stream the session plays on (the relay and // PulseAudio are media playback); they are never forwarded to the guest. @@ -962,7 +964,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { // sized once when the session starts and cannot change. A foldable recreates the Surface // on the other panel, and recomputing the size there told the compositor a 21:9 buffer // was 16:9 - the picture came back squashed sideways. While a session runs, keep its size. - val size = if (SessionState.running) SessionState.outputSize else outputSize() + // Follow screen is the exception: there the compositor tells gamescope the new size, so the + // session takes the shape of the Surface it now has. + val previous = SessionState.outputSize + val live = if (SessionState.followScreen && !isInPictureInPictureMode) + com.droiddeck.launcher.session.SessionDisplay.followSize(holder.surfaceFrame.width(), holder.surfaceFrame.height()) + else null + val size = live ?: if (SessionState.running) previous else outputSize() SessionState.outputSize = size if (!SessionState.running) SessionState.refreshHz = refreshHz() // How far the panel enlarges the session, for texture sharpness "Auto" (TextureFiltering): @@ -1021,6 +1029,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) }, applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(), SessionState.fpsLimit, ) + if (SessionState.running && size != previous) { + Log.i(TAG, "follow screen: ${previous.first}x${previous.second} -> ${size.first}x${size.second} on a new surface") + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } if (!SessionState.running) SessionEvents.record("compositor.started") // The service owns everything below the compositor. It is started whenever no session is // running - NOT only when the compositor was just started: the compositor lives for the @@ -1049,9 +1061,51 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (resized) { Log.i(TAG, "surface resized to ${width}x$height - rebinding the compositor") CompositorHost.resize(holder.surface) { applyFrameGen() } + followWindow(width, height) } } + /** + * Follow screen: the session's display takes the window's new size (a Fold opening onto its + * inner panel, or closing onto the cover), so the picture fills the panel instead of being + * letterboxed onto it. Not in picture-in-picture: that window is a preview of the session, + * and resizing the guest to it would leave Steam laid out for a thumbnail. + */ + private fun followWindow(width: Int, height: Int) { + if (!SessionState.followScreen || isInPictureInPictureMode || !CompositorHost.isStarted) return + val size = com.droiddeck.launcher.session.SessionDisplay.followSize(width, height) ?: return + if (size == SessionState.outputSize) return + Log.i(TAG, "follow screen: ${SessionState.outputSize.first}x${SessionState.outputSize.second} -> ${size.first}x${size.second}") + SessionState.outputSize = size + val panel = panelBounds() + SessionState.upscaleRatio = maxOf(panel.width(), panel.height()).toFloat() / maxOf(size.first, size.second).coerceAtLeast(1) + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } + + /** Whether this session's resolution is Follow screen; decided once, when it starts. */ + private fun followChoice(): Boolean { + val mode = SessionPrefs.prefMode(intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM) + return SessionPrefs.resolutionChoice(this, mode, com.droiddeck.launcher.session.SessionDisplay.panelSize(this)) == + com.droiddeck.launcher.session.SessionDisplay.FOLLOW_SCREEN + } + + /** + * Follow screen on a large panel (a Fold's inner screen) may turn with the device: a window + * locked to landscape on a squarer panel is letterboxed by the system itself, which no output + * size can undo. A phone-sized panel (the cover screen) stays landscape as before. + */ + private fun applyFollowOrientation(config: android.content.res.Configuration) { + if (!SessionState.followScreen) return + val wanted = if (config.smallestScreenWidthDp >= 600) android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER + else android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + if (requestedOrientation != wanted) requestedOrientation = wanted + } + + override fun onConfigurationChanged(newConfig: android.content.res.Configuration) { + super.onConfigurationChanged(newConfig) + applyFollowOrientation(newConfig) + } + override fun surfaceDestroyed(holder: SurfaceHolder) { surfaceW = 0 surfaceH = 0 @@ -1891,6 +1945,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onPictureInPictureModeChanged(inPip: Boolean, newConfig: android.content.res.Configuration) { super.onPictureInPictureModeChanged(inPip, newConfig) if (::pip.isInitialized) pip.changed(inPip) + // The panel may have changed while the session was a thumbnail (folded or unfolded in PiP). + if (!inPip && surfaceW > 0) followWindow(surfaceW, surfaceH) } override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) { @@ -2018,10 +2074,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) + // ALWAYS from Android 11: SHORT_EDGES left a black strip along a camera on the long edge, + // which is where a Fold's inner-screen camera sits with the device held in landscape. if (Build.VERSION.SDK_INT >= 28) { window.attributes = window.attributes.apply { - layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + layoutInDisplayCutoutMode = if (Build.VERSION.SDK_INT >= 30) + WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS + else WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt index 7963875eb3..bae8d79db6 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt @@ -6,6 +6,11 @@ import android.view.WindowManager /** The virtual display advertised to the guest, before it is fitted onto the Android panel. */ object SessionDisplay { const val MATCH_SCREEN = "screen" + /** The window's size, kept in step while the session runs: a foldable opening or closing, a + * split screen or a freeform window resizes the guest's display instead of letterboxing it. */ + const val FOLLOW_SCREEN = "follow" + /** Below this a side is a picture-in-picture or a sliver of split screen: not worth a resize. */ + const val FOLLOW_MIN_SIDE = 480 /** * Presets are heights, not sizes: "720p" is the panel's own shape at 720 lines, never narrower @@ -25,7 +30,7 @@ object SessionDisplay { (maxOf(panel.first, panel.second) and 1.inv()) to (minOf(panel.first, panel.second) and 1.inv()) fun resolveChoice(panel: Pair, choice: String): Pair { - if (choice == MATCH_SCREEN) return screenSize(panel) + if (choice == MATCH_SCREEN || choice == FOLLOW_SCREEN) return screenSize(panel) presetHeight(choice)?.let { return resolve(panel, it, SessionPrefs.SHAPE_AUTO) } // Fixed sizes (custom, or a preset saved before presets were heights) keep their exact size, // including legacy ones past the custom dialog's limit. @@ -34,13 +39,19 @@ object SessionDisplay { else resolve(panel, 720, SessionPrefs.SHAPE_AUTO) } - /** The height presets the panel is tall enough for, then the panel itself, with no duplicate sizes. */ + /** The height presets the panel is tall enough for, then the panel itself, with no duplicate + * sizes, then Follow screen. */ fun resolutionOptions(panel: Pair): List { val panelHeight = minOf(panel.first, panel.second) return PRESET_HEIGHTS.filter { it <= panelHeight }.map { "${it}p" } - .filter { resolveChoice(panel, it) != screenSize(panel) } + MATCH_SCREEN + .filter { resolveChoice(panel, it) != screenSize(panel) } + MATCH_SCREEN + FOLLOW_SCREEN } + /** A Follow screen session's display for a window of this size: its own shape, square or + * portrait included, with even sides; null when it is too small to follow (PiP). */ + fun followSize(width: Int, height: Int): Pair? = + if (minOf(width, height) < FOLLOW_MIN_SIDE) null else (width and 1.inv()) to (height and 1.inv()) + fun panelSize(context: Context): Pair { val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager @Suppress("DEPRECATION") diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 87cd57b444..da7dd04f90 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -487,7 +487,8 @@ object SessionPrefs { fun resolutionChoice(context: Context, mode: String, panel: Pair): String { val saved = prefs(context) saved.getString("displayResolution.$mode", null)?.let { value -> - if (value == SessionDisplay.MATCH_SCREEN || SessionDisplay.presetHeight(value) != null) return value + if (value == SessionDisplay.MATCH_SCREEN || value == SessionDisplay.FOLLOW_SCREEN || + SessionDisplay.presetHeight(value) != null) return value parseResolution(value)?.let { return "${it.first}x${it.second}" } } if (!resolutionChosen(context, mode) && !saved.contains("shape")) return SessionDisplay.DEFAULT_RESOLUTION @@ -497,7 +498,8 @@ object SessionPrefs { } fun setResolutionChoice(context: Context, mode: String, choice: String) { - val value = if (choice == SessionDisplay.MATCH_SCREEN || SessionDisplay.presetHeight(choice) != null) choice else { + val value = if (choice == SessionDisplay.MATCH_SCREEN || choice == SessionDisplay.FOLLOW_SCREEN || + SessionDisplay.presetHeight(choice) != null) choice else { val size = requireNotNull(parseResolution(choice)) { "Invalid resolution" } "${size.first}x${size.second}" } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 69d4ea65ee..71fd002c6e 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -352,6 +352,9 @@ class SessionService : Service() { guest.add("BL_WIDTH=" + size.first) guest.add("BL_HEIGHT=" + size.second) + // Follow screen: the output is resized under gamescope when a foldable opens or closes, + // and the session script asks gamescope to resize Steam's X screen along with it. + if (SessionState.followScreen) guest.add("BL_FOLLOW_OUTPUT=1") if (SessionState.hdr) { // The activity opened the compositor's HDR gate: gamescope offers HDR to its clients // and DXVK takes the HDR10 swapchain when a game asks for one. diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt index af47468ccb..57c377bcbd 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -96,6 +96,10 @@ object SessionState { @Volatile var outputSize: Pair = Pair(1920, 1080) + /** The session's resolution is "Follow screen": outputSize tracks the window while it runs. */ + @Volatile + var followScreen = false + @Volatile var refreshHz: Float = 60f diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt b/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt index 429ba1bb98..2af914e4dc 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt @@ -20,11 +20,13 @@ fun ResolutionRow( val size = SessionDisplay.resolveChoice(panel, choice) val screen = SessionDisplay.screenSize(panel) val choices = SessionDisplay.resolutionOptions(panel) - val selected = if (size == screen) SessionDisplay.MATCH_SCREEN else choice + val selected = if (choice == SessionDisplay.FOLLOW_SCREEN) choice + else if (size == screen) SessionDisplay.MATCH_SCREEN else choice var editCustom by remember { mutableStateOf(false) } val options = choices.map { value -> val dimensions = SessionDisplay.resolveChoice(panel, value) value to if (value == SessionDisplay.MATCH_SCREEN) stringResource(R.string.display_match_screen, screen.first, screen.second) + else if (value == SessionDisplay.FOLLOW_SCREEN) stringResource(R.string.display_follow_screen) else if (value == SessionDisplay.DEFAULT_RESOLUTION) stringResource(R.string.mode_res_default, "${dimensions.first}×${dimensions.second}") else "${dimensions.first}×${dimensions.second}" } + (custom to if (selected in choices) stringResource(R.string.mode_res_custom) diff --git a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java index 84330c98b4..c40fb1b527 100644 --- a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java +++ b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java @@ -312,6 +312,11 @@ public final class WaylandCompositor { * list stops at the desktop size, as the X server's does on X11. Set before the compositor starts. */ public static native void nativeSetOutputSize(int width, int height); + /** A new output size for a running compositor ("Follow screen": a foldable opening or closing). + * The compositor thread re-sends the wl_output mode and an xdg_toplevel configure to every + * client, and gamescope / labwc resize their output to it. Any thread. */ + public static native void nativeResizeOutput(int width, int height); + /** Re-arms the one-shot first-frame notice, for a new session under a compositor that has * already presented a previous one. */ public static native void nativeResetFirstFrame(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 50f9c637ed..e21c36943a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1948,6 +1948,7 @@ Frame rate %1$s Sets scaling to %2$s. Match screen (%1$d×%2$d) + Follow screen (resizes when folded or unfolded) Steam Show device details Hide device details diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt index 373af869ca..6093ea80cb 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt @@ -27,15 +27,24 @@ class SessionDisplayTest { @Test fun optionsListTheHeightsThePanelCanShowThenThePanel() { val thor = SessionDisplay.resolutionOptions(1920 to 1080) - assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN), thor) - assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN), SessionDisplay.resolutionOptions(2400 to 1080)) - assertEquals(listOf("720p", "900p", "1080p", SessionDisplay.MATCH_SCREEN), SessionDisplay.resolutionOptions(3120 to 1440)) + assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), thor) + assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), + SessionDisplay.resolutionOptions(2400 to 1080)) + assertEquals(listOf("720p", "900p", "1080p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), + SessionDisplay.resolutionOptions(3120 to 1440)) val small = SessionDisplay.resolutionOptions(1280 to 720) - assertEquals(listOf(SessionDisplay.MATCH_SCREEN), small) + assertEquals(listOf(SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), small) assertEquals(1280 to 720, SessionDisplay.resolveChoice(1280 to 720, SessionDisplay.MATCH_SCREEN)) assertEquals(2560 to 1440, SessionDisplay.resolveChoice(2560 to 1440, SessionDisplay.MATCH_SCREEN)) } + @Test fun followScreenKeepsTheWindowShapeAndSkipsTinyWindows() { + assertEquals(2520 to 1080, SessionDisplay.resolveChoice(1080 to 2520, SessionDisplay.FOLLOW_SCREEN)) + assertEquals(1968 to 2184, SessionDisplay.followSize(1968, 2184)) + assertEquals(2184 to 1968, SessionDisplay.followSize(2185, 1969)) + assertNull(SessionDisplay.followSize(640, 360)) + } + @Test fun resolutionCapsPreserveTheSelectedShapeWithoutExceedingPanelHeight() { assertEquals(1280 to 720, SessionDisplay.resolve(1920 to 1080, 720, SessionPrefs.SHAPE_AUTO)) assertEquals(960 to 720, SessionDisplay.resolve(1440 to 1080, 720, SessionPrefs.SHAPE_EXACT)) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 3dbeaef1ab..4220b0ae2c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -189,7 +189,12 @@ if [ -z "${BL_INSIDE:-}" ]; then width=${BL_WIDTH:-1280} height=${BL_HEIGHT:-720} nested=$width - if [ "$mode" = steam ] && [ "${BL_GAMESCOPE_STRETCH_16X9:-0}" = 1 ]; then + # Follow screen (BL_FOLLOW_OUTPUT=1): the app resizes gamescope's output when a foldable opens or + # closes, and with Steam on its own Xwayland gamescope resizes Steam's X screen with it, as on a + # Steam Deck docking. A stretched 16:9 nested size would pin it, so the two are exclusive. + if [ "${BL_FOLLOW_OUTPUT:-0}" = 1 ]; then + echo "== gamescope: the output follows the screen (${width}x${height} to start)" + elif [ "$mode" = steam ] && [ "${BL_GAMESCOPE_STRETCH_16X9:-0}" = 1 ]; then wide=$(( (height * 16 + 4) / 9 / 2 * 2 )) if [ "$wide" -gt "$width" ]; then nested=$wide diff --git a/tools/linuxfs/preload/xmode.c b/tools/linuxfs/preload/xmode.c new file mode 100644 index 0000000000..8579f70bcc --- /dev/null +++ b/tools/linuxfs/preload/xmode.c @@ -0,0 +1,66 @@ +/* + * The Steam client's own screen, in a Follow screen session. + * + * With two Xwayland servers the client sizes them through GAMESCOPE_XWAYLAND_MODE_CONTROL + * ({server, width, height, allow_super_res} on the root window): the games' server (1) to a game's + * Game Resolution, and its own (0) to what it recommends for the display - 1920x1080 on a Fold's + * 2448x1848 inner panel. From then on it reads that 16:9 size back as the display's "native" one, + * so its interface and every game it starts are 16:9 with bars on a near-square panel, and stay so + * when the phone folds onto the cover screen. + * + * gamescope already keeps server 0 at the output's size and resizes it when the output changes (a + * Steam Deck docking; here, the app resizing the output as the phone folds and unfolds). So in a + * Follow screen session (BL_FOLLOW_OUTPUT=1) a request for server 0 is dropped and the client's + * screen stays the panel's; requests for the games' servers pass through untouched. Any other + * session, and any other property, is passed straight on. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +/* Xlib's types, without needing its headers in the build image. */ +typedef struct _XDisplay Display; +typedef unsigned long Atom; +typedef unsigned long Window; +typedef int Bool; + +typedef int (*change_property_fn)(Display *, Window, Atom, Atom, int, int, const unsigned char *, int); +typedef Atom (*intern_atom_fn)(Display *, const char *, Bool); + +static int following(void) { + static int cached = -1; + if (cached < 0) { + const char *v = getenv("BL_FOLLOW_OUTPUT"); + cached = v && v[0] == '1'; + } + return cached; +} + +int XChangeProperty(Display *dpy, Window w, Atom property, Atom type, int format, int mode, + const unsigned char *data, int nelements) { + static change_property_fn real; + if (!real) real = (change_property_fn)dlsym(RTLD_NEXT, "XChangeProperty"); + if (!real) return 0; + if (following() && format == 32 && nelements == 4 && data) { + static intern_atom_fn intern; + static Display *atom_dpy; + static Atom mode_control; + if (!intern) intern = (intern_atom_fn)dlsym(RTLD_NEXT, "XInternAtom"); + /* Atoms belong to a connection; the client's is the one that writes this. */ + if (intern && atom_dpy != dpy) { + mode_control = intern(dpy, "GAMESCOPE_XWAYLAND_MODE_CONTROL", 1); + atom_dpy = dpy; + } + /* Format-32 property data is an array of long in Xlib. */ + const long *v = (const long *)data; + if (mode_control && property == mode_control && v[0] == 0) { + static int said; + if (said++ < 3) + fprintf(stderr, "== steam: kept its screen at the panel's size (it asked for %ldx%ld)\n", v[1], v[2]); + return 1; + } + } + return real(dpy, w, property, type, format, mode, data, nelements); +}