2017-09-14 19:03:56 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2015 Marco Martin <mart@kde.org> *
|
2018-11-12 04:02:46 +00:00
|
|
|
* Copyright (C) 2018 Bhushan Shah <bshah@kde.org> *
|
2017-09-14 19:03:56 +00:00
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "phonepanel.h"
|
|
|
|
|
|
2020-10-01 07:38:05 +00:00
|
|
|
#include <qplatformdefs.h>
|
2020-02-06 17:17:47 +00:00
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDBusPendingReply>
|
2020-02-08 12:17:53 +00:00
|
|
|
#include <QFile>
|
2020-02-08 16:37:01 +00:00
|
|
|
#include <QDebug>
|
2020-02-06 17:17:47 +00:00
|
|
|
#include <QStandardPaths>
|
2017-09-14 19:03:56 +00:00
|
|
|
#include <QProcess>
|
2020-02-06 17:17:47 +00:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2020-02-10 19:45:51 +00:00
|
|
|
#include <QScreen>
|
2020-10-01 07:38:05 +00:00
|
|
|
#include <unistd.h>
|
2020-02-06 17:17:47 +00:00
|
|
|
|
2020-03-20 00:08:59 +00:00
|
|
|
constexpr int SCREENSHOT_DELAY = 200;
|
|
|
|
|
|
2020-10-01 07:38:05 +00:00
|
|
|
/* -- Static Helpers --------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static int readData(int theFile, QByteArray &theDataOut)
|
|
|
|
|
{
|
|
|
|
|
// implementation based on QtWayland file qwaylanddataoffer.cpp
|
|
|
|
|
char lBuffer[4096];
|
|
|
|
|
int lRetryCount = 0;
|
|
|
|
|
ssize_t lBytesRead = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
lBytesRead = QT_READ(theFile, lBuffer, sizeof lBuffer);
|
|
|
|
|
// give user 30 sec to click a window, afterwards considered as error
|
|
|
|
|
if (lBytesRead == -1 && (errno == EAGAIN) && ++lRetryCount < 30000) {
|
|
|
|
|
usleep(1000);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lBytesRead > 0) {
|
|
|
|
|
theDataOut.append(lBuffer, lBytesRead);
|
|
|
|
|
lBytesRead = readData(theFile, theDataOut);
|
|
|
|
|
}
|
|
|
|
|
return lBytesRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QImage readImage(int thePipeFd)
|
|
|
|
|
{
|
|
|
|
|
QByteArray lContent;
|
|
|
|
|
if (readData(thePipeFd, lContent) != 0) {
|
|
|
|
|
close(thePipeFd);
|
|
|
|
|
return QImage();
|
|
|
|
|
}
|
|
|
|
|
close(thePipeFd);
|
|
|
|
|
|
|
|
|
|
QDataStream lDataStream(lContent);
|
|
|
|
|
QImage lImage;
|
|
|
|
|
lDataStream >> lImage;
|
|
|
|
|
return lImage;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 19:03:56 +00:00
|
|
|
PhonePanel::PhonePanel(QObject *parent, const QVariantList &args)
|
|
|
|
|
: Plasma::Containment(parent, args)
|
|
|
|
|
{
|
|
|
|
|
//setHasConfigurationInterface(true);
|
2020-04-12 14:14:51 +00:00
|
|
|
m_kscreenInterface = new org::kde::KScreen(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/kscreen"), QDBusConnection::sessionBus(), this);
|
2020-04-29 16:17:43 +00:00
|
|
|
m_screenshotInterface = new org::kde::kwin::Screenshot(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QDBusConnection::sessionBus(), this);
|
2017-09-14 19:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 00:08:59 +00:00
|
|
|
PhonePanel::~PhonePanel() = default;
|
2017-09-14 19:03:56 +00:00
|
|
|
|
|
|
|
|
void PhonePanel::executeCommand(const QString &command)
|
|
|
|
|
{
|
2020-03-20 00:08:59 +00:00
|
|
|
qWarning() << "Executing" << command;
|
2017-09-14 19:03:56 +00:00
|
|
|
QProcess::startDetached(command);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-12 04:02:46 +00:00
|
|
|
void PhonePanel::toggleTorch()
|
|
|
|
|
{
|
|
|
|
|
if (!m_running) {
|
2018-12-31 03:13:55 +00:00
|
|
|
gst_init(nullptr, nullptr);
|
2018-11-12 04:02:46 +00:00
|
|
|
// create elements
|
|
|
|
|
m_source = gst_element_factory_make("droidcamsrc", "source");
|
|
|
|
|
m_sink = gst_element_factory_make("fakesink", "sink");
|
|
|
|
|
m_pipeline = gst_pipeline_new("torch-pipeline");
|
|
|
|
|
if (!m_pipeline || !m_source || !m_sink) {
|
|
|
|
|
qDebug() << "Failed to turn on torch: failed to create elements";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
gst_bin_add_many(GST_BIN(m_pipeline), m_source, m_sink, NULL);
|
|
|
|
|
if (gst_element_link(m_source, m_sink) != TRUE) {
|
|
|
|
|
qDebug() << "Failed to turn on torch: failed to link source and sink";
|
|
|
|
|
g_object_unref(m_pipeline);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
g_object_set(m_source, "mode", 2, NULL);
|
|
|
|
|
g_object_set(m_source, "video-torch", TRUE, NULL);
|
|
|
|
|
if (gst_element_set_state(m_pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
|
qDebug() << "Failed to turn on torch: failed to start pipeline";
|
|
|
|
|
g_object_unref(m_pipeline);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_running = true;
|
|
|
|
|
} else {
|
|
|
|
|
gst_element_set_state(m_pipeline, GST_STATE_NULL);
|
|
|
|
|
gst_object_unref(m_pipeline);
|
|
|
|
|
m_running = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 14:14:51 +00:00
|
|
|
bool PhonePanel::autoRotate()
|
|
|
|
|
{
|
|
|
|
|
QDBusPendingReply<bool> reply = m_kscreenInterface->getAutoRotate();
|
|
|
|
|
reply.waitForFinished();
|
|
|
|
|
if (reply.isError()) {
|
|
|
|
|
qWarning() << "Getting auto rotate failed:" << reply.error().name() << reply.error().message();
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return reply.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PhonePanel::setAutoRotate(bool value)
|
|
|
|
|
{
|
|
|
|
|
QDBusPendingReply<> reply = m_kscreenInterface->setAutoRotate(value);
|
|
|
|
|
reply.waitForFinished();
|
|
|
|
|
if (reply.isError()) {
|
|
|
|
|
qWarning() << "Setting auto rotate failed:" << reply.error().name() << reply.error().message();
|
|
|
|
|
} else {
|
|
|
|
|
emit autoRotateChanged(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 17:17:47 +00:00
|
|
|
void PhonePanel::takeScreenshot()
|
|
|
|
|
{
|
2020-10-01 07:38:05 +00:00
|
|
|
QString filePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
|
|
|
|
if (filePath.isEmpty()) {
|
|
|
|
|
qWarning() << "Couldn't find a writable location for the screenshot!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QDir picturesDir(filePath);
|
|
|
|
|
if (!picturesDir.mkpath(QStringLiteral("Screenshots"))) {
|
|
|
|
|
qWarning() << "Couldn't create folder at"
|
|
|
|
|
<< picturesDir.path() + QStringLiteral("/Screenshots")
|
|
|
|
|
<< "to take screenshot.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
filePath += QStringLiteral("/Screenshots/Screenshot_%1.png").arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyyMMdd_hhmmss")));
|
|
|
|
|
|
2020-02-08 12:45:33 +00:00
|
|
|
// wait ~200 ms to wait for rest of animations
|
2020-03-20 00:08:59 +00:00
|
|
|
QTimer::singleShot(SCREENSHOT_DELAY, [=]() {
|
2020-10-01 07:38:05 +00:00
|
|
|
int lPipeFds[2];
|
|
|
|
|
if (pipe2(lPipeFds, O_CLOEXEC|O_NONBLOCK) != 0) {
|
|
|
|
|
qWarning() << "Could not take screenshot";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QDBusInterface lInterface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QStringLiteral("org.kde.kwin.Screenshot"));
|
|
|
|
|
// Take fullscreen screenshot, and no pointer
|
|
|
|
|
QDBusPendingCall pcall = lInterface.asyncCall("screenshotFullscreen", QVariant::fromValue(QDBusUnixFileDescriptor(lPipeFds[1])), false);
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
|
|
|
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher* watcher) {
|
|
|
|
|
if (watcher->isError()) {
|
|
|
|
|
const auto error = watcher->error();
|
|
|
|
|
qWarning() << "Error calling KWin DBus interface:" << error.name() << error.message();
|
2020-02-08 12:45:33 +00:00
|
|
|
}
|
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
});
|
2020-10-01 07:38:05 +00:00
|
|
|
auto lWatcher = new QFutureWatcher<QImage>(this);
|
|
|
|
|
QObject::connect(lWatcher, &QFutureWatcher<QImage>::finished, this,
|
|
|
|
|
[lWatcher, filePath, this] () {
|
|
|
|
|
lWatcher->deleteLater();
|
|
|
|
|
const QImage lImage = lWatcher->result();
|
|
|
|
|
qDebug() << lImage;
|
|
|
|
|
if(!lImage.save(filePath, "PNG")) {
|
|
|
|
|
qWarning() << "Failed to save screenshot to" << filePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
lWatcher->setFuture(QtConcurrent::run(readImage, lPipeFds[0]));
|
|
|
|
|
close(lPipeFds[1]);
|
2020-02-06 17:17:47 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 19:03:56 +00:00
|
|
|
K_EXPORT_PLASMA_APPLET_WITH_JSON(quicksettings, PhonePanel, "metadata.json")
|
|
|
|
|
|
|
|
|
|
#include "phonepanel.moc"
|