Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/omarchy-brightness-display
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fi
# Drop overlapping brightness key events so concurrent invocations do not race.
# Hardware key repeat present on some devices can otherwise glitch OSD rendering.
exec {lock_fd}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display.lock"
flock -n "$lock_fd" || exit 0
flock -n "$lock_fd" || exit 1

if use_apple_display; then
if (( no_osd )); then
Expand Down Expand Up @@ -115,7 +115,7 @@ else
fi

# Set brightness of the display device.
brightnessctl -d "$device" set "$step" >/dev/null
brightnessctl -d "$device" set "$step" >/dev/null || exit 1

# Show the new brightness in OSD
(( no_osd )) || omarchy-osd -i brightness -p "$(backlight_brightness "$device")"
Expand Down
2 changes: 1 addition & 1 deletion bin/omarchy-brightness-display-apple
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fi

if ! sudo asdcontrol "$device" -- "$step" >/dev/null; then
retry_with_fresh_device
sudo asdcontrol "$device" -- "$step" >/dev/null
sudo asdcontrol "$device" -- "$step" >/dev/null || exit 1
fi

(( no_osd )) || omarchy-osd -i brightness -p "$(current_brightness "$device")"
6 changes: 1 addition & 5 deletions bin/omarchy-brightness-display-ddc
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,5 @@ fi
(( target > 100 )) && target=100
(( raw_target = (target * maximum + 50) / 100 ))

if ! ddcutil --bus "$bus" --skip-ddc-checks --noverify setvcp 10 "$raw_target" >/dev/null 2>&1; then
rm -f "$cache_file"
exit 1
fi

ddcutil --bus "$bus" --verify setvcp 10 "$raw_target" >/dev/null 2>&1 || exit 1
Comment thread
Murderlon marked this conversation as resolved.
Resolved
printf '%s\n' "$target"
89 changes: 66 additions & 23 deletions shell/plugins/panels/monitor/Panel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ Panel {

// manageIpc: false so this panel can own the single IpcHandler the target
// permits — needed for the brightness + state methods below.

// Brightness is written optimistically: the slider moves first and
// `omarchy-brightness-display` verifies afterwards. brightnessPercent is both
// what the slider shows and the value the hardware still owes us,
// sentBrightnessPercent is what the write in flight is verifying, and
// confirmedBrightnessPercent is the last percentage the hardware accepted.
property int brightnessPercent: 0
property int pendingBrightnessPercent: 0
property bool brightnessSetQueued: false
property int sentBrightnessPercent: 0
property int confirmedBrightnessPercent: 0
property bool brightnessOsdPending: false
property bool brightnessWritePending: false
property int brightnessGeneration: 0
property int brightnessWriteToken: 0
property bool brightnessAvailable: false
property string internalMonitor: ""
property string externalMonitor: ""
Expand Down Expand Up @@ -204,7 +214,7 @@ Panel {
function brightnessIpc(percent) {
var value = Number(percent)
root.setBrightness(value)
return "got " + root.pendingBrightnessPercent
return "got " + root.brightnessPercent
}

function stateIpc() {
Expand All @@ -230,26 +240,55 @@ Panel {
}

function refresh() {
if (!stateProc.running) stateProc.running = true
if (!stateProc.running) {
stateProc.brightnessGeneration = root.brightnessWritePending || brightnessDebounce.running
? -1 : root.brightnessGeneration
stateProc.running = true
}
}

function setBrightness(value) {
var percent = Model.clampBrightness(value)
root.brightnessPercent = percent
root.pendingBrightnessPercent = percent
root.brightnessGeneration++

if (setBrightnessProc.running) {
root.brightnessSetQueued = true
return
}
if (root.brightnessWritePending || setBrightnessProc.running) return

root.brightnessSetQueued = false
brightnessDebounce.stop()
root.brightnessWritePending = true
root.brightnessWriteToken++
root.sentBrightnessPercent = percent
setBrightnessProc.command = ["omarchy-brightness-display", "--no-osd", "--monitor", root.focusedMonitor, percent + "%"]
setBrightnessProc.running = true
}

function finishBrightnessWrite(exitCode, token) {
if (!root.brightnessWritePending || token !== root.brightnessWriteToken) return
root.brightnessWritePending = false
root.brightnessGeneration++
if (exitCode === 0) root.confirmedBrightnessPercent = root.sentBrightnessPercent

if (root.brightnessPercent !== root.sentBrightnessPercent) {
// exited can run before Process clears running. Read the newest preview
// after it stops, including input that arrives before this callback.
Qt.callLater(function() {
if (token === root.brightnessWriteToken && !root.brightnessWritePending)
root.setBrightness(root.brightnessPercent)
})
return
}

// A reread here races the hardware and can hide the slider on an empty read.
if (exitCode !== 0) root.brightnessPercent = root.confirmedBrightnessPercent
if (root.brightnessOsdPending) {
root.brightnessOsdPending = false
if (exitCode === 0) root.showBrightnessOsd(root.brightnessPercent)
}
}

function previewBrightness(value) {
root.brightnessPercent = Model.clampBrightness(value)
root.brightnessGeneration++
brightnessDebounce.restart()
}

Expand Down Expand Up @@ -385,14 +424,20 @@ Panel {

Process {
id: stateProc
property int brightnessGeneration: -1
command: ["omarchy-monitor-state"]
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
var lines = String(text || "").split("\n")
var brightness = String(lines[0] || "").trim()
root.brightnessAvailable = brightness !== "unavailable" && brightness !== ""
root.brightnessPercent = root.brightnessAvailable ? Math.max(0, Math.min(100, parseInt(brightness, 10))) : 0
if (stateProc.brightnessGeneration === root.brightnessGeneration
&& !root.brightnessWritePending && !brightnessDebounce.running) {
root.brightnessAvailable = brightness !== "unavailable" && brightness !== ""
var read = root.brightnessAvailable ? Math.max(0, Math.min(100, parseInt(brightness, 10))) : 0
if (root.brightnessAvailable) root.confirmedBrightnessPercent = read
root.brightnessPercent = read
Comment thread
Murderlon marked this conversation as resolved.
Resolved
}
Comment thread
Murderlon marked this conversation as resolved.
Resolved
root.internalMonitor = String(lines[1] || "").trim()
root.externalMonitor = String(lines[2] || "").trim()
root.internalEnabled = String(lines[3] || "").trim() !== ""
Expand All @@ -414,18 +459,16 @@ Panel {
Process {
id: setBrightnessProc
stdout: StdioCollector { waitForEnd: true }
// Do NOT call refresh() after a brightness set completes. The local
// brightnessPercent we just wrote is authoritative; re-reading via
// `omarchy-brightness-display` races the hardware/driver and can
// return an empty string, which the parser then coerces to 0 —
// visible as a "bounce to zero" after h/l keypresses. External
// brightness changes are still picked up by the 5s periodic refresh,
// the open-time refresh, and Component.onCompleted.
onExited: function(exitCode, exitStatus) {
root.finishBrightnessWrite(exitStatus === 0 ? exitCode : 1, root.brightnessWriteToken)
}
onRunningChanged: {
if (running) return
if (root.brightnessSetQueued) {
root.setBrightness(root.pendingBrightnessPercent)
}
// A failed start only changes running. Let a normal exit complete first.
var token = root.brightnessWriteToken
Qt.callLater(function() {
if (!setBrightnessProc.running) root.finishBrightnessWrite(1, token)
})
}
}

Expand Down Expand Up @@ -476,8 +519,8 @@ Panel {
var wheel = Util.wheelSteps(root.wheelAccumulator, delta)
root.wheelAccumulator = wheel.remainder
if (wheel.steps === 0) return
root.brightnessOsdPending = true
root.setBrightness(root.brightnessPercent + wheel.steps * 5)
root.showBrightnessOsd(root.brightnessPercent)
}
}

Expand Down
17 changes: 14 additions & 3 deletions test/shell.d/brightness-display-apple-cache-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ chmod +x "$stub_dir/sudo"
cat >"$stub_dir/asdcontrol" <<STUB
#!/bin/bash
printf '%s\n' "\$*" >>"$asd_log"
# --detect reports nothing, so detection never yields a device.
if [[ \$1 == "--detect" ]]; then
if [[ -n \${ASD_DETECT_DEVICE:-} ]]; then
printf '%s: Mock display\n' "\$ASD_DETECT_DEVICE"
fi
exit 0
fi
# A brightness read (a lone device arg) returns a plausible value; a set
# (<device> -- <step>) just succeeds.
if (( \$# > 1 )) && [[ \${ASD_WRITE_FAIL:-0} == "1" ]]; then
exit 1
fi
if [[ \$# -eq 1 ]]; then
printf '%s: BRIGHTNESS=30000\n' "\$1"
fi
Expand Down Expand Up @@ -122,6 +125,14 @@ if [[ -n $real_hiddev ]]; then
grep -qF -- "$real_hiddev -- +5%" "$asd_log" ||
fail "wrapper did not trust a valid cached hiddev node: $real_hiddev"
pass "wrapper trusts a cached hiddev character device without re-detecting"

: >"$asd_log"
if ASD_DETECT_DEVICE="$real_hiddev" ASD_WRITE_FAIL=1 XDG_RUNTIME_DIR="$xdg_dir" PATH="$stub_dir:$ROOT/bin:$PATH" \
"$ROOT/bin/omarchy-brightness-display-apple" --no-osd 50% >/dev/null 2>&1; then
fail "a rejected Apple brightness retry is reported to the panel"
fi
(( $(grep -c -- ' -- 50%' "$asd_log") == 2 )) || fail "Apple brightness retries once after refreshing its device"
pass "a rejected Apple brightness retry is reported to the panel"
else
pass "no /dev/hiddev* character device present; skipping the valid-cache case"
fi
Expand Down
Loading