diff --git a/engine/framegen/io.py b/engine/framegen/io.py index 8be95bf..70da6ef 100644 --- a/engine/framegen/io.py +++ b/engine/framegen/io.py @@ -306,6 +306,7 @@ WAYLAND = sys.platform.startswith("linux") and os.environ.get("XDG_SESSION_TYPE" MONITOR = os.environ.get("SMOOVPLAY_MONITOR", "") # The window's title (SMOOVPLAY_TITLE): lets a window rule tell launchers apart (the projector one vs. the desktop one). WINDOW_TITLE = os.environ.get("SMOOVPLAY_TITLE", "SmoovPlay") +X11 = sys.platform.startswith("linux") and not WAYLAND # window capture's overlay there: framegen/window_x11.py # SMOOVPLAY_SUPERSAMPLE=2: RTX VSR runs even when the window is no bigger than the picture (it renders at 2x and the GPU # shrinks that back), so it cleans up a capture card's compression blocks on a 1080p screen too. try: @@ -705,6 +706,8 @@ class GLWindowSink: (glfw.FOCUSED, glfw.FALSE), (glfw.FOCUS_ON_SHOW, glfw.FALSE), (glfw.MOUSE_PASSTHROUGH, glfw.TRUE)): glfw.window_hint(hint, value) + if X11: # shown by _make_overlay, once the window manager has been told to leave it alone + glfw.window_hint(glfw.VISIBLE, glfw.FALSE) self.win = self.glwin = glfw.create_window(min(w, 1600), min(h, 900), WINDOW_TITLE, None, None) glfw.default_window_hints() glfw.make_context_current(self.win) @@ -823,6 +826,7 @@ class GLWindowSink: self.menu = Menu(st, allowed=self.allowed, disabled=self.disabled, restart_reasons=restart_reasons) self.menu_panel, self.toast_panel = _Panel(GL, self.hdr10), _Panel(GL, self.hdr10) self.mini_panel, self._mini_state = _Panel(GL, self.hdr10), None + self.wm_panel, self._wm_state = _Panel(GL, self.hdr10), None # the Summit watermark self._frametimes, self._last_write, self._graph_drawn, self._gpu = collections.deque(maxlen=480), 0.0, 0.0, None self._menu_top = None # (GL y of the menu's top, the picture's (y0, height) it was placed in) while it's open self._menu_at = None # (x, GL y of the top) the menu was last drawn at, for the mouse @@ -1194,6 +1198,9 @@ class GLWindowSink: and out of the capture, which then sees the app underneath (framegen/window.py).""" import ctypes + if sys.platform != "win32": + self._make_overlay_x11() + return from .window import exclude_from_capture hwnd = self.glfw.get_win32_window(self.win) if not exclude_from_capture(hwnd): @@ -1205,10 +1212,56 @@ class GLWindowSink: u32.SetWindowLongW(hwnd, -20, u32.GetWindowLongW(hwnd, -20) | 0x08000000) # GWL_EXSTYLE |= WS_EX_NOACTIVATE self._follow() + def _make_overlay_x11(self) -> None: + """Window capture on X11 (framegen/window_x11.py): the picture goes over the game as an override-redirect + window, one the window manager doesn't manage: no frame, never focused (the game keeps the keys), and above + everything, a fullscreen game included. Clicks go through it (GLFW's mouse passthrough). The capture reads + the game's own off-screen picture, so this window isn't in it.""" + import ctypes + import ctypes.util + glfw = self.glfw + self._x11 = None + try: + class Attrs(ctypes.Structure): # XSetWindowAttributes + _fields_ = [("background_pixmap", ctypes.c_ulong), ("background_pixel", ctypes.c_ulong), + ("border_pixmap", ctypes.c_ulong), ("border_pixel", ctypes.c_ulong), + ("bit_gravity", ctypes.c_int), ("win_gravity", ctypes.c_int), + ("backing_store", ctypes.c_int), ("backing_planes", ctypes.c_ulong), + ("backing_pixel", ctypes.c_ulong), ("save_under", ctypes.c_int), + ("event_mask", ctypes.c_long), ("do_not_propagate_mask", ctypes.c_long), + ("override_redirect", ctypes.c_int), ("colormap", ctypes.c_ulong), + ("cursor", ctypes.c_ulong)] + x = ctypes.CDLL(ctypes.util.find_library("X11") or "libX11.so.6") + dpy = ctypes.cast(glfw.get_x11_display(), ctypes.c_void_p) + xid = int(glfw.get_x11_window(self.win)) + x.XChangeWindowAttributes.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, + ctypes.POINTER(Attrs)] + x.XRaiseWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong] + x.XFlush.argtypes = [ctypes.c_void_p] + x.XChangeWindowAttributes(dpy, xid, 1 << 9, ctypes.byref(Attrs(override_redirect=1))) # CWOverrideRedirect + self._x11 = (x, dpy, xid) + except Exception as e: # noqa: BLE001 - still an always-on-top window that takes no focus when it opens + print(f"[display] the picture over the game is an ordinary window here ({str(e)[:120]}): if it takes the " + "keys, click the game") + self._follow() + glfw.show_window(self.win) + self._raise_x11() + + def _raise_x11(self) -> None: + """Back above everything: a window manager raising the game (a click on it) puts it over an unmanaged window.""" + if getattr(self, "_x11", None): + x, dpy, xid = self._x11 + x.XRaiseWindow(dpy, xid) + x.XFlush(dpy) + def _start_hotkeys(self, picked: dict | None) -> None: """Window capture: the overlay never has the keys, so its own come through system-wide shortcuts (framegen/hotkeys.py), registered for this thread, the one that draws (_events takes their messages). Says which it got, over the picture and to the launcher ("[window-keys] ...").""" + if sys.platform != "win32": # no system-wide shortcuts off Windows yet: the keys all go to the game + self.KEYS_HINT = "Summit is enhancing this game. Close the game to stop." + self.toast(self.KEYS_HINT, 6.0) + return from .hotkeys import Hotkeys, describe try: self.hotkeys = Hotkeys(picked) @@ -1263,7 +1316,10 @@ class GLWindowSink: if where is None: # minimised: stay put until it's back return client, monitor = where + if X11 and self.settings.fullscreen: # the monitor --monitor names, else the one the game is on + monitor = self._monitor_rect(client) or monitor r = monitor if self.settings.fullscreen else client + self._raise_x11() if self.hotkeys is not None: self._hint_on_hover(r) at = (r.x, r.y, r.w, r.h) @@ -1272,6 +1328,23 @@ class GLWindowSink: self.glfw.set_window_pos(self.win, r.x, r.y) self.glfw.set_window_size(self.win, r.w, r.h) + def _monitor_rect(self, client): + """Window capture on X11: the monitor the picture covers in fullscreen, as a rectangle in screen pixels: the + one --monitor names (io.MONITOR), else the one holding the middle of the game's window. None if GLFW can't say.""" + glfw = self.glfw + try: + rects = [] + for m in glfw.get_monitors(): + (x, y), mode = glfw.get_monitor_pos(m), glfw.get_video_mode(m) + rects.append((m, type(client)(x, y, mode.size.width, mode.size.height))) + if MONITOR: + want = pick_monitor(glfw) + return next(r for m, r in rects if m == want) + cx, cy = client.x + client.w // 2, client.y + client.h // 2 + return next((r for _, r in rects if r.x <= cx < r.x + r.w and r.y <= cy < r.y + r.h), None) + except Exception: # noqa: BLE001 + return None + def _set_fullscreen(self, on: bool) -> None: glfw, win = self.glfw, self.win if self.overlay_of is not None: # over the app's monitor, as a borderless window: no fullscreen mode change @@ -1385,8 +1458,11 @@ class GLWindowSink: if self.dx is not None: self.dx.resize(fw, fh) # cheap when the size hasn't changed s = min(fw / self.w, fh / self.h) # letterbox, keep aspect - if self.overlay_of is not None: # window capture: the frame is the app's window squeezed to the stream's - return fw, fh, s, fw, fh, 0, 0 # size, so it's stretched back over it to fill the overlay exactly + if self.overlay_of is not None and not X11: # window capture: the frame is the app's window squeezed to the + return fw, fh, s, fw, fh, 0, 0 # stream's size, so it's stretched back over it to fill the overlay exactly + if self.overlay_of is not None: # X11: frames keep the game's shape, so fullscreen fits them to the monitor + dw, dh = round(self.w * s), round(self.h * s) + return fw, fh, s, dw, dh, (fw - dw) // 2, (fh - dh) // 2 dw, dh, x0, y0 = layout(self.fill, fw, fh, self.w, self.h) # (Fill the screen: always the same 16:9 picture) return fw, fh, s, dw, dh, x0, y0 @@ -1571,6 +1647,8 @@ class GLWindowSink: elif self.overlay and text: first, nl, rest = text.partition("\n") # the upscaler goes on the first line; a second is the test line self._blit_text(f"{first} {label}{nl}{rest}", ux, uy + uh, s) + if self.settings.watermark: + self._draw_watermark(ux, uy, uw, uh) self._draw_ui(ux, uy, uw, uh) self._present() self.settle() @@ -1676,6 +1754,19 @@ class GLWindowSink: self._graph_drawn, self._mini_state = now, key self._blit_corner(self.mini_panel, x0, y0, dw, dh, ui) + def _draw_watermark(self, x0: int, y0: int, dw: int, dh: int) -> None: + """The Summit watermark (framegen/watermark.py, settings "watermark"): small, in a bottom corner, away from the + stats. Drawn again only when its size changes.""" + from . import watermark as wm + ui = max(0.5, dh / 1080) + if self._wm_state != round(ui, 3): + self.wm_panel.update(wm.render(ui), [wm.BG], wm.OPACITY) + self._wm_state = round(ui, 3) + p = self.wm_panel + x, top = wm.place(wm.corner(self.settings.overlay_corner, bool(self.overlay)), p.w, p.h, x0, y0, dw, dh, + int(12 * ui)) + p.blit(x, top, self._target()) + def _blit_text(self, text: str, x0: int, top: int, s: float) -> None: GL = self.GL if text != self._ov_text: diff --git a/engine/run.py b/engine/run.py index c00fbed..1875be9 100644 --- a/engine/run.py +++ b/engine/run.py @@ -239,6 +239,10 @@ def main() -> int: ap.add_argument("--window", metavar="APP", help="--source window: the app's window, as a preset (ps-remote-play, xbox, moonlight), exe:NAME, " "hwnd:N or part of its title (framegen/window.py, Experimental)") + ap.add_argument("--window-wait", metavar="SECONDS", type=float, default=0.0, + help="--source window on Linux: wait this long for the window to open (a game just started)") + ap.add_argument("--watermark", choices=["on", "off"], default=None, + help="the Summit watermark in a corner of the video window. Default: the saved setting (on)") ap.add_argument("--window-keys", metavar="KEYS", default=None, help="--source window: SmoovPlay's shortcuts while the app has the keys, as " '"menu=Ctrl+Alt+K;quit=Ctrl+Alt+End" (actions: menu, quit, fullscreen, stats, smooth; the ' @@ -411,9 +415,13 @@ def main() -> int: except ValueError as e: ap.error(f"--window-keys: {e}") from framegen.sources import Clock60 - from framegen.window import open_window - try: - src = Clock60(open_window(a.window)) # the app draws when it likes: put it on a 60 Hz beat + try: # the app draws when it likes: put it on a 60 Hz beat + if sys.platform == "win32": + from framegen.window import open_window + src = Clock60(open_window(a.window)) + else: # Linux, X11 (framegen/window_x11.py) + from framegen.window_x11 import open_window + src = Clock60(open_window(a.window, wait_s=a.window_wait)) except RuntimeError as e: print(f"[window] {e}", file=sys.stderr) return 1 @@ -430,7 +438,8 @@ def main() -> int: ("interp", False if a.off else None), ("vsync", a.vsync), ("fullscreen", a.fullscreen), ("minimize_chiaki", a.minimize_chiaki), ("simulate_30", True if a.test_mode else None), ("steady_hud", a.steady_hud), ("vfg_quality", a.vfg_quality), ("present", a.present), - ("hdr", a.hdr), ("fill", a.fill), ("replay", a.replay), ("replay_clip", a.clip_length)): + ("hdr", a.hdr), ("fill", a.fill), ("replay", a.replay), ("replay_clip", a.clip_length), + ("watermark", None if a.watermark is None else a.watermark == "on")): if value is not None: settings.override(key, value) if a.look: @@ -627,6 +636,11 @@ def main() -> int: if kind == "chiaki" and hasattr(src, "set_hdr"): src.set_hdr(hdr_out, hdr_peak if hdr10[0] else None, why=hdr_why) sink = MultiSink(window, FileSink(a.out, src.w, src.h) if a.out else None) + if window is not None and sys.platform != "win32": + # `summit run --enhance` (or anything else) stops the engine with SIGTERM: leave as Esc does, so the window + # closes, clips finish saving and the summary is printed. + import signal + signal.signal(signal.SIGTERM, lambda *_: setattr(window, "cmd", "quit")) settings.watch(model_rows) # before Remote's own watcher, so what it publishes is already up to date model_rows() remote = None