Skip to content

Commit

Permalink
umu_run: handle display connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
R1kaB3rN committed Jul 13, 2024
1 parent 15decf6 commit 652e515
Showing 1 changed file with 79 additions and 33 deletions.
112 changes: 79 additions & 33 deletions umu/umu_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
sys.path.append(os.environ["UMU_CLIENT_RTPATH"])

from Xlib import Xatom, display
from Xlib.error import DisplayConnectionError
from Xlib.xobject.drawable import Window

from umu.umu_consts import (
DEBUG_FORMAT,
Expand Down Expand Up @@ -441,7 +443,17 @@ def build_command(

def get_window_client_ids() -> list[str]:
"""Get the list of client windows."""
d = display.Display(":1")
d: display.Display
root: Window

try:
# FIXME: For the general case, don't assume gamescope will create a
# server associated to :1
d = display.Display(":1")
except (DisplayConnectionError, ConnectionRefusedError) as e:
log.exception(e)
return []

try:
root = d.screen().root

Expand All @@ -465,50 +477,70 @@ def get_window_client_ids() -> list[str]:
time.sleep(wait_interval)
elapsed_time += wait_interval
return []
except Exception as e:
log.exception(e)
finally:
d.close()

return []


def set_steam_game_property( # noqa: D103
window_ids: list[str], steam_assigned_layer_id: int
) -> None:
d = display.Display(":1")
d: display.Display
window: Window

try:
root = d.screen().root
log.debug("Root: %s", root)

for window_id in window_ids:
log.debug("window_id: %s", window_id)
log.debug("steam_layer: %s", steam_assigned_layer_id)
try:
window = d.create_resource_object("window", int(window_id))
window.get_full_property(
d.intern_atom("STEAM_GAME"), Xatom.CARDINAL
)
window.change_property(
d.intern_atom("STEAM_GAME"),
Xatom.CARDINAL,
32,
[int(steam_assigned_layer_id)],
)
log.debug(
"Successfully set STEAM_GAME property for window ID: %s",
window_id,
)
except Exception as e:
log.error(
"Error setting STEAM_GAME property for window ID: %s",
window_id,
)
log.exception(e)
except Exception as e:
d = display.Display(":1")
except (DisplayConnectionError, ConnectionRefusedError) as e:
log.exception(e)
finally:
d.close()
return

root = d.screen().root
log.debug("Root: %s", root)

for window_id in window_ids:
log.debug("window_id: %s", window_id)
log.debug("steam_layer: %s", steam_assigned_layer_id)
try:
window = d.create_resource_object("window", int(window_id))
atom = d.intern_atom("STEAM_GAME")

window.get_full_property(
atom,
Xatom.CARDINAL,
)
window.change_property(
atom,
Xatom.CARDINAL,
32,
[int(steam_assigned_layer_id)],
)
log.debug(
"Successfully set STEAM_GAME property for window ID: %s",
window_id,
)
except Exception as e:
log.error(
"Error setting STEAM_GAME property for window ID: %s",
window_id,
)
log.exception(e)
finally:
d.close()


def get_gamescope_baselayer_order() -> list[int] | None: # noqa: D103
d = display.Display(":0")
d: display.Display
root: Window

try:
d = display.Display(":0")
except (DisplayConnectionError, ConnectionRefusedError) as e:
log.exception(e)
return None

try:
root = d.screen().root

Expand All @@ -519,8 +551,11 @@ def get_gamescope_baselayer_order() -> list[int] | None: # noqa: D103
prop = root.get_full_property(atom, Xatom.CARDINAL)

if prop:
log.debug("GAMESCOPECTRL_BASELAYER_APPID property: %s", prop)
log.debug("GAMESCOPECTRL_BASELAYER_APPID value: %s", prop.value)
# Extract and return the value
return prop.value # type: ignore

log.debug("GAMESCOPECTRL_BASELAYER_APPID property not found")
except Exception as e:
log.error("Error getting GAMESCOPECTRL_BASELAYER_APPID property")
Expand All @@ -547,8 +582,19 @@ def rearrange_gamescope_baselayer_order( # noqa


def set_gamescope_baselayer_order(rearranged: list[int]) -> None: # noqa
d: display.Display
root: Window

try:
# FIXME: :0 is invalid when under tty sessions > 1. Instead of
# connecting to :0, we need to use the current DISPLAY. While
# this is OK for the Steam Deck, it is not OK for the general case
d = display.Display(":0")
except (DisplayConnectionError, ConnectionRefusedError) as e:
log.exception(e)
return

try:
root = d.screen().root

# Intern the atom for GAMESCOPECTRL_BASELAYER_APPID
Expand Down

0 comments on commit 652e515

Please sign in to comment.