Extracted from MR !466 with minor tweaks in logic and commit message. Makes it possible for us to apply a dimming animation without getting the laptop screen's baseline brightness reset through ScreenBrightnessController::onExternalBrightnessChangeObserved()
. CC @zamundaaa, @nclarius (thanks in advance for any reviews)
Commit message
In BacklightBrightness::onDeviceChanged()
,the externalBrightnessChangeObserved
signal is emitted afterfiltering out udev events while the brightness animationis running. Unfortunately, these signals are received with ashort delay compared to when the timer was started, possiblybecause the KAuth helper runs on a different timeline anddefinitely because udev device change notifications are notsynchronous calls.
This commit introduces a more robust way of filtering out changeevents that BacklightBrightness
itself was asking for.
In essence, we will now filter out events where the brightnessmoves toward the current animation target brightness(dubbed "executed brightness" as it's not always animated).
There is a rub, in that the brightness helper can still continueits animation e.g. toward higher values until it receives themessage to turn around toward the lower value that the user hassuddenly decided to request instead. In order to ignore theseovershooting outliers, we also introduce an "expected min/max"pair of brightness values that gets initialized with the previousbrightness target.
This isn't perfect, as change events from further back thanthe previous KAuth invocation could still emit an unexpectedexternalBrightnessChangeObserved
signal. This only happenswhen you slide back and forth on the brightness slider really fast,though, and is unlikely to be encountered in normal use.
In addition to reducing unwanted change signals, this new methodalso allows BacklightBrightness to exactly track udev brightnessvalues and will correctly detect external changes even duringa brightness animation.
Testing
Restart powerdevil service with the patch applied, ideally under X11 where PowerDevil uses KAuth helper animations rather than getting each individual value from KWin. Enable verbose logging, then monitor powerdevil logs with:
journalctl --user-unit plasma-powerdevil.service -f
Then drag the brightness applet slider for "Built-in screen" and make sure you don't see this message:
[BacklightBrightness]: External brightness change observed: /
If you go back and forth like a zig-zagging hyperspeed maniac, you may still see a handful of these. For more in-depth debugging, I've uploaded my extra debug logging commit from a separately uploaded branch, for anyone to inspect the deeper workings in case it's necessary - mostly for personal reference, though.