KDEAmbi/wayland_poc.cpp
Tobias J. Endres 710918a622 feat: Refactor wayland_poc to use QDBus for xdg-desktop-portal interaction
This commit refactors the wayland_poc executable to use Qt's QDBus
module for interacting with xdg-desktop-portal. This approach bypasses
the problematic direct inclusion of PipeWire session manager headers
(e.g., session-manager.h) that were causing persistent compilation errors.

The wayland_poc now focuses solely on requesting screen capture permission
via D-Bus and retrieving the PipeWire node ID, without attempting to
create or manage PipeWire streams directly.

This change aims to provide a working foundation for the xdg-desktop-portal
interaction, which can then be integrated with the core PipeWire stream
capture logic (demonstrated by tutorial5.c).
2025-08-14 04:26:53 +02:00

76 lines
2.4 KiB
C++

#include <QCoreApplication>
#include <QDebug>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QVariantMap>
// QDBus callback for xdg-desktop-portal response
void handlePortalResponse(QDBusPendingCallWatcher *watcher) {
QDBusPendingCall reply = *watcher;
watcher->deleteLater();
if (reply.isError()) {
qCritical() << "D-Bus call to xdg-desktop-portal failed:" << reply.error().message();
QCoreApplication::quit();
return;
}
QList<QVariant> arguments = reply.reply().arguments();
if (arguments.isEmpty()) {
qCritical() << "xdg-desktop-portal response has no arguments.";
QCoreApplication::quit();
return;
}
QVariant firstArg = arguments.at(0);
if (!firstArg.canConvert<QVariantMap>()) {
qCritical() << "xdg-desktop-portal response first argument is not a map.";
QCoreApplication::quit();
return;
}
QVariantMap responseMap = firstArg.toMap();
if (!responseMap.contains("pipewire_node_id")) {
qCritical() << "xdg-desktop-portal response missing pipewire_node_id.";
QCoreApplication::quit();
return;
}
quint32 node_id = responseMap.value("pipewire_node_id").toUInt();
qDebug() << "xdg-desktop-portal granted screen capture. PipeWire node ID:" << node_id;
QCoreApplication::quit(); // Quit after getting the node ID for POC
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
qDebug() << "Requesting screen capture via xdg-desktop-portal...";
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (!sessionBus.isConnected()) {
qCritical() << "Failed to connect to D-Bus session bus.";
return -1;
}
QDBusMessage message = QDBusMessage::createMethodCall(
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.ScreenCast",
"SelectSources"
);
QVariantMap options;
options.insert("multiple", false); // Request single source
message << "" << options; // parent_window (empty), options
QDBusPendingCall pendingCall = sessionBus.asyncCall(message); // Use asyncCall
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, handlePortalResponse);
return app.exec();
}