Skip to content
Advertisement

Check if at least one Wayland session is running

I’m using Qt and want to check if any Wayland session is running.

For now I have this just for test code, that works as expected:

QProcess process;
process.setProgram("bash");
process.setArguments({"-c", "loginctl list-sessions --no-legend | awk '{print $1}'"});
process.start();
process.waitForFinished();
const QByteArrayList sessionsList = process.readAll().split('n');

foreach (const QByteArray &sessionID, sessionsList) {
    if (sessionID.isEmpty())
        continue;
    process.setArguments({"-c", "loginctl show-session " + sessionID});
    process.start();
    process.waitForFinished();
    if (process.readAll().contains("Type=wayland"))
        qDebug() << "At least one Wayland session is running";
}

Is it possible to get rid of QProcess and use Linux C++ API?

Advertisement

Answer

You can try to connect to Wayland. If you can connect to a Wayland composer it is running otherwise is not running or someone haven’t set the environment variables correctly and not used the default name.

#include <wayland/wayland-client-core.h>

bool isWaylandRunning(){
    auto display = wl_display_connect(nullptr);
    if (display){
        wl_display_disconnect(display);
        return true;
    } else {
        return false;
    }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement