brightness: add ipc handler for get/set (#546)
* services(brightness): add IpcHandler with brightnessctl like set function * allow get/set for specific monitor Also handle invalid formats --------- Co-authored-by: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
This commit is contained in:
parent
1d5360d3f3
commit
1066e467be
1 changed files with 78 additions and 6 deletions
|
|
@ -17,22 +17,41 @@ Singleton {
|
||||||
return monitors.find(m => m.modelData === screen);
|
return monitors.find(m => m.modelData === screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMonitor(query: string): var {
|
||||||
|
if (query === "active") {
|
||||||
|
return monitors.find(m => Hypr.monitorFor(m.modelData)?.focused);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.startsWith("model:")) {
|
||||||
|
const model = query.slice(6);
|
||||||
|
return monitors.find(m => m.modelData.model === model);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.startsWith("serial:")) {
|
||||||
|
const serial = query.slice(7);
|
||||||
|
return monitors.find(m => m.modelData.serialNumber === serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.startsWith("id:")) {
|
||||||
|
const id = parseInt(query.slice(3), 10);
|
||||||
|
return monitors.find(m => Hypr.monitorFor(m.modelData)?.id === id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return monitors.find(m => m.modelData.name === query);
|
||||||
|
}
|
||||||
|
|
||||||
function increaseBrightness(): void {
|
function increaseBrightness(): void {
|
||||||
const focusedName = Hypr.focusedMonitor.name;
|
const monitor = getMonitor("active");
|
||||||
const monitor = monitors.find(m => focusedName === m.modelData.name);
|
|
||||||
if (monitor)
|
if (monitor)
|
||||||
monitor.setBrightness(monitor.brightness + 0.1);
|
monitor.setBrightness(monitor.brightness + 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function decreaseBrightness(): void {
|
function decreaseBrightness(): void {
|
||||||
const focusedName = Hypr.focusedMonitor.name;
|
const monitor = getMonitor("active");
|
||||||
const monitor = monitors.find(m => focusedName === m.modelData.name);
|
|
||||||
if (monitor)
|
if (monitor)
|
||||||
monitor.setBrightness(monitor.brightness - 0.1);
|
monitor.setBrightness(monitor.brightness - 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadableId: "brightness"
|
|
||||||
|
|
||||||
onMonitorsChanged: {
|
onMonitorsChanged: {
|
||||||
ddcMonitors = [];
|
ddcMonitors = [];
|
||||||
ddcProc.running = true;
|
ddcProc.running = true;
|
||||||
|
|
@ -78,6 +97,59 @@ Singleton {
|
||||||
onPressed: root.decreaseBrightness()
|
onPressed: root.decreaseBrightness()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IpcHandler {
|
||||||
|
target: "brightness"
|
||||||
|
|
||||||
|
function get(): real {
|
||||||
|
return getFor("active");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allows searching by active/model/serial/id/name
|
||||||
|
function getFor(query: string): real {
|
||||||
|
return root.getMonitor(query)?.brightness ?? -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set(value: string): string {
|
||||||
|
return setFor("active", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles brightness value like brightnessctl: 0.1, +0.1, 0.1-, 10%, +10%, 10%-
|
||||||
|
function setFor(query: string, value: string): string {
|
||||||
|
const monitor = root.getMonitor(query);
|
||||||
|
if (!monitor)
|
||||||
|
return "Invalid monitor: " + query;
|
||||||
|
|
||||||
|
let targetBrightness;
|
||||||
|
if (value.endsWith("%-")) {
|
||||||
|
const percent = parseFloat(value.slice(0, -2));
|
||||||
|
targetBrightness = monitor.brightness - (percent / 100);
|
||||||
|
} else if (value.startsWith("+") && value.endsWith("%")) {
|
||||||
|
const percent = parseFloat(value.slice(1, -1));
|
||||||
|
targetBrightness = monitor.brightness + (percent / 100);
|
||||||
|
} else if (value.endsWith("%")) {
|
||||||
|
const percent = parseFloat(value.slice(0, -1));
|
||||||
|
targetBrightness = percent / 100;
|
||||||
|
} else if (value.startsWith("+")) {
|
||||||
|
const increment = parseFloat(value.slice(1));
|
||||||
|
targetBrightness = monitor.brightness + increment;
|
||||||
|
} else if (value.endsWith("-")) {
|
||||||
|
const decrement = parseFloat(value.slice(0, -1));
|
||||||
|
targetBrightness = monitor.brightness - decrement;
|
||||||
|
} else if (value.includes("%") || value.includes("-") || value.includes("+")) {
|
||||||
|
return `Invalid brightness format: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||||
|
} else {
|
||||||
|
targetBrightness = parseFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNaN(targetBrightness))
|
||||||
|
return `Failed to parse value: ${value}\nExpected: 0.1, +0.1, 0.1-, 10%, +10%, 10%-`;
|
||||||
|
|
||||||
|
monitor.setBrightness(targetBrightness);
|
||||||
|
|
||||||
|
return `Set monitor ${monitor.modelData.name} brightness to ${+monitor.brightness.toFixed(2)}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
component Monitor: QtObject {
|
component Monitor: QtObject {
|
||||||
id: monitor
|
id: monitor
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue