65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import argparse
|
|
import os
|
|
import sys
|
|
|
|
|
|
# parse_args: function definition for ScanSlicer behavior.
|
|
def parse_args(argv=None):
|
|
parser = argparse.ArgumentParser(
|
|
prog="scanslicer",
|
|
description="ScanSlicer Qt UI (Wayland/X11)",
|
|
)
|
|
parser.add_argument(
|
|
"path",
|
|
nargs="?",
|
|
help="Optional input directory containing images",
|
|
)
|
|
parser.add_argument(
|
|
"--backend",
|
|
choices=("auto", "wayland", "x11"),
|
|
default="auto",
|
|
help="Display backend selection (default: auto)",
|
|
)
|
|
return parser.parse_args(argv)
|
|
|
|
|
|
# _detect_backend_from_qpa_env: function definition for ScanSlicer behavior.
|
|
def _detect_backend_from_qpa_env():
|
|
qpa_value = os.environ.get("QT_QPA_PLATFORM", "").strip().lower()
|
|
if not qpa_value:
|
|
return None
|
|
|
|
tokens = [token.strip() for token in qpa_value.split(";") if token.strip()]
|
|
for token in tokens:
|
|
if token == "wayland":
|
|
return "wayland"
|
|
if token in ("xcb", "x11"):
|
|
return "x11"
|
|
return None
|
|
|
|
|
|
# resolve_runtime_backend: function definition for ScanSlicer behavior.
|
|
def resolve_runtime_backend(backend):
|
|
if backend in ("wayland", "x11"):
|
|
return backend
|
|
|
|
env_backend = _detect_backend_from_qpa_env()
|
|
if env_backend is not None:
|
|
return env_backend
|
|
|
|
session_type = os.environ.get("XDG_SESSION_TYPE", "").strip().lower()
|
|
if session_type == "wayland":
|
|
return "wayland"
|
|
if session_type == "x11":
|
|
return "x11"
|
|
|
|
runtime_dir = os.environ.get("XDG_RUNTIME_DIR", "").strip()
|
|
wayland_display = os.environ.get("WAYLAND_DISPLAY", "").strip()
|
|
if runtime_dir and wayland_display and os.path.exists(os.path.join(runtime_dir, wayland_display)):
|
|
return "wayland"
|
|
|
|
if os.environ.get("DISPLAY", "").strip():
|
|
return "x11"
|
|
|
|
# Fall back to Wayland when auto-detection has no clear signal.
|
|
return "wayland"
|