Changeset View
Changeset View
Standalone View
Standalone View
src/Platforms/PlatformKWinWayland.cpp
- This file was added.
1 | /* This file is part of Spectacle, the KDE screenshot utility | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2016 Martin Graesslin <mgraesslin@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Lesser 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 Lesser 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 | * SPDX-License-Identifier: LGPL-2.0-or-later | ||||
20 | */ | ||||
21 | | ||||
22 | #include "PlatformKWinWayland.h" | ||||
23 | | ||||
24 | #include <qplatformdefs.h> | ||||
25 | #include <QtConcurrent> | ||||
26 | #include <QApplication> | ||||
27 | #include <QImage> | ||||
28 | #include <QPixmap> | ||||
29 | #include <QDBusInterface> | ||||
30 | #include <QDBusUnixFileDescriptor> | ||||
31 | #include <QDBusPendingCall> | ||||
32 | #include <QFutureWatcher> | ||||
33 | | ||||
34 | /* -- Static Helpers --------------------------------------------------------------------------- */ | ||||
35 | | ||||
36 | static int readData(int theFile, QByteArray &theDataOut) | ||||
37 | { | ||||
38 | // implementation based on QtWayland file qwaylanddataoffer.cpp | ||||
39 | char lBuffer[4096]; | ||||
40 | int lRetryCount = 0; | ||||
41 | ssize_t lBytesRead = 0; | ||||
42 | while (true) { | ||||
43 | lBytesRead = QT_READ(theFile, lBuffer, sizeof lBuffer); | ||||
44 | // give user 30 sec to click a window, afterwards considered as error | ||||
45 | if (lBytesRead == -1 && (errno == EAGAIN) && ++lRetryCount < 30000) { | ||||
46 | usleep(1000); | ||||
47 | } else { | ||||
48 | break; | ||||
49 | } | ||||
50 | } | ||||
51 | | ||||
52 | if (lBytesRead > 0) { | ||||
53 | theDataOut.append(lBuffer, lBytesRead); | ||||
54 | lBytesRead = readData(theFile, theDataOut); | ||||
55 | } | ||||
56 | return lBytesRead; | ||||
57 | } | ||||
58 | | ||||
59 | static QImage readImage(int thePipeFd) | ||||
60 | { | ||||
61 | QByteArray lContent; | ||||
62 | if (readData(thePipeFd, lContent) != 0) { | ||||
63 | close(thePipeFd); | ||||
64 | return QImage(); | ||||
65 | } | ||||
66 | close(thePipeFd); | ||||
67 | | ||||
68 | QDataStream lDataStream(lContent); | ||||
69 | QImage lImage; | ||||
70 | lDataStream >> lImage; | ||||
71 | return lImage; | ||||
72 | } | ||||
73 | | ||||
74 | /* -- General Plumbing ------------------------------------------------------------------------- */ | ||||
75 | | ||||
76 | PlatformKWinWayland::PlatformKWinWayland(QObject *parent) : | ||||
77 | Platform(parent) | ||||
78 | {} | ||||
79 | | ||||
80 | QString PlatformKWinWayland::platformName() const | ||||
81 | { | ||||
82 | return QStringLiteral("KWinWayland"); | ||||
83 | } | ||||
84 | | ||||
85 | Platform::GrabModes PlatformKWinWayland::supportedGrabModes() const | ||||
86 | { | ||||
87 | Platform::GrabModes lSupportedModes({ GrabMode::AllScreens, GrabMode::WindowUnderCursor }); | ||||
88 | if (QApplication::screens().count() > 1) { | ||||
89 | lSupportedModes |= Platform::GrabMode::CurrentScreen; | ||||
90 | } | ||||
91 | return lSupportedModes; | ||||
92 | } | ||||
93 | | ||||
94 | Platform::ShutterModes PlatformKWinWayland::supportedShutterModes() const | ||||
95 | { | ||||
96 | return { ShutterMode::OnClick }; | ||||
97 | } | ||||
98 | | ||||
99 | void PlatformKWinWayland::doGrab(ShutterMode theShutterMode, GrabMode theGrabMode, bool theIncludePointer, bool theIncludeDecorations) | ||||
100 | { | ||||
101 | if (theShutterMode != ShutterMode::OnClick) { | ||||
102 | emit newScreenshotFailed(); | ||||
103 | return; | ||||
104 | } | ||||
105 | | ||||
106 | switch(theGrabMode) { | ||||
107 | case GrabMode::AllScreens: | ||||
108 | return doGrabHelper(QStringLiteral("screenshotFullscreen"), theIncludePointer); | ||||
109 | case GrabMode::CurrentScreen: | ||||
110 | return doGrabHelper(QStringLiteral("screenshotScreen"), theIncludePointer); | ||||
111 | case GrabMode::WindowUnderCursor: { | ||||
112 | int lOpMask = theIncludeDecorations ? 1 : 0; | ||||
113 | if (theIncludePointer) { | ||||
114 | lOpMask |= 1 << 1; | ||||
115 | } | ||||
116 | return doGrabHelper(QStringLiteral("interactive"), lOpMask); | ||||
117 | } | ||||
118 | case GrabMode::InvalidChoice: | ||||
119 | case GrabMode::ActiveWindow: | ||||
120 | case GrabMode::TransientWithParent: | ||||
121 | emit newScreenshotFailed(); | ||||
122 | return; | ||||
123 | } | ||||
124 | } | ||||
125 | | ||||
126 | /* -- Grab Helpers ----------------------------------------------------------------------------- */ | ||||
127 | | ||||
128 | void PlatformKWinWayland::startReadImage(int theReadPipe) | ||||
129 | { | ||||
130 | auto lWatcher = new QFutureWatcher<QImage>(this); | ||||
131 | QObject::connect(lWatcher, &QFutureWatcher<QImage>::finished, this, | ||||
132 | [lWatcher, this] () { | ||||
133 | lWatcher->deleteLater(); | ||||
134 | const QImage lImage = lWatcher->result(); | ||||
135 | emit newScreenshotTaken(QPixmap::fromImage(lImage)); | ||||
136 | } | ||||
137 | ); | ||||
138 | lWatcher->setFuture(QtConcurrent::run(readImage, theReadPipe)); | ||||
139 | } | ||||
140 | | ||||
141 | template <typename ArgType> | ||||
142 | void PlatformKWinWayland::callDBus(const QString &theGrabMethod, ArgType theArgument, int theWriteFile) | ||||
143 | { | ||||
144 | QDBusInterface lInterface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.kwin.Screenshot")); | ||||
145 | lInterface.asyncCall(theGrabMethod, QVariant::fromValue(QDBusUnixFileDescriptor(theWriteFile)), theArgument); | ||||
146 | } | ||||
147 | | ||||
148 | template <typename ArgType> | ||||
149 | void PlatformKWinWayland::doGrabHelper(const QString &theGrabMethod, ArgType theArgument) | ||||
150 | { | ||||
151 | int lPipeFds[2]; | ||||
152 | if (pipe2(lPipeFds, O_CLOEXEC|O_NONBLOCK) != 0) { | ||||
153 | emit newScreenshotFailed(); | ||||
154 | return; | ||||
155 | } | ||||
156 | | ||||
157 | callDBus(theGrabMethod, theArgument, lPipeFds[1]); | ||||
158 | startReadImage(lPipeFds[0]); | ||||
159 | | ||||
160 | close(lPipeFds[1]); | ||||
161 | } |