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 #include "GnomeWaylandImageGrabber.h"
21 
GnomeWaylandImageGrabber()22 GnomeWaylandImageGrabber::GnomeWaylandImageGrabber() : AbstractRectAreaImageGrabber(new WaylandSnippingArea)
23 {
24 	addSupportedCaptureMode(CaptureModes::RectArea);
25 	addSupportedCaptureMode(CaptureModes::LastRectArea);
26 	addSupportedCaptureMode(CaptureModes::ActiveWindow);
27 	addSupportedCaptureMode(CaptureModes::FullScreen);
28 	addSupportedCaptureMode(CaptureModes::CurrentScreen);
29 }
30 
activeWindowRect() const31 QRect GnomeWaylandImageGrabber::activeWindowRect() const
32 {
33 	// Gnome Wayland captures active window directly
34 	return {};
35 }
36 
getCursorWithPosition() const37 CursorDto GnomeWaylandImageGrabber::getCursorWithPosition() const
38 {
39 	// Gnome Wayland merges the cursor already
40 	return {};
41 }
42 
grab()43 void GnomeWaylandImageGrabber::grab()
44 {
45     QDBusInterface interface(QLatin1String("org.gnome.Shell.Screenshot"), QLatin1String("/org/gnome/Shell/Screenshot"), QLatin1String("org.gnome.Shell.Screenshot"));
46     QDBusPendingReply<bool, QString> reply;
47     if (captureMode() == CaptureModes::ActiveWindow) {
48         reply = interface.asyncCall(QLatin1String("ScreenshotWindow"), true, isCaptureCursorEnabled(), false, tmpScreenshotFilename());
49     } else {
50         reply = interface.asyncCall(QLatin1String("Screenshot"), isCaptureCursorEnabled(), false, tmpScreenshotFilename());
51     }
52 
53     reply.waitForFinished();
54 
55     if (reply.isError()) {
56         qCritical("Invalid reply from DBus: %s", qPrintable(reply.error().message()));
57         emit canceled();
58     } else {
59         auto pathToTmpScreenshot = reply.argumentAt<1>();
60         postProcessing(QPixmap(pathToTmpScreenshot));
61     }
62 }
63 
postProcessing(const QPixmap & pixmap)64 void GnomeWaylandImageGrabber::postProcessing(const QPixmap& pixmap)
65 {
66     if (captureMode() == CaptureModes::ActiveWindow) {
67         emit finished(CaptureDto(pixmap));
68     } else {
69 	    setCaptureRectFromCorrectSource();
70         emit finished(CaptureDto(pixmap.copy(mCaptureRect)));
71     }
72 }
73 
tmpScreenshotFilename() const74 QString GnomeWaylandImageGrabber::tmpScreenshotFilename() const
75 {
76     auto path = QLatin1String("/tmp/");
77     auto filename = QLatin1String("ksnip-") + QString::number(MathHelper::randomInt());
78     auto extension = QLatin1String(".png");
79     return path + filename + extension;
80 }
81 
fullScreenRect() const82 QRect GnomeWaylandImageGrabber::fullScreenRect() const
83 {
84 	// Copy with empty rect will return full screen
85 	return {};
86 }
87