1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-06 05:25:41 -05:00

brightness: add udev monitor, bind OSDs to netlink events

fixes #863
This commit is contained in:
bbedward
2025-12-01 11:54:20 -05:00
parent 94851a51aa
commit e5d11ce535
11 changed files with 553 additions and 42 deletions

View File

@@ -856,6 +856,13 @@ Singleton {
return brightnessUserSetValues[deviceName];
}
function clearBrightnessUserSetValue(deviceName) {
var newValues = Object.assign({}, brightnessUserSetValues);
delete newValues[deviceName];
brightnessUserSetValues = newValues;
saveSettings();
}
function setBrightnessExponent(deviceName, exponent) {
var newValues = Object.assign({}, brightnessExponentValues);
if (exponent !== undefined && exponent !== null) {

View File

@@ -159,7 +159,6 @@ Row {
}
return targetDevice.displayMax || 100;
}
value: !isDragging ? targetBrightness : value
showValue: true
unit: {
if (!targetDevice)
@@ -177,5 +176,10 @@ Row {
}
thumbOutlineColor: Theme.surfaceContainer
trackColor: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
Binding on value {
value: root.targetBrightness
when: !brightnessSlider.isDragging
}
}
}

View File

@@ -134,11 +134,12 @@ BasePill {
function handleBrightnessWheel(delta) {
const deviceName = getPinnedBrightnessDevice();
if (!deviceName)
if (!deviceName) {
return;
}
const currentBrightness = DisplayService.getDeviceBrightness(deviceName);
const newBrightness = delta > 0 ? Math.min(100, currentBrightness + 5) : Math.max(1, currentBrightness - 5);
DisplayService.setBrightness(newBrightness, deviceName, false);
DisplayService.setBrightness(newBrightness, deviceName);
}
function getBatteryIconColor() {

View File

@@ -107,7 +107,6 @@ DankOSD {
}
thumbOutlineColor: Theme.surfaceContainer
alwaysShowValue: SettingsData.osdAlwaysShowValue
value: !isDragging ? root.targetBrightness : value
onSliderValueChanged: newValue => {
if (DisplayService.brightnessAvailable) {
@@ -119,6 +118,11 @@ DankOSD {
onContainsMouseChanged: {
setChildHovered(containsMouse);
}
Binding on value {
value: root.targetBrightness
when: !brightnessSlider.isDragging
}
}
}
}
@@ -163,7 +167,12 @@ DankOSD {
y: gap * 2 + Theme.iconSize
property bool dragging: false
property int value: !dragging ? root.targetBrightness : value
property int value: 50
Binding on value {
value: root.targetBrightness
when: !vertSlider.dragging
}
readonly property int minimum: {
const deviceInfo = DisplayService.getCurrentDeviceInfo();
@@ -255,12 +264,14 @@ DankOSD {
}
function updateBrightness(mouse) {
if (DisplayService.brightnessAvailable) {
const ratio = 1.0 - (mouse.y / height);
const newValue = Math.round(vertSlider.minimum + ratio * (vertSlider.maximum - vertSlider.minimum));
DisplayService.setBrightness(newValue, DisplayService.lastIpcDevice, true);
resetHideTimer();
if (!DisplayService.brightnessAvailable) {
return;
}
const ratio = 1.0 - (mouse.y / height);
const newValue = Math.round(vertSlider.minimum + ratio * (vertSlider.maximum - vertSlider.minimum));
vertSlider.value = newValue;
DisplayService.setBrightness(newValue, DisplayService.lastIpcDevice, true);
resetHideTimer();
}
}
}

View File

@@ -14,6 +14,8 @@ Singleton {
property var deviceBrightness: ({})
property var deviceBrightnessUserSet: ({})
property var deviceMaxCache: ({})
property var userControlledDevices: ({})
property var pendingOsdDevices: ({})
property int brightnessVersion: 0
property string currentDevice: ""
property string lastIpcDevice: ""
@@ -28,6 +30,7 @@ Singleton {
}
property int maxBrightness: 100
property bool brightnessInitialized: false
property bool suppressOsd: true
signal brightnessChanged(bool showOsd)
signal deviceSwitched
@@ -38,11 +41,47 @@ Singleton {
property bool automationAvailable: false
property bool gammaControlAvailable: false
function markDeviceUserControlled(deviceId) {
const newControlled = Object.assign({}, userControlledDevices);
newControlled[deviceId] = Date.now();
userControlledDevices = newControlled;
}
function isDeviceUserControlled(deviceId) {
const controlTime = userControlledDevices[deviceId];
if (!controlTime) {
return false;
}
return (Date.now() - controlTime) < 1000;
}
function clearDeviceUserControlled(deviceId) {
const newControlled = Object.assign({}, userControlledDevices);
delete newControlled[deviceId];
userControlledDevices = newControlled;
}
function markDevicePendingOsd(deviceId) {
const newPending = Object.assign({}, pendingOsdDevices);
newPending[deviceId] = true;
pendingOsdDevices = newPending;
}
function clearDevicePendingOsd(deviceId) {
const newPending = Object.assign({}, pendingOsdDevices);
delete newPending[deviceId];
pendingOsdDevices = newPending;
}
function updateSingleDevice(device) {
const isUserControlled = isDeviceUserControlled(device.id);
if (isUserControlled) {
return;
}
const deviceIndex = devices.findIndex(d => d.id === device.id);
if (deviceIndex !== -1) {
const newDevices = [...devices];
const existingDevice = devices[deviceIndex];
const cachedMax = deviceMaxCache[device.id];
let displayMax = cachedMax || (device.class === "ddc" ? device.max : 100);
@@ -71,7 +110,17 @@ Singleton {
let displayValue = device.currentPercent;
if (isExponential) {
if (userSetValue !== undefined) {
displayValue = userSetValue;
const exponent = SessionData.getBrightnessExponent(device.id);
const expectedHardware = Math.round(Math.pow(userSetValue / 100.0, exponent) * 100.0);
if (Math.abs(device.currentPercent - expectedHardware) > 2) {
const newUserSet = Object.assign({}, deviceBrightnessUserSet);
delete newUserSet[device.id];
deviceBrightnessUserSet = newUserSet;
SessionData.clearBrightnessUserSetValue(device.id);
displayValue = linearToExponential(device.currentPercent, device.id);
} else {
displayValue = userSetValue;
}
} else {
displayValue = linearToExponential(device.currentPercent, device.id);
}
@@ -83,9 +132,22 @@ Singleton {
deviceBrightness = newBrightness;
brightnessVersion++;
if (oldValue !== undefined && oldValue !== displayValue && brightnessInitialized) {
brightnessChanged(true);
const isPendingOsd = pendingOsdDevices[device.id] === true;
if (isPendingOsd) {
clearDevicePendingOsd(device.id);
if (!suppressOsd) {
brightnessChanged(true);
}
return;
}
if (!brightnessInitialized || oldValue === displayValue) {
return;
}
if (suppressOsd) {
return;
}
brightnessChanged(true);
}
function updateFromBrightnessState(state) {
@@ -167,13 +229,12 @@ Singleton {
function setBrightness(percentage, device, suppressOsd) {
const actualDevice = device === "" ? getDefaultDevice() : (device || currentDevice || getDefaultDevice());
if (!actualDevice) {
console.warn("DisplayService: No device selected for brightness change");
return;
}
if (actualDevice && actualDevice !== lastIpcDevice) {
if (actualDevice !== lastIpcDevice) {
lastIpcDevice = actualDevice;
}
@@ -183,12 +244,15 @@ Singleton {
let minValue = 0;
let maxValue = 100;
if (isExponential) {
switch (true) {
case isExponential:
minValue = 1;
maxValue = 100;
} else {
break;
default:
minValue = (deviceInfo && (deviceInfo.class === "backlight" || deviceInfo.class === "ddc")) ? 1 : 0;
maxValue = deviceInfo?.displayMax || 100;
break;
}
if (maxValue <= 0) {
@@ -203,6 +267,12 @@ Singleton {
return;
}
if (suppressOsd) {
markDeviceUserControlled(actualDevice);
} else {
markDevicePendingOsd(actualDevice);
}
const newBrightness = Object.assign({}, deviceBrightness);
newBrightness[actualDevice] = clampedValue;
deviceBrightness = newBrightness;
@@ -215,10 +285,6 @@ Singleton {
SessionData.setBrightnessUserSetValue(actualDevice, clampedValue);
}
if (!suppressOsd) {
brightnessChanged(true);
}
const params = {
"device": actualDevice,
"percent": clampedValue
@@ -634,6 +700,13 @@ Singleton {
brightnessChanged();
}
Timer {
id: osdSuppressTimer
interval: 2000
running: true
onTriggered: suppressOsd = false
}
Component.onCompleted: {
nightModeEnabled = SessionData.nightModeEnabled;
deviceBrightnessUserSet = Object.assign({}, SessionData.brightnessUserSetValues);
@@ -674,6 +747,13 @@ Singleton {
function onBrightnessDeviceUpdate(device) {
updateSingleDevice(device);
}
function onLoginctlEvent(event) {
if (event.event === "unlock" || event.event === "resume") {
suppressOsd = true;
osdSuppressTimer.restart();
}
}
}
// Session Data Connections
@@ -746,7 +826,7 @@ Singleton {
if (targetDevice && targetDevice !== root.currentDevice) {
root.setCurrentDevice(targetDevice, false);
}
root.setBrightness(clampedValue, targetDevice, false);
root.setBrightness(clampedValue, targetDevice);
if (targetDevice) {
return "Brightness set to " + clampedValue + "% on " + targetDevice;
@@ -787,7 +867,7 @@ Singleton {
const newBrightness = Math.min(maxValue, currentBrightness + stepValue);
root.setBrightness(newBrightness, actualDevice, false);
root.setBrightness(newBrightness, actualDevice);
return "Brightness increased by " + stepValue + "%" + (targetDevice ? " on " + targetDevice : "");
}
@@ -824,7 +904,7 @@ Singleton {
const newBrightness = Math.max(minValue, currentBrightness - stepValue);
root.setBrightness(newBrightness, actualDevice, false);
root.setBrightness(newBrightness, actualDevice);
return "Brightness decreased by " + stepValue + "%" + (targetDevice ? " on " + targetDevice : "");
}