plugins: use more std::unique_ptr

This commit is contained in:
Xaver Hugl 2022-11-14 13:03:55 +01:00
parent 738b04a364
commit 34aa734c7b
9 changed files with 52 additions and 61 deletions

View file

@ -105,7 +105,7 @@ void DontCrashAuroraeDestroyDecoTest::testBorderlessMaximizedWindows()
// verify that the deco is Aurorae // verify that the deco is Aurorae
QCOMPARE(qstrcmp(window->decoration()->metaObject()->className(), "Aurorae::Decoration"), 0); QCOMPARE(qstrcmp(window->decoration()->metaObject()->className(), "Aurorae::Decoration"), 0);
// find the maximize button // find the maximize button
QQuickItem *item = window->decoration()->findChild<QQuickItem *>("maximizeButton"); QQuickItem *item = window->decoration()->property("item").value<QQuickItem *>()->findChild<QQuickItem *>("maximizeButton");
QVERIFY(item); QVERIFY(item);
const QPointF scenePoint = item->mapToScene(QPoint(0, 0)); const QPointF scenePoint = item->mapToScene(QPoint(0, 0));

View file

@ -256,8 +256,9 @@ Decoration::Decoration(QObject *parent, const QVariantList &args)
Decoration::~Decoration() Decoration::~Decoration()
{ {
delete m_qmlContext; m_item.reset();
delete m_view; m_qmlContext.reset();
m_view.reset();
Helper::instance().unref(); Helper::instance().unref();
} }
@ -268,7 +269,7 @@ void Decoration::init()
auto s = settings(); auto s = settings();
connect(s.data(), &KDecoration2::DecorationSettings::reconfigured, this, &Decoration::configChanged); connect(s.data(), &KDecoration2::DecorationSettings::reconfigured, this, &Decoration::configChanged);
m_qmlContext = new QQmlContext(Helper::instance().rootContext(), this); m_qmlContext = std::make_unique<QQmlContext>(Helper::instance().rootContext());
m_qmlContext->setContextProperty(QStringLiteral("decoration"), this); m_qmlContext->setContextProperty(QStringLiteral("decoration"), this);
auto component = Helper::instance().component(m_themeName); auto component = Helper::instance().component(m_themeName);
if (!component) { if (!component) {
@ -295,7 +296,7 @@ void Decoration::init()
// m_theme->setTabDragMimeType(tabDragMimeType()); // m_theme->setTabDragMimeType(tabDragMimeType());
m_qmlContext->setContextProperty(QStringLiteral("auroraeTheme"), theme); m_qmlContext->setContextProperty(QStringLiteral("auroraeTheme"), theme);
} }
m_item = qobject_cast<QQuickItem *>(component->create(m_qmlContext)); m_item.reset(qobject_cast<QQuickItem *>(component->create(m_qmlContext.get())));
if (!m_item) { if (!m_item) {
if (component->isError()) { if (component->isError()) {
const auto errors = component->errors(); const auto errors = component->errors();
@ -306,27 +307,25 @@ void Decoration::init()
return; return;
} }
m_item->setParent(m_qmlContext);
QVariant visualParent = property("visualParent"); QVariant visualParent = property("visualParent");
if (visualParent.isValid()) { if (visualParent.isValid()) {
m_item->setParentItem(visualParent.value<QQuickItem *>()); m_item->setParentItem(visualParent.value<QQuickItem *>());
visualParent.value<QQuickItem *>()->setProperty("drawBackground", false); visualParent.value<QQuickItem *>()->setProperty("drawBackground", false);
} else { } else {
m_view = new KWin::OffscreenQuickView(this, KWin::OffscreenQuickView::ExportMode::Image); m_view = std::make_unique<KWin::OffscreenQuickView>(this, KWin::OffscreenQuickView::ExportMode::Image);
m_item->setParentItem(m_view->contentItem()); m_item->setParentItem(m_view->contentItem());
auto updateSize = [this]() { auto updateSize = [this]() {
m_item->setSize(m_view->contentItem()->size()); m_item->setSize(m_view->contentItem()->size());
}; };
updateSize(); updateSize();
connect(m_view->contentItem(), &QQuickItem::widthChanged, m_item, updateSize); connect(m_view->contentItem(), &QQuickItem::widthChanged, m_item.get(), updateSize);
connect(m_view->contentItem(), &QQuickItem::heightChanged, m_item, updateSize); connect(m_view->contentItem(), &QQuickItem::heightChanged, m_item.get(), updateSize);
connect(m_view, &KWin::OffscreenQuickView::repaintNeeded, this, &Decoration::updateBuffer); connect(m_view.get(), &KWin::OffscreenQuickView::repaintNeeded, this, &Decoration::updateBuffer);
} }
m_supportsMask = m_item->property("supportsMask").toBool(); m_supportsMask = m_item->property("supportsMask").toBool();
setupBorders(m_item); setupBorders(m_item.get());
// TODO: Is there a more efficient way to react to border changes? // TODO: Is there a more efficient way to react to border changes?
auto trackBorders = [this](KWin::Borders *borders) { auto trackBorders = [this](KWin::Borders *borders) {
@ -619,6 +618,11 @@ KDecoration2::DecoratedClient *Decoration::clientPointer() const
return client().toStrongRef().data(); return client().toStrongRef().data();
} }
QQuickItem *Decoration::item() const
{
return m_item.get();
}
ThemeProvider::ThemeProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args) ThemeProvider::ThemeProvider(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
: KDecoration2::DecorationThemeProvider(parent, data, args) : KDecoration2::DecorationThemeProvider(parent, data, args)
, m_data(data) , m_data(data)

View file

@ -34,6 +34,7 @@ class Decoration : public KDecoration2::Decoration
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(KDecoration2::DecoratedClient *client READ clientPointer CONSTANT) Q_PROPERTY(KDecoration2::DecoratedClient *client READ clientPointer CONSTANT)
Q_PROPERTY(QQuickItem *item READ item)
public: public:
explicit Decoration(QObject *parent = nullptr, const QVariantList &args = QVariantList()); explicit Decoration(QObject *parent = nullptr, const QVariantList &args = QVariantList());
~Decoration() override; ~Decoration() override;
@ -43,6 +44,7 @@ public:
Q_INVOKABLE QVariant readConfig(const QString &key, const QVariant &defaultValue = QVariant()); Q_INVOKABLE QVariant readConfig(const QString &key, const QVariant &defaultValue = QVariant());
KDecoration2::DecoratedClient *clientPointer() const; KDecoration2::DecoratedClient *clientPointer() const;
QQuickItem *item() const;
public Q_SLOTS: public Q_SLOTS:
void init() override; void init() override;
@ -71,15 +73,15 @@ private:
bool m_supportsMask{false}; bool m_supportsMask{false};
QRect m_contentRect; // the geometry of the part of the buffer that is not a shadow when buffer was created. QRect m_contentRect; // the geometry of the part of the buffer that is not a shadow when buffer was created.
QQuickItem *m_item = nullptr; std::unique_ptr<QQuickItem> m_item;
QQmlContext *m_qmlContext = nullptr; std::unique_ptr<QQmlContext> m_qmlContext;
KWin::Borders *m_borders; KWin::Borders *m_borders;
KWin::Borders *m_maximizedBorders; KWin::Borders *m_maximizedBorders;
KWin::Borders *m_extendedBorders; KWin::Borders *m_extendedBorders;
KWin::Borders *m_padding; KWin::Borders *m_padding;
QString m_themeName; QString m_themeName;
KWin::OffscreenQuickView *m_view; std::unique_ptr<KWin::OffscreenQuickView> m_view;
}; };
class ThemeProvider : public KDecoration2::DecorationThemeProvider class ThemeProvider : public KDecoration2::DecorationThemeProvider

View file

@ -72,16 +72,13 @@ void AuroraeThemePrivate::initButtonFrame(AuroraeButtonType type)
************************************************/ ************************************************/
AuroraeTheme::AuroraeTheme(QObject *parent) AuroraeTheme::AuroraeTheme(QObject *parent)
: QObject(parent) : QObject(parent)
, d(new AuroraeThemePrivate) , d(std::make_unique<AuroraeThemePrivate>())
{ {
connect(this, &AuroraeTheme::themeChanged, this, &AuroraeTheme::borderSizesChanged); connect(this, &AuroraeTheme::themeChanged, this, &AuroraeTheme::borderSizesChanged);
connect(this, &AuroraeTheme::buttonSizesChanged, this, &AuroraeTheme::borderSizesChanged); connect(this, &AuroraeTheme::buttonSizesChanged, this, &AuroraeTheme::borderSizesChanged);
} }
AuroraeTheme::~AuroraeTheme() AuroraeTheme::~AuroraeTheme() = default;
{
delete d;
}
bool AuroraeTheme::isValid() const bool AuroraeTheme::isValid() const
{ {

View file

@ -210,7 +210,7 @@ private:
*/ */
void padding(int &left, int &top, int &right, int &bottom) const; void padding(int &left, int &top, int &right, int &bottom) const;
AuroraeThemePrivate *const d; const std::unique_ptr<AuroraeThemePrivate> d;
}; };
} // namespace } // namespace

View file

@ -313,13 +313,9 @@ void NightColorManager::resetAllTimers()
void NightColorManager::cancelAllTimers() void NightColorManager::cancelAllTimers()
{ {
delete m_slowUpdateStartTimer; m_slowUpdateStartTimer.reset();
delete m_slowUpdateTimer; m_slowUpdateTimer.reset();
delete m_quickAdjustTimer; m_quickAdjustTimer.reset();
m_slowUpdateStartTimer = nullptr;
m_slowUpdateTimer = nullptr;
m_quickAdjustTimer = nullptr;
} }
void NightColorManager::resetQuickAdjustTimer(int targetTemp) void NightColorManager::resetQuickAdjustTimer(int targetTemp)
@ -328,9 +324,9 @@ void NightColorManager::resetQuickAdjustTimer(int targetTemp)
// allow tolerance of one TEMPERATURE_STEP to compensate if a slow update is coincidental // allow tolerance of one TEMPERATURE_STEP to compensate if a slow update is coincidental
if (tempDiff > TEMPERATURE_STEP) { if (tempDiff > TEMPERATURE_STEP) {
cancelAllTimers(); cancelAllTimers();
m_quickAdjustTimer = new QTimer(this); m_quickAdjustTimer = std::make_unique<QTimer>();
m_quickAdjustTimer->setSingleShot(false); m_quickAdjustTimer->setSingleShot(false);
connect(m_quickAdjustTimer, &QTimer::timeout, this, [this, targetTemp]() { connect(m_quickAdjustTimer.get(), &QTimer::timeout, this, [this, targetTemp]() {
quickAdjust(targetTemp); quickAdjust(targetTemp);
}); });
@ -361,16 +357,14 @@ void NightColorManager::quickAdjust(int targetTemp)
if (nextTemp == targetTemp) { if (nextTemp == targetTemp) {
// stop timer, we reached the target temp // stop timer, we reached the target temp
delete m_quickAdjustTimer; m_quickAdjustTimer.reset();
m_quickAdjustTimer = nullptr;
resetSlowUpdateStartTimer(); resetSlowUpdateStartTimer();
} }
} }
void NightColorManager::resetSlowUpdateStartTimer() void NightColorManager::resetSlowUpdateStartTimer()
{ {
delete m_slowUpdateStartTimer; m_slowUpdateStartTimer.reset();
m_slowUpdateStartTimer = nullptr;
if (!m_running || m_quickAdjustTimer) { if (!m_running || m_quickAdjustTimer) {
// only reenable the slow update start timer when quick adjust is not active anymore // only reenable the slow update start timer when quick adjust is not active anymore
@ -384,9 +378,9 @@ void NightColorManager::resetSlowUpdateStartTimer()
} }
// set up the next slow update // set up the next slow update
m_slowUpdateStartTimer = new QTimer(this); m_slowUpdateStartTimer = std::make_unique<QTimer>();
m_slowUpdateStartTimer->setSingleShot(true); m_slowUpdateStartTimer->setSingleShot(true);
connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &NightColorManager::resetSlowUpdateStartTimer); connect(m_slowUpdateStartTimer.get(), &QTimer::timeout, this, &NightColorManager::resetSlowUpdateStartTimer);
updateTransitionTimings(false); updateTransitionTimings(false);
updateTargetTemperature(); updateTargetTemperature();
@ -404,8 +398,7 @@ void NightColorManager::resetSlowUpdateStartTimer()
void NightColorManager::resetSlowUpdateTimer() void NightColorManager::resetSlowUpdateTimer()
{ {
delete m_slowUpdateTimer; m_slowUpdateTimer.reset();
m_slowUpdateTimer = nullptr;
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();
const bool isDay = daylight(); const bool isDay = daylight();
@ -419,14 +412,14 @@ void NightColorManager::resetSlowUpdateTimer()
if (m_prev.first <= now && now <= m_prev.second) { if (m_prev.first <= now && now <= m_prev.second) {
int availTime = now.msecsTo(m_prev.second); int availTime = now.msecsTo(m_prev.second);
m_slowUpdateTimer = new QTimer(this); m_slowUpdateTimer = std::make_unique<QTimer>();
m_slowUpdateTimer->setSingleShot(false); m_slowUpdateTimer->setSingleShot(false);
if (isDay) { if (isDay) {
connect(m_slowUpdateTimer, &QTimer::timeout, this, [this]() { connect(m_slowUpdateTimer.get(), &QTimer::timeout, this, [this]() {
slowUpdate(m_dayTargetTemp); slowUpdate(m_dayTargetTemp);
}); });
} else { } else {
connect(m_slowUpdateTimer, &QTimer::timeout, this, [this]() { connect(m_slowUpdateTimer.get(), &QTimer::timeout, this, [this]() {
slowUpdate(m_nightTargetTemp); slowUpdate(m_nightTargetTemp);
}); });
} }
@ -454,8 +447,7 @@ void NightColorManager::slowUpdate(int targetTemp)
commitGammaRamps(nextTemp); commitGammaRamps(nextTemp);
if (nextTemp == targetTemp) { if (nextTemp == targetTemp) {
// stop timer, we reached the target temp // stop timer, we reached the target temp
delete m_slowUpdateTimer; m_slowUpdateTimer.reset();
m_slowUpdateTimer = nullptr;
} }
} }
@ -463,12 +455,11 @@ void NightColorManager::preview(uint previewTemp)
{ {
resetQuickAdjustTimer((int)previewTemp); resetQuickAdjustTimer((int)previewTemp);
if (m_previewTimer) { if (m_previewTimer) {
delete m_previewTimer; m_previewTimer.reset();
m_previewTimer = nullptr;
} }
m_previewTimer = new QTimer(this); m_previewTimer = std::make_unique<QTimer>();
m_previewTimer->setSingleShot(true); m_previewTimer->setSingleShot(true);
connect(m_previewTimer, &QTimer::timeout, this, &NightColorManager::stopPreview); connect(m_previewTimer.get(), &QTimer::timeout, this, &NightColorManager::stopPreview);
m_previewTimer->start(15000); m_previewTimer->start(15000);
QDBusMessage message = QDBusMessage::createMethodCall( QDBusMessage message = QDBusMessage::createMethodCall(

View file

@ -290,10 +290,10 @@ private:
double m_latFixed; double m_latFixed;
double m_lngFixed; double m_lngFixed;
QTimer *m_slowUpdateStartTimer = nullptr; std::unique_ptr<QTimer> m_slowUpdateStartTimer;
QTimer *m_slowUpdateTimer = nullptr; std::unique_ptr<QTimer> m_slowUpdateTimer;
QTimer *m_quickAdjustTimer = nullptr; std::unique_ptr<QTimer> m_quickAdjustTimer;
QTimer *m_previewTimer = nullptr; std::unique_ptr<QTimer> m_previewTimer;
int m_currentTemp = DEFAULT_DAY_TEMPERATURE; int m_currentTemp = DEFAULT_DAY_TEMPERATURE;
int m_targetTemperature = DEFAULT_DAY_TEMPERATURE; int m_targetTemperature = DEFAULT_DAY_TEMPERATURE;

View file

@ -578,15 +578,14 @@ void ScreenCastStream::tryEnqueue(pw_buffer *buffer)
// a corrupted buffer. // a corrupted buffer.
if (Compositor::self()->scene()->supportsNativeFence()) { if (Compositor::self()->scene()->supportsNativeFence()) {
Q_ASSERT_X(eglGetCurrentContext(), "tryEnqueue", "no current context"); Q_ASSERT_X(eglGetCurrentContext(), "tryEnqueue", "no current context");
m_pendingFence = new EGLNativeFence(kwinApp()->outputBackend()->sceneEglDisplay()); m_pendingFence = std::make_unique<EGLNativeFence>(kwinApp()->outputBackend()->sceneEglDisplay());
if (!m_pendingFence->isValid()) { if (!m_pendingFence->isValid()) {
qCWarning(KWIN_SCREENCAST) << "Failed to create a native EGL fence"; qCWarning(KWIN_SCREENCAST) << "Failed to create a native EGL fence";
glFinish(); glFinish();
enqueue(); enqueue();
} else { } else {
m_pendingNotifier = new QSocketNotifier(m_pendingFence->fileDescriptor(), m_pendingNotifier = std::make_unique<QSocketNotifier>(m_pendingFence->fileDescriptor(), QSocketNotifier::Read);
QSocketNotifier::Read, this); connect(m_pendingNotifier.get(), &QSocketNotifier::activated, this, &ScreenCastStream::enqueue);
connect(m_pendingNotifier, &QSocketNotifier::activated, this, &ScreenCastStream::enqueue);
} }
} else { } else {
// The compositing backend doesn't support native fences. We don't have any other choice // The compositing backend doesn't support native fences. We don't have any other choice
@ -600,14 +599,12 @@ void ScreenCastStream::enqueue()
{ {
Q_ASSERT_X(m_pendingBuffer, "enqueue", "pending buffer must be valid"); Q_ASSERT_X(m_pendingBuffer, "enqueue", "pending buffer must be valid");
delete m_pendingFence; m_pendingFence.reset();
delete m_pendingNotifier; m_pendingNotifier.reset();
pw_stream_queue_buffer(pwStream, m_pendingBuffer); pw_stream_queue_buffer(pwStream, m_pendingBuffer);
m_pendingBuffer = nullptr; m_pendingBuffer = nullptr;
m_pendingFence = nullptr;
m_pendingNotifier = nullptr;
} }
QVector<const spa_pod *> ScreenCastStream::buildFormats(bool fixate, char buffer[2048]) QVector<const spa_pod *> ScreenCastStream::buildFormats(bool fixate, char buffer[2048])

View file

@ -121,8 +121,8 @@ private:
QHash<struct pw_buffer *, std::shared_ptr<DmaBufTexture>> m_dmabufDataForPwBuffer; QHash<struct pw_buffer *, std::shared_ptr<DmaBufTexture>> m_dmabufDataForPwBuffer;
pw_buffer *m_pendingBuffer = nullptr; pw_buffer *m_pendingBuffer = nullptr;
QSocketNotifier *m_pendingNotifier = nullptr; std::unique_ptr<QSocketNotifier> m_pendingNotifier;
EGLNativeFence *m_pendingFence = nullptr; std::unique_ptr<EGLNativeFence> m_pendingFence;
std::optional<std::chrono::nanoseconds> m_start; std::optional<std::chrono::nanoseconds> m_start;
quint64 m_sequential = 0; quint64 m_sequential = 0;
bool m_hasDmaBuf = false; bool m_hasDmaBuf = false;