diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index f5a569ddc..cc719ce35 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -406,3 +406,13 @@ carried `DXVK_HDR=0`. Whether the display can show HDR now comes from the host's either direction. The 10-bit output format is chosen only while HDR is enabled, so an SDR session keeps the 8-bit output it had, and the output images are made again in the other format when the setting changes. On a display that can show HDR, a session that starts with HDR off leaves `DXVK_HDR` unset instead of forcing it to 0, so a game started after HDR is switched on finds it. + +## 0140 Focus is chosen again when a window turns visible or fully transparent + +A window is a focus candidate only while its `_NET_WM_WINDOW_OPACITY` is above zero, but a change of that property +did not ask for focus to be chosen again. Wine sets the property for layered windows, and a window that fades in maps +fully transparent, so it was skipped when it mapped and stayed skipped once it was opaque until something else +changed focus. In a nested session with no other window the SDL window stayed hidden, and the game ran with nothing +on screen. Focus is now chosen again whenever a window's opacity crosses zero in either direction; a change between +two visible opacities leaves focus alone. `tools/tests/test_gamescope_focus_opacity.py` compiles the rule from this +patch and checks the property handler applies it. diff --git a/tools/gamescope/patches/0140-focus-follows-window-opacity.patch b/tools/gamescope/patches/0140-focus-follows-window-opacity.patch new file mode 100644 index 000000000..e4bd4f99b --- /dev/null +++ b/tools/gamescope/patches/0140-focus-follows-window-opacity.patch @@ -0,0 +1,32 @@ +diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp +index cfec551..e98d320 100644 +--- a/src/steamcompmgr.cpp ++++ b/src/steamcompmgr.cpp +@@ -6945,6 +6945,12 @@ handle_system_tray_opcode(xwayland_ctx_t *ctx, XClientMessageEvent *ev) + } + } + ++static bool ++opacity_changes_focus_candidacy( unsigned int oldOpacity, unsigned int newOpacity ) ++{ ++ return ( oldOpacity > TRANSLUCENT ) != ( newOpacity > TRANSLUCENT ); ++} ++ + /* See http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.4 */ + static void + handle_wm_change_state(xwayland_ctx_t *ctx, steamcompmgr_win_t *w, XClientMessageEvent *ev) +@@ -7329,8 +7335,14 @@ handle_property_notify(xwayland_ctx_t *ctx, XPropertyEvent *ev) + + if (newOpacity != w->opacity) + { ++ const bool bFocusCandidacyChanged = opacity_changes_focus_candidacy( w->opacity, newOpacity ); + w->opacity = newOpacity; + ++ if ( bFocusCandidacyChanged ) ++ { ++ MakeFocusDirty(); ++ } ++ + if ( gameFocused && ( w == ctx->focus.overlayWindow || w == ctx->focus.notificationWindow ) ) + { + hasRepaintNonBasePlane = true; diff --git a/tools/tests/test_gamescope_focus_opacity.py b/tools/tests/test_gamescope_focus_opacity.py new file mode 100644 index 000000000..3d5be6302 --- /dev/null +++ b/tools/tests/test_gamescope_focus_opacity.py @@ -0,0 +1,70 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PATCH = ROOT / "tools/gamescope/patches/0140-focus-follows-window-opacity.patch" + + +def added_lines(): + return [line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")] + + +def patched_rule(): + text = "\n".join(added_lines()) + start = text.index("static bool") + return text[start:text.index("\n}\n", start) + 3] + + +PREAMBLE = r''' +#include +#include + +#define TRANSLUCENT 0x00000000 +#define OPAQUE 0xffffffff +''' + +MAIN = r''' +int main() { + assert(opacity_changes_focus_candidacy(TRANSLUCENT, OPAQUE)); + assert(opacity_changes_focus_candidacy(TRANSLUCENT, 0x01010101u)); + assert(opacity_changes_focus_candidacy(OPAQUE, TRANSLUCENT)); + assert(opacity_changes_focus_candidacy(0x80808080u, TRANSLUCENT)); + assert(!opacity_changes_focus_candidacy(0x40404040u, 0xc0c0c0c0u)); + assert(!opacity_changes_focus_candidacy(OPAQUE, 0x80808080u)); + assert(!opacity_changes_focus_candidacy(TRANSLUCENT, TRANSLUCENT)); + assert(!opacity_changes_focus_candidacy(OPAQUE, OPAQUE)); + puts("ok"); + return 0; +} +''' + + +@unittest.skipUnless(shutil.which("c++"), "needs a C++ compiler") +class FocusOpacityRuleTest(unittest.TestCase): + def test_only_crossing_fully_transparent_changes_focus_candidacy(self): + with tempfile.TemporaryDirectory() as tmp: + source = Path(tmp) / "opacity.cpp" + binary = Path(tmp) / "opacity" + source.write_text(PREAMBLE + patched_rule() + MAIN) + subprocess.run(["c++", "-std=c++17", "-Wall", "-Werror", str(source), "-o", str(binary)], check=True) + self.assertEqual(subprocess.run([str(binary)], capture_output=True, text=True, check=True).stdout.strip(), "ok") + + +class FocusOpacityHandlerTest(unittest.TestCase): + def test_opacity_property_change_reevaluates_focus_when_candidacy_changes(self): + hunks = PATCH.read_text().split("\n@@ ") + handler = next(hunk for hunk in hunks if "handle_property_notify(" in hunk.split("\n", 1)[0]) + added = [line[1:].strip() for line in handler.splitlines() if line.startswith("+")] + self.assertIn("const bool bFocusCandidacyChanged = opacity_changes_focus_candidacy( w->opacity, newOpacity );", added) + self.assertIn("MakeFocusDirty();", added) + context = [line[1:].strip() for line in handler.splitlines() if line.startswith(" ")] + self.assertIn("w->opacity = newOpacity;", context) + self.assertLess(added.index("const bool bFocusCandidacyChanged = opacity_changes_focus_candidacy( w->opacity, newOpacity );"), added.index("MakeFocusDirty();")) + + +if __name__ == "__main__": + unittest.main()