From 166b5f96fc499dac4e3c26d68e35f5789971a9b1 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Sat, 11 Mar 2023 11:55:41 +0200 Subject: [PATCH] effects/screenshot: Use more FileDescriptor It helps to express the ownership of the pipe fd in ScreenShotSinkPipe2 better. ScreenShotSinkPipe2 is not made to take a FileDescriptor too because it's complicated by CaptureInteractive(). --- .../screenshot/screenshotdbusinterface2.cpp | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/effects/screenshot/screenshotdbusinterface2.cpp b/src/effects/screenshot/screenshotdbusinterface2.cpp index a4c911a7a9..b0a7bcfacb 100644 --- a/src/effects/screenshot/screenshotdbusinterface2.cpp +++ b/src/effects/screenshot/screenshotdbusinterface2.cpp @@ -172,14 +172,13 @@ class ScreenShotSinkPipe2 : public QObject public: ScreenShotSinkPipe2(int fileDescriptor, QDBusMessage replyMessage); - ~ScreenShotSinkPipe2(); void cancel(); void flush(const QImage &image); private: QDBusMessage m_replyMessage; - int m_fileDescriptor; + FileDescriptor m_fileDescriptor; }; ScreenShotSource2::ScreenShotSource2(const QFuture &future) @@ -233,13 +232,6 @@ ScreenShotSinkPipe2::ScreenShotSinkPipe2(int fileDescriptor, QDBusMessage replyM { } -ScreenShotSinkPipe2::~ScreenShotSinkPipe2() -{ - if (m_fileDescriptor != -1) { - close(m_fileDescriptor); - } -} - void ScreenShotSinkPipe2::cancel() { QDBusConnection::sessionBus().send(m_replyMessage.createErrorReply(s_errorCancelled, @@ -248,7 +240,7 @@ void ScreenShotSinkPipe2::cancel() void ScreenShotSinkPipe2::flush(const QImage &image) { - if (m_fileDescriptor == -1) { + if (!m_fileDescriptor.isValid()) { return; } @@ -261,15 +253,11 @@ void ScreenShotSinkPipe2::flush(const QImage &image) results.insert(QStringLiteral("stride"), quint32(image.bytesPerLine())); QDBusConnection::sessionBus().send(m_replyMessage.createReply(results)); - QtConcurrent::run([](int fileDescriptor, const QImage &image) { + QtConcurrent::run([fileDescriptor = std::move(m_fileDescriptor), image]() mutable { const QByteArray buffer(reinterpret_cast(image.constBits()), image.sizeInBytes()); - writeBufferToPipe(FileDescriptor(fileDescriptor), buffer); - }, - m_fileDescriptor, image); - - // The ownership of the pipe file descriptor has been moved to the worker thread. - m_fileDescriptor = -1; + writeBufferToPipe(std::move(fileDescriptor), buffer); + }); } ScreenShotDBusInterface2::ScreenShotDBusInterface2(ScreenShotEffect *effect)