1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org>
SPDX-FileCopyrightText: 2024 Jakob Petsovits <jpetso@petsovits.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "osd.h"
#include "debug.h"
#include "shellcorona.h"
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusReply>
#include <QDebug>
#include <QTimer>
#include <QWindow>
#include <KRuntimePlatform>
#include <PlasmaQuick/SharedQmlEngine>
#include <klocalizedstring.h>
#include <algorithm> // std::ranges::sort
using namespace Qt::StringLiterals;
Osd::Osd(const KSharedConfig::Ptr &config, ShellCorona *corona)
: QObject(corona)
, m_corona(corona)
, m_osdConfigGroup(config, u"OSD"_s)
{
QDBusConnection::sessionBus().registerObject(u"/org/kde/osdService"_s, this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
}
Osd::~Osd()
{
}
void Osd::screenBrightnessChanged(int percent, const QString &displayId, const QString &displayLabel, int priority, const QRect &screenRect)
{
// Ensure that an element in m_screenBrightnessInfo exists with unique displayId and sorting by priority
m_screenBrightnessInfo.insert(displayId,
{
.id = displayId,
.label = displayLabel,
.screenRect = screenRect,
.priority = priority,
.percent = percent,
});
// Don't show screen brightness OSD on mobile, only emit event (specified in showProgress parameters)
if (m_corona->numScreens() == 1 && m_screenBrightnessInfo.size() == 1 && screenRect == m_corona->screenGeometry(0)) {
showProgress(u"video-display-brightness"_s, percent, 100, {}, false);
} else if (m_screenBrightnessInfo.size() == 1) {
showProgress(u"video-display-brightness"_s, percent, 100, displayLabel, false);
} else {
// TODO: show one progress OSD on each corresponding screen
QList<ScreenBrightnessInfo> sortedByPriority = m_screenBrightnessInfo.values();
std::ranges::sort(sortedByPriority, [](const auto &a, const auto &b) {
return a.priority < b.priority;
});
QStringList percentages;
for (const auto &info : std::as_const(sortedByPriority)) {
percentages += i18nc("Brightness OSD: display name and brightness percentage", "%1: %2%", info.label, info.percent);
}
showText(u"video-display-brightness"_s, percentages.join(u"\n"_s));
}
}
void Osd::brightnessChanged(int percent)
{
// Only emit event, don't show OSD on Plasma Mobile
showProgress(u"video-display-brightness"_s, percent, 100, {}, false);
}
void Osd::keyboardBrightnessChanged(int percent)
{
showProgress(u"input-keyboard-brightness"_s, percent, 100);
}
void Osd::volumeChanged(int percent)
{
volumeChanged(percent, 100);
}
void Osd::volumeChanged(int percent, int maximumPercent)
{
QString icon;
if (percent <= 0) {
icon = u"audio-volume-muted"_s;
showText(icon, i18nc("OSD informing that the system is muted, keep short", "Audio Muted"));
return;
} else if (percent <= 25) {
icon = u"audio-volume-low"_s;
} else if (percent <= 75) {
icon = u"audio-volume-medium"_s;
} else if (percent <= 100) {
icon = u"audio-volume-high"_s;
} else if (percent <= 125) {
icon = u"audio-volume-high-warning"_s;
} else {
icon = u"audio-volume-high-danger"_s;
}
// Plasma Mobile supplies its own OSD, so just emit the signal but don't show it here.
showProgress(icon, percent, maximumPercent, {}, false);
}
void Osd::microphoneVolumeChanged(int percent)
{
QString icon;
if (percent <= 0) {
icon = u"microphone-sensitivity-muted"_s;
showText(icon, i18nc("OSD informing that the microphone is muted, keep short", "Microphone Muted"));
return;
} else if (percent <= 25) {
icon = u"microphone-sensitivity-low"_s;
} else if (percent <= 75) {
icon = u"microphone-sensitivity-medium"_s;
} else {
icon = u"microphone-sensitivity-high"_s;
}
showProgress(icon, percent, 100);
}
void Osd::mediaPlayerVolumeChanged(int percent, const QString &playerName, const QString &playerIconName)
{
if (percent == 0) {
showText(playerIconName, i18nc("OSD informing that some media app is muted, eg. Amarok Muted", "%1 Muted", playerName));
} else {
showProgress(playerIconName, percent, 100, playerName);
}
}
void Osd::kbdLayoutChanged(const QString &layoutName)
{
if (m_osdConfigGroup.readEntry("kbdLayoutChangedEnabled", true)) {
showText(u"keyboard-layout"_s, layoutName);
}
}
void Osd::virtualDesktopChanged(const QString ¤tVirtualDesktopName)
{
// FIXME: need a VD icon
showText(QString(), currentVirtualDesktopName);
}
void Osd::touchpadEnabledChanged(bool touchpadEnabled)
{
if (touchpadEnabled) {
showText(u"input-touchpad-on"_s, i18nc("touchpad was enabled, keep short", "Touchpad On"));
} else {
showText(u"input-touchpad-off"_s, i18nc("touchpad was disabled, keep short", "Touchpad Off"));
}
}
void Osd::wifiEnabledChanged(bool wifiEnabled)
{
if (wifiEnabled) {
showText(u"network-wireless-on"_s, i18nc("wireless lan was enabled, keep short", "Wifi On"));
} else {
showText(u"network-wireless-off"_s, i18nc("wireless lan was disabled, keep short", "Wifi Off"));
}
}
void Osd::bluetoothEnabledChanged(bool bluetoothEnabled)
{
if (bluetoothEnabled) {
showText(u"preferences-system-bluetooth"_s, i18nc("Bluetooth was enabled, keep short", "Bluetooth On"));
} else {
showText(u"preferences-system-bluetooth-inactive"_s, i18nc("Bluetooth was disabled, keep short", "Bluetooth Off"));
}
}
void Osd::wwanEnabledChanged(bool wwanEnabled)
{
if (wwanEnabled) {
showText(u"network-mobile-on"_s, i18nc("mobile internet was enabled, keep short", "Mobile Internet On"));
} else {
showText(u"network-mobile-off"_s, i18nc("mobile internet was disabled, keep short", "Mobile Internet Off"));
}
}
void Osd::virtualKeyboardEnabledChanged(bool virtualKeyboardEnabled)
{
if (virtualKeyboardEnabled) {
showText(u"input-keyboard-virtual-on"_s,
i18nc("on screen keyboard was enabled because physical keyboard got unplugged, keep short", "On-Screen Keyboard Activated"));
} else {
showText(u"input-keyboard-virtual-off"_s,
i18nc("on screen keyboard was disabled because physical keyboard was plugged in, keep short", "On-Screen Keyboard Deactivated"));
}
}
void Osd::powerManagementInhibitedChanged(bool inhibited)
{
if (inhibited) {
showText(QStringLiteral("system-suspend-inhibited"), i18nc("power management was inhibited, keep short", "Sleep and Screen Locking Blocked"));
} else {
showText(QStringLiteral("system-suspend-uninhibited"), i18nc("power management was uninhibited, keep short", "Sleep and Screen Locking Unblocked"));
}
}
void Osd::powerProfileChanged(const QString &profile)
{
QString icon;
QString name;
if (profile == u"power-saver") {
icon = QStringLiteral("battery-profile-powersave");
name = i18nc("Power profile was changed to power save mode, keep short", "Power Save Mode");
} else if (profile == u"balanced") {
icon = QStringLiteral("speedometer");
name = i18nc("Power profile was changed to balanced mode, keep short", "Balanced Power Mode");
} else if (profile == u"performance") {
icon = QStringLiteral("battery-profile-performance");
name = i18nc("Power profile was changed to performance mode, keep short", "Performance Mode");
}
showText(icon, name);
}
bool Osd::init()
{
if (!m_osdConfigGroup.readEntry("Enabled", true)) {
return false;
}
if (m_osdObject && m_osdObject->rootObject()) {
return true;
}
if (!m_osdObject) {
m_osdObject = new PlasmaQuick::SharedQmlEngine(this);
}
m_osdObject->setSourceFromModule("org.kde.plasma.workspace.osd", "Osd");
if (m_osdObject->status() != QQmlComponent::Ready) {
qCWarning(PLASMASHELL) << "Failed to load OSD QML file";
return false;
}
m_timeout = m_osdObject->rootObject()->property("timeout").toInt();
if (!m_osdTimer) {
m_osdTimer = new QTimer(this);
m_osdTimer->setSingleShot(true);
connect(m_osdTimer, &QTimer::timeout, this, &Osd::hideOsd);
}
return true;
}
void Osd::showProgress(const QString &icon, const int percent, const int maximumPercent, const QString &additionalText, bool showOnMobile)
{
if (!init()) {
return;
}
int value = qBound(0, percent, maximumPercent);
if (!showOnMobile && KRuntimePlatform::runtimePlatform().contains(u"phone"_s)) {
Q_EMIT osdProgress(icon, value, maximumPercent, additionalText);
return;
}
// Update max value first to prevent value from being clamped
auto *rootObject = m_osdObject->rootObject();
rootObject->setProperty("osdMaxValue", maximumPercent);
rootObject->setProperty("osdValue", value);
rootObject->setProperty("osdAdditionalText", additionalText);
rootObject->setProperty("showingProgress", true);
rootObject->setProperty("icon", icon);
Q_EMIT osdProgress(icon, value, maximumPercent, additionalText);
showOsd();
}
void Osd::showText(const QString &icon, const QString &text)
{
if (!init()) {
return;
}
auto *rootObject = m_osdObject->rootObject();
rootObject->setProperty("showingProgress", false);
rootObject->setProperty("osdValue", text);
rootObject->setProperty("icon", icon);
Q_EMIT osdText(icon, text);
showOsd();
}
void Osd::showOsd()
{
m_osdTimer->stop();
auto *rootObject = m_osdObject->rootObject();
// if our OSD understands animating the opacity, do it;
// otherwise just show it to not break existing lnf packages
if (rootObject->property("animateOpacity").isValid()) {
rootObject->setProperty("animateOpacity", false);
rootObject->setProperty("opacity", 1);
rootObject->setProperty("visible", true);
rootObject->setProperty("animateOpacity", true);
rootObject->setProperty("opacity", 0);
} else {
rootObject->setProperty("visible", true);
}
m_osdTimer->start(m_timeout);
}
void Osd::hideOsd()
{
auto *rootObject = m_osdObject->rootObject();
if (!rootObject) {
return;
}
rootObject->setProperty("visible", false);
// this is needed to prevent fading from "old" values when the OSD shows up
rootObject->setProperty("osdValue", 0);
m_screenBrightnessInfo.clear();
}
#include "moc_osd.cpp"
|