shift-shell/envmanager/devicepresets.cpp
Devin Lin 839d5e2bff panels: Add support for defining device specific panel tweaks
This adds support for specifying options needed to deal with phone
display panel pecularities (ex. screen curves, notches, punch holes)

This is implemented as settings in ~/.config/plasmamobilerc, which can
set panel heights, paddings, and center spacings to duck display
cutouts. The pixel values are scaling independent, and so are not
affected when the display scaling is changed.

This is then exposed over DBus, so that components from outside of
plasmashell (ex. KWin) can access it easily without needing to connect to
kscreen themselves. Each screen is exposed as a single object.

Currently support is only added in the status bar and the navigation
panel.

Currently all screens have the settings applied. In the future, we may
want to limit this just to the internal screen (?)

---

This also adds a "devices" folder (in `devices/configs`) where per-device configs can be set.

This is installed to `/usr/share/plasma-mobile-device-configs`.
In `plasmamobilerc` (installed to `/etc/xdg/plasmamobilerc`, or
`~/.config/plasmamobilerc`), envmanager will read:

```toml
[Device]
device=oneplus-enchilada
```

for the device config to use and write its settings to
`~/.config/plasma-mobile/plasmamobilerc`.
2025-11-04 23:08:18 -05:00

88 lines
4 KiB
C++

// SPDX-FileCopyrightText: 2025 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "devicepresets.h"
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <utility>
using namespace Qt::Literals::StringLiterals;
const QString GENERAL_CONFIG_FILE = u"plasmamobilerc"_s;
const QString WRITE_TO_CONFIG_FILE = u"plasma-mobile/plasmamobilerc"_s;
const QString DEVICE_CONFIG_GROUP = u"Device"_s;
DevicePresets::DevicePresets(QObject *parent)
: QObject{parent}
, m_mobileConfig{KSharedConfig::openConfig(WRITE_TO_CONFIG_FILE, KConfig::SimpleConfig)}
{
}
void setKey(KConfigGroup &fallbackGroup, KConfigGroup &fromGroup, KConfigGroup &toGroup, QString fromKey, QString toKey)
{
if (fromGroup.hasKey(fromKey)) {
toGroup.writeEntry(toKey, fromGroup.readEntry(fromKey), KConfigGroup::Notify);
} else if (fallbackGroup.hasKey(fromKey)) {
toGroup.writeEntry(toKey, fallbackGroup.readEntry(fromKey), KConfigGroup::Notify);
} else {
toGroup.deleteEntry(toKey, KConfigGroup::Notify);
}
}
void DevicePresets::initialize()
{
// Open mobile config to read from all locations (/etc/xdg/plasmamobilerc, ~/.config/plasmamobilerc, etc.)
auto mobileConfig = KSharedConfig::openConfig(GENERAL_CONFIG_FILE);
auto deviceGroup = KConfigGroup{mobileConfig, DEVICE_CONFIG_GROUP};
// Read device id
const QString device = deviceGroup.readEntry(u"device"_s, {});
QString presetFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, u"plasma-mobile-device-presets/"_s + device + ".conf");
if (!QFile{presetFile}.exists()) {
presetFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, u"plasma-mobile-device-presets/default.conf"_s);
if (!QFile{presetFile}.exists()) {
qWarning() << "Failed to find any device preset file";
return;
}
}
// Open preset file /usr/share/plasma-mobile-device-presets/device.conf
auto presetConfig = KSharedConfig::openConfig(QFileInfo{presetFile}.absoluteFilePath(), KConfig::SimpleConfig);
if (!presetConfig) {
return;
}
// Write presets to ~/.config/plasma-mobile/plasmamobilerc
// This is then read by components/mobileshellstate (PanelSettingsDBusObjectManager)
auto presetPanelsGroup = KConfigGroup{presetConfig, u"Panels"_s};
auto mobilePanelsGroup = KConfigGroup{m_mobileConfig, u"Panels"_s};
// <Preset file group name, plasmamobilerc group name>
QList<std::pair<QString, QString>> groupPairs = {{u"Top"_s, u"WhenOnTop"_s},
{u"Bottom"_s, u"WhenOnBottom"_s},
{u"Left"_s, u"WhenOnLeft"_s},
{u"Right"_s, u"WhenOnRight"_s}};
// Convert preset file settings into plasmamobilerc ones
for (const auto &p : groupPairs) {
auto presetGroup = KConfigGroup{&presetPanelsGroup, p.first};
auto writeGroup = KConfigGroup{&mobilePanelsGroup, p.second};
// Try to read the value from presetGroup first (ex. [Panels][Top] statusBarHeight=...)
// If it doesn't exist, then fallback to parent group (ex. [Panels] statusBarHeight=...)
setKey(presetPanelsGroup, presetGroup, writeGroup, u"statusBarHeight"_s, u"statusBarHeight"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"navigationPanelHeight"_s, u"navigationPanelHeight"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"leftPadding"_s, u"statusBarLeftPadding"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"leftPadding"_s, u"navigationPanelLeftPadding"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"rightPadding"_s, u"statusBarRightPadding"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"rightPadding"_s, u"navigationPanelRightPadding"_s);
setKey(presetPanelsGroup, presetGroup, writeGroup, u"centerSpacing"_s, u"statusBarCenterSpacing"_s);
writeGroup.sync();
}
m_mobileConfig->sync();
}