Skip to content

Commit be38d26

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 f7fa64a commit be38d26

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/tray_linux.cpp

Lines changed: 57 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,59 @@ 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+
const QString candidate_path = runtime_dir.filePath(entry);
177+
if (!QFileInfo::exists(candidate_path)) {
178+
continue;
179+
}
180+
if (entry == QStringLiteral("wayland-0")) {
181+
return entry;
182+
}
183+
if (selected.isEmpty()) {
184+
selected = entry;
185+
}
186+
}
187+
return selected;
188+
}
189+
190+
bool try_autodiscover_wayland_display() {
191+
const QString discovered = discover_wayland_display_name();
192+
if (discovered.isEmpty()) {
193+
return false;
194+
}
195+
return qputenv("WAYLAND_DISPLAY", discovered.toLocal8Bit());
196+
}
197+
144198
bool has_x11_display_endpoint() {
145199
const QByteArray display_env = qgetenv("DISPLAY");
146200
if (display_env.isEmpty()) {
@@ -799,6 +853,9 @@ extern "C" {
799853

800854
int tray_init(struct tray *tray) {
801855
if (QApplication::instance() == nullptr) {
856+
if (try_autodiscover_wayland_display() && g_log_cb != nullptr) {
857+
g_log_cb(1, "Qt tray: auto-discovered WAYLAND_DISPLAY from XDG_RUNTIME_DIR");
858+
}
802859
if (should_force_headless_qpa_fallback()) {
803860
qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("minimal"));
804861
if (g_log_cb != nullptr) {

0 commit comments

Comments
 (0)