| 1 | /* |
| 2 | * Copyright (C) 2020 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "WaylandImageGrabber.h" |
| 21 | |
| 22 | WaylandImageGrabber::WaylandImageGrabber(const QSharedPointer<IConfig> &config) : |
| 23 | AbstractImageGrabber(config), |
| 24 | mRequestTokenCounter(1) |
| 25 | { |
| 26 | addSupportedCaptureMode(CaptureModes::RectArea); |
| 27 | addSupportedCaptureMode(CaptureModes::LastRectArea); |
| 28 | addSupportedCaptureMode(CaptureModes::ActiveWindow); |
| 29 | addSupportedCaptureMode(CaptureModes::CurrentScreen); |
| 30 | addSupportedCaptureMode(CaptureModes::WindowUnderCursor); |
| 31 | addSupportedCaptureMode(CaptureModes::FullScreen); |
| 32 | } |
| 33 | |
| 34 | void WaylandImageGrabber::grab() |
| 35 | { |
| 36 | auto message = getDBusMessage(); |
| 37 | auto pendingCall = QDBusConnection::sessionBus().asyncCall(message); |
| 38 | auto watcher = new QDBusPendingCallWatcher(pendingCall); |
| 39 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &WaylandImageGrabber::portalResponse); |
| 40 | } |
| 41 | |
| 42 | QDBusMessage WaylandImageGrabber::getDBusMessage() |
| 43 | { |
| 44 | auto message = QDBusMessage::createMethodCall( |
| 45 | QLatin1String("org.freedesktop.portal.Desktop"), |
| 46 | QLatin1String("/org/freedesktop/portal/desktop"), |
| 47 | QLatin1String("org.freedesktop.portal.Screenshot"), |
| 48 | QLatin1String("Screenshot")); |
| 49 | |
| 50 | message << QLatin1String("wayland:") << QVariantMap{ |
| 51 | {QLatin1String("interactive"), isInteractiveCapture()}, |
| 52 | {QLatin1String("handle_token"), getRequestToken()} |
| 53 | }; |
| 54 | return message; |
| 55 | } |
| 56 | |
| 57 | void WaylandImageGrabber::gotScreenshotResponse(uint response, const QVariantMap& results) |
| 58 | { |
| 59 | if (isResponseValid(response, results)) { |
| 60 | auto path = getPathToScreenshot(results); |
| 61 | auto screenshot = createPixmapFromPath(path); |
| 62 | |
| 63 | if (screenshot.isNull()) { |
| 64 | qCritical("Failed to load screenshot from '%s'", qPrintable(path)); |
| 65 | emit canceled(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if(isTemporaryImage(path)) { |
| 70 | emit finished(CaptureDto(screenshot)); |
| 71 | } else { |
| 72 | emit finished(CaptureFromFileDto(screenshot, path)); |
| 73 | } |
| 74 | } else { |
| 75 | qCritical("Failed to take screenshot"); |
| 76 | emit canceled(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | QPixmap WaylandImageGrabber::createPixmapFromPath(const QString &path) const |
| 81 | { |
| 82 | auto capture = QPixmap::fromImage(QImage(path)); |
| 83 | if(mConfig->scaleGenericWaylandScreenshotsEnabled()) { |
| 84 | capture.setDevicePixelRatio(mHdpiScaler.scaleFactor()); |
| 85 | } |
| 86 | |
| 87 | return capture; |
| 88 | } |
| 89 | |
| 90 | bool WaylandImageGrabber::isResponseValid(uint response, const QVariantMap &results) |
| 91 | { |
| 92 | return !response && results.contains(QLatin1String("uri")); |
| 93 | } |
| 94 | |
| 95 | bool WaylandImageGrabber::isTemporaryImage(const QString &path) |
| 96 | { |
| 97 | return path.contains(QLatin1String("/tmp/")); |
| 98 | } |
| 99 | |
| 100 | QString WaylandImageGrabber::getPathToScreenshot(const QVariantMap &results) |
| 101 | { |
| 102 | auto uri = results.value(QLatin1String("uri")).toString(); |
| 103 | if (uri.isEmpty()) { |
| 104 | return results.value(QLatin1String("path")).toString(); |
| 105 | } |
| 106 | return QUrl(uri).toLocalFile(); |
| 107 | } |
| 108 | |
| 109 | QString WaylandImageGrabber::getRequestToken() |
| 110 | { |
| 111 | mRequestTokenCounter += 1; |
| 112 | return QString("u%1").arg(mRequestTokenCounter); |
| 113 | } |
| 114 | |
| 115 | bool WaylandImageGrabber::isInteractiveCapture() const |
| 116 | { |
| 117 | return captureMode() != CaptureModes::FullScreen; |
| 118 | } |
| 119 | |
| 120 | void WaylandImageGrabber::portalResponse(QDBusPendingCallWatcher *watcher) |
| 121 | { |
| 122 | QDBusPendingReply<QDBusObjectPath> reply = *watcher; |
| 123 | |
| 124 | if (reply.isError()) { |
| 125 | qCritical("Error: %s", qPrintable(reply.error().message())); |
| 126 | } else { |
| 127 | QDBusConnection::sessionBus().connect(QString(), |
| 128 | reply.value().path(), |
| 129 | QLatin1String("org.freedesktop.portal.Request"), |
| 130 | QLatin1String("Response"), |
| 131 | this, |
| 132 | SLOT(gotScreenshotResponse(uint,QVariantMap))); |
| 133 | } |
| 134 | } |