From 65a2f88d247b480a3ae8e2d302d979ee1b36fbca Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Thu, 18 Aug 2022 11:01:38 +0300 Subject: [PATCH] wayland: Clean up current mode handling in kde-output-device-v2 Compare OutputModeV2 handles to find the current mode, this is more robust than testing mode properties such as size or refresh rate. --- src/wayland/outputdevice_v2_interface.cpp | 40 ++++------------------- src/wayland/outputdevice_v2_interface.h | 16 ++------- src/waylandoutputdevicev2.cpp | 19 +++++++---- 3 files changed, 23 insertions(+), 52 deletions(-) diff --git a/src/wayland/outputdevice_v2_interface.cpp b/src/wayland/outputdevice_v2_interface.cpp index 33f63ec8d1..570a16d335 100644 --- a/src/wayland/outputdevice_v2_interface.cpp +++ b/src/wayland/outputdevice_v2_interface.cpp @@ -176,18 +176,18 @@ int OutputDeviceV2Interface::refreshRate() const return d->currentMode->refreshRate(); } +QList OutputDeviceV2Interface::modes() const +{ + return d->modes; +} + void OutputDeviceV2Interface::setCurrentMode(OutputDeviceModeV2Interface *mode) { Q_ASSERT(d->modes.contains(mode)); if (mode == d->currentMode) { return; } - if (d->currentMode) { - // another mode has the current flag - remove - d->currentMode->setFlags(d->currentMode->flags() & ~uint(OutputDeviceModeV2Interface::ModeFlag::Current)); - } - mode->setFlags(mode->flags() | OutputDeviceModeV2Interface::ModeFlag::Current); d->currentMode = mode; const auto clientResources = d->resourceMap(); @@ -199,18 +199,6 @@ void OutputDeviceV2Interface::setCurrentMode(OutputDeviceModeV2Interface *mode) d->updateGeometry(); } -bool OutputDeviceV2Interface::setCurrentMode(const QSize &size, int refreshRate) -{ - auto mode = std::find_if(d->modes.begin(), d->modes.end(), [size, refreshRate](OutputDeviceModeV2Interface *mode) { - return mode->size() == size && mode->refreshRate() == refreshRate; - }); - if (mode == d->modes.end()) { - return false; - } - setCurrentMode(*mode); - return true; -} - int32_t OutputDeviceV2InterfacePrivate::toTransform() const { switch (transform) { @@ -488,7 +476,7 @@ OutputDeviceV2Interface::Transform OutputDeviceV2Interface::transform() const return d->transform; } -void OutputDeviceV2Interface::setModes(const QList &modes) +void OutputDeviceV2Interface::setModes(const QList &modes, OutputDeviceModeV2Interface *currentMode) { if (modes.isEmpty()) { qCWarning(KWIN_CORE) << "Tried to set no modes for output"; @@ -499,25 +487,16 @@ void OutputDeviceV2Interface::setModes(const QListmodes; d->modes.clear(); - d->currentMode = nullptr; for (OutputDeviceModeV2Interface *outputDeviceMode : modes) { d->modes << outputDeviceMode; outputDeviceMode->setParent(this); - for (auto resource : clientResources) { d->sendNewMode(resource, outputDeviceMode); } - - if (outputDeviceMode->flags().testFlag(OutputDeviceModeV2Interface::ModeFlag::Current)) { - d->currentMode = outputDeviceMode; - } - } - - if (!d->currentMode) { - d->currentMode = d->modes.at(0); } + d->currentMode = currentMode; for (auto resource : clientResources) { d->sendCurrentMode(resource); } @@ -764,11 +743,6 @@ OutputDeviceModeV2Interface::ModeFlags OutputDeviceModeV2Interface::flags() cons return d->m_flags; } -void OutputDeviceModeV2Interface::setFlags(OutputDeviceModeV2Interface::ModeFlags flags) -{ - d->m_flags = flags; -} - void OutputDeviceModeV2InterfacePrivate::bindResource(wl_resource *resource) { send_size(resource, m_size.width(), m_size.height()); diff --git a/src/wayland/outputdevice_v2_interface.h b/src/wayland/outputdevice_v2_interface.h index c4b3907931..c913344865 100644 --- a/src/wayland/outputdevice_v2_interface.h +++ b/src/wayland/outputdevice_v2_interface.h @@ -97,7 +97,7 @@ public: QString name() const; QSize pixelSize() const; int refreshRate() const; - + QList modes() const; qreal scale() const; SubPixel subPixel() const; Transform transform() const; @@ -123,15 +123,8 @@ public: void setSubPixel(SubPixel subPixel); void setTransform(Transform transform); - void setModes(const QList &modes); + void setModes(const QList &modes, KWaylandServer::OutputDeviceModeV2Interface *currentMode); void setCurrentMode(KWaylandServer::OutputDeviceModeV2Interface *mode); - - /** - * Makes the mode with the specified @a size and @a refreshRate current. - * Returns @c false if no mode with the given attributes exists; otherwise returns @c true. - */ - bool setCurrentMode(const QSize &size, int refreshRate); - void setEdid(const QByteArray &edid); void setEnabled(bool enabled); void setUuid(const QUuid &uuid); @@ -160,8 +153,7 @@ class KWIN_EXPORT OutputDeviceModeV2Interface : public QObject Q_OBJECT public: enum class ModeFlag { - Current = 0x1, - Preferred = 0x2, + Preferred = 0x1, }; Q_ENUM(ModeFlag) Q_DECLARE_FLAGS(ModeFlags, ModeFlag) @@ -174,8 +166,6 @@ public: int refreshRate() const; OutputDeviceModeV2Interface::ModeFlags flags() const; - void setFlags(OutputDeviceModeV2Interface::ModeFlags newFlags); - static OutputDeviceModeV2Interface *get(wl_resource *native); private: diff --git a/src/waylandoutputdevicev2.cpp b/src/waylandoutputdevicev2.cpp index 73346e1ccc..46832062fd 100644 --- a/src/waylandoutputdevicev2.cpp +++ b/src/waylandoutputdevicev2.cpp @@ -97,23 +97,24 @@ WaylandOutputDevice::WaylandOutputDevice(Output *output, QObject *parent) void WaylandOutputDevice::updateModes(Output *output) { QList deviceModes; + OutputDeviceModeV2Interface *currentMode = nullptr; const auto modes = output->modes(); deviceModes.reserve(modes.size()); for (const std::shared_ptr &mode : modes) { OutputDeviceModeV2Interface::ModeFlags flags; - - if (output->currentMode() == mode) { - flags |= OutputDeviceModeV2Interface::ModeFlag::Current; - } if (mode->flags() & OutputMode::Flag::Preferred) { flags |= OutputDeviceModeV2Interface::ModeFlag::Preferred; } OutputDeviceModeV2Interface *deviceMode = new OutputDeviceModeV2Interface(mode, mode->size(), mode->refreshRate(), flags); deviceModes << deviceMode; + + if (output->currentMode() == mode) { + currentMode = deviceMode; + } } - m_outputDeviceV2->setModes(deviceModes); + m_outputDeviceV2->setModes(deviceModes, currentMode); } void WaylandOutputDevice::handleModesChanged() @@ -143,7 +144,13 @@ void WaylandOutputDevice::handleTransformChanged() void WaylandOutputDevice::handleCurrentModeChanged() { - m_outputDeviceV2->setCurrentMode(m_platformOutput->modeSize(), m_platformOutput->refreshRate()); + const auto modes = m_outputDeviceV2->modes(); + for (KWaylandServer::OutputDeviceModeV2Interface *mode : modes) { + if (mode->handle().lock() == m_platformOutput->currentMode()) { + m_outputDeviceV2->setCurrentMode(mode); + break; + } + } } void WaylandOutputDevice::handleCapabilitiesChanged()