Merge pull request #4622

f9fa89b linux: use screenshot portal for Wayland QR scanning (Waozi)

ACKs: jpk68, selsta
This commit is contained in:
tobtoht committed 2026-11-16 20:53:40 +00:00
commit 8e163c60da
4 files changed
+274 -17

No files matched your search

+3
View File
@@ -161,6 +161,9 @@ set(QT_COMPONENTS
Widgets
Xml
)
if(UNIX AND NOT APPLE AND NOT ANDROID)
list(APPEND QT_COMPONENTS DBus)
endif()
if(QML_TESTS)
list(APPEND QT_COMPONENTS QuickTest Test)
endif()
+12 -2
View File
@@ -17,7 +17,7 @@ ENV CMAKE_PREFIX_PATH="${QT_PREFIX}"
RUN apt update && \
DEBIAN_FRONTEND=noninteractive apt install -y \
aria2 automake autopoint bison bzip2 ca-certificates curl gettext git g++ make \
gperf libarchive-tools libgl1-mesa-dev libglib2.0-dev libpng-dev libpthread-stubs0-dev \
gperf libarchive-tools libexpat1-dev libgl1-mesa-dev libglib2.0-dev libpng-dev libpthread-stubs0-dev \
libtool-bin libudev-dev mesa-common-dev \
ninja-build pkg-config python3 python-is-python3 wget xutils-dev && \
rm -rf /var/lib/apt/lists/*
@@ -41,6 +41,16 @@ RUN wget -q https://github.com/Kitware/CMake/releases/download/v4.3.3/cmake-4.3.
rm cmake-4.3.3-linux-x86_64.tar.gz && \
cmake --version
RUN wget https://dbus.freedesktop.org/releases/dbus/dbus-1.12.2.tar.gz && \
echo "272bb5091770b047c8188b926d5e6038fa4fe6745488b2add96b23e2d9a83d88 dbus-1.12.2.tar.gz" | sha256sum -c && \
tar -xzf dbus-1.12.2.tar.gz && \
rm dbus-1.12.2.tar.gz && \
cd dbus-1.12.2 && \
./configure --prefix="${QT_PREFIX}" --disable-shared --enable-static --disable-systemd --disable-launchd --disable-selinux --disable-apparmor --disable-libaudit --disable-xml-docs --disable-doxygen-docs --disable-ducktype-docs --disable-modular-tests --disable-embedded-tests --disable-x11-autolaunch --without-x --with-systemdsystemunitdir=no --with-systemduserunitdir=no && \
make -j$THREADS && \
make -j$THREADS install && \
rm -rf $(pwd)
RUN git clone -b xorgproto-2020.1 --depth 1 https://gitlab.freedesktop.org/xorg/proto/xorgproto && \
cd xorgproto && \
git reset --hard c62e8203402cafafa5ba0357b6d1c019156c9f36 && \
@@ -218,7 +228,7 @@ RUN rm -f /usr/lib/x86_64-linux-gnu/libX11.a \
-submodules qtbase,qtdeclarative,qtshadertools,qtsvg,qttools \
-prefix ${QT_PREFIX} -opensource -confirm-license -release -static -no-avx \
-opengl desktop -xcb -no-feature-xlib -system-freetype -fontconfig -no-feature-eglfs -no-glib \
-no-dbus -no-feature-qml-worker-script -no-feature-linuxfb -no-openssl -no-feature-sql -no-feature-kms \
-dbus-linked -no-feature-qml-worker-script -no-feature-linuxfb -no-openssl -no-feature-sql -no-feature-kms \
-no-feature-testlib -no-feature-quick-particles -no-feature-qml-xmllistmodel -no-feature-qml-debug \
-no-feature-quickcontrols2-imagine -no-feature-quickcontrols2-universal -no-feature-quickcontrols2-fluentwinui3 \
-no-feature-printsupport -no-feature-pdf -no-feature-vulkan -no-feature-sessionmanager -no-feature-gtk3 \
+237 -13
View File
@@ -58,6 +58,15 @@
#endif
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
#include "qt/utils.h"
#include <QDBusConnection>
#include <QDBusError>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDBusObjectPath>
#include <QDBusPendingReply>
#include <QDBusVariant>
#include <QEventLoop>
#include <QTimer>
#endif
#include "QR-Code-scanner/Decoder.h"
@@ -67,7 +76,7 @@
namespace
{
QPixmap screenshot()
std::unordered_set<QWindow *> hideVisibleWindows()
{
std::unordered_set<QWindow *> hidden;
const QWindowList windows = QGuiApplication::allWindows();
@@ -80,24 +89,49 @@ QPixmap screenshot()
window->hide();
}
}
#if defined(Q_OS_MAC)
// Let Qt submit the native hide operations before blocking the GUI thread.
QGuiApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
// Give WindowServer time to finish removing the window.
QThread::msleep(100);
#endif
const auto unhide = sg::make_scope_guard([&hidden]() {
for (QWindow *window : hidden)
{
window->show();
}
});
return QGuiApplication::primaryScreen()->grabWindow(0);
return hidden;
}
void showWindows(const std::unordered_set<QWindow *> &windows)
{
for (QWindow *window : windows)
{
window->show();
}
}
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
bool isWayland()
{
if (QGuiApplication::platformName().contains("wayland", Qt::CaseInsensitive))
{
return true;
}
if (!qEnvironmentVariableIsEmpty("WAYLAND_DISPLAY"))
{
return true;
}
return QString::fromLocal8Bit(qgetenv("XDG_SESSION_TYPE")).compare(QStringLiteral("wayland"), Qt::CaseInsensitive) == 0;
}
QVariant unwrapDbusVariant(const QVariant &value)
{
if (value.canConvert<QDBusVariant>())
{
return value.value<QDBusVariant>().variant();
}
return value;
}
#endif
} // namespace
#if defined(Q_OS_WIN)
@@ -132,6 +166,191 @@ OSHelper::OSHelper(QObject *parent) : QObject(parent)
}
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
void OSHelper::resetScreenshotPortalResponse()
{
m_screenshotPortalResponseReceived = false;
m_screenshotPortalResponse = 1;
m_screenshotPortalResults.clear();
}
void OSHelper::handleScreenshotPortalResponse(uint responseCode, const QVariantMap &responseResults)
{
m_screenshotPortalResponseReceived = true;
m_screenshotPortalResponse = responseCode;
m_screenshotPortalResults = responseResults;
emit screenshotPortalResponseReceived();
}
QImage OSHelper::screenshotPortal()
{
if (m_screenshotPortalInProgress)
{
qWarning() << "XDG screenshot portal request is already in progress";
return QImage();
}
m_screenshotPortalInProgress = true;
const auto resetInProgress = sg::make_scope_guard([this]() {
m_screenshotPortalInProgress = false;
});
QDBusConnection sessionBus = QDBusConnection::sessionBus();
QDBusInterface portal(
QStringLiteral("org.freedesktop.portal.Desktop"),
QStringLiteral("/org/freedesktop/portal/desktop"),
QStringLiteral("org.freedesktop.portal.Screenshot"),
sessionBus);
if (!portal.isValid())
{
qWarning() << "XDG screenshot portal is unavailable:" << sessionBus.lastError().message();
return QImage();
}
resetScreenshotPortalResponse();
const QString handleToken = QStringLiteral("monero_gui_%1_%2")
.arg(QRandomGenerator::global()->generate(), 8, 16, QLatin1Char('0'))
.arg(QRandomGenerator::global()->generate(), 8, 16, QLatin1Char('0'));
QString sender = sessionBus.baseService();
if (sender.startsWith(QLatin1Char(':')))
{
sender.remove(0, 1);
}
sender.replace(QLatin1Char('.'), QLatin1Char('_'));
if (sender.isEmpty())
{
qWarning() << "XDG screenshot portal cannot determine DBus sender name";
return QImage();
}
QString requestPath = QStringLiteral("/org/freedesktop/portal/desktop/request/%1/%2").arg(sender, handleToken);
if (!sessionBus.connect(
QStringLiteral("org.freedesktop.portal.Desktop"),
requestPath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handleScreenshotPortalResponse(uint,QVariantMap))))
{
qWarning() << "Failed to connect to XDG screenshot portal response";
return QImage();
}
const auto disconnectRequest = sg::make_scope_guard([this, sessionBus, &requestPath]() mutable {
sessionBus.disconnect(
QStringLiteral("org.freedesktop.portal.Desktop"),
requestPath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handleScreenshotPortalResponse(uint,QVariantMap)));
});
QVariantMap options;
options.insert(QStringLiteral("interactive"), true);
options.insert(QStringLiteral("modal"), true);
options.insert(QStringLiteral("handle_token"), handleToken);
QDBusPendingReply<QDBusObjectPath> reply = portal.asyncCallWithArgumentList(
QStringLiteral("Screenshot"),
QVariantList{QString(), options});
reply.waitForFinished();
if (reply.isError())
{
qWarning() << "XDG screenshot portal request failed:" << reply.error().message();
return QImage();
}
const QString returnedRequestPath = reply.value().path();
if (returnedRequestPath != requestPath)
{
sessionBus.disconnect(
QStringLiteral("org.freedesktop.portal.Desktop"),
requestPath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handleScreenshotPortalResponse(uint,QVariantMap)));
requestPath = returnedRequestPath;
if (!sessionBus.connect(
QStringLiteral("org.freedesktop.portal.Desktop"),
requestPath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Response"),
this,
SLOT(handleScreenshotPortalResponse(uint,QVariantMap))))
{
qWarning() << "Failed to connect to XDG screenshot portal response";
return QImage();
}
}
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
QObject::connect(this, SIGNAL(screenshotPortalResponseReceived()), &loop, SLOT(quit()));
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(60000);
if (!m_screenshotPortalResponseReceived)
{
loop.exec();
}
if (!m_screenshotPortalResponseReceived)
{
sessionBus.asyncCall(QDBusMessage::createMethodCall(
QStringLiteral("org.freedesktop.portal.Desktop"),
requestPath,
QStringLiteral("org.freedesktop.portal.Request"),
QStringLiteral("Close")));
qWarning() << "Timed out waiting for XDG screenshot portal response";
return QImage();
}
if (m_screenshotPortalResponse != 0)
{
qWarning() << "XDG screenshot portal request was not approved:" << m_screenshotPortalResponse;
return QImage();
}
const QVariant uriValue = unwrapDbusVariant(m_screenshotPortalResults.value(QStringLiteral("uri")));
const QUrl uri(uriValue.toString());
if (!uri.isLocalFile())
{
qWarning() << "XDG screenshot portal returned an unsupported URI:" << uri;
return QImage();
}
const QString imagePath = uri.toLocalFile();
QImage image(imagePath);
QFile::remove(imagePath);
if (image.isNull())
{
qWarning() << "Failed to load XDG screenshot portal image:" << uri;
}
return image;
}
#endif
QImage OSHelper::screenshot()
{
const std::unordered_set<QWindow *> hidden = hideVisibleWindows();
const auto unhide = sg::make_scope_guard([&hidden]() {
showWindows(hidden);
});
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
if (isWayland())
{
return screenshotPortal();
}
#endif
return QGuiApplication::primaryScreen()->grabWindow(0).toImage();
}
void OSHelper::createDesktopEntry() const
{
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
@@ -144,13 +363,18 @@ QString OSHelper::downloadLocation() const
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
}
QList<QString> OSHelper::grabQrCodesFromScreen() const
QList<QString> OSHelper::grabQrCodesFromScreen()
{
QList<QString> codes;
try
{
const QImage image = screenshot().toImage();
const QImage image = screenshot();
if (image.isNull())
{
return codes;
}
const std::vector<std::string> decoded = QrDecoder().decode(image);
std::for_each(decoded.begin(), decoded.end(), [&codes](const std::string &code) {
codes.push_back(QString::fromStdString(code));
+22 -2
View File
@@ -30,8 +30,12 @@
#define OSHELPER_H
#include <QList>
#include <QImage>
#include <QObject>
#include <QString>
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
#include <QVariantMap>
#endif
/**
* @brief The OSHelper class - exports to QML some OS-related functions
*/
@@ -47,7 +51,7 @@ public:
Q_INVOKABLE void createDesktopEntry() const;
Q_INVOKABLE QString downloadLocation() const;
Q_INVOKABLE QList<QString> grabQrCodesFromScreen() const;
Q_INVOKABLE QList<QString> grabQrCodesFromScreen();
Q_INVOKABLE bool openFile(const QString &filePath) const;
Q_INVOKABLE bool openContainingFolder(const QString &filePath) const;
Q_INVOKABLE QString openSaveFileDialog(const QString &title, const QString &folder, const QString &filename) const;
@@ -62,10 +66,26 @@ public:
static std::pair<quint8, QString> getNetworkTypeAndAddressFromFile(const QString &wallet);
private:
QImage screenshot();
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
QImage screenshotPortal();
void resetScreenshotPortalResponse();
bool m_screenshotPortalResponseReceived = false;
uint m_screenshotPortalResponse = 1;
QVariantMap m_screenshotPortalResults;
bool m_screenshotPortalInProgress = false;
#endif
signals:
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
void screenshotPortalResponseReceived();
#endif
public slots:
private slots:
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
void handleScreenshotPortalResponse(uint responseCode, const QVariantMap &responseResults);
#endif
};
#endif // OSHELPER_H