From ff76d0ec99c3b9247bc4ff50985042008127d7ee Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 27 Mar 2026 22:35:42 -0400 Subject: [PATCH] Autodiscover WAYLAND_DISPLAY from XDG_RUNTIME_DIR Add automatic discovery of a Wayland display socket when WAYLAND_DISPLAY is not set. Introduce discover_wayland_display_name() to scan XDG_RUNTIME_DIR for wayland-* entries (preferring wayland-0) and try_autodiscover_wayland_display() to export the found name via qputenv. Call the autodiscovery at tray initialization and emit a log message on success. Also include QStringList header required for the new code. --- src/tray_linux.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/tray_linux.cpp b/src/tray_linux.cpp index c796d2f..a9facdf 100644 --- a/src/tray_linux.cpp +++ b/src/tray_linux.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,58 @@ namespace { return QFileInfo::exists(socket_path); } + QString discover_wayland_display_name() { + if (!qEnvironmentVariableIsEmpty("WAYLAND_DISPLAY")) { + return QString(); + } + + const QByteArray runtime_dir_env = qgetenv("XDG_RUNTIME_DIR"); + if (runtime_dir_env.isEmpty()) { + return QString(); + } + + const QString runtime_dir_path = QString::fromLocal8Bit(runtime_dir_env).trimmed(); + if (runtime_dir_path.isEmpty()) { + return QString(); + } + + const QDir runtime_dir(runtime_dir_path); + if (!runtime_dir.exists()) { + return QString(); + } + + const QStringList entries = runtime_dir.entryList( + QStringList() << QStringLiteral("wayland-*"), + QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System, + QDir::Name + ); + if (entries.isEmpty()) { + return QString(); + } + + QString selected; + for (const QString &entry : entries) { + if (const QString candidate_path = runtime_dir.filePath(entry); !QFileInfo::exists(candidate_path)) { + continue; + } + if (entry == QStringLiteral("wayland-0")) { + return entry; + } + if (selected.isEmpty()) { + selected = entry; + } + } + return selected; + } + + bool try_autodiscover_wayland_display() { + const QString discovered = discover_wayland_display_name(); + if (discovered.isEmpty()) { + return false; + } + return qputenv("WAYLAND_DISPLAY", discovered.toLocal8Bit()); + } + bool has_x11_display_endpoint() { const QByteArray display_env = qgetenv("DISPLAY"); if (display_env.isEmpty()) { @@ -799,6 +852,9 @@ extern "C" { int tray_init(struct tray *tray) { if (QApplication::instance() == nullptr) { + if (try_autodiscover_wayland_display() && g_log_cb != nullptr) { + g_log_cb(1, "Qt tray: auto-discovered WAYLAND_DISPLAY from XDG_RUNTIME_DIR"); + } if (should_force_headless_qpa_fallback()) { qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal")); if (g_log_cb != nullptr) {