Skip to content

Commit

Permalink
fix: check baselayer sequence before rearranging (#193)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
R1kaB3rN authored Sep 25, 2024
1 parent 3e71418 commit 9547922
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
30 changes: 24 additions & 6 deletions umu/umu_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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

Expand Down
16 changes: 15 additions & 1 deletion umu/umu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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]],
Expand Down

0 comments on commit 9547922

Please sign in to comment.