From 95479221da168fdab415123e96cc1d69a6653938 Mon Sep 17 00:00:00 2001 From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:51:08 -0700 Subject: [PATCH] fix: check baselayer sequence before rearranging (#193) * umu_run: check baselayer before rearranging it - The current window setup codepath that's gated behind the check for XDG_CURRENT_DESKTOP and STEAM_MULTIPLE_XWAYLANDS didn't account for situations when umu-launcher is being used with unofficial gamescope session scripts within a non-Steam-deck environment. In those cases, both those variables would be set as those scripts would create 2 xwayland servers just like the Steam Deck gamescope session, causing the game window to not appear in the foreground as the launcher would rearrange the already correct baselayer sequence. While the current logic was incorrect, it highlighted an existing problem -- the launcher shouldn't always be assuming the sequence is incorrect and rearranging the baselayer order sequence only if the window atom exists. Instead, it should only be rearranging it when it needs to be. * umu_run: update comment * umu_test: add test when passed a sequence that should not be rearranged --- umu/umu_run.py | 30 ++++++++++++++++++++++++------ umu/umu_test.py | 16 +++++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/umu/umu_run.py b/umu/umu_run.py index 54b4d8852..4017126e7 100755 --- a/umu/umu_run.py +++ b/umu/umu_run.py @@ -457,9 +457,16 @@ def get_gamescope_baselayer_order( def rearrange_gamescope_baselayer_order( sequence: list[int], -) -> tuple[list[int], int]: +) -> tuple[list[int], int] | None: """Rearrange a gamescope base layer sequence retrieved from a window.""" - rearranged: list[int] = [sequence[0], sequence[-1], *sequence[1:-1]] + rearranged: list[int] + + # Gamescope identifies Steam's window by the App ID 769 or by the atom + # STEAM_BIGPICTURE. This id must be the last element in the sequence + if sequence and sequence[-1] == 769: + return None + + rearranged = [sequence[0], sequence[-1], *sequence[1:-1]] log.debug("Rearranging base layer sequence") log.debug("'%s' -> '%s'", sequence, rearranged) @@ -491,11 +498,16 @@ def window_setup( # noqa gamescope_baselayer_sequence: list[int], game_window_ids: set[str], ) -> None: + rearranged_gamescope_baselayer: tuple[list[int], int] | None = None + if gamescope_baselayer_sequence: - # Rearrange the sequence - # TODO: Consider only rearranging the sequence when we need to. + rearranged_gamescope_baselayer = rearrange_gamescope_baselayer_order( + gamescope_baselayer_sequence + ) + + if rearranged_gamescope_baselayer: rearranged_sequence, steam_assigned_layer_id = ( - rearrange_gamescope_baselayer_order(gamescope_baselayer_sequence) + rearranged_gamescope_baselayer ) # Assign our window a STEAM_GAME id @@ -512,6 +524,7 @@ def monitor_baselayer( ) -> None: """Monitor for broken gamescope baselayer sequences.""" root_primary: Window = d_primary.screen().root + rearranged_gamescope_baselayer: tuple[list[int], int] | None = None atom = d_primary.get_atom("GAMESCOPECTRL_BASELAYER_APPID") root_primary.change_attributes(event_mask=X.PropertyChangeMask) @@ -528,7 +541,12 @@ def monitor_baselayer( if prop and prop.value == gamescope_baselayer_sequence: log.debug("Broken base layer sequence detected") log.debug("Property value for atom '%s': %s", atom, prop.value) - rearranged, _ = rearrange_gamescope_baselayer_order(prop.value) + rearranged_gamescope_baselayer = ( + rearrange_gamescope_baselayer_order(prop.value) + ) + + if rearranged_gamescope_baselayer: + rearranged, _ = rearranged_gamescope_baselayer set_gamescope_baselayer_order(d_primary, rearranged) continue diff --git a/umu/umu_test.py b/umu/umu_test.py index f39401e00..4d5a4cfeb 100644 --- a/umu/umu_test.py +++ b/umu/umu_test.py @@ -187,6 +187,20 @@ def tearDown(self): if self.test_usr.exists(): rmtree(self.test_usr.as_posix()) + def test_rearrange_gamescope_baselayer_none(self): + """Test rearrange_gamescope_baselayer_order when passed correct seq. + + A rearranged sequence should only be returned when the last element + is 769. Otherwise, None should be returned + """ + baselayer = [1, 2, 3, 769] + result = umu_run.rearrange_gamescope_baselayer_order(baselayer) + + self.assertTrue( + result is None, + f"Expected None to be returned for sequence {baselayer}", + ) + def test_rearrange_gamescope_baselayer_order_err(self): """Test rearrange_gamescope_baselayer_order for unexpected seq.""" baselayer = [] @@ -195,7 +209,7 @@ def test_rearrange_gamescope_baselayer_order_err(self): umu_run.rearrange_gamescope_baselayer_order(baselayer) def test_rearrange_gamescope_baselayer_order(self): - """Test rearrange_gamescope_baselayer_order for expected sequences.""" + """Test rearrange_gamescope_baselayer_order when passed a sequence.""" baselayer = [1, 2, 3, 4] expected = ( [baselayer[0], baselayer[-1], *baselayer[1:-1]],