Commit 8fbe7ceb authored by David Redondo's avatar David Redondo 🏎
Browse files

Also look up display configuration in readable locations

This can be useful for hardware vendors to set a default
configuration. One example could be setting the scale on a
phone to 2 or rotating an output that is installed rotated.
Settings stored in the local directory still take precedence.
parent 67962a1c
/********************************************************************
Copyright 2018 Roman Gilg <subdiff@gmail.com>
Copyright 2021 David Redondo <kde@david-redondo.de>
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
......@@ -26,4 +27,8 @@ QString dirPath()
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) % QStringLiteral("/kscreen/");
}
QString findFile(const QString &filePath)
{
return QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kscreen/") % filePath);
}
}
/********************************************************************
Copyright 2018 Roman Gilg <subdiff@gmail.com>
Copyright 2021 David Redondo <kde@david-redondo.de>
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
......@@ -22,6 +23,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace Globals
{
QString dirPath();
/**
* Tries to find the specified file realtive to dirPath(). Also considers presets if there is no
* existing file under dirPath() yet.
* @returns The abosolute path to a matching file if on exists or an empty string
*/
QString findFile(const QString &filePath);
}
#endif
......@@ -39,6 +39,7 @@ public:
std::unique_ptr<Config> readOpenLidFile();
bool writeFile();
bool writeOpenLidFile();
static QString configsDirPath();
KScreen::ConfigPtr data() const
{
......@@ -78,7 +79,6 @@ private:
static QString s_configsDirName;
static QString s_fixedConfigFileName;
static QString configsDirPath();
};
#endif
......@@ -113,11 +113,17 @@ void Output::readInGlobalPartFromInfo(KScreen::OutputPtr output, const QVariantM
QVariantMap Output::getGlobalData(KScreen::OutputPtr output)
{
QFile file(globalFileName(output->hashMd5()));
QString fileName = Globals::findFile(s_dirName % output->hashMd5());
if (fileName.isEmpty()) {
qCDebug(KSCREEN_KDED) << "No file for" << s_dirName % output->hashMd5();
return QVariantMap();
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
qCDebug(KSCREEN_KDED) << "Failed to open file" << file.fileName();
return QVariantMap();
}
qCDebug(KSCREEN_KDED) << "Found global data at" << file.fileName();
QJsonDocument parser;
return parser.fromJson(file.readAll()).toVariant().toMap();
}
......
......@@ -55,6 +55,7 @@ private Q_SLOTS:
void workstationTwoExternalDiferentSize();
void switchDisplayTwoScreens();
void globalOutputData();
void outputPreset();
};
KScreen::ConfigPtr testScreenConfig::loadConfig(const QByteArray &fileName)
......@@ -503,6 +504,45 @@ void testScreenConfig::globalOutputData()
QFile::remove(::Output::dirPath() + output->hashMd5());
}
void testScreenConfig::outputPreset()
{
const ConfigPtr currentConfig = loadConfig("singleOutput.json");
currentConfig->setSupportedFeatures(currentConfig->supportedFeatures() | KScreen::Config::Feature::PerOutputScaling);
auto defaultOutput = Generator::self()->idealConfig(currentConfig)->connectedOutputs().first();
QCOMPARE(defaultOutput->currentModeId(), QLatin1String("3"));
QCOMPARE(defaultOutput->rotation(), KScreen::Output::None);
QCOMPARE(defaultOutput->scale(), 1.0);
// Create the preset
QTemporaryDir dataDir;
qputenv("XDG_DATA_DIRS", dataDir.path().toUtf8());
QStandardPaths::standardLocations(QStandardPaths::DataLocation);
auto presetOutput = defaultOutput->clone();
presetOutput->setCurrentModeId(QStringLiteral("2"));
presetOutput->setRotation(KScreen::Output::Left);
presetOutput->setScale(2.0);
::Output::writeGlobal(presetOutput);
QDir(dataDir.path()).mkpath(QStringLiteral("kscreen/outputs"));
QFile::copy(::Output::dirPath() + presetOutput->hashMd5(), dataDir.filePath(QStringLiteral("kscreen/outputs/") % presetOutput->hashMd5()));
QFile::remove(::Output::dirPath() + presetOutput->hashMd5());
auto config = Generator::self()->idealConfig(currentConfig);
auto output = config->connectedOutputs().first();
QCOMPARE(output->currentModeId(), QLatin1String("2"));
QCOMPARE(output->rotation(), KScreen::Output::Left);
QCOMPARE(output->scale(), 2.0);
// But local global settings should still overwrite
::Output::writeGlobal(defaultOutput);
config = Generator::self()->idealConfig(currentConfig);
output = config->connectedOutputs().first();
QCOMPARE(output->currentModeId(), QLatin1String("3"));
QCOMPARE(output->rotation(), KScreen::Output::None);
QCOMPARE(output->scale(), 1.0);
QFile::remove(::Output::dirPath() + defaultOutput->hashMd5());
}
QTEST_MAIN(testScreenConfig)
#include "testgenerator.moc"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment