From 3e57b845fdfbe6cd2cdddea6c5c8f3f93cc764c6 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 27 Mar 2026 18:46:13 -0400 Subject: [PATCH] Force minimal QPA when no display endpoint Add detection for reachable Wayland/X11 endpoints and fall back to the minimal QPA if none are present. Introduce QDir include and helper functions has_wayland_display_endpoint(), has_x11_display_endpoint(), and should_force_headless_qpa_fallback() to check WAYLAND_DISPLAY and DISPLAY (including XDG_RUNTIME_DIR sockets and /tmp/.X11-unix/X sockets, as well as remote/TCP displays). If QT_QPA_PLATFORM is unset and no local display endpoints are found, set QT_QPA_PLATFORM=minimal before creating QApplication and emit a log via g_log_cb. This prevents trying to use GUI QPAs when no local display is reachable. --- src/tray_linux.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/tray_linux.cpp b/src/tray_linux.cpp index 0ce2df9..c796d2f 100644 --- a/src/tray_linux.cpp +++ b/src/tray_linux.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,75 @@ namespace { return !qgetenv("WAYLAND_DISPLAY").isEmpty(); } + bool has_wayland_display_endpoint() { + const QByteArray wayland_display = qgetenv("WAYLAND_DISPLAY"); + if (wayland_display.isEmpty()) { + return false; + } + + const QString display_name = QString::fromLocal8Bit(wayland_display).trimmed(); + if (display_name.isEmpty()) { + return false; + } + + if (const QFileInfo direct_path(display_name); direct_path.exists()) { + return true; + } + + const QByteArray runtime_dir = qgetenv("XDG_RUNTIME_DIR"); + if (runtime_dir.isEmpty()) { + return false; + } + + const QString socket_path = QDir(QString::fromLocal8Bit(runtime_dir)).filePath(display_name); + return QFileInfo::exists(socket_path); + } + + bool has_x11_display_endpoint() { + const QByteArray display_env = qgetenv("DISPLAY"); + if (display_env.isEmpty()) { + return false; + } + + const QString display = QString::fromLocal8Bit(display_env).trimmed(); + if (display.isEmpty()) { + return false; + } + + if (display.startsWith('/')) { + return QFileInfo::exists(display); + } + + if (!display.startsWith(':')) { + // Remote/TCP displays are not locally discoverable; treat as potentially usable. + return true; + } + + int digit_end = 1; + while (digit_end < display.size() && display.at(digit_end).isDigit()) { + digit_end++; + } + if (digit_end == 1) { + return true; + } + + bool ok = false; + const int display_number = display.mid(1, digit_end - 1).toInt(&ok); + if (!ok) { + return true; + } + + const QString socket_path = QStringLiteral("/tmp/.X11-unix/X%1").arg(display_number); + return QFileInfo::exists(socket_path); + } + + bool should_force_headless_qpa_fallback() { + if (!qEnvironmentVariableIsEmpty("QT_QPA_PLATFORM")) { + return false; + } + return !has_wayland_display_endpoint() && !has_x11_display_endpoint(); + } + QPoint screen_anchor_point(const QScreen *screen) { if (screen == nullptr) { return QPoint(); @@ -729,6 +799,12 @@ extern "C" { int tray_init(struct tray *tray) { if (QApplication::instance() == nullptr) { + if (should_force_headless_qpa_fallback()) { + qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal")); + if (g_log_cb != nullptr) { + g_log_cb(2, "Qt tray: no reachable WAYLAND_DISPLAY or DISPLAY endpoint, forcing QT_QPA_PLATFORM=minimal"); + } + } static int argc = 0; g_app = std::make_unique(argc, nullptr); g_app_owned = true;