Skip to content

Commit ff76d0e

Browse files
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.
1 parent 3e57b84 commit ff76d0e

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/tray_linux.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QMenu>
3030
#include <QPixmap>
3131
#include <QScreen>
32+
#include <QStringList>
3233
#include <QStyle>
3334
#include <QSystemTrayIcon>
3435
#include <QThread>
@@ -141,6 +142,58 @@ namespace {
141142
return QFileInfo::exists(socket_path);
142143
}
143144

145+
QString discover_wayland_display_name() {
146+
if (!qEnvironmentVariableIsEmpty("WAYLAND_DISPLAY")) {
147+
return QString();
148+
}
149+
150+
const QByteArray runtime_dir_env = qgetenv("XDG_RUNTIME_DIR");
151+
if (runtime_dir_env.isEmpty()) {
152+
return QString();
153+
}
154+
155+
const QString runtime_dir_path = QString::fromLocal8Bit(runtime_dir_env).trimmed();
156+
if (runtime_dir_path.isEmpty()) {
157+
return QString();
158+
}
159+
160+
const QDir runtime_dir(runtime_dir_path);
161+
if (!runtime_dir.exists()) {
162+
return QString();
163+
}
164+
165+
const QStringList entries = runtime_dir.entryList(
166+
QStringList() << QStringLiteral("wayland-*"),
167+
QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System,
168+
QDir::Name
169+
);
170+
if (entries.isEmpty()) {
171+
return QString();
172+
}
173+
174+
QString selected;
175+
for (const QString &entry : entries) {
176+
if (const QString candidate_path = runtime_dir.filePath(entry); !QFileInfo::exists(candidate_path)) {
177+
continue;
178+
}
179+
if (entry == QStringLiteral("wayland-0")) {
180+
return entry;
181+
}
182+
if (selected.isEmpty()) {
183+
selected = entry;
184+
}
185+
}
186+
return selected;
187+
}
188+
189+
bool try_autodiscover_wayland_display() {
190+
const QString discovered = discover_wayland_display_name();
191+
if (discovered.isEmpty()) {
192+
return false;
193+
}
194+
return qputenv("WAYLAND_DISPLAY", discovered.toLocal8Bit());
195+
}
196+
144197
bool has_x11_display_endpoint() {
145198
const QByteArray display_env = qgetenv("DISPLAY");
146199
if (display_env.isEmpty()) {
@@ -799,6 +852,9 @@ extern "C" {
799852

800853
int tray_init(struct tray *tray) {
801854
if (QApplication::instance() == nullptr) {
855+
if (try_autodiscover_wayland_display() && g_log_cb != nullptr) {
856+
g_log_cb(1, "Qt tray: auto-discovered WAYLAND_DISPLAY from XDG_RUNTIME_DIR");
857+
}
802858
if (should_force_headless_qpa_fallback()) {
803859
qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal"));
804860
if (g_log_cb != nullptr) {

0 commit comments

Comments
 (0)