diff options
| author | fox <fox@fakecake.org> | 2026-07-02 11:06:59 +0300 |
|---|---|---|
| committer | fox <fox@fakecake.org> | 2026-07-02 11:06:59 +0300 |
| commit | 935d430e777cb69288fe0e16c8ec033487e7b1c0 (patch) | |
| tree | 6dc76a9a7b17f9e1d178872fbeb999c46f8079d3 | |
| parent | a782622e31e74008ab8558df00f8723a8ade7286 (diff) | |
| parent | 425388b5ba61d97890d9788b5442f7e7167169cd (diff) | |
| download | fungus-launcher-935d430e777cb69288fe0e16c8ec033487e7b1c0.tar.gz fungus-launcher-935d430e777cb69288fe0e16c8ec033487e7b1c0.tar.xz | |
Merge branch 'dev' into 'master'
we don't really need imagemagick See merge request git/faugus-launcher!23
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | faugus-launcher.spec | 2 | ||||
| -rw-r--r-- | faugus/add_game_dialog.py | 8 | ||||
| -rw-r--r-- | faugus/run_manager.py | 363 | ||||
| -rw-r--r-- | ruff.toml | 1 |
5 files changed, 185 insertions, 191 deletions
diff --git a/README.md b/README.md index 1d04f39..8337a2d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ the application back to its description (i.e. a simple and lightweight game laun ### Dependencies: ``` -meson ninja pygobject requests pillow psutil imagemagick icoextract bubblewrap +meson ninja pygobject requests pillow psutil icoextract bubblewrap ``` ## Build from source diff --git a/faugus-launcher.spec b/faugus-launcher.spec index 860b397..0a83a0b 100644 --- a/faugus-launcher.spec +++ b/faugus-launcher.spec @@ -9,7 +9,7 @@ Source0: https://gitlab.fakecake.org/git/faugus-launcher/-/archive/master BuildArch: noarch BuildRequires: meson gtk-update-icon-cache python3-devel -Requires: python3 python3-gobject python3-requests python3-icoextract python3-pillow python3-psutil ImageMagick libcanberra-gtk3 vulkan-loader(x86-32) bubblewrap +Requires: python3 python3-gobject python3-requests python3-icoextract python3-pillow python3-psutil libcanberra-gtk3 vulkan-loader(x86-32) bubblewrap %description A simple and lightweight app for running Windows games using UMU-Launcher/UMU-Proton. diff --git a/faugus/add_game_dialog.py b/faugus/add_game_dialog.py index fe016dd..e8d68c5 100644 --- a/faugus/add_game_dialog.py +++ b/faugus/add_game_dialog.py @@ -610,8 +610,8 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog): else: print(f"Error extracting icon: {result.stderr}") else: - command_magick = shutil.which("magick") or shutil.which("convert") - os.system(f'{command_magick} "{self.icon_extracted}" "{self.icon_converted}"') + img = Image.open(self.icon_extracted) + img.save(self.icon_converted) if os.path.isfile(self.icon_extracted): os.remove(self.icon_extracted) @@ -921,8 +921,8 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog): else: print(f"Error extracting icon: {result.stderr}") else: - command_magick = shutil.which("magick") or shutil.which("convert") - os.system(f'{command_magick} "{self.icon_extracted}" "{self.icon_converted}"') + img = Image.open(self.icon_extracted) + img.save(self.icon_converted) if os.path.isfile(self.icon_extracted): os.remove(self.icon_extracted) diff --git a/faugus/run_manager.py b/faugus/run_manager.py index 01cef30..4a2614e 100644 --- a/faugus/run_manager.py +++ b/faugus/run_manager.py @@ -12,7 +12,7 @@ import psutil # type: ignore import sys import time from pathlib import Path -from threading import Thread +from threading import Thread, Event gi.require_version("Gtk", "3.0") @@ -52,6 +52,7 @@ class FaugusRun: self.process = None self.splash_window = None self.error_output = [] + self._cleanup_event = Event() # signals cleanup thread to stop self.load_config() signal.signal(signal.SIGUSR1, self.on_sigusr1) @@ -113,6 +114,10 @@ class FaugusRun: print(f"Runner: {self.game.runner or 'default'}") print(f"Prefix: {self.game.prefix}\n") + # Start the cleanup watchdog thread (monitors for orphaned bwrap) + self._watchdog_thread = Thread(target=self._watchdog_loop, daemon=True) + self._watchdog_thread.start() + self.execute_final_command() def build_bwrap_command( @@ -152,10 +157,13 @@ class FaugusRun: inner_cmd = " ".join(inner_cmd_parts) # ── Sandbox namespaces ────────────────────────────────────────── + # NOTE: We deliberately omit --as-pid-1. Without it, bwrap installs + # a proper reaper (PID 1) inside the namespace that reaps zombies + # and exits when the command exits. With --as-pid-1, bwrap becomes + # PID 1 itself and can linger after the inner command finishes. bwrap_args.extend([ "bwrap", "--die-with-parent", - "--as-pid-1", "--unshare-user", "--unshare-cgroup", "--unshare-pid", @@ -302,6 +310,8 @@ class FaugusRun: self.on_process_exit, ) + + def show_error_dialog_steam_runtime(self): """Show error when UMU Steam Runtime is not available.""" SteamRuntimeWarningDialog() @@ -379,101 +389,27 @@ class FaugusRun: sys.exit() def _show_process_error(self, exit_code: int) -> bool: - """Show an error dialog with the last few stderr lines.""" + """Show process error in the splash window with accumulated output.""" if not self.splash_window: self.show_splash_window() - # Take the last 10 non-empty lines for brevity - error_lines = [ - line for line in self.error_output[-10:] - if line.strip() - ] - if error_lines: - error_msg = "\n".join(error_lines) - else: - error_msg = "No diagnostic output available." - + # Update label with exit code self.label.set_text( "Process exited with code %d" % exit_code ) - if self.splash_window: - self.splash_window.show_all() - - # Queue a timeout to destroy the splash after showing the error - GLib.timeout_add_seconds(1, self._show_error_dialog, exit_code, error_msg) - return False - - def _show_error_dialog(self, exit_code: int, error_msg: str) -> bool: - """Show the error dialog after a brief delay.""" - if self.splash_window: - self.splash_window.destroy() - self.splash_window = None - - dialog = Gtk.Dialog(title="Faugus Launcher") - dialog.set_resizable(False) - dialog.set_icon_from_file(faugus_png) - subprocess.Popen(["canberra-gtk-play", "-f", faugus_notification]) - - content_area = dialog.get_content_area() - content_area.set_border_width(0) - content_area.set_halign(Gtk.Align.CENTER) - content_area.set_valign(Gtk.Align.CENTER) - content_area.set_vexpand(True) - content_area.set_hexpand(True) - - box_top = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10) - box_top.set_margin_start(20) - box_top.set_margin_end(20) - box_top.set_margin_top(20) - box_top.set_margin_bottom(20) - - label = Gtk.Label( - label="The game runner terminated with exit code %d." % exit_code, - ) - label.set_halign(Gtk.Align.CENTER) - label.set_line_wrap(True) - label.set_max_width_chars(60) - - label2 = Gtk.Label( - label="Check the terminal output for details.", - ) - label2.set_halign(Gtk.Align.CENTER) - - box_top.pack_start(label, True, True, 0) - box_top.pack_start(label2, True, True, 0) - - # Error output details in a scrolled text view - if error_msg and error_msg != "No diagnostic output available.": - scrolled = Gtk.ScrolledWindow() - scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) - scrolled.set_min_content_height(120) - text_view = Gtk.TextView() - text_view.set_editable(False) - text_view.set_cursor_visible(False) - buffer = text_view.get_buffer() - buffer.set_text(error_msg) - scrolled.add(text_view) - box_top.pack_start(scrolled, True, True, 0) - - button_ok = Gtk.Button(label="Ok") - button_ok.set_size_request(150, -1) - button_ok.connect("clicked", lambda w: dialog.response(Gtk.ResponseType.OK)) - box_bottom = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10) - box_bottom.set_margin_start(10) - box_bottom.set_margin_end(10) - box_bottom.set_margin_bottom(10) - box_bottom.pack_start(button_ok, True, True, 0) + # Show accumulated stderr in the text view + if self.error_output: + error_text = "\n".join(self.error_output) + self._append_output(f"\n--- stderr ({len(self.error_output)} lines) ---") + self._append_output(error_text) - content_area.add(box_top) - content_area.add(box_bottom) + # Reveal output view and close button + self.scrolled_output.set_visible(True) + self.close_button.set_visible(True) + self.close_button.set_sensitive(True) - dialog.show_all() - dialog.run() - dialog.destroy() - Gtk.main_quit() - sys.exit() - return False # unreachable, but satisfies lint + return False def show_error_dialog_no_proton(self): """Show error when no proton versions are installed at all.""" @@ -607,16 +543,91 @@ class FaugusRun: self.default_runner = cfg.get("default-runner", "") self.game_directories = cfg.get("game-directories", "").strip() + def _watchdog_loop(self): + """Background thread that monitors our specific bwrap process. + + Runs every 5 seconds. If the launcher subprocess has exited but + our bwrap process (tracked by PID) is still alive, it is killed. + Only targets our own bwrap — never indiscriminately kills bwrap. + """ + while not self._cleanup_event.is_set(): + self._cleanup_event.wait(timeout=5.0) + if self._cleanup_event.is_set(): + break + + # Check if our main process is still alive + if self.process and self.process.poll() is None: + continue # game is still running, no action needed + + # Process has exited — check if our specific bwrap is still alive + if self.process and self.process.pid: + try: + bwrap_proc = psutil.Process(self.process.pid) + if bwrap_proc.is_running() and bwrap_proc.status() != psutil.STATUS_ZOMBIE: + print(f"[watchdog] Killing orphaned bwrap (PID {bwrap_proc.pid})") + bwrap_proc.kill() + bwrap_proc.wait(timeout=2) + except psutil.NoSuchProcess: + pass # already gone + except psutil.TimeoutExpired: + pass # give up, it'll be cleaned up by the system + def _show_sandbox_warning(self, exec_path: str, valid_dirs: list[str]) -> bool: """Show the sandbox whitelist warning dialog.""" SandboxWarningDialog(exec_path, valid_dirs) return False # do not repeat + def _kill_children(self): + """Kill the bwrap process and all its descendants. + + Uses a two-phase approach: SIGTERM first, then SIGKILL after + a brief timeout, to give the process tree a chance to clean up. + """ + if self.process and self.process.poll() is None: + try: + parent = psutil.Process(self.process.pid) + children = parent.children(recursive=True) + + # Phase 1: SIGTERM to all children, then parent + for child in children: + try: + child.send_signal(signal.SIGTERM) + except (psutil.NoSuchProcess, psutil.AccessDenied): + pass + try: + parent.send_signal(signal.SIGTERM) + except (psutil.NoSuchProcess, psutil.AccessDenied): + pass + + # Phase 2: Wait briefly, then SIGKILL stragglers + time.sleep(0.5) + still_alive = parent.children(recursive=True) + try: + still_alive.append(parent) + except psutil.NoSuchProcess: + pass + + for proc in still_alive: + try: + if proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE: + proc.kill() + except (psutil.NoSuchProcess, psutil.AccessDenied): + pass + + # Final wait to let zombies be reaped + try: + parent.wait(timeout=2) + except (psutil.TimeoutExpired, psutil.NoSuchProcess): + pass + + except psutil.NoSuchProcess: + pass + def show_splash_window(self): self.splash_window = Gtk.Window(title="Faugus Launcher") self.splash_window.set_decorated(False) self.splash_window.set_resizable(False) - self.splash_window.set_default_size(280, -1) + self.splash_window.set_default_size(840, 400) self.splash_window.set_icon_from_file(faugus_png) frame = Gtk.Frame() @@ -624,38 +635,75 @@ class FaugusRun: frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN) grid = Gtk.Grid() - grid.set_halign(Gtk.Align.CENTER) - grid.set_valign(Gtk.Align.CENTER) + grid.set_hexpand(True) + grid.set_vexpand(True) + grid.set_row_spacing(10) frame.add(grid) + # Logo image_path = faugus_png pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path) - pixbuf = pixbuf.scale_simple(75, 75, GdkPixbuf.InterpType.BILINEAR) - image = Gtk.Image.new_from_pixbuf(pixbuf) image.set_margin_top(20) - image.set_margin_start(20) - image.set_margin_end(20) - image.set_margin_bottom(20) + image.set_margin_bottom(5) grid.attach(image, 0, 0, 1, 1) - self.label = Gtk.Label() - self.label.set_margin_start(20) - self.label.set_margin_end(20) - self.label.set_margin_bottom(20) - + # Game title + self.label = Gtk.Label(label=self.game.title or self.game.gameid or "") + self.label.set_halign(Gtk.Align.CENTER) + self.label.set_margin_bottom(5) grid.attach(self.label, 0, 1, 1, 1) + # Live output text view (hidden until output arrives) + self.output_buffer = Gtk.TextBuffer() + self.text_view = Gtk.TextView(buffer=self.output_buffer) + self.text_view.set_editable(False) + self.text_view.set_cursor_visible(False) + self.text_view.set_monospace(True) + self.text_view.set_margin_top(5) + self.text_view.set_margin_bottom(5) + self.text_view.set_margin_start(10) + self.text_view.set_margin_end(10) + self.text_view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR) + self.text_view.get_style_context().add_class("output-view") + + self.scrolled_output = Gtk.ScrolledWindow() + self.scrolled_output.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + self.scrolled_output.set_min_content_height(120) + self.scrolled_output.set_hexpand(True) + self.scrolled_output.set_vexpand(True) + self.scrolled_output.set_visible(False) + self.scrolled_output.add(self.text_view) + grid.attach(self.scrolled_output, 0, 2, 1, 1) + + # Close button (hidden until error) + self.close_button = Gtk.Button(label="Close") + self.close_button.set_sensitive(False) + self.close_button.set_size_request(150, -1) + self.close_button.connect("clicked", self._on_splash_close) + grid.attach(self.close_button, 0, 3, 1, 1) + self.splash_window.add(frame) self.splash_window.show_all() - def on_output(self, source, condition): - try: - source.set_encoding(None) - except Exception: - pass + def _on_splash_close(self, _widget): + """Close the splash window and quit the application.""" + if self.splash_window: + self.splash_window.destroy() + self.splash_window = None + Gtk.main_quit() + sys.exit(0) + + def _append_output(self, line: str) -> None: + """Append a line to the output text buffer.""" + buf = self.output_buffer + end = buf.get_end_iter() + buf.insert(end, line + "\n") + # Scroll to bottom + self.text_view.scroll_to_iter(end, 0.0, True, 0.0, 1.0) + def on_output(self, source, condition): def remove_ansi_escape(text): ansi_escape = re.compile( r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])" @@ -663,6 +711,7 @@ class FaugusRun: return ansi_escape.sub("", text) raw = source.readline() + if raw: if isinstance(raw, bytes): line = raw.decode("utf-8", errors="ignore") @@ -675,82 +724,34 @@ class FaugusRun: if self.process and source is self.process.stderr and clean_line: self.error_output.append(clean_line) + # Show output in splash window (io_add_watch already runs in main thread) + self._show_output_in_splash(clean_line) + self.check_game_output(clean_line) print(line, end="") return True + def _show_output_in_splash(self, line: str) -> None: + """Append a line to the splash output text view.""" + if self.splash_window and self.scrolled_output: + self.scrolled_output.set_visible(True) + self._append_output(line) + def check_game_output(self, clean_line): - if ( - "Downloading" in clean_line - or "Updating BattlEye..." in clean_line - or "Updating Easy Anti-Cheat..." in clean_line - or "Updating UMU-Launcher..." in clean_line - ): - if self.splash_window: - self.splash_window.show_all() - - if "Updating UMU-Launcher..." in clean_line: - self.label.set_text("Updating UMU-Launcher...") - if "UMU-Launcher is up to date." in clean_line: - self.label.set_text("UMU-Launcher is up to date") - if "Updating BattlEye..." in clean_line: - self.label.set_text("Updating BattlEye...") - if "Updating Easy Anti-Cheat..." in clean_line: - self.label.set_text("Updating Easy Anti-Cheat...") - if "Components are up to date." in clean_line: - self.label.set_text("Components are up to date") - if "Downloading UMU-Proton" in clean_line: - self.label.set_text("Downloading UMU-Proton...") - if "Downloading steamrt3 (latest)" in clean_line: - self.label.set_text("Downloading Steam Runtime...") - if "SteamLinuxRuntime_sniper.tar.xz" in clean_line: - self.label.set_text("Extracting Steam Runtime...") - if "Extracting UMU-Proton" in clean_line: - self.label.set_text("Extracting UMU-Proton...") - if "UMU-Proton is up to date" in clean_line: - self.label.set_text("UMU-Proton is up to date") - if "steamrt3 is up to date" in clean_line: - self.label.set_text("Steam Runtime is up to date") - if "->" in clean_line and "GE-Proton" in clean_line: - self.label.set_text("GE-Proton is up to date") - if "->" in clean_line and "UMU-Proton" in clean_line: - self.label.set_text("UMU-Proton is up to date") - if "mtree is OK" in clean_line: - self.label.set_text("Steam Runtime is up to date") - - if "Downloading GE-Proton" in clean_line: - self.label.set_text("Downloading GE-Proton...") - if "Extracting GE-Proton" in clean_line: - self.label.set_text("Extracting GE-Proton...") - if "GE-Proton is up to date" in clean_line: - self.label.set_text("GE-Proton is up to date") - if "Downloading Proton-EM" in clean_line: - self.label.set_text("Downloading Proton-EM...") - if "Extracting Proton-EM" in clean_line: - self.label.set_text("Extracting Proton-EM...") - if "Proton-EM is up to date" in clean_line: - self.label.set_text("Proton-EM is up to date") - - if "network error" in clean_line: - self.show_error_dialog(network_error=True) - - if "steamrt" in clean_line.lower() and ("not found" in clean_line.lower() or "missing" in clean_line.lower() or "error" in clean_line.lower()): - self.show_error_dialog_steam_runtime() + """Detect when the game has started and close the splash window. - if ( - "fsync" in clean_line - or "NTSync" in clean_line - - or "Selected GPU" in clean_line - or "Skipping fix execution" in clean_line - or "Executable a unix path" in clean_line - or "status: 0" in clean_line - or "PosixPath" in clean_line - or "SingleInstance" in clean_line - or "mtree is OK" in clean_line - ): + Heuristic: certain log lines indicate the game runtime is active + and we can safely dismiss the splash. + """ + game_running_signals = [ + "fsync", "NTSync", + "Selected GPU", "Skipping fix execution", + "Executable a unix path", "status: 0", + "PosixPath", "SingleInstance", "mtree is OK", + ] + if any(signal in clean_line for signal in game_running_signals): GLib.timeout_add_seconds(0, self.close_splash_window) def close_splash_window(self): @@ -786,6 +787,9 @@ class FaugusRun: def do_process_exit(self): """Shared cleanup: kill children, save playtime, close UI, quit.""" + # Stop the watchdog thread + self._cleanup_event.set() + # Kill any remaining child processes (bwrap + descendants) self._kill_children() @@ -809,17 +813,6 @@ class FaugusRun: GLib.idle_add(self.close_splash_window) GLib.idle_add(Gtk.main_quit) - def _kill_children(self): - """Kill the bwrap process and all its descendants.""" - if self.process and self.process.poll() is None: - try: - parent = psutil.Process(self.process.pid) - for child in parent.children(recursive=True): - child.kill() - parent.kill() - except psutil.NoSuchProcess: - pass - def handle_command(game: Game): run = FaugusRun(game) diff --git a/ruff.toml b/ruff.toml index 945f3a5..019010b 100644 --- a/ruff.toml +++ b/ruff.toml @@ -17,6 +17,7 @@ ignore = [ "UP031", # percent-format vs f-string (cosmetic, pre-existing in codebase) "PLW0603",# global statement (used deliberately for module-level state flags) + "PERF203",# try-except within loop (acceptable in cleanup/watchdog code) ] |
