Changeset View
Changeset View
Standalone View
Standalone View
src/BackendPlugins/WaylandKWinScreenshotPlugin.cpp
- This file was moved from src/Platforms/PlatformKWinWayland.cpp.
1 | /* This file is part of Spectacle, the KDE screenshot utility | 1 | /* This file is part of Spectacle, the KDE screenshot utility | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2016 Martin Graesslin <mgraesslin@kde.org> | 2 | * Copyright (C) 2019 Leon De Andrade <leondeandrade@hotmail.com> | ||
3 | * | 3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | 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 | 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 | 6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | 7 | * (at your option) any later version. | ||
8 | * | 8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | 9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | 12 | * GNU General Public License for more details. | ||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public License | 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 | 15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | * | 18 | * | ||
19 | * SPDX-License-Identifier: LGPL-2.0-or-later | 19 | * SPDX-License-Identifier: LGPL-2.0-or-later | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "PlatformKWinWayland.h" | 22 | | ||
23 | #include "WaylandKWinScreenshotPlugin.h" | ||||
23 | 24 | | |||
24 | #include <qplatformdefs.h> | 25 | #include <qplatformdefs.h> | ||
25 | #include <QtConcurrent> | 26 | #include <QtConcurrent> | ||
26 | #include <QApplication> | 27 | #include <QApplication> | ||
27 | #include <QImage> | 28 | #include <QImage> | ||
28 | #include <QPixmap> | 29 | #include <QPixmap> | ||
30 | #include <QDBusConnection> | ||||
31 | #include <QDBusConnectionInterface> | ||||
29 | #include <QDBusInterface> | 32 | #include <QDBusInterface> | ||
30 | #include <QDBusUnixFileDescriptor> | 33 | #include <QDBusUnixFileDescriptor> | ||
31 | #include <QDBusPendingCall> | 34 | #include <QDBusPendingCall> | ||
32 | #include <QFutureWatcher> | 35 | #include <QFutureWatcher> | ||
33 | 36 | | |||
34 | /* -- Static Helpers --------------------------------------------------------------------------- */ | 37 | /* -- Static Helpers --------------------------------------------------------------------------- */ | ||
35 | 38 | | |||
36 | static int readData(int theFile, QByteArray &theDataOut) | 39 | static int readData(int theFile, QByteArray &theDataOut) | ||
Show All 29 Lines | 63 | { | |||
66 | close(thePipeFd); | 69 | close(thePipeFd); | ||
67 | 70 | | |||
68 | QDataStream lDataStream(lContent); | 71 | QDataStream lDataStream(lContent); | ||
69 | QImage lImage; | 72 | QImage lImage; | ||
70 | lDataStream >> lImage; | 73 | lDataStream >> lImage; | ||
71 | return lImage; | 74 | return lImage; | ||
72 | } | 75 | } | ||
73 | 76 | | |||
74 | /* -- General Plumbing ------------------------------------------------------------------------- */ | | |||
75 | 77 | | |||
76 | PlatformKWinWayland::PlatformKWinWayland(QObject *parent) : | 78 | /* -- WaylandKWinScreenshotPlugin ------------------------------------------------------------------------- */ | ||
77 | Platform(parent) | 79 | | ||
78 | {} | 80 | WaylandKWinScreenshotPlugin::WaylandKWinScreenshotPlugin(QObject *parent) : ScreenshotInterface {parent} {} | ||
81 | | ||||
79 | 82 | | |||
80 | QString PlatformKWinWayland::platformName() const | 83 | QString WaylandKWinScreenshotPlugin::name() const | ||
81 | { | 84 | { | ||
82 | return QStringLiteral("KWinWayland"); | 85 | return QStringLiteral("Wayland Screenshot Backend"); | ||
83 | } | 86 | } | ||
84 | 87 | | |||
85 | Platform::GrabModes PlatformKWinWayland::supportedGrabModes() const | 88 | Platform WaylandKWinScreenshotPlugin::platform() const { | ||
89 | return Platform::Wayland; | ||||
90 | } | ||||
91 | | ||||
92 | GrabModes WaylandKWinScreenshotPlugin::supportedGrabModes() const | ||||
86 | { | 93 | { | ||
87 | Platform::GrabModes lSupportedModes({ GrabMode::AllScreens, GrabMode::WindowUnderCursor }); | 94 | GrabModes lSupportedModes({ GrabMode::AllScreens, GrabMode::WindowUnderCursor }); | ||
88 | if (QApplication::screens().count() > 1) { | 95 | if (QApplication::screens().count() > 1) { | ||
89 | lSupportedModes |= Platform::GrabMode::CurrentScreen; | 96 | lSupportedModes |= GrabMode::CurrentScreen; | ||
90 | } | 97 | } | ||
91 | return lSupportedModes; | 98 | return lSupportedModes; | ||
92 | } | 99 | } | ||
93 | 100 | | |||
94 | Platform::ShutterModes PlatformKWinWayland::supportedShutterModes() const | 101 | ShutterModes WaylandKWinScreenshotPlugin::supportedShutterModes() const | ||
95 | { | 102 | { | ||
96 | return { ShutterMode::OnClick }; | 103 | return { ShutterMode::OnClick }; | ||
97 | } | 104 | } | ||
98 | 105 | | |||
99 | void PlatformKWinWayland::doGrab(ShutterMode theShutterMode, GrabMode theGrabMode, bool theIncludePointer, bool theIncludeDecorations) | 106 | bool WaylandKWinScreenshotPlugin::requirementsComplied() const | ||
107 | { | ||||
108 | return true; | ||||
109 | } | ||||
110 | | ||||
111 | void WaylandKWinScreenshotPlugin::doGrab(ShutterMode theShutterMode, GrabMode theGrabMode, bool theIncludePointer, bool theIncludeDecorations) | ||||
100 | { | 112 | { | ||
101 | if (theShutterMode != ShutterMode::OnClick) { | 113 | if (theShutterMode != ShutterMode::OnClick) { | ||
102 | emit newScreenshotFailed(); | 114 | emit newScreenshotFailed(); | ||
103 | return; | 115 | return; | ||
104 | } | 116 | } | ||
105 | 117 | | |||
106 | switch(theGrabMode) { | 118 | switch(theGrabMode) { | ||
107 | case GrabMode::AllScreens: | 119 | case GrabMode::AllScreens: | ||
Show All 10 Lines | |||||
118 | case GrabMode::InvalidChoice: | 130 | case GrabMode::InvalidChoice: | ||
119 | case GrabMode::ActiveWindow: | 131 | case GrabMode::ActiveWindow: | ||
120 | case GrabMode::TransientWithParent: | 132 | case GrabMode::TransientWithParent: | ||
121 | emit newScreenshotFailed(); | 133 | emit newScreenshotFailed(); | ||
122 | return; | 134 | return; | ||
123 | } | 135 | } | ||
124 | } | 136 | } | ||
125 | 137 | | |||
126 | /* -- Grab Helpers ----------------------------------------------------------------------------- */ | 138 | void WaylandKWinScreenshotPlugin::startReadImage(int theReadPipe) | ||
127 | | ||||
128 | void PlatformKWinWayland::startReadImage(int theReadPipe) | | |||
129 | { | 139 | { | ||
130 | auto lWatcher = new QFutureWatcher<QImage>(this); | 140 | auto lWatcher = new QFutureWatcher<QImage>(this); | ||
131 | QObject::connect(lWatcher, &QFutureWatcher<QImage>::finished, this, | 141 | QObject::connect(lWatcher, &QFutureWatcher<QImage>::finished, this, | ||
132 | [lWatcher, this] () { | 142 | [lWatcher, this] () { | ||
133 | lWatcher->deleteLater(); | 143 | lWatcher->deleteLater(); | ||
134 | const QImage lImage = lWatcher->result(); | 144 | const QImage lImage = lWatcher->result(); | ||
135 | emit newScreenshotTaken(QPixmap::fromImage(lImage)); | 145 | emit newScreenshotTaken(QPixmap::fromImage(lImage)); | ||
136 | } | 146 | } | ||
137 | ); | 147 | ); | ||
138 | lWatcher->setFuture(QtConcurrent::run(readImage, theReadPipe)); | 148 | lWatcher->setFuture(QtConcurrent::run(readImage, theReadPipe)); | ||
139 | } | 149 | } | ||
140 | 150 | | |||
141 | template <typename ArgType> | 151 | template <typename ArgType> | ||
142 | void PlatformKWinWayland::callDBus(const QString &theGrabMethod, ArgType theArgument, int theWriteFile) | 152 | void WaylandKWinScreenshotPlugin::doGrabHelper(const QString &theGrabMethod, ArgType theArgument) | ||
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 | { | 153 | { | ||
151 | int lPipeFds[2]; | 154 | int lPipeFds[2]; | ||
152 | if (pipe2(lPipeFds, O_CLOEXEC|O_NONBLOCK) != 0) { | 155 | if (pipe2(lPipeFds, O_CLOEXEC|O_NONBLOCK) != 0) { | ||
153 | emit newScreenshotFailed(); | 156 | emit newScreenshotFailed(); | ||
154 | return; | 157 | return; | ||
155 | } | 158 | } | ||
156 | 159 | | |||
157 | callDBus(theGrabMethod, theArgument, lPipeFds[1]); | 160 | callDBus(theGrabMethod, theArgument, lPipeFds[1]); | ||
158 | startReadImage(lPipeFds[0]); | 161 | startReadImage(lPipeFds[0]); | ||
159 | 162 | | |||
160 | close(lPipeFds[1]); | 163 | close(lPipeFds[1]); | ||
161 | } | 164 | } | ||
165 | | ||||
166 | template <typename ArgType> | ||||
167 | void WaylandKWinScreenshotPlugin::callDBus(const QString &theGrabMethod, ArgType theArgument, int theWriteFile) | ||||
168 | { | ||||
169 | QDBusInterface lInterface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.kwin.Screenshot")); | ||||
170 | lInterface.asyncCall(theGrabMethod, QVariant::fromValue(QDBusUnixFileDescriptor(theWriteFile)), theArgument); | ||||
171 | } | ||||
172 | | ||||
173 | #include "moc_WaylandKWinScreenshotPlugin.cpp" | ||||
174 | No newline at end of file |