Add option to use wl_display_add_socket_auto

Summary:
If automaticSocketNaming is enabled, it will use wl_display_add_socket_auto
to allocate the next free socket. The resulting name can be retrieved using
socketName after a successful start afterwards.

Test Plan: Ran the new autotest, passes. kwin_wayland still uses the old behaviour.

Reviewers: #kwin, #plasma, romangg

Reviewed By: #kwin, #plasma, romangg

Subscribers: davidedmundson, zzag, romangg, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D17122
This commit is contained in:
Fabian Vogt 2018-11-23 14:42:18 +01:00
parent 57e1bc0430
commit 2184206c8b
3 changed files with 67 additions and 1 deletions

View file

@ -43,6 +43,7 @@ private Q_SLOTS:
void testClientConnection();
void testConnectNoSocket();
void testOutputManagement();
void testAutoSocketName();
};
void TestWaylandServerDisplay::testSocketName()
@ -215,6 +216,29 @@ void TestWaylandServerDisplay::testOutputManagement()
QVERIFY(kwin->isValid());
}
void TestWaylandServerDisplay::testAutoSocketName()
{
QTemporaryDir runtimeDir;
QVERIFY(runtimeDir.isValid());
QVERIFY(qputenv("XDG_RUNTIME_DIR", runtimeDir.path().toUtf8()));
Display display0;
display0.setAutomaticSocketNaming(true);
QSignalSpy socketNameChangedSpy0(&display0, SIGNAL(socketNameChanged(QString)));
display0.start();
QVERIFY(display0.isRunning());
QCOMPARE(socketNameChangedSpy0.count(), 0);
QCOMPARE(display0.socketName(), QStringLiteral("wayland-0"));
Display display1;
display1.setAutomaticSocketNaming(true);
QSignalSpy socketNameChangedSpy1(&display1, SIGNAL(socketNameChanged(QString)));
display1.start();
QVERIFY(display1.isRunning());
QCOMPARE(socketNameChangedSpy1.count(), 1);
QCOMPARE(display1.socketName(), QStringLiteral("wayland-1"));
}
QTEST_GUILESS_MAIN(TestWaylandServerDisplay)
#include "test_display.moc"

View file

@ -84,6 +84,7 @@ public:
wl_event_loop *loop = nullptr;
QString socketName = QStringLiteral("wayland-0");
bool running = false;
bool automaticSocketNaming = false;
QList<OutputInterface*> outputs;
QList<OutputDeviceInterface*> outputdevices;
QVector<SeatInterface*> seats;
@ -161,13 +162,39 @@ QString Display::socketName() const
return d->socketName;
}
void Display::setAutomaticSocketNaming(bool automaticSocketNaming)
{
if (d->automaticSocketNaming == automaticSocketNaming) {
return;
}
d->automaticSocketNaming = automaticSocketNaming;
emit automaticSocketNamingChanged(automaticSocketNaming);
}
bool Display::automaticSocketNaming() const
{
return d->automaticSocketNaming;
}
void Display::start(StartMode mode)
{
Q_ASSERT(!d->running);
Q_ASSERT(!d->display);
d->display = wl_display_create();
if (mode == StartMode::ConnectToSocket) {
if (wl_display_add_socket(d->display, qPrintable(d->socketName)) != 0) {
if (d->automaticSocketNaming) {
const char *socket = wl_display_add_socket_auto(d->display);
if (socket == nullptr) {
qCWarning(KWAYLAND_SERVER) << "Failed to create Wayland socket";
return;
}
const QString newEffectiveSocketName = QString::fromUtf8(socket);
if (d->socketName != newEffectiveSocketName) {
d->socketName = newEffectiveSocketName;
emit socketNameChanged(d->socketName);
}
} else if (wl_display_add_socket(d->display, qPrintable(d->socketName)) != 0) {
qCWarning(KWAYLAND_SERVER) << "Failed to create Wayland socket";
return;
}

View file

@ -100,14 +100,28 @@ class KWAYLANDSERVER_EXPORT Display : public QObject
{
Q_OBJECT
Q_PROPERTY(QString socketName READ socketName WRITE setSocketName NOTIFY socketNameChanged)
Q_PROPERTY(bool automaticSocketNaming READ automaticSocketNaming WRITE setAutomaticSocketNaming NOTIFY automaticSocketNamingChanged)
Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
public:
explicit Display(QObject *parent = nullptr);
virtual ~Display();
/**
* Sets the basename of the socket to @p name. If @p name is empty, it will use
* wl_display_add_socket_auto to get a free socket with a filename "wayland-%d".
**/
void setSocketName(const QString &name);
QString socketName() const;
/**
* If automaticSocketNaming is true, the manually set socketName is ignored
* and it will use wl_display_add_socket_auto on start to get a free socket with
* a filename "wayland-%d" instead. The effective socket is written into socketName.
* @since 5.55
**/
void setAutomaticSocketNaming(bool automaticSocketNaming);
bool automaticSocketNaming() const;
quint32 serial();
quint32 nextSerial();
@ -316,6 +330,7 @@ public:
Q_SIGNALS:
void socketNameChanged(const QString&);
void automaticSocketNamingChanged(bool);
void runningChanged(bool);
void aboutToTerminate();
void clientConnected(KWayland::Server::ClientConnection*);