/
screenshotservice.cpp
196 lines (162 loc) · 5.01 KB
/
screenshotservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
**
** Copyright (C) 2013 Jolla Ltd.
** Contact: Robin Burchell <robin.burchell@jollamobile.com>
**
** This file is part of lipstick.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "hwcrenderstage.h"
#include "lipstickcompositor.h"
#include "screenshotservice.h"
#include <QGuiApplication>
#include <QImage>
#include <QScreen>
#include <QTransform>
#include <QThreadPool>
#include <private/qquickwindow_p.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include <sys/select.h>
ScreenshotResult::ScreenshotResult(QObject *parent)
: QObject(parent)
, m_notifier(0, QSocketNotifier::Read)
, m_notifierId(0)
, m_status(Error)
{
}
ScreenshotResult::ScreenshotResult(int notifierId, const QUrl &path, QObject *parent)
: QObject(parent)
, m_notifier(notifierId, QSocketNotifier::Read)
, m_path(path)
, m_notifierId(notifierId)
{
connect(&m_notifier, &QSocketNotifier::activated, this, [this] {
if (m_status == Writing) {
quint64 status = Writing;
ssize_t unused = ::read(m_notifier.socket(), &status, sizeof(status));
Q_UNUSED(unused);
m_status = Status(status);
emit statusChanged();
if (m_status == Finished) {
emit finished();
} else if (m_status == Error) {
emit error();
}
deleteLater();
}
});
}
ScreenshotResult::~ScreenshotResult()
{
if (m_notifierId != 0) {
::close(m_notifierId);
}
}
ScreenshotResult::Status ScreenshotResult::status() const
{
return m_status;
}
QUrl ScreenshotResult::path() const
{
return m_path;
}
void ScreenshotResult::waitForFinished()
{
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(m_notifierId, &fdread);
for (;;) {
const int ret = select(m_notifierId + 1, &fdread, nullptr, nullptr, nullptr);
if (ret >= 0) {
quint64 status = Writing;
ssize_t unused = ::read(m_notifierId, &status, sizeof(status));
Q_UNUSED(unused);
m_status = Status(status);
break;
} else if (errno != EINTR) {
m_status = Error;
break;
}
}
deleteLater();
}
class ScreenshotWriter : public QRunnable
{
public:
ScreenshotWriter(int notifierId, const QImage &image, const QString &path, int rotation);
~ScreenshotWriter();
void run() override;
private:
QImage m_image;
const QString m_path;
const int m_notifierId;
const int m_rotation;
};
ScreenshotWriter::ScreenshotWriter(int notifierId, const QImage &image, const QString &path, int rotation)
: m_image(image)
, m_path(path)
, m_notifierId(::dup(notifierId))
, m_rotation(rotation)
{
setAutoDelete(true);
}
ScreenshotWriter::~ScreenshotWriter()
{
::close(m_notifierId);
}
void ScreenshotWriter::run()
{
if (m_rotation != 0) {
QTransform xform;
xform.rotate(m_rotation);
m_image = m_image.transformed(xform, Qt::SmoothTransformation);
}
const quint64 status = m_image.save(m_path)
? ScreenshotResult::Finished
: ScreenshotResult::Error;
const QByteArray path = m_path.toUtf8();
int r = chown(path.constData(), getuid(), getgid());
if (r != 0) {
qWarning() << "Screenshot owner/group could not be set" << strerror(errno);
}
ssize_t unused = ::write(m_notifierId, &status, sizeof(status));
Q_UNUSED(unused);
}
ScreenshotResult *ScreenshotService::saveScreenshot(const QString &path)
{
LipstickCompositor *compositor = LipstickCompositor::instance();
if (!compositor) {
return nullptr;
}
const int notifierId = ::eventfd(0, 0);
if (notifierId == -1) {
return nullptr;
}
ScreenshotResult * const result = new ScreenshotResult(notifierId, path, compositor);
if (path.isEmpty()) {
qWarning() << "Screenshot path is empty.";
const quint64 status = ScreenshotResult::Error;
ssize_t unused = ::write(notifierId, &status, sizeof(status));
Q_UNUSED(unused);
return result;
}
QQuickWindowPrivate *wd = QQuickWindowPrivate::get(compositor);
HwcRenderStage *renderStage = (HwcRenderStage *) wd->customRenderStage;
if (renderStage)
renderStage->setBypassHwc(true);
QImage grab(compositor->grabWindow());
if (renderStage) {
renderStage->setBypassHwc(false);
}
const int rotation(QGuiApplication::primaryScreen()->angleBetween(
Qt::PrimaryOrientation, compositor->topmostWindowOrientation()));
QThreadPool::globalInstance()->start(new ScreenshotWriter(notifierId, grab, path, rotation));
return result;
}