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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org>
SPDX-FileCopyrightText: 2010 Alejandro Fiestas <alex@eyeos.org>
SPDX-FileCopyrightText: 2010-2013 Lukáš Tinkl <ltinkl@redhat.com>
SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
SPDX-FileCopyrightText: 2024 Jakob Petsovits <jpetso@petsovits.com>
SPDX-FileCopyrightText: 2024 Xaver Hugl <xaver.hugl@gmail.com>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include "screenbrightnesscontroller.h"
#include "backlightbrightness.h"
#include "ddcutildetector.h"
#include "displaybrightness.h"
#include "externalbrightnesscontrol.h"
#include "kwinbrightness.h"
#include <brightnessosdwidget.h>
#include <powerdevil_debug.h>
#include <KScreen/ConfigMonitor>
#include <KScreen/EDID>
#include <KScreen/GetConfigOperation>
#include <KScreen/Output>
#include <QDebug>
#include <QPropertyAnimation>
#include <algorithm>
#include <ranges>
using namespace Qt::Literals::StringLiterals;
ScreenBrightnessController::ScreenBrightnessController()
: QObject()
, m_detectors({
{
.detector = new KWinDisplayDetector(this),
.debugName = "kwin brightness control",
.displayIdPrefix = "kwin:",
},
{
.detector = new BacklightDetector(this),
.debugName = "internal display backlight",
.displayIdPrefix = "backlight:",
},
{
.detector = new DDCutilDetector(this),
.debugName = "libddcutil",
.displayIdPrefix = "ddc:",
},
})
, m_externalBrightnessController(std::make_unique<ExternalBrightnessController>())
{
connect(m_externalBrightnessController.get(), &ExternalBrightnessController::activeChanged, this, &ScreenBrightnessController::onDetectorDisplaysChanged);
const auto op = new KScreen::GetConfigOperation(KScreen::GetConfigOperation::NoOptions, this);
connect(op, &KScreen::GetConfigOperation::finished, this, [this](KScreen::ConfigOperation *configOp) {
if (configOp->hasError()) {
return;
}
m_kscreenConfig = static_cast<KScreen::GetConfigOperation *>(configOp)->config();
KScreen::ConfigMonitor::instance()->addConfig(m_kscreenConfig);
});
}
ScreenBrightnessController::~ScreenBrightnessController()
{
}
void ScreenBrightnessController::detectDisplays()
{
qCDebug(POWERDEVIL) << "Trying to detect displays for brightness control...";
m_finishedDetectingCount = 0;
for (const DetectorInfo &detectorInfo : m_detectors) {
DisplayBrightnessDetector *detector = detectorInfo.detector;
disconnect(detector, nullptr, this, nullptr);
connect(detector, &DisplayBrightnessDetector::detectionFinished, this, [this, detector]() {
disconnect(detector, &DisplayBrightnessDetector::detectionFinished, this, nullptr);
if (++m_finishedDetectingCount; m_finishedDetectingCount == m_detectors.size()) {
onDetectorDisplaysChanged();
Q_EMIT detectionFinished();
}
connect(detector, &DisplayBrightnessDetector::displaysChanged, this, &ScreenBrightnessController::onDetectorDisplaysChanged);
});
detector->detect();
}
}
bool ScreenBrightnessController::isSupported() const
{
return !m_sortedDisplayIds.isEmpty();
}
QStringList ScreenBrightnessController::displayIds() const
{
return m_sortedDisplayIds;
}
QStringList ScreenBrightnessController::displayIds(const DisplayFilter &filter) const
{
auto appendIfIncluded = [this, &filter](QStringList &&filtered, const QString &displayId) {
const DisplayMatch &match = m_displaysById.find(displayId)->second.match;
if (filter.includes(match)) {
filtered.append(displayId);
}
return filtered;
};
return std::accumulate(m_sortedDisplayIds.constBegin(), m_sortedDisplayIds.constEnd(), QStringList{}, appendIfIncluded);
}
void ScreenBrightnessController::onDisplayDestroyed(QObject *obj)
{
for (auto &[id, info] : m_displaysById) {
if (info.display == obj) {
// we'll do the proper removal in onDetectorDisplaysChanged() which should come
// right afterwards, just don't call it anymore including through QObject::disconnect()
info.zombie = true;
}
}
}
void ScreenBrightnessController::onDetectorDisplaysChanged()
{
m_sortedDisplayIds.clear();
QStringList legacyDisplayIds;
std::unordered_map<QString, DisplayInfo> newDisplayById;
QList<DisplayBrightness *> newForExternalControl;
// for backwards compatibility with legacy API clients, set the same brightness to all displays
// of the first detector - e.g. to only the backlight display, or to all external DDC monitors
DisplayBrightnessDetector *firstSupportedDetector = nullptr;
// add new displays
for (const DetectorInfo &detectorInfo : m_detectors) {
const bool shouldUseExternalControl = m_externalBrightnessController->isActive() && !dynamic_cast<KWinDisplayDetector *>(detectorInfo.detector);
QList<DisplayBrightness *> detectorDisplays = detectorInfo.detector->displays();
if (!detectorDisplays.isEmpty() && !shouldUseExternalControl) {
if (firstSupportedDetector == nullptr) {
firstSupportedDetector = detectorInfo.detector;
}
qCDebug(POWERDEVIL) << "Using" << detectorInfo.debugName << "for brightness controls.";
}
for (DisplayBrightness *display : std::as_const(detectorDisplays)) {
const QString displayId = QString::fromLocal8Bit(detectorInfo.displayIdPrefix) + display->id();
if (shouldUseExternalControl) {
newForExternalControl.push_back(display);
} else {
auto &info = newDisplayById[displayId];
info = DisplayInfo{
.display = display,
.detector = detectorInfo.detector,
.match =
DisplayMatch{
.maxBrightness = display->maxBrightness(),
.isInternal = display->isInternal(),
.edidData = display->edidData(),
},
};
info.brightnessLogic.setValueRange(display->knownSafeMinBrightness(), display->maxBrightness());
info.brightnessLogic.setValue(display->brightness());
m_sortedDisplayIds.push_back(displayId);
if (detectorInfo.detector == firstSupportedDetector) {
legacyDisplayIds.append(displayId);
}
}
}
}
QStringList removedDisplayIds;
for (const auto &[displayId, info] : m_displaysById) {
const auto it = newDisplayById.find(displayId);
const bool removed = it == newDisplayById.end();
const bool replaced = !removed && it->second.display != info.display;
if ((removed || replaced) && !info.zombie) {
disconnect(info.display, nullptr, this, nullptr);
}
if (removed) {
removedDisplayIds.push_back(displayId);
}
}
QStringList addedDisplayIds;
QStringList brightnessChangedDisplayIds;
auto newRememberedDisplayState = m_rememberedDisplayState;
for (auto &[displayId, info] : newDisplayById) {
const auto it = m_displaysById.find(displayId);
const auto rememberedIt = m_rememberedDisplayState.find(info.match);
const bool added = it == m_displaysById.end();
const bool readded = added && rememberedIt != m_rememberedDisplayState.end();
const bool replaced = !added && info.display != it->second.display;
const bool valueChanged = replaced
&& (it->second.brightnessLogic.info().value != info.display->brightness()
|| it->second.brightnessLogic.info().valueMax != info.display->maxBrightness());
if (added || replaced) {
connect(info.display, &QObject::destroyed, this, &ScreenBrightnessController::onDisplayDestroyed);
connect(info.display, &DisplayBrightness::externalBrightnessChangeObserved, this, &ScreenBrightnessController::onExternalBrightnessChangeObserved);
} else {
// migrate any local state from the old element to its replacement
info.trackingError = it->second.trackingError;
}
if (valueChanged) {
brightnessChangedDisplayIds.push_back(displayId);
}
if (added) {
addedDisplayIds.push_back(displayId);
}
if (readded) {
const RememberedDisplayState &rds = rememberedIt->second;
info.brightnessLogic.setValueRange(rds.minBrightness, info.display->maxBrightness());
if (info.display->supportsDimmingMultiplier()) {
info.dimmingRatio = dimmingRatioForDisplay(displayId);
info.display->setDimmingMultiplier(info.dimmingRatio);
} else if (rds.latestActiveDimmingRatio.has_value()
&& info.display->brightness() == brightnessMultiplied(rds.brightness, *rds.latestActiveDimmingRatio, rds.minBrightness)) {
qCDebug(POWERDEVIL) << "Re-recognized display" << displayId << "at brightness" << rds.brightness << "* dimming ratio"
<< *rds.latestActiveDimmingRatio;
info.brightnessLogic.setValue(rds.brightness);
info.dimmingRatio = dimmingRatioForDisplay(displayId);
if (info.dimmingRatio != *rds.latestActiveDimmingRatio) {
qCDebug(POWERDEVIL) << "=> reset brightness for" << displayId << "to current dimming ratio" << info.dimmingRatio;
const PowerDevil::BrightnessLogic::BrightnessInfo bi = info.brightnessLogic.info();
const int dimmedValue = brightnessMultiplied(bi.value, info.dimmingRatio, bi.valueMin);
info.display->setBrightness(dimmedValue);
}
}
// delay removing the display state from m_rememberedDisplayState, in case several
// simultaneously added displays both use the same EDID (some EDIDs lack serial numbers)
newRememberedDisplayState.erase(info.match);
}
}
m_displaysById = std::move(newDisplayById);
m_rememberedDisplayState = std::move(newRememberedDisplayState);
m_externalBrightnessController->setDisplays(newForExternalControl);
for (const QString &removed : removedDisplayIds) {
Q_EMIT displayRemoved(removed);
}
for (const QString &added : addedDisplayIds) {
Q_EMIT displayAdded(added);
}
if (!addedDisplayIds.isEmpty() || !removedDisplayIds.isEmpty()) {
Q_EMIT displayIdsChanged(m_sortedDisplayIds);
}
for (const QString &changed : brightnessChangedDisplayIds) {
const auto &[id, info] = *m_displaysById.find(changed);
qCDebug(POWERDEVIL) << "Screen brightness of display" << changed << "after detection/reconfiguration:" << info.brightnessLogic.info().value;
Q_EMIT brightnessChanged(changed, info.brightnessLogic.info(), QString(), QString(), SuppressIndicator);
}
const QString previousFirstDisplayId = m_legacyDisplayIds.value(0, QString());
if (m_legacyDisplayIds != legacyDisplayIds) {
m_legacyDisplayIds = legacyDisplayIds;
Q_EMIT legacyDisplayIdsChanged(m_legacyDisplayIds);
}
if (!isSupported()) {
qCDebug(POWERDEVIL) << "No suitable displays detected. Brightness controls are unsupported in this configuration.";
return;
}
// legacy API needs to emit a brightness change signal for the first display, regardless of
// whether the new first display was newly added or an existing display moved up to index 0
const QString &newFirstDisplayId = m_legacyDisplayIds.first();
if (newFirstDisplayId != previousFirstDisplayId || brightnessChangedDisplayIds.contains(newFirstDisplayId)) {
const auto &[id, info] = *m_displaysById.find(newFirstDisplayId);
qCDebug(POWERDEVIL) << "Screen brightness of first display after detection/reconfiguration:" << info.brightnessLogic.info().value;
Q_EMIT legacyBrightnessInfoChanged(info.brightnessLogic.info(), SuppressIndicator);
}
}
QString ScreenBrightnessController::label(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
QString result = it->second.display->label();
qCDebug(POWERDEVIL) << "Screen label of" << displayId << "is" << result;
return result;
}
qCWarning(POWERDEVIL) << "Query screen label failed: no display with id" << displayId;
return QString();
}
bool ScreenBrightnessController::isInternal(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
bool result = it->second.display->isInternal();
qCDebug(POWERDEVIL) << "Screen " << displayId << (result ? "is internal" : "is not internal");
return result;
}
qCWarning(POWERDEVIL) << "Query screen is internal failed: no display with id" << displayId;
return false;
}
int ScreenBrightnessController::knownSafeMinBrightness(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
int result = it->second.display->knownSafeMinBrightness();
qCDebug(POWERDEVIL) << "Screen knownSafeMinBrightness of" << displayId << "is" << result;
return result;
}
qCWarning(POWERDEVIL) << "Query screen knownSafeMinBrightness failed: no display with id" << displayId;
return 0;
}
int ScreenBrightnessController::minBrightness(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end()) {
int result = it->second.brightnessLogic.info().valueMin;
qCDebug(POWERDEVIL) << "Screen minBrightness of" << displayId << "is" << result;
return result;
}
qCWarning(POWERDEVIL) << "Query screen minBrightness failed: no display with id" << displayId;
return 0;
}
int ScreenBrightnessController::maxBrightness(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end()) {
int result = it->second.brightnessLogic.info().valueMax;
qCDebug(POWERDEVIL) << "Screen maxBrightness of" << displayId << "is" << result;
return result;
}
qCWarning(POWERDEVIL) << "Query screen maxBrightness failed: no display with id" << displayId;
return 0;
}
int ScreenBrightnessController::brightness(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end()) {
int result = it->second.brightnessLogic.info().value;
qCDebug(POWERDEVIL) << "Screen brightness of" << displayId << "is" << result;
return result;
}
qCWarning(POWERDEVIL) << "Query screen brightness failed: no display with id" << displayId;
return 0;
}
int ScreenBrightnessController::brightnessMultiplied(int value, double multiplier, int min) const
{
return multiplier == 1.0 ? value : std::max<int>(min, value * multiplier);
}
void ScreenBrightnessController::setBrightness(const QString &displayId,
int value,
const QString &sourceClientName,
const QString &sourceClientContext,
IndicatorHint hint)
{
if (auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
auto &[id, info] = *it;
const PowerDevil::BrightnessLogic::BrightnessInfo bi = info.brightnessLogic.info();
const int boundedValue = std::clamp(value, bi.valueMin, bi.valueMax);
const int dimmedValue = info.display->supportsDimmingMultiplier() ? boundedValue : brightnessMultiplied(boundedValue, info.dimmingRatio, bi.valueMin);
qCDebug(POWERDEVIL) << "Set screen brightness of" << displayId << "to" << dimmedValue << "/" << bi.valueMax;
if (value != boundedValue) {
qCDebug(POWERDEVIL) << "- clamped from" << value;
}
if (boundedValue != dimmedValue) {
qCDebug(POWERDEVIL) << "- dimmed as" << boundedValue << "*" << info.dimmingRatio;
}
// notify only when the internally tracked brightness value is actually different
if (bi.value != boundedValue) {
info.brightnessLogic.setValue(boundedValue);
info.trackingError = 0.0;
Q_EMIT brightnessChanged(displayId, info.brightnessLogic.info(), sourceClientName, sourceClientContext, hint);
// legacy API without displayId parameter: notify only if the first supported display changed
if (!m_legacyDisplayIds.isEmpty() && displayId == m_legacyDisplayIds.first()) {
Q_EMIT legacyBrightnessInfoChanged(info.brightnessLogic.info(), hint);
}
}
// but always call setBrightness() on the display, both for changes to the
// brightness multiplier and in case we're unaware of an external change
info.display->setBrightness(dimmedValue);
} else {
qCWarning(POWERDEVIL) << "Set screen brightness failed: no display with id" << displayId;
}
}
void ScreenBrightnessController::adjustBrightnessRatio(const QString &displayId,
double delta,
const QString &sourceClientName,
const QString &sourceClientContext,
IndicatorHint hint)
{
if (auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
auto &[id, info] = *it;
double oldRatio = info.brightnessLogic.valueAsRatio();
double targetRatio = oldRatio + delta + info.trackingError;
setBrightness(displayId, info.brightnessLogic.valueFromRatio(targetRatio), sourceClientName, sourceClientContext, hint);
info.trackingError = targetRatio - info.brightnessLogic.valueAsRatio();
} else {
qCWarning(POWERDEVIL) << "Adjust screen brightness ratio failed: no display with id" << displayId;
}
}
void ScreenBrightnessController::adjustBrightnessRatio(double delta, const QString &sourceClientName, const QString &sourceClientContext, IndicatorHint hint)
{
if (m_sortedDisplayIds.isEmpty()) {
qCWarning(POWERDEVIL) << "Adjust screen brightness ratio failed: no displays available to adjust";
return;
}
// if we're going to adjust brightness and accumulate tracking errors, let's make sure at least
// one display will actually change its brightness as a result
bool any = std::ranges::any_of(std::as_const(m_sortedDisplayIds), [this, delta](const QString &displayId) {
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
// return true if the display still has room to go in the direction of the delta
const PowerDevil::BrightnessLogic::BrightnessInfo bi = it->second.brightnessLogic.info();
return (bi.value < bi.valueMax && delta > 0.0) || (bi.value > bi.valueMin && delta < 0.0);
}
return false;
});
if (!any) {
return;
}
for (const QString &displayId : std::as_const(m_sortedDisplayIds)) {
adjustBrightnessRatio(displayId, delta, sourceClientName, sourceClientContext, hint);
}
}
void ScreenBrightnessController::adjustBrightnessStep(PowerDevil::BrightnessLogic::StepAdjustmentAction adjustment,
const QString &sourceClientName,
const QString &sourceClientContext,
IndicatorHint hint)
{
if (m_sortedDisplayIds.isEmpty()) {
qCWarning(POWERDEVIL) << "Adjust screen brightness step failed: no displays available to adjust";
return;
}
double referenceDisplayDelta = 0.0;
for (const QString &displayId : std::as_const(m_sortedDisplayIds)) {
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie) {
const auto &[id, info] = *it;
double oldRatio = info.brightnessLogic.valueAsRatio();
int referenceDisplayBrightness = info.brightnessLogic.adjusted(adjustment);
if (referenceDisplayBrightness < 0) {
return;
}
if (referenceDisplayBrightness != info.brightnessLogic.info().value) {
referenceDisplayDelta = info.brightnessLogic.ratio(referenceDisplayBrightness) - oldRatio;
break;
}
}
}
if (referenceDisplayDelta == 0.0) {
return;
}
for (const QString &displayId : std::as_const(m_sortedDisplayIds)) {
adjustBrightnessRatio(displayId, referenceDisplayDelta, sourceClientName, sourceClientContext, hint);
}
}
int ScreenBrightnessController::brightnessSteps(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end()) {
return it->second.brightnessLogic.steps();
}
qCWarning(POWERDEVIL) << "Query screen brightnessSteps failed: no display with id" << displayId;
return 1;
}
void ScreenBrightnessController::setDimmingRatio(const QString &dimmingId, double ratio)
{
const double boundedRatio = std::clamp(ratio, 0.0, 1.0);
m_dimmingLimits[dimmingId] = DimmingLimit{
.ratio = std::clamp(ratio, 0.0, 1.0),
};
qCDebug(POWERDEVIL) << "Set screen brightness dimming ratio with ID" << dimmingId << "to" << boundedRatio;
for (auto &[id, info] : m_displaysById) {
const double newRatio = dimmingRatioForDisplay(id);
if (info.display->supportsDimmingMultiplier()) {
info.display->setDimmingMultiplier(newRatio);
} else {
if (info.dimmingRatio == newRatio) {
continue;
}
info.dimmingRatio = newRatio;
// set brightness to the currently stored value, but with the new multiplier
setBrightness(id, info.brightnessLogic.info().value, u"(internal)"_s, u"dimming"_s);
if (newRatio != 1.0 && info.match.isValid()) {
m_rememberedDisplayState[info.match] = RememberedDisplayState{
.brightness = info.brightnessLogic.info().value,
.minBrightness = info.brightnessLogic.info().valueMin,
.latestActiveDimmingRatio = info.dimmingRatio,
};
}
}
}
}
double ScreenBrightnessController::dimmingRatioForDisplay(const QString &displayId)
{
Q_UNUSED(displayId); // we may add display filtering options in the future
return std::accumulate(m_dimmingLimits.begin(), m_dimmingLimits.end(), 1.0, [](double current, const auto &kv) {
return std::min(current, kv.second.ratio);
});
}
KScreen::OutputPtr ScreenBrightnessController::tryMatchKScreenOutput(const QString &displayId) const
{
if (const auto it = m_displaysById.find(displayId); it != m_displaysById.end() && !it->second.zombie && m_kscreenConfig) {
const DisplayMatch &displayMatch = it->second.match;
for (const KScreen::OutputPtr &output : m_kscreenConfig->outputs()) {
bool matched = DisplayFilter()
.includeByDefault(false)
.isInternalEquals(output->type() == KScreen::Output::Panel)
.includeEdids(output->edid() ? QList<QByteArray>{output->edid()->rawData()} : QList<QByteArray>{})
.includes(displayMatch);
if (matched) {
return output;
}
}
} else if (!m_kscreenConfig) {
qCWarning(POWERDEVIL) << "Match KScreen::Output failed: config not initialized";
return KScreen::OutputPtr{};
} else {
qCWarning(POWERDEVIL) << "Match KScreen::Output failed: no display with id" << displayId;
return KScreen::OutputPtr{};
}
// no match found (not an error)
return KScreen::OutputPtr{};
}
void ScreenBrightnessController::onExternalBrightnessChangeObserved(DisplayBrightness *display, int value)
{
const auto it = std::ranges::find_if(m_displaysById, [display](const std::pair<QString, DisplayInfo> &pair) {
return pair.second.display == display;
});
if (it == m_displaysById.end()) {
qCWarning(POWERDEVIL) << "External brightness change of untracked display" << display->id() << "to" << value << "/" << display->maxBrightness();
return;
}
auto &[displayId, info] = *it;
const PowerDevil::BrightnessLogic::BrightnessInfo bi = info.brightnessLogic.info();
if (value == brightnessMultiplied(bi.value, info.dimmingRatio, bi.valueMin)) {
qCDebug(POWERDEVIL) << "External brightness change of display" << displayId << "ignored - same as previous value";
return;
}
qCDebug(POWERDEVIL) << "External brightness change of display" << displayId << "to" << value << "/" << bi.valueMax;
info.brightnessLogic.setValue(value);
info.dimmingRatio = 1.0;
info.trackingError = 0.0;
Q_EMIT brightnessChanged(displayId, info.brightnessLogic.info(), QString(), QString(), SuppressIndicator);
// legacy API without displayId parameter: notify only if the first supported display changed
if (displayId == m_legacyDisplayIds.first()) {
Q_EMIT legacyBrightnessInfoChanged(info.brightnessLogic.info(), SuppressIndicator);
}
}
//
// legacy API without displayId parameter
QStringList ScreenBrightnessController::legacyDisplayIds() const
{
return m_legacyDisplayIds;
}
int ScreenBrightnessController::knownSafeMinBrightness() const
{
return knownSafeMinBrightness(m_legacyDisplayIds.value(0, QString()));
}
int ScreenBrightnessController::minBrightness() const
{
return minBrightness(m_legacyDisplayIds.value(0, QString()));
}
int ScreenBrightnessController::maxBrightness() const
{
return maxBrightness(m_legacyDisplayIds.value(0, QString()));
}
int ScreenBrightnessController::brightness() const
{
return brightness(m_legacyDisplayIds.value(0, QString()));
}
void ScreenBrightnessController::setBrightness(int value, IndicatorHint hint)
{
if (m_legacyDisplayIds.isEmpty()) {
qCWarning(POWERDEVIL) << "Set screen brightness failed: no supported display available";
}
for (const QString &displayId : std::as_const(m_legacyDisplayIds)) {
setBrightness(displayId, value, QString(), QString(), hint);
}
}
int ScreenBrightnessController::brightnessSteps() const
{
return brightnessSteps(m_legacyDisplayIds.value(0, QString()));
}
#include "moc_screenbrightnesscontroller.cpp"
|