From 1b1f0c6b32546faf525105f8f64ee7e8b66e5e13 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 17 Mar 2023 17:12:08 +0200 Subject: [PATCH] effects/screenshot: Provide information about captured window or screen This can be useful for the screenshot capture tool if it needs some information about the captured window or screen, for example to generate the file name. --- .../screenshot/org.kde.KWin.ScreenShot2.xml | 18 ++++++++ .../screenshot/screenshotdbusinterface2.cpp | 43 ++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/effects/screenshot/org.kde.KWin.ScreenShot2.xml b/src/effects/screenshot/org.kde.KWin.ScreenShot2.xml index 00490c6ef8..1c7011c453 100644 --- a/src/effects/screenshot/org.kde.KWin.ScreenShot2.xml +++ b/src/effects/screenshot/org.kde.KWin.ScreenShot2.xml @@ -53,6 +53,8 @@ image type is "raw" * "format" (u): The image format, as defined in QImage::Format. Available only if the image type is "raw" + * "windowId" (s): The window id of the captured window. Available + since version 4. --> @@ -96,6 +98,8 @@ image type is "raw" * "format" (u): The image format, as defined in QImage::Format. Available only if the image type is "raw" + * "windowId" (s): The window id of the captured window. Available + since version 4. --> @@ -181,6 +185,8 @@ image type is "raw" * "format" (u): The image format, as defined in QImage::Format. Available only if the image type is "raw" + * "screen" (s): The name of the captured screen, same as QScreen::name(). + Available since version 4 --> @@ -222,6 +228,8 @@ image type is "raw" * "format" (u): The image format, as defined in QImage::Format. Available only if the image type is "raw" + * "screen" (s): The name of the captured screen, same as QScreen::name(). + Available since version 4 --> @@ -260,6 +268,16 @@ image type is "raw" * "format" (u): The image format, as defined in QImage::Format. Available only if the image type is "raw" + + The following results get returned when taking a window screenshot: + + * "windowId" (s): The window id of the captured window. Available + since version 4 + + The following results get returned when taking a monitor screenshot: + + * "screen" (s): The name of the captured screen, same as QScreen::name(). + Available since version 4 --> diff --git a/src/effects/screenshot/screenshotdbusinterface2.cpp b/src/effects/screenshot/screenshotdbusinterface2.cpp index b0a7bcfacb..dbc40ba843 100644 --- a/src/effects/screenshot/screenshotdbusinterface2.cpp +++ b/src/effects/screenshot/screenshotdbusinterface2.cpp @@ -133,6 +133,8 @@ public: bool isCompleted() const; void marshal(ScreenShotSinkPipe2 *sink); + virtual QVariantMap attributes() const; + Q_SIGNALS: void cancelled(); void completed(); @@ -148,6 +150,11 @@ class ScreenShotSourceScreen2 : public ScreenShotSource2 public: ScreenShotSourceScreen2(ScreenShotEffect *effect, EffectScreen *screen, ScreenShotFlags flags); + + QVariantMap attributes() const override; + +private: + EffectScreen *m_screen; }; class ScreenShotSourceArea2 : public ScreenShotSource2 @@ -164,6 +171,11 @@ class ScreenShotSourceWindow2 : public ScreenShotSource2 public: ScreenShotSourceWindow2(ScreenShotEffect *effect, EffectWindow *window, ScreenShotFlags flags); + + QVariantMap attributes() const override; + +private: + EffectWindow *m_window; }; class ScreenShotSinkPipe2 : public QObject @@ -174,7 +186,7 @@ public: ScreenShotSinkPipe2(int fileDescriptor, QDBusMessage replyMessage); void cancel(); - void flush(const QImage &image); + void flush(const QImage &image, const QVariantMap &attributes); private: QDBusMessage m_replyMessage; @@ -200,18 +212,31 @@ bool ScreenShotSource2::isCompleted() const return m_future.isFinished(); } +QVariantMap ScreenShotSource2::attributes() const +{ + return QVariantMap(); +} + void ScreenShotSource2::marshal(ScreenShotSinkPipe2 *sink) { - sink->flush(m_future.result()); + sink->flush(m_future.result(), attributes()); } ScreenShotSourceScreen2::ScreenShotSourceScreen2(ScreenShotEffect *effect, EffectScreen *screen, ScreenShotFlags flags) : ScreenShotSource2(effect->scheduleScreenShot(screen, flags)) + , m_screen(screen) { } +QVariantMap ScreenShotSourceScreen2::attributes() const +{ + return QVariantMap{ + {QStringLiteral("screen"), m_screen->name()}, + }; +} + ScreenShotSourceArea2::ScreenShotSourceArea2(ScreenShotEffect *effect, const QRect &area, ScreenShotFlags flags) @@ -223,9 +248,17 @@ ScreenShotSourceWindow2::ScreenShotSourceWindow2(ScreenShotEffect *effect, EffectWindow *window, ScreenShotFlags flags) : ScreenShotSource2(effect->scheduleScreenShot(window, flags)) + , m_window(window) { } +QVariantMap ScreenShotSourceWindow2::attributes() const +{ + return QVariantMap{ + {QStringLiteral("windowId"), m_window->internalId().toString()}, + }; +} + ScreenShotSinkPipe2::ScreenShotSinkPipe2(int fileDescriptor, QDBusMessage replyMessage) : m_replyMessage(replyMessage) , m_fileDescriptor(fileDescriptor) @@ -238,14 +271,14 @@ void ScreenShotSinkPipe2::cancel() s_errorCancelledMessage)); } -void ScreenShotSinkPipe2::flush(const QImage &image) +void ScreenShotSinkPipe2::flush(const QImage &image, const QVariantMap &attributes) { if (!m_fileDescriptor.isValid()) { return; } // Note that the type of the data stored in the vardict matters. Be careful. - QVariantMap results; + QVariantMap results = attributes; results.insert(QStringLiteral("type"), QStringLiteral("raw")); results.insert(QStringLiteral("format"), quint32(image.format())); results.insert(QStringLiteral("width"), quint32(image.width())); @@ -278,7 +311,7 @@ ScreenShotDBusInterface2::~ScreenShotDBusInterface2() int ScreenShotDBusInterface2::version() const { - return 3; + return 4; } bool ScreenShotDBusInterface2::checkPermissions() const