diff --git a/backends/xrandr/xrandrconfig.cpp b/backends/xrandr/xrandrconfig.cpp index 8e31596..5959c91 100644 --- a/backends/xrandr/xrandrconfig.cpp +++ b/backends/xrandr/xrandrconfig.cpp @@ -1,635 +1,676 @@ /************************************************************************************* * Copyright (C) 2012 by Alejandro Fiestas Olivares * * Copyright (C) 2012 - 2015 by Daniel Vrátil * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library 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 * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #include "xrandrconfig.h" #include "xrandrscreen.h" #include "xrandr.h" #include "xrandrmode.h" #include "xrandroutput.h" #include "xrandrcrtc.h" #include "config.h" #include "output.h" #include "edid.h" #include "../xcbwrapper.h" #include #include #include using namespace KScreen; XRandRConfig::XRandRConfig() : QObject() , m_screen(nullptr) { m_screen = new XRandRScreen(this); XCB::ScopedPointer resources(XRandR::screenResources()); xcb_randr_crtc_t *crtcs = xcb_randr_get_screen_resources_crtcs(resources.data()); const int crtcsCount = xcb_randr_get_screen_resources_crtcs_length(resources.data()); for (int i = 0; i < crtcsCount; ++i) { addNewCrtc(crtcs[i]); } xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_outputs(resources.data()); const int outputsCount = xcb_randr_get_screen_resources_outputs_length(resources.data()); for (int i = 0; i < outputsCount; ++i) { addNewOutput(outputs[i]); } } XRandRConfig::~XRandRConfig() { qDeleteAll(m_outputs); qDeleteAll(m_crtcs); delete m_screen; } XRandROutput::Map XRandRConfig::outputs() const { return m_outputs; } XRandROutput* XRandRConfig::output(xcb_randr_output_t output) const { return m_outputs[output]; } XRandRCrtc::Map XRandRConfig::crtcs() const { return m_crtcs; } XRandRCrtc* XRandRConfig::crtc(xcb_randr_crtc_t crtc) const { return m_crtcs[crtc]; } XRandRScreen* XRandRConfig::screen() const { return m_screen; } void XRandRConfig::addNewOutput(xcb_randr_output_t id) { XRandROutput *xOutput = new XRandROutput(id, this); m_outputs.insert(id, xOutput); } void XRandRConfig::addNewCrtc(xcb_randr_crtc_t crtc) { m_crtcs.insert(crtc, new XRandRCrtc(crtc, this)); } void XRandRConfig::removeOutput(xcb_randr_output_t id) { delete m_outputs.take(id); } KScreen::ConfigPtr XRandRConfig::toKScreenConfig() const { KScreen::ConfigPtr config(new KScreen::Config); - auto features = Config::Feature::Writable | Config::Feature::PrimaryDisplay; + const Config::Features features = Config::Feature::Writable | Config::Feature::PrimaryDisplay | + Config::Feature::OutputReplication; config->setSupportedFeatures(features); KScreen::OutputList kscreenOutputs; for (auto iter = m_outputs.constBegin(); iter != m_outputs.constEnd(); ++iter) { KScreen::OutputPtr kscreenOutput = (*iter)->toKScreenOutput(); kscreenOutputs.insert(kscreenOutput->id(), kscreenOutput); } config->setOutputs(kscreenOutputs); config->setScreen(m_screen->toKScreenScreen()); return config; } void XRandRConfig::applyKScreenConfig(const KScreen::ConfigPtr &config) { const KScreen::OutputList kscreenOutputs = config->outputs(); const QSize newScreenSize = screenSize(config); const QSize currentScreenSize = m_screen->currentSize(); // When the current screen configuration is bigger than the new size (like // when rotating an output), the XSetScreenSize can fail or apply the smaller // size only partially, because we apply the size (we have to) before the // output changes. To prevent all kinds of weird screen sizes from happening, // we initially set such screen size, that it can take the current as well // as the new configuration, then we apply the output changes, and finally then // (if necessary) we reduce the screen size to fix the new configuration precisely. const QSize intermediateScreenSize = QSize(qMax(newScreenSize.width(), currentScreenSize.width()), qMax(newScreenSize.height(), currentScreenSize.height())); int neededCrtcs = 0; xcb_randr_output_t primaryOutput = 0; xcb_randr_output_t oldPrimaryOutput = 0; for (const XRandROutput *xrandrOutput : m_outputs) { if (xrandrOutput->isPrimary()) { oldPrimaryOutput = xrandrOutput->id(); break; } } - KScreen::OutputList toDisable, toEnable, toChange; + KScreen::OutputList toDisable, toEnable, toChange, toReplicate; for (const KScreen::OutputPtr &kscreenOutput : kscreenOutputs) { xcb_randr_output_t outputId = kscreenOutput->id(); XRandROutput *currentOutput = output(outputId); //Only set the output as primary if it is enabled. if (kscreenOutput->isPrimary() && kscreenOutput->isEnabled()) { primaryOutput = outputId; } const bool currentEnabled = currentOutput->isEnabled(); if (!kscreenOutput->isEnabled() && currentEnabled) { toDisable.insert(outputId, kscreenOutput); continue; } else if (kscreenOutput->isEnabled() && !currentEnabled) { toEnable.insert(outputId, kscreenOutput); ++neededCrtcs; continue; } else if (!kscreenOutput->isEnabled() && !currentEnabled) { continue; } ++neededCrtcs; + // Update replication when it is supposed to be a replica or changes not to be anymore. + if (kscreenOutput->replicationSource() || currentOutput->replicationSource()) { + if (int sourceId = kscreenOutput->replicationSource()) { + kscreenOutput->setPos(config->output(sourceId)->pos()); + } + if (!toReplicate.contains(outputId)) { + toReplicate.insert(outputId, kscreenOutput); + } + } + if (kscreenOutput->currentModeId() != currentOutput->currentModeId()) { if (!toChange.contains(outputId)) { toChange.insert(outputId, kscreenOutput); } } if (kscreenOutput->pos() != currentOutput->position()) { if (!toChange.contains(outputId)) { toChange.insert(outputId, kscreenOutput); } } if (kscreenOutput->rotation() != currentOutput->rotation()) { if (!toChange.contains(outputId)) { toChange.insert(outputId, kscreenOutput); } } XRandRMode *currentMode = currentOutput->modes().value( kscreenOutput->currentModeId().toInt()); // For some reason, in some environments currentMode is null // which doesn't make sense because it is the *current* mode... // Since we haven't been able to figure out the reason why // this happens, we are adding this debug code to try to // figure out how this happened. if (!currentMode) { qWarning() << "Current mode is null:" << "ModeId:" << currentOutput->currentModeId() << "Mode: " << currentOutput->currentMode() << "Output: " << currentOutput->id(); printConfig(config); printInternalCond(); continue; } // When the output would not fit into new screen size, we need to disable and reposition it. const QRect geom = kscreenOutput->geometry(); if (geom.right() > newScreenSize.width() || geom.bottom() > newScreenSize.height()) { if (!toDisable.contains(outputId)) { qCDebug(KSCREEN_XRANDR) << "The new output would not fit into screen - new geometry: " << geom << ", new screen size:" << newScreenSize; toDisable.insert(outputId, kscreenOutput); } } } const KScreen::ScreenPtr kscreenScreen = config->screen(); if (newScreenSize.width() > kscreenScreen->maxSize().width() || newScreenSize.height() > kscreenScreen->maxSize().height()) { qCDebug(KSCREEN_XRANDR) << "The new screen size is too big - requested: " << newScreenSize << ", maximum: " << kscreenScreen->maxSize(); return; } qCDebug(KSCREEN_XRANDR) << "Needed CRTCs: " << neededCrtcs; XCB::ScopedPointer screenResources(XRandR::screenResources()); if (neededCrtcs > screenResources->num_crtcs) { qCDebug(KSCREEN_XRANDR) << "We need more CRTCs than we have available - requested: " << neededCrtcs << ", available: " << screenResources->num_crtcs; return; } qCDebug(KSCREEN_XRANDR) << "Actions to perform:" << "\n" << "\tPrimary Output:" << (primaryOutput != oldPrimaryOutput); if (primaryOutput != oldPrimaryOutput) { qCDebug(KSCREEN_XRANDR) << "\t\tOld:" << oldPrimaryOutput << "\n" << "\t\tNew:" << primaryOutput; } qCDebug(KSCREEN_XRANDR) << "\tChange Screen Size:" << (newScreenSize != currentScreenSize); if (newScreenSize != currentScreenSize) { qCDebug(KSCREEN_XRANDR) << "\t\tOld:" << currentScreenSize << "\n" << "\t\tIntermediate:" << intermediateScreenSize << "\n" << "\t\tNew:" << newScreenSize; } qCDebug(KSCREEN_XRANDR) << "\tDisable outputs:" << !toDisable.isEmpty(); if (!toDisable.isEmpty()) { qCDebug(KSCREEN_XRANDR) << "\t\t" << toDisable.keys(); } qCDebug(KSCREEN_XRANDR) << "\tChange outputs:" << !toChange.isEmpty(); if (!toChange.isEmpty()) { qCDebug(KSCREEN_XRANDR) << "\t\t" << toChange.keys(); } qCDebug(KSCREEN_XRANDR) << "\tEnable outputs:" << !toEnable.isEmpty(); if (!toEnable.isEmpty()) { qCDebug(KSCREEN_XRANDR) << "\t\t" << toEnable.keys(); } // Grab the server so that no-one else can do changes to XRandR and to block // change notifications until we are done XCB::GrabServer grabber; //If there is nothing to do, not even bother if (oldPrimaryOutput == primaryOutput && toDisable.isEmpty() && - toEnable.isEmpty() && toChange.isEmpty()) { + toEnable.isEmpty() && toChange.isEmpty() && toReplicate.isEmpty()) { if (newScreenSize != currentScreenSize) { setScreenSize(newScreenSize); } return; } for (const KScreen::OutputPtr &output : toDisable) { disableOutput(output); } if (intermediateScreenSize != currentScreenSize) { setScreenSize(intermediateScreenSize); } bool forceScreenSizeUpdate = false; for (const KScreen::OutputPtr &output : toChange) { if (!changeOutput(output)) { /* If we disabled the output before changing it and XRandR failed * to re-enable it, then update screen size too */ if (toDisable.contains(output->id())) { - //output->setEnabled(false); + output->setEnabled(false); qCDebug(KSCREEN_XRANDR) << "Output failed to change: " << output->name(); forceScreenSizeUpdate = true; } } } for (const KScreen::OutputPtr &output : toEnable) { if (!enableOutput(output)) { - //output->setEnabled(false); qCDebug(KSCREEN_XRANDR) << "Output failed to be Enabled: " << output->name(); forceScreenSizeUpdate = true; } } + for (KScreen::OutputPtr &output : toReplicate) { + if (replicateOutput(output)) { + forceScreenSizeUpdate = true; + } + } + if (oldPrimaryOutput != primaryOutput) { setPrimaryOutput(primaryOutput); } if (forceScreenSizeUpdate || intermediateScreenSize != newScreenSize) { QSize newSize = newScreenSize; if (forceScreenSizeUpdate) { newSize = screenSize(config); qCDebug(KSCREEN_XRANDR) << "Forced to change screen size: " << newSize; } setScreenSize(newSize); } } void XRandRConfig::printConfig(const ConfigPtr &config) const { qCDebug(KSCREEN_XRANDR) << "KScreen version:" /*<< LIBKSCREEN_VERSION*/; if (!config) { qCDebug(KSCREEN_XRANDR) << "Config is invalid"; return; } if (!config->screen()) { qCDebug(KSCREEN_XRANDR) << "No screen in the configuration, broken backend"; return; } qCDebug(KSCREEN_XRANDR) << "Screen:" << "\n" << "\tmaxSize:" << config->screen()->maxSize() << "\n" << "\tminSize:" << config->screen()->minSize() << "\n" << "\tcurrentSize:" << config->screen()->currentSize(); const OutputList outputs = config->outputs(); for (const OutputPtr &output : outputs) { qCDebug(KSCREEN_XRANDR) << "\n-----------------------------------------------------\n" << "\n" << "Id: " << output->id() << "\n" << "Name: " << output->name() << "\n" << "Type: " << output->type() << "\n" << "Connected: " << output->isConnected(); if (!output->isConnected()) { continue; } qCDebug(KSCREEN_XRANDR) << "Enabled: " << output->isEnabled() << "\n" << "Primary: " << output->isPrimary() << "\n" << "Rotation: " << output->rotation() << "\n" << "Pos: " << output->pos() << "\n" << "MMSize: " << output->sizeMm(); if (output->currentMode()) { qCDebug(KSCREEN_XRANDR) << "Size: " << output->currentMode()->size(); } qCDebug(KSCREEN_XRANDR) << "Clones: " << (output->clones().isEmpty() ? QStringLiteral("None") : QString::number(output->clones().count())) << "\n" << "Mode: " << output->currentModeId() << "\n" << "Preferred Mode: " << output->preferredModeId() << "\n" << "Preferred modes: " << output->preferredModes() << "\n" << "Modes: "; ModeList modes = output->modes(); for (const ModePtr &mode : modes) { qCDebug(KSCREEN_XRANDR) << "\t" << mode->id() << " " << mode->name() << " " << mode->size() << " " << mode->refreshRate(); } Edid* edid = output->edid(); qCDebug(KSCREEN_XRANDR) << "EDID Info: "; if (edid && edid->isValid()) { qCDebug(KSCREEN_XRANDR) << "\tDevice ID: " << edid->deviceId() << "\n" << "\tName: " << edid->name() << "\n" << "\tVendor: " << edid->vendor() << "\n" << "\tSerial: " << edid->serial() << "\n" << "\tEISA ID: " << edid->eisaId() << "\n" << "\tHash: " << edid->hash() << "\n" << "\tWidth: " << edid->width() << "\n" << "\tHeight: " << edid->height() << "\n" << "\tGamma: " << edid->gamma() << "\n" << "\tRed: " << edid->red() << "\n" << "\tGreen: " << edid->green() << "\n" << "\tBlue: " << edid->blue() << "\n" << "\tWhite: " << edid->white(); } else { qCDebug(KSCREEN_XRANDR) << "\tUnavailable"; } } } void XRandRConfig::printInternalCond() const { qCDebug(KSCREEN_XRANDR) << "Internal config in xrandr"; for (const XRandROutput *output : m_outputs) { qCDebug(KSCREEN_XRANDR) << "Id: " << output->id() << "\n" << "Current Mode: " << output->currentMode() << "\n" << "Current mode id: " << output->currentModeId() << "\n" << "Connected: " << output->isConnected() << "\n" << "Enabled: " << output->isEnabled() << "\n" << "Primary: " << output->isPrimary(); if (!output->isEnabled()) { continue; } XRandRMode::Map modes = output->modes(); for (const XRandRMode *mode : modes) { qCDebug(KSCREEN_XRANDR) << "\t" << mode->id() << "\n" << "\t" << mode->name() << "\n" << "\t" << mode->size() << mode->refreshRate(); } } } QSize XRandRConfig::screenSize(const KScreen::ConfigPtr &config) const { QRect rect; for (const KScreen::OutputPtr &output : config->outputs()) { if (!output->isConnected() || !output->isEnabled()) { continue; } const ModePtr currentMode = output->currentMode(); if (!currentMode) { qCDebug(KSCREEN_XRANDR) << "Output: " << output->name() << " has no current Mode!"; continue; } const QRect outputGeom = output->geometry(); rect = rect.united(outputGeom); } const QSize size = QSize(rect.width(), rect.height()); qCDebug(KSCREEN_XRANDR) << "Requested screen size is" << size; return size; } bool XRandRConfig::setScreenSize(const QSize &size) const { const double dpi = 25.4 * XRandR::screen()->height_in_pixels / XRandR::screen()->height_in_millimeters; const int widthMM = (25.4 * size.width()) / dpi; const int heightMM = (25.4 * size.height()) / dpi; qCDebug(KSCREEN_XRANDR) << "RRSetScreenSize" << "\n" << "\tDPI:" << dpi << "\n" << "\tSize:" << size << "\n" << "\tSizeMM:" << QSize(widthMM, heightMM); xcb_randr_set_screen_size(XCB::connection(), XRandR::rootWindow(), size.width(), size.height(), widthMM, heightMM); m_screen->update(size); return true; } void XRandRConfig::setPrimaryOutput(xcb_randr_output_t outputId) const { qCDebug(KSCREEN_XRANDR) << "RRSetOutputPrimary" << "\n" << "\tNew primary:" << outputId; xcb_randr_set_output_primary(XCB::connection(), XRandR::rootWindow(), outputId); for (XRandROutput *output : m_outputs) { output->setIsPrimary(output->id() == outputId); } } bool XRandRConfig::disableOutput(const OutputPtr &kscreenOutput) const { XRandROutput *xOutput = output(kscreenOutput->id()); Q_ASSERT(xOutput); Q_ASSERT(xOutput->crtc()); if (!xOutput->crtc()) { qCWarning(KSCREEN_XRANDR) << "Attempting to disable output without CRTC, wth?"; return false; } const xcb_randr_crtc_t crtc = xOutput->crtc()->crtc(); qCDebug(KSCREEN_XRANDR) << "RRSetCrtcConfig (disable output)" << "\n" << "\tCRTC:" << crtc; auto cookie = xcb_randr_set_crtc_config(XCB::connection(), crtc, XCB_CURRENT_TIME, XCB_CURRENT_TIME, 0, 0, XCB_NONE, XCB_RANDR_ROTATION_ROTATE_0, 0, nullptr); XCB::ScopedPointer reply(xcb_randr_set_crtc_config_reply(XCB::connection(), cookie, nullptr)); if (!reply) { qCDebug(KSCREEN_XRANDR) << "\tResult: unknown (error)"; return false; } qCDebug(KSCREEN_XRANDR) << "\tResult:" << reply->status; // Update the cached output now, otherwise we get RRNotify_CrtcChange notification // for an outdated output, which can lead to a crash. if (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS) { xOutput->update(XCB_NONE, XCB_NONE, xOutput->isConnected() ? XCB_RANDR_CONNECTION_CONNECTED : XCB_RANDR_CONNECTION_DISCONNECTED, kscreenOutput->isPrimary()); } return (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS); } bool XRandRConfig::enableOutput(const OutputPtr &kscreenOutput) const { - xcb_randr_output_t outputs[1] { static_cast(kscreenOutput->id()) }; - XRandRCrtc *freeCrtc = nullptr; qCDebug(KSCREEN_XRANDR) << m_crtcs; for (XRandRCrtc *crtc : m_crtcs) { crtc->update(); qCDebug(KSCREEN_XRANDR) << "Testing CRTC" << crtc->crtc() << "\n" << "\tFree:" << crtc->isFree() << "\n" << "\tMode:" << crtc->mode() << "\n" << "\tPossible outputs:" << crtc->possibleOutputs() << "\n" << "\tConnected outputs:" << crtc->outputs() << "\n" << "\tGeometry:" << crtc->geometry(); if (crtc->isFree() && crtc->possibleOutputs().contains(kscreenOutput->id())) { freeCrtc = crtc; break; } } if (!freeCrtc) { qCWarning(KSCREEN_XRANDR) << "Failed to get free CRTC for output" << kscreenOutput->id(); return false; } const int modeId = kscreenOutput->currentMode() ? kscreenOutput->currentModeId().toInt() : kscreenOutput->preferredModeId().toInt(); qCDebug(KSCREEN_XRANDR) << "RRSetCrtcConfig (enable output)" << "\n" << "\tOutput:" << kscreenOutput->id() << "(" << kscreenOutput->name() << ")" << "\n" << "\tNew CRTC:" << freeCrtc->crtc() << "\n" << "\tPos:" << kscreenOutput->pos() << "\n" << "\tMode:" << kscreenOutput->currentMode() << "Preferred:" << kscreenOutput->preferredModeId() << "\n" << "\tRotation:" << kscreenOutput->rotation(); - auto cookie = xcb_randr_set_crtc_config(XCB::connection(), freeCrtc->crtc(), - XCB_CURRENT_TIME, XCB_CURRENT_TIME, - kscreenOutput->pos().rx(), kscreenOutput->pos().ry(), - modeId, - kscreenOutput->rotation(), - 1, outputs); + if (!sendConfig(kscreenOutput, freeCrtc)) { + return false; + } - XCB::ScopedPointer - reply(xcb_randr_set_crtc_config_reply(XCB::connection(), cookie, nullptr)); + output(kscreenOutput->id())->update(freeCrtc->crtc(), modeId, XCB_RANDR_CONNECTION_CONNECTED, + kscreenOutput->isPrimary()); + return true; +} - if (!reply) { - qCDebug(KSCREEN_XRANDR) << "Result: unknown (error)"; - return false; +bool XRandRConfig::changeOutput(const KScreen::OutputPtr &kscreenOutput) const +{ + XRandROutput *xOutput = output(kscreenOutput->id()); + Q_ASSERT(xOutput); + + if (!xOutput->crtc()) { + qCDebug(KSCREEN_XRANDR) << "Output" << kscreenOutput->id() + << "has no CRTC, falling back to enableOutput()"; + return enableOutput(kscreenOutput); } - qCDebug(KSCREEN_XRANDR) << "\tResult:" << reply->status; - if (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS) { - XRandROutput *xOutput = output(kscreenOutput->id()); - xOutput->update(freeCrtc->crtc(), modeId, XCB_RANDR_CONNECTION_CONNECTED, - kscreenOutput->isPrimary()); + int modeId = kscreenOutput->currentMode() ? kscreenOutput->currentModeId().toInt() : + kscreenOutput->preferredModeId().toInt(); + + qCDebug(KSCREEN_XRANDR) << "RRSetCrtcConfig (change output)" << "\n" + << "\tOutput:" << kscreenOutput->id() << "(" << kscreenOutput->name() + << ")" << "\n" + << "\tCRTC:" << xOutput->crtc()->crtc() << "\n" + << "\tPos:" << kscreenOutput->pos() << "\n" + << "\tMode:" << modeId << kscreenOutput->currentMode() << "\n" + << "\tRotation:" << kscreenOutput->rotation(); + + if (!sendConfig(kscreenOutput, xOutput->crtc())) { + return false; } - return (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS); + + xOutput->update(xOutput->crtc()->crtc(), modeId, + XCB_RANDR_CONNECTION_CONNECTED, kscreenOutput->isPrimary()); + return true; } -bool XRandRConfig::changeOutput(const OutputPtr &kscreenOutput) const +bool XRandRConfig::replicateOutput(const KScreen::OutputPtr &kscreenOutput) const { XRandROutput *xOutput = output(kscreenOutput->id()); Q_ASSERT(xOutput); + if (!xOutput->crtc()) { qCDebug(KSCREEN_XRANDR) << "Output" << kscreenOutput->id() << "has no CRTC, falling back to enableOutput()"; - return enableOutput(kscreenOutput); + enableOutput(kscreenOutput); } int modeId = kscreenOutput->currentMode() ? kscreenOutput->currentModeId().toInt() : kscreenOutput->preferredModeId().toInt(); qCDebug(KSCREEN_XRANDR) << "RRSetCrtcConfig (change output)" << "\n" << "\tOutput:" << kscreenOutput->id() << "(" << kscreenOutput->name() << ")" << "\n" << "\tCRTC:" << xOutput->crtc()->crtc() << "\n" << "\tPos:" << kscreenOutput->pos() << "\n" << "\tMode:" << modeId << kscreenOutput->currentMode() << "\n" << "\tRotation:" << kscreenOutput->rotation(); + if (!xOutput->setReplicationSource(kscreenOutput->replicationSource())) { + return false; + } + if (!sendConfig(kscreenOutput, xOutput->crtc())) { + return false; + } + + xOutput->update(xOutput->crtc()->crtc(), modeId, + XCB_RANDR_CONNECTION_CONNECTED, kscreenOutput->isPrimary()); + return true; +} + +bool XRandRConfig::sendConfig(const KScreen::OutputPtr &kscreenOutput, XRandRCrtc *crtc) const +{ xcb_randr_output_t outputs[1] { static_cast(kscreenOutput->id()) }; + const int modeId = kscreenOutput->currentMode() ? kscreenOutput->currentModeId().toInt() : + kscreenOutput->preferredModeId().toInt(); - auto cookie = xcb_randr_set_crtc_config(XCB::connection(), xOutput->crtc()->crtc(), - XCB_CURRENT_TIME, XCB_CURRENT_TIME, - kscreenOutput->pos().rx(), kscreenOutput->pos().ry(), - modeId, - kscreenOutput->rotation(), - 1, outputs); + auto cookie = xcb_randr_set_crtc_config(XCB::connection(), crtc->crtc(), + XCB_CURRENT_TIME, XCB_CURRENT_TIME, + kscreenOutput->pos().rx(), kscreenOutput->pos().ry(), + modeId, + kscreenOutput->rotation(), + 1, outputs); XCB::ScopedPointer reply(xcb_randr_set_crtc_config_reply(XCB::connection(), cookie, nullptr)); - if (!reply) { qCDebug(KSCREEN_XRANDR) << "\tResult: unknown (error)"; return false; } qCDebug(KSCREEN_XRANDR) << "\tResult: " << reply->status; - - if (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS) { - xOutput->update(xOutput->crtc()->crtc(), modeId, - XCB_RANDR_CONNECTION_CONNECTED, kscreenOutput->isPrimary()); - } return (reply->status == XCB_RANDR_SET_CONFIG_SUCCESS); } diff --git a/backends/xrandr/xrandrconfig.h b/backends/xrandr/xrandrconfig.h index 66283de..7947066 100644 --- a/backends/xrandr/xrandrconfig.h +++ b/backends/xrandr/xrandrconfig.h @@ -1,74 +1,77 @@ /************************************************************************************* * Copyright 2012, 2013 Daniel Vrátil * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library 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 * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #pragma once #include #include "xrandr.h" #include "xrandrcrtc.h" #include "xrandroutput.h" class XRandRScreen; namespace KScreen { class Config; } class XRandRConfig : public QObject { Q_OBJECT public: XRandRConfig(); ~XRandRConfig() override; XRandROutput::Map outputs() const; XRandROutput *output(xcb_randr_output_t output) const; XRandRCrtc::Map crtcs() const; XRandRCrtc *crtc(xcb_randr_crtc_t crtc) const; XRandRScreen *screen() const; void addNewOutput(xcb_randr_output_t id); void addNewCrtc(xcb_randr_crtc_t crtc); void removeOutput(xcb_randr_output_t id); KScreen::ConfigPtr toKScreenConfig() const; void applyKScreenConfig(const KScreen::ConfigPtr &config); private: QSize screenSize(const KScreen::ConfigPtr &config) const; bool setScreenSize(const QSize &size) const; void setPrimaryOutput(xcb_randr_output_t outputId) const; bool disableOutput(const KScreen::OutputPtr &output) const; bool enableOutput(const KScreen::OutputPtr &output) const; bool changeOutput(const KScreen::OutputPtr &output) const; + bool replicateOutput(const KScreen::OutputPtr &output) const; + + bool sendConfig(const KScreen::OutputPtr &kscreenOutput, XRandRCrtc *crtc) const; /** * We need to print stuff to discover the damn bug * where currentMode is null */ void printConfig(const KScreen::ConfigPtr &config) const; void printInternalCond() const; XRandROutput::Map m_outputs; XRandRCrtc::Map m_crtcs; XRandRScreen *m_screen; }; diff --git a/backends/xrandr/xrandroutput.cpp b/backends/xrandr/xrandroutput.cpp index ef22520..48cb13b 100644 --- a/backends/xrandr/xrandroutput.cpp +++ b/backends/xrandr/xrandroutput.cpp @@ -1,349 +1,521 @@ /************************************************************************************* * Copyright (C) 2012 by Alejandro Fiestas Olivares * * Copyright (C) 2012, 2013 by Daniel Vrátil * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library 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 * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #include "xrandroutput.h" #include "config.h" #include "xrandr.h" #include "xrandrconfig.h" #include "xrandrmode.h" #include "../utils.h" +#include + Q_DECLARE_METATYPE(QList) +#define DOUBLE_TO_FIXED(d) ((xcb_render_fixed_t) ((d) * 65536)) +#define FIXED_TO_DOUBLE(f) ((double) ((f) / 65536.0)) + +xcb_render_fixed_t fOne = DOUBLE_TO_FIXED(1); +xcb_render_fixed_t fZero = DOUBLE_TO_FIXED(0); + XRandROutput::XRandROutput(xcb_randr_output_t id, XRandRConfig *config) : QObject(config) , m_config(config) , m_id(id) , m_primary(false) , m_type(KScreen::Output::Unknown) + , m_replicationSource(XCB_NONE) , m_crtc(nullptr) { init(); } XRandROutput::~XRandROutput() { } xcb_randr_output_t XRandROutput::id() const { return m_id; } bool XRandROutput::isConnected() const { return m_connected == XCB_RANDR_CONNECTION_CONNECTED; } bool XRandROutput::isEnabled() const { return m_crtc != nullptr && m_crtc->mode() != XCB_NONE; } bool XRandROutput::isPrimary() const { return m_primary; } QPoint XRandROutput::position() const { return m_crtc ? m_crtc->geometry().topLeft() : QPoint(); } QSize XRandROutput::size() const { return m_crtc ? m_crtc->geometry().size() : QSize(); } XRandRMode::Map XRandROutput::modes() const { return m_modes; } QString XRandROutput::currentModeId() const { return m_crtc ? QString::number(m_crtc->mode()) : QString(); } XRandRMode* XRandROutput::currentMode() const { if (!m_crtc) { return nullptr; } unsigned int modeId = m_crtc->mode(); if (!m_modes.contains(modeId)) { return nullptr; } return m_modes[modeId]; } KScreen::Output::Rotation XRandROutput::rotation() const { return static_cast(m_crtc ? m_crtc->rotation() : XCB_RANDR_ROTATION_ROTATE_0); } +bool XRandROutput::isHorizontal() const +{ + const auto rot = rotation(); + return rot == KScreen::Output::Rotation::None || rot == KScreen::Output::Rotation::Inverted; +} + QByteArray XRandROutput::edid() const { if (m_edid.isNull()) { m_edid = XRandR::outputEdid(m_id); } return m_edid; } XRandRCrtc* XRandROutput::crtc() const { return m_crtc; } +xcb_randr_output_t XRandROutput::replicationSource() const +{ + return m_replicationSource; +} + void XRandROutput::update() { init(); } void XRandROutput::update(xcb_randr_crtc_t crtc, xcb_randr_mode_t mode, xcb_randr_connection_t conn, bool primary) { qCDebug(KSCREEN_XRANDR) << "XRandROutput" << m_id << "update" << "\n" << "\tm_connected:" << m_connected << "\n" << "\tm_crtc" << m_crtc << "\n" << "\tCRTC:" << crtc << "\n" << "\tMODE:" << mode << "\n" << "\tConnection:" << conn << "\n" << "\tPrimary:" << primary; // Connected or disconnected if (isConnected() != (conn == XCB_RANDR_CONNECTION_CONNECTED)) { if (conn == XCB_RANDR_CONNECTION_CONNECTED) { // New monitor has been connected, refresh everything init(); } else { // Disconnected m_connected = conn; m_clones.clear(); m_heightMm = 0; m_widthMm = 0; m_type = KScreen::Output::Unknown; qDeleteAll(m_modes); m_modes.clear(); m_preferredModes.clear(); m_edid.clear(); } } else if (conn == XCB_RANDR_CONNECTION_CONNECTED) { // the output changed in some way, let's update the internal // list of modes, as it may have changed XCB::OutputInfo outputInfo(m_id, XCB_TIME_CURRENT_TIME); if (outputInfo) { updateModes(outputInfo); } m_hotplugModeUpdate = XRandR::hasProperty(m_id, "hotplug_mode_update"); } // A monitor has been enabled or disabled // We don't use isEnabled(), because it checks for crtc && crtc->mode(), however // crtc->mode may already be unset due to xcb_randr_crtc_tChangeNotify coming before // xcb_randr_output_tChangeNotify and reseting the CRTC mode if ((m_crtc == nullptr) != (crtc == XCB_NONE)) { if (crtc == XCB_NONE && mode == XCB_NONE) { // Monitor has been disabled m_crtc->disconectOutput(m_id); m_crtc = nullptr; } else { m_crtc = m_config->crtc(crtc); m_crtc->connectOutput(m_id); } } // Primary has changed m_primary = primary; } void XRandROutput::setIsPrimary(bool primary) { m_primary = primary; } void XRandROutput::init() { XCB::OutputInfo outputInfo(m_id, XCB_TIME_CURRENT_TIME); Q_ASSERT(outputInfo); if (!outputInfo) { return; } XCB::PrimaryOutput primary(XRandR::rootWindow()); m_name = QString::fromUtf8((const char *) xcb_randr_get_output_info_name(outputInfo.data()), outputInfo->name_len); m_type = fetchOutputType(m_id, m_name); m_icon = QString(); m_connected = (xcb_randr_connection_t) outputInfo->connection; m_primary = (primary->output == m_id); xcb_randr_output_t *clones = xcb_randr_get_output_info_clones(outputInfo.data()); for (int i = 0; i < outputInfo->num_clones; ++i) { m_clones.append(clones[i]); } m_widthMm = outputInfo->mm_width; m_heightMm = outputInfo->mm_height; m_crtc = m_config->crtc(outputInfo->crtc); if (m_crtc) { m_crtc->connectOutput(m_id); } m_hotplugModeUpdate = XRandR::hasProperty(m_id, "hotplug_mode_update"); updateModes(outputInfo); } void XRandROutput::updateModes(const XCB::OutputInfo &outputInfo) { /* Init modes */ XCB::ScopedPointer screenResources(XRandR::screenResources()); Q_ASSERT(screenResources); if (!screenResources) { return; } xcb_randr_mode_info_t *modes = xcb_randr_get_screen_resources_modes(screenResources.data()); xcb_randr_mode_t *outputModes = xcb_randr_get_output_info_modes(outputInfo.data()); m_preferredModes.clear(); qDeleteAll(m_modes); m_modes.clear(); for (int i = 0; i < outputInfo->num_modes; ++i) { /* Resources->modes contains all possible modes, we are only interested * in those listed in outputInfo->modes. */ for (int j = 0; j < screenResources->num_modes; ++j) { if (modes[j].id != outputModes[i]) { continue; } XRandRMode *mode = new XRandRMode(modes[j], this); m_modes.insert(mode->id(), mode); if (i < outputInfo->num_preferred) { m_preferredModes.append(QString::number(mode->id())); } break; } } } KScreen::Output::Type XRandROutput::fetchOutputType(xcb_randr_output_t outputId, const QString &name) { QString type = QString::fromUtf8(typeFromProperty(outputId)); if (type.isEmpty()) { type = name; } return Utils::guessOutputType(type, name); } QByteArray XRandROutput::typeFromProperty(xcb_randr_output_t outputId) { QByteArray type; XCB::InternAtom atomType(true, 13, "ConnectorType"); if (!atomType) { return type; } auto cookie = xcb_randr_get_output_property(XCB::connection(), outputId, atomType->atom, XCB_ATOM_ANY, 0, 100, false, false); XCB::ScopedPointer reply(xcb_randr_get_output_property_reply(XCB::connection(), cookie, nullptr)); if (!reply) { return type; } if (!(reply->type == XCB_ATOM_ATOM && reply->format == 32 && reply->num_items == 1)) { return type; } const uint8_t *prop = xcb_randr_get_output_property_data(reply.data()); XCB::AtomName atomName(*reinterpret_cast(prop)); if (!atomName) { return type; } char *connectorType = xcb_get_atom_name_name(atomName); if (!connectorType) { return type; } type = connectorType; return type; } +bool isScaling(const xcb_render_transform_t &tr) +{ + return tr.matrix11 != fZero && tr.matrix12 == fZero && tr.matrix13 == fZero && + tr.matrix21 == fZero && tr.matrix22 != fZero && tr.matrix23 == fZero && + tr.matrix31 == fZero && tr.matrix32 == fZero && tr.matrix33 == fOne; +} + +xcb_render_transform_t zeroMatrix() +{ + return { DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), + DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), + DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0) }; +} + +xcb_render_transform_t XRandROutput::currentTransform() const +{ + auto cookie = xcb_randr_get_crtc_transform(XCB::connection(), m_crtc->crtc()); + xcb_generic_error_t *error = nullptr; + auto *reply = xcb_randr_get_crtc_transform_reply(XCB::connection(), cookie, &error); + if (error) { + return zeroMatrix(); + } + + const xcb_render_transform_t transform = reply->pending_transform; + free(reply); + return transform; +} + +QSizeF XRandROutput::scaledSize(xcb_render_transform_t transform) const +{ + const QSize ownSize = size(); + if (!ownSize.isValid()) { + return QSize(); + } + + const qreal width = FIXED_TO_DOUBLE(transform.matrix11) * ownSize.width(); + const qreal height = FIXED_TO_DOUBLE(transform.matrix22) * ownSize.height(); + + return QSizeF(width, height); +} + +bool XRandROutput::isReplicaOf(XRandROutput *output, xcb_render_transform_t ownTransform) const +{ + if (output->id() == m_id) { + return false; + } + if (output->position() != position()) { + return false; + } + if (output->replicationSource() != XCB_NONE) { + return false; + } + + const QSizeF sSize = scaledSize(ownTransform); + if (!sSize.isValid()) { + return false; + } + + const auto outputTransform = output->currentTransform(); + if (!isScaling(outputTransform)) { + return false; + } + + if (sSize != output->scaledSize(outputTransform)) { + return false; + } + + return true; +} + +xcb_render_transform_t unityTransform() +{ + return { DOUBLE_TO_FIXED(1), DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), + DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(1), DOUBLE_TO_FIXED(0), + DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(0), DOUBLE_TO_FIXED(1) }; +} + +xcb_render_transform_t XRandROutput::getReplicationTransform(XRandROutput *source) +{ + if (!source) { + return unityTransform(); + } + + const auto *sourceMode = source->currentMode(); + const auto *ownMode = currentMode(); + if (!sourceMode || !ownMode) { + return unityTransform(); + } + + QSize sourceSize = sourceMode->size(); + QSize size = ownMode->size(); + + if (isHorizontal()) { + if (!source->isHorizontal()) { + sourceSize.transpose(); + } + } else if (source->isHorizontal()) { + size.transpose(); + } + + const qreal widthFactor = sourceSize.width() / (qreal)size.width(); + const qreal heightFactor = sourceSize.height() / (qreal)size.height(); + + xcb_render_transform_t transform = unityTransform(); + transform.matrix11 = DOUBLE_TO_FIXED(widthFactor); + transform.matrix22 = DOUBLE_TO_FIXED(heightFactor); + + return transform; +} + +bool XRandROutput::updateReplication() +{ + XRandROutput *source = m_config->output(m_replicationSource); + if (source && (!source->isEnabled() || !source->isConnected())) { + return false; + } + + xcb_render_transform_t transform = getReplicationTransform(source); + QByteArray filterName(isScaling(transform) ? "bilinear" : "nearest"); + + auto cookie = xcb_randr_set_crtc_transform_checked(XCB::connection(), + m_crtc->crtc(), + transform, + filterName.size(), filterName.data(), + 0, nullptr); + xcb_generic_error_t *error = xcb_request_check(XCB::connection(), cookie); + if (error) { + qCDebug(KSCREEN_XRANDR) << "Error on replication transformation!"; + free(error); + return false; + } + free(error); + return true; +} + +bool XRandROutput::setReplicationSource(xcb_randr_output_t source) +{ + if (!m_crtc) { + return false; + } + if (m_replicationSource == source) { + return true; + } + xcb_randr_output_t oldSource = m_replicationSource; + m_replicationSource = source; + if (!updateReplication()) { + m_replicationSource = oldSource; + return false; + } + return true; +} + KScreen::OutputPtr XRandROutput::toKScreenOutput() const { KScreen::OutputPtr kscreenOutput(new KScreen::Output); const bool signalsBlocked = kscreenOutput->signalsBlocked(); kscreenOutput->blockSignals(true); kscreenOutput->setId(m_id); kscreenOutput->setType(m_type); kscreenOutput->setSizeMm(QSize(m_widthMm, m_heightMm)); kscreenOutput->setName(m_name); kscreenOutput->setIcon(m_icon); //See https://bugzilla.redhat.com/show_bug.cgi?id=1290586 //QXL will be creating a new mode we need to jump to every time the display is resized kscreenOutput->setFollowPreferredMode(m_hotplugModeUpdate); kscreenOutput->setConnected(isConnected()); if (isConnected()) { KScreen::ModeList kscreenModes; for (auto iter = m_modes.constBegin(), end = m_modes.constEnd(); iter != end; ++iter) { XRandRMode *mode = iter.value(); kscreenModes.insert(QString::number(iter.key()), mode->toKScreenMode()); } kscreenOutput->setModes(kscreenModes); kscreenOutput->setPreferredModes(m_preferredModes); kscreenOutput->setPrimary(m_primary); kscreenOutput->setClones([](const QList &clones) { QList kclones; kclones.reserve(clones.size()); for (xcb_randr_output_t o : clones) { kclones.append(static_cast(o)); } return kclones; }(m_clones)); kscreenOutput->setEnabled(isEnabled()); if (isEnabled()) { kscreenOutput->setSize(size()); kscreenOutput->setPos(position()); kscreenOutput->setRotation(rotation()); kscreenOutput->setCurrentModeId(currentModeId()); } + kscreenOutput->setReplicationSource(m_replicationSource); } - kscreenOutput->blockSignals(signalsBlocked); return kscreenOutput; } diff --git a/backends/xrandr/xrandroutput.h b/backends/xrandr/xrandroutput.h index 08c6968..0d1b27a 100644 --- a/backends/xrandr/xrandroutput.h +++ b/backends/xrandr/xrandroutput.h @@ -1,106 +1,125 @@ /************************************************************************************* * Copyright 2012, 2013 Daniel Vrátil * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library 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 * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #pragma once #include "output.h" #include "xrandrmode.h" #include "../xcbwrapper.h" #include #include #include class XRandRConfig; class XRandRCrtc; namespace KScreen { class Config; class Output; } class XRandROutput : public QObject { Q_OBJECT public: typedef QMap Map; explicit XRandROutput(xcb_randr_output_t id, XRandRConfig *config); ~XRandROutput() override; void disabled(); void disconnected(); void update(); void update(xcb_randr_crtc_t crtc, xcb_randr_mode_t mode, xcb_randr_connection_t conn, bool primary); void setIsPrimary(bool primary); xcb_randr_output_t id() const; bool isEnabled() const; bool isConnected() const; bool isPrimary() const; QPoint position() const; QSize size() const; + QSizeF scaledSize(xcb_render_transform_t transform) const; QString currentModeId() const; XRandRMode::Map modes() const; XRandRMode* currentMode() const; KScreen::Output::Rotation rotation() const; bool isHorizontal() const; QByteArray edid() const; XRandRCrtc* crtc() const; KScreen::OutputPtr toKScreenOutput() const; + bool updateReplication(); + bool setReplicationSource(xcb_randr_output_t source); + + xcb_randr_output_t replicationSource() const; + + xcb_render_transform_t currentTransform() const; + private: void init(); void updateModes(const XCB::OutputInfo &outputInfo); static KScreen::Output::Type fetchOutputType(xcb_randr_output_t outputId, const QString &name); static QByteArray typeFromProperty(xcb_randr_output_t outputId); + xcb_render_transform_t getReplicationTransform(XRandROutput *source); + + /** + * This makes an educated guess based on position, size and scale if @param output is a + * replication source for this output. + * + * @return true if this output can be seen as a replica of @param output + */ + bool isReplicaOf(XRandROutput *output, xcb_render_transform_t ownTransform) const; + XRandRConfig *m_config; xcb_randr_output_t m_id; QString m_name; QString m_icon; mutable QByteArray m_edid; xcb_randr_connection_t m_connected; bool m_primary; KScreen::Output::Type m_type; XRandRMode::Map m_modes; QStringList m_preferredModes; QList m_clones; + xcb_randr_output_t m_replicationSource; unsigned int m_widthMm; unsigned int m_heightMm; bool m_hotplugModeUpdate = false; XRandRCrtc *m_crtc; }; Q_DECLARE_METATYPE(XRandROutput::Map)