fix(linux,crostini): fix OldLondon glyph clipping and prevent audio buffer underruns
This commit is contained in:
parent
4caed5bc15
commit
e1f4c35c35
49
main.py
49
main.py
|
|
@ -4,12 +4,16 @@ import signal
|
|||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import tkinter as tk
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
# Unterdrückt die "Hello from the pygame community"-Konsole beim Start
|
||||
os.environ.setdefault("PYGAME_HIDE_SUPPORT_PROMPT", "1")
|
||||
if sys.platform.startswith("linux"):
|
||||
# Entspannt PulseAudio/CRAS-Pufferung unter Linux & ChromeOS (Crostini) gegen krakeligen Ton
|
||||
os.environ.setdefault("PULSE_LATENCY_MSEC", "80")
|
||||
|
||||
import pygame
|
||||
|
||||
|
|
@ -23,6 +27,31 @@ from view.window_state import WindowStatePersistence
|
|||
APP_WM_CLASS = "goth-cardgame-collection"
|
||||
|
||||
|
||||
def _install_linux_label_descent_fix():
|
||||
"""
|
||||
Xft/FreeType unter Linux & Crostini meldet für Zierschriften (wie OldLondon) zu knappe
|
||||
Unterlängen (descent), wodurch Buchstaben wie g, j, p, y unten abgeschnitten werden.
|
||||
Sorgt bei reinen Text-Labels auf Linux automatisch für minimalen vertikalen Innenabstand.
|
||||
"""
|
||||
if not sys.platform.startswith("linux") or getattr(tk.Label, "_goth_descent_patched", False):
|
||||
return
|
||||
|
||||
orig_init = tk.Label.__init__
|
||||
|
||||
def _patched_label_init(self, master=None, cnf={}, **kw):
|
||||
has_image = ("image" in kw and kw["image"] is not None) or (isinstance(cnf, dict) and cnf.get("image"))
|
||||
if not has_image:
|
||||
cur_pady = kw.get("pady", cnf.get("pady") if isinstance(cnf, dict) else None)
|
||||
if cur_pady is None:
|
||||
kw["pady"] = 3
|
||||
elif isinstance(cur_pady, int) and cur_pady < 3:
|
||||
kw["pady"] = 3
|
||||
orig_init(self, master, cnf, **kw)
|
||||
|
||||
tk.Label.__init__ = _patched_label_init
|
||||
tk.Label._goth_descent_patched = True
|
||||
|
||||
|
||||
def _prepare_os_app_identity():
|
||||
if sys.platform.startswith("win"):
|
||||
try:
|
||||
|
|
@ -33,7 +62,6 @@ def _prepare_os_app_identity():
|
|||
except Exception:
|
||||
pass
|
||||
elif sys.platform.startswith("linux"):
|
||||
# Hilft X11/Tkinter bereits vor Fenstererstellung bei der Ressourcen-Zuordnung
|
||||
os.environ.setdefault("RESOURCE_NAME", APP_WM_CLASS)
|
||||
try:
|
||||
import ctypes
|
||||
|
|
@ -42,6 +70,20 @@ def _prepare_os_app_identity():
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
# Audio-Pre-Init vor jeglicher Pygame-Initialisierung setzen
|
||||
try:
|
||||
is_linux = sys.platform.startswith("linux")
|
||||
is_cros = is_linux and (
|
||||
Path("/dev/.cros_milestone").exists()
|
||||
or "sommelier" in os.environ.get("XDG_RUNTIME_DIR", "").lower()
|
||||
or Path("/opt/google/cros-containers").exists()
|
||||
)
|
||||
freq = 48000 if is_linux else 44100
|
||||
buf_size = 8192 if is_cros else (4096 if is_linux else 1024)
|
||||
pygame.mixer.pre_init(frequency=freq, size=-16, channels=2, buffer=buf_size)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _extract_ico_frames(icon_path: Path) -> tuple[Image.Image | None, list[Image.Image]]:
|
||||
"""Liest das größte Bild aus game_icon.ico (auch wenn es intern PNG/JPEG/BMP-codiert ist)."""
|
||||
|
|
@ -221,7 +263,6 @@ def _apply_cross_platform_icon(app, root_path: Path):
|
|||
app.tk.call("tk", "appname", APP_WM_CLASS)
|
||||
except Exception:
|
||||
pass
|
||||
# Erzwingt die exakte X11 WM_CLASS am Top-Level-Fenster für Sommelier/Wayland
|
||||
try:
|
||||
app.update_idletasks()
|
||||
subprocess.run(
|
||||
|
|
@ -256,7 +297,6 @@ def _apply_cross_platform_icon(app, root_path: Path):
|
|||
|
||||
if cascade_pils:
|
||||
try:
|
||||
# Unter Linux/XWayland max. 128x128 übergeben, da 256x256 in _NET_WM_ICON oft grau wird
|
||||
usable_pils = (
|
||||
[im for im in cascade_pils if im.width <= 128]
|
||||
if sys.platform.startswith("linux")
|
||||
|
|
@ -374,9 +414,10 @@ def _install_clean_shutdown(app: GothCardCollectionView):
|
|||
|
||||
def main():
|
||||
_prepare_os_app_identity()
|
||||
_install_linux_label_descent_fix()
|
||||
|
||||
root_path = Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parent))
|
||||
|
||||
# Vor dem Erstellen des Tk-Fensters aufrufen, damit garcon das Icon bereits kennt!
|
||||
ensure_crostini_shelf_icon((root_path / APP_ICON_REL_PATH).resolve(), APP_WM_CLASS)
|
||||
|
||||
FontLoader.register_fonts(root_path)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# models/sound_manager.py
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from models.database import Database
|
||||
from resources.code_res.game_constants import (
|
||||
DEFAULT_VOL_AMBIANCE, DEFAULT_VOL_MENU,
|
||||
INGAME_MUSIC_FILE, MENU_MUSIC_FILE, SOUND_MAP
|
||||
)
|
||||
|
||||
if sys.platform.startswith("linux"):
|
||||
# Verhindert Audio-Stottern / Knacksen in VM- & Container-Umgebungen (insb. ChromeOS Crostini)
|
||||
os.environ.setdefault("PULSE_LATENCY_MSEC", "80")
|
||||
|
||||
try:
|
||||
import pygame
|
||||
PYGAME_AVAILABLE = True
|
||||
|
|
@ -14,6 +21,16 @@ except ImportError:
|
|||
PYGAME_AVAILABLE = False
|
||||
|
||||
|
||||
def _is_crostini_env() -> bool:
|
||||
if not sys.platform.startswith("linux"):
|
||||
return False
|
||||
return (
|
||||
Path("/dev/.cros_milestone").exists()
|
||||
or "sommelier" in os.environ.get("XDG_RUNTIME_DIR", "").lower()
|
||||
or Path("/opt/google/cros-containers").exists()
|
||||
)
|
||||
|
||||
|
||||
class SoundManager:
|
||||
def __init__(self, sounds_dir: Path):
|
||||
self.ambiance_dir = sounds_dir / "ambiance"
|
||||
|
|
@ -27,8 +44,25 @@ class SoundManager:
|
|||
self._ready = False
|
||||
|
||||
if PYGAME_AVAILABLE:
|
||||
self._init_mixer()
|
||||
|
||||
def _init_mixer(self):
|
||||
is_linux = sys.platform.startswith("linux")
|
||||
is_cros = _is_crostini_env()
|
||||
|
||||
# ChromeOS CRAS arbeitet nativ mit 48 kHz und benötigt über die VM-Grenze einen großen Puffer
|
||||
freq = 48000 if is_linux else 44100
|
||||
buf_size = 8192 if is_cros else (4096 if is_linux else 1024)
|
||||
|
||||
try:
|
||||
if pygame.mixer.get_init():
|
||||
pygame.mixer.quit()
|
||||
pygame.mixer.pre_init(frequency=freq, size=-16, channels=2, buffer=buf_size)
|
||||
pygame.mixer.init(frequency=freq, size=-16, channels=2, buffer=buf_size)
|
||||
self._ready = True
|
||||
except Exception:
|
||||
try:
|
||||
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
|
||||
pygame.mixer.init()
|
||||
self._ready = True
|
||||
except Exception:
|
||||
self._ready = False
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ APP_WM_CLASS = "goth-cardgame-collection"
|
|||
class GothCardCollectionView(tk.Tk):
|
||||
def __init__(self, ctrl, root_dir: Path):
|
||||
super().__init__(baseName=APP_WM_CLASS, className=APP_WM_CLASS)
|
||||
if sys.platform.startswith("linux"):
|
||||
self.option_add("*Label.padY", 4)
|
||||
self.ctrl = ctrl
|
||||
self.title(t("app_title"))
|
||||
self.configure(bg=BG_COLOR)
|
||||
|
|
|
|||
Loading…
Reference in New Issue