| 1 | /* |
| 2 | * Copyright (C) 2017 Damir Porobic <https://github.com/damirporobic> |
| 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 | /* |
| 21 | * Most part of this class comes from the KWinWaylandImageGrabber implementation |
| 22 | * from KDE Spectacle, implemented by Martin Graesslin <[email protected]> |
| 23 | */ |
| 24 | |
| 25 | #include "KdeWaylandImageGrabber.h" |
| 26 | |
| 27 | static int readData(int fd, QByteArray& data) |
| 28 | { |
| 29 | // implementation based on QtWayland file qwaylanddataoffer.cpp |
| 30 | char buffer[4096]; |
| 31 | int retryCount = 0; |
| 32 | int n; |
| 33 | while (true) { |
| 34 | n = QT_READ(fd, buffer, sizeof buffer); |
| 35 | // give user 30 sec to click a window, afterwards considered as error |
| 36 | if (n == -1 && (errno == EAGAIN) && ++retryCount < 30000) { |
| 37 | usleep(1000); |
| 38 | } else { |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | if (n > 0) { |
| 43 | data.append(buffer, n); |
| 44 | n = readData(fd, data); |
| 45 | } |
| 46 | return n; |
| 47 | } |
| 48 | |
| 49 | static QImage readImage(int pipeFd) |
| 50 | { |
| 51 | QByteArray content; |
| 52 | if (readData(pipeFd, content) != 0) { |
| 53 | close(pipeFd); |
| 54 | return QImage(); |
| 55 | } |
| 56 | close(pipeFd); |
| 57 | QDataStream dataStream(content); |
| 58 | QImage image; |
| 59 | dataStream >> image; |
| 60 | return image; |
| 61 | }; |
| 62 | |
| 63 | KdeWaylandImageGrabber::KdeWaylandImageGrabber(const QSharedPointer<IConfig> &config) : AbstractImageGrabber(config) |
| 64 | { |
| 65 | addSupportedCaptureMode(CaptureModes::RectArea); |
| 66 | addSupportedCaptureMode(CaptureModes::ActiveWindow); |
| 67 | addSupportedCaptureMode(CaptureModes::WindowUnderCursor); |
| 68 | addSupportedCaptureMode(CaptureModes::CurrentScreen); |
| 69 | addSupportedCaptureMode(CaptureModes::FullScreen); |
| 70 | } |
| 71 | |
| 72 | void KdeWaylandImageGrabber::grab() |
| 73 | { |
| 74 | if (captureMode() == CaptureModes::FullScreen) { |
| 75 | prepareDBus(QLatin1String("screenshotFullscreen"), isCaptureCursorEnabled()); |
| 76 | } else if (captureMode() == CaptureModes::CurrentScreen) { |
| 77 | prepareDBus(QLatin1String("screenshotScreen"), isCaptureCursorEnabled()); |
| 78 | } else if (captureMode() == CaptureModes::ActiveWindow) { |
| 79 | prepareDBus(QLatin1String("screenshotWindow"), isCaptureCursorEnabled()); |
| 80 | } else { |
| 81 | int mask = 1; |
| 82 | if (isCaptureCursorEnabled()) { |
| 83 | mask |= 1 << 1; |
| 84 | } |
| 85 | prepareDBus(QLatin1String("interactive"), mask); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | template<typename T> |
| 90 | void KdeWaylandImageGrabber::prepareDBus(const QString& mode, T mask) |
| 91 | { |
| 92 | int pipeFds[2]; |
| 93 | if (pipe2(pipeFds, O_CLOEXEC | O_NONBLOCK) != 0) { |
| 94 | emit canceled(); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | callDBus(pipeFds[1], mode, mask); |
| 99 | startReadImage(pipeFds[0]); |
| 100 | |
| 101 | close(pipeFds[1]); |
| 102 | } |
| 103 | |
| 104 | template<typename T> |
| 105 | void KdeWaylandImageGrabber::callDBus(int writeFd, const QString& mode, T mask) |
| 106 | { |
| 107 | QDBusInterface interface(QLatin1String("org.kde.KWin"), QLatin1String("/Screenshot"), QLatin1String("org.kde.kwin.Screenshot")); |
| 108 | interface.asyncCall(mode, QVariant::fromValue(QDBusUnixFileDescriptor(writeFd)), mask); |
| 109 | } |
| 110 | |
| 111 | void KdeWaylandImageGrabber::startReadImage(int readPipe) |
| 112 | { |
| 113 | auto watcher = new QFutureWatcher<QImage>(this); |
| 114 | QObject::connect(watcher, &QFutureWatcher<QImage>::finished, this, |
| 115 | [watcher, this] { |
| 116 | watcher->deleteLater(); |
| 117 | auto image = watcher->result(); |
| 118 | emit finished(CaptureDto(QPixmap::fromImage(image))); |
| 119 | }); |
| 120 | watcher->setFuture(QtConcurrent::run(readImage, readPipe)); |
| 121 | } |