Skip to content

Commit 748e8e8

Browse files
committed
fix(RunningApp): focus apps by app id instead of window id
1 parent 539abf9 commit 748e8e8

File tree

9 files changed

+117
-149
lines changed

9 files changed

+117
-149
lines changed

core/global/launch_manager.gd

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,21 @@ func _init() -> void:
108108
# If the app has a gamepad profile, set it
109109
if self._current_app:
110110
set_app_gamepad_profile(self._current_app)
111-
112111
_xwayland_primary.focused_app_updated.connect(on_focused_app_changed)
113112

113+
# Listen for when focusable apps change
114+
var on_focusable_apps_changed := func(from: PackedInt64Array, to: PackedInt64Array):
115+
if from == to:
116+
return
117+
logger.debug("Focusable apps changed from", from, "to", to)
118+
# If focusable apps has changed and the currently focused app no longer exists,
119+
# remove the manual focus
120+
var baselayer_app := _xwayland_primary.baselayer_app
121+
to.append(_xwayland_primary.focused_app)
122+
if baselayer_app > 0 and not baselayer_app in to:
123+
_xwayland_primary.remove_baselayer_app()
124+
_xwayland_primary.focusable_apps_updated.connect(on_focusable_apps_changed)
125+
114126
# Whenever the in-game state is entered, set the gamepad profile
115127
var on_game_state_entered := func(_from: State):
116128
if _current_app:
@@ -201,7 +213,6 @@ func launch(app: LibraryLaunchItem) -> RunningApp:
201213
env["DISPLAY"] = _xwayland_game.name
202214
else:
203215
env["DISPLAY"] = ""
204-
var display := env["DISPLAY"] as String
205216

206217
# Set the OGUI ID environment variable
207218
env["OGUI_ID"] = app.name

core/systems/launcher/running_app.gd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func update_wayland_app() -> void:
189189
state = STATE.RUNNING
190190
#grab_focus() # How can we grab wayland window focus?
191191

192+
# Update the focus state of the app
193+
focused = is_focused()
194+
192195
var state_str := {
193196
STATE.STARTED: "started",
194197
STATE.RUNNING: "running",
@@ -373,7 +376,7 @@ func get_child_pids() -> PackedInt32Array:
373376

374377
## Returns whether or not the app can be switched to/focused
375378
func can_focus() -> bool:
376-
return window_id > 0
379+
return self.app_id > 0
377380

378381

379382
## Return true if the currently running app is focused
@@ -383,8 +386,8 @@ func is_focused() -> bool:
383386
var xwayland_primary := gamescope.get_xwayland(gamescope.XWAYLAND_TYPE_PRIMARY)
384387
if not xwayland_primary:
385388
return false
386-
var focused_window := xwayland_primary.focused_window
387-
return window_id == focused_window or focused_window in window_ids
389+
var focused_app := xwayland_primary.focused_app
390+
return self.app_id == focused_app
388391

389392

390393
## Focuses to the app's window
@@ -394,14 +397,15 @@ func grab_focus() -> void:
394397
var xwayland_primary := gamescope.get_xwayland(gamescope.XWAYLAND_TYPE_PRIMARY)
395398
if not xwayland_primary:
396399
return
397-
xwayland_primary.set_baselayer_window(window_id)
400+
xwayland_primary.baselayer_app = self.app_id
398401
focused = true
399402

400403

401404
## Switches the app window to the given window ID. Returns an error if unable
402405
## to switch to the window
403406
func switch_window(win_id: int, focus: bool = true) -> int:
404407
# Error if the window does not belong to the running app
408+
# TODO: Look into how window switching can work with Wayland windows
405409
if not win_id in window_ids:
406410
return ERR_DOES_NOT_EXIST
407411

@@ -418,6 +422,7 @@ func switch_window(win_id: int, focus: bool = true) -> int:
418422
window_id = win_id
419423
if focus:
420424
grab_focus()
425+
xwayland_primary.baselayer_window = win_id
421426
return OK
422427

423428

@@ -446,11 +451,10 @@ func _ensure_app_id() -> void:
446451

447452
# Try setting the app ID on each possible Window. If they are valid windows,
448453
# gamescope will make these windows available as focusable windows.
449-
var app_name := launch_item.name
450454
for window in possible_windows:
451455
if xwayland.has_app_id(window):
452456
continue
453-
xwayland.set_app_id(window, window)
457+
xwayland.set_app_id(window, self.app_id)
454458

455459

456460
## Returns whether or not the window id of the running app needs to be discovered

core/systems/network/unix_socket_client.gd

Lines changed: 0 additions & 59 deletions
This file was deleted.

core/ui/components/input_texture_rect.gd

Lines changed: 0 additions & 53 deletions
This file was deleted.

extensions/Cargo.lock

Lines changed: 27 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ godot = { git = "https://github.com/godot-rust/gdext", branch = "master", featur
1313
"register-docs",
1414
] }
1515
nix = { version = "0.29.0", features = ["term", "process"] }
16-
once_cell = "1.19.0"
16+
once_cell = "1.20.1"
1717
tokio = { version = "1.39.3", features = ["full"] }
1818
zbus = "4.4.0"
1919
zvariant = "4.2.0"

0 commit comments

Comments
 (0)