Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
134 changes: 102 additions & 32 deletions bin/omarchy-brightness-display
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

no_osd=0
monitor=""
drm_path="${OMARCHY_DRM_PATH:-/sys/class/drm}"

# omarchy-brightness-display-ddc's "this connector answers no DDC display",
# as opposed to a transient read failure worth retrying.
no_ddc_display=3

while (( $# > 0 )); do
case "$1" in
Expand Down Expand Up @@ -35,6 +40,61 @@ monitor_is_internal() {
[[ $monitor =~ ^(eDP|LVDS|DSI)- ]]
}

# All-in-ones wire their built-in panel to a DisplayPort connector, so its
# output name carries no hint that it is internal and the panel gets routed to
# DDC/CI, which a built-in panel does not answer. The kernel backlight is the
# right target there -- but only on a machine with no eDP/LVDS/DSI connector to
# own that backlight instead, or a laptop's DDC-less external monitor would move
# the laptop's own panel. Read the connectors rather than counting Hyprland
# outputs: Hyprland invents a FALLBACK output when every monitor is disabled.
machine_has_internal_connector() {
local status

for status in "$drm_path"/card*-*/status; do
[[ -e $status ]] || continue
[[ $status =~ -(eDP|LVDS|DSI)-[^/]+/status$ ]] || continue
[[ $(<"$status") == "connected" ]] && return 0
done

return 1
}

ddc_listed_connectors() {
ddcutil --skip-ddc-checks detect --brief 2>/dev/null | awk '
/DRM connector:/ {
connector = $NF
sub(/^card[0-9]+-/, "", connector)
print connector
}
'
}

# Fall back only when the named monitor is the unique connected connector that
# ddcutil did not list. Otherwise a second DDC-less external on an all-in-one
# would steer the built-in backlight.
backlight_owns_named_monitor() {
local status path conn listed
local -a unddc=()

! machine_has_internal_connector || return 1
omarchy-hw-display >/dev/null 2>&1 || return 1
[[ -n $monitor ]] || return 1

listed=$(ddc_listed_connectors)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Brightness changes repeat bus scans

Every query or adjustment that takes the all-in-one fallback runs ddcutil detect again through ddc_listed_connectors. This bypasses the DDC helper’s 60-second negative cache, so brightness keys and panel refreshes repeat a full bus scan before reaching the kernel backlight. During adjustments, the scan also holds the lock that drops overlapping key presses. If scans are slow, brightness changes can lag and repeated presses can be dropped.

Cache the detected connector list and refresh it when displays change.

for status in "$drm_path"/card*-*/status; do
[[ -e $status ]] || continue
[[ $(<"$status") == "connected" ]] || continue
path=${status%/status}
conn=${path##*/}
conn=${conn#*-}
if ! grep -Fxq -- "$conn" <<<"$listed"; then
unddc+=("$conn")
fi
done

(( ${#unddc[@]} == 1 )) && [[ ${unddc[0]} == "$monitor" ]]
}

use_apple_display() {
omarchy-hyprland-monitor-focused-apple "$monitor"
}
Expand Down Expand Up @@ -72,7 +132,9 @@ if (( $# == 0 )); then
exit
elif use_ddc_display; then
omarchy-brightness-display-ddc "$monitor"
exit
ddc_status=$?
(( ddc_status == 0 )) && exit 0
(( ddc_status == no_ddc_display )) && backlight_owns_named_monitor || exit 1
elif ! use_backlight_display; then
exit 1
fi
Expand Down Expand Up @@ -107,43 +169,51 @@ if use_apple_display; then
else
omarchy-brightness-display-apple "$step"
fi
elif use_ddc_display; then
brightness="$(omarchy-brightness-display-ddc "$monitor" "$step")" || exit 1
(( no_osd )) || omarchy-osd -i brightness -p "$brightness"
exit
fi

if use_ddc_display; then
brightness="$(omarchy-brightness-display-ddc "$monitor" "$step")"
ddc_status=$?
if (( ddc_status == 0 )); then
(( no_osd )) || omarchy-osd -i brightness -p "$brightness"
exit 0
fi
(( ddc_status == no_ddc_display )) && backlight_owns_named_monitor || exit 1
elif ! use_backlight_display; then
exit 1
else
# Current device highlighted
device="$(omarchy-hw-display)" || exit 1

# Current brightness percentage
current=$(backlight_brightness "$device") || exit 1
fi

# Apply non-uniform step size: 1% steps if at or below 5%, otherwise set an
# absolute target percentage to avoid raw backlight rounding causing uneven OSD steps.
if [[ $step == "+5%" ]]; then
if (( current < 5 )); then
(( target = current + 1 ))
else
(( target = current + 5 ))
fi
# Current device highlighted
device="$(omarchy-hw-display)" || exit 1

(( target > 100 )) && target=100
step="$target%"
elif [[ $step == "5%-" ]]; then
if (( current <= 5 )); then
(( target = current - 1 ))
else
(( target = current - 5 ))
fi
# Current brightness percentage
current=$(backlight_brightness "$device") || exit 1

(( target < 1 )) && target=1
step="$target%"
# Apply non-uniform step size: 1% steps if at or below 5%, otherwise set an
# absolute target percentage to avoid raw backlight rounding causing uneven OSD steps.
if [[ $step == "+5%" ]]; then
if (( current < 5 )); then
(( target = current + 1 ))
else
(( target = current + 5 ))
fi

# Set brightness of the display device.
brightnessctl -d "$device" set "$step" >/dev/null
(( target > 100 )) && target=100
step="$target%"
elif [[ $step == "5%-" ]]; then
if (( current <= 5 )); then
(( target = current - 1 ))
else
(( target = current - 5 ))
fi

# Show the new brightness in OSD
(( no_osd )) || omarchy-osd -i brightness -p "$(backlight_brightness "$device")"
(( target < 1 )) && target=1
step="$target%"
fi

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

# Show the new brightness in OSD
(( no_osd )) || omarchy-osd -i brightness -p "$(backlight_brightness "$device")"
23 changes: 16 additions & 7 deletions bin/omarchy-brightness-display-ddc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ step="${2:-}"

[[ -n $monitor ]] || exit 1

# Distinguishes "this connector answers no DDC display" from a transient read
# failure, so a caller can route the connector somewhere else rather than retry
# a bus that is never going to appear.
no_ddc_display=3

cache_dir="${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display-ddc"
cache_name="${monitor//[^[:alnum:]_.-]/_}"
cache_file="$cache_dir/$cache_name.bus"
Expand Down Expand Up @@ -51,7 +56,7 @@ find_bus() {
if [[ $bus == "unavailable" ]]; then
now=$(date +%s)
if [[ $cached_value =~ ^[0-9]+$ ]] && (( now - cached_value < unavailable_cache_seconds )); then
return 1
return "$no_ddc_display"
fi
bus=""
rm -f "$cache_file"
Expand All @@ -61,7 +66,7 @@ find_bus() {
bus="$(detect_bus)" || return 1
if [[ ! $bus =~ ^[0-9]+$ ]]; then
cache_unavailable
return 1
return "$no_ddc_display"
fi
mkdir -p "$cache_dir" 2>/dev/null || true
printf '%s\n' "$bus" >"$cache_file" 2>/dev/null || true
Expand Down Expand Up @@ -95,7 +100,7 @@ read_brightness() {
read -r cached_bus cached_maximum _ <"$cache_file" || true
fi

bus="$(find_bus)" || return 1
bus="$(find_bus)" || return $?
values="$(read_vcp "$bus")" || {
# Only a successful read caches the maximum. A bus that answered before is detected again next call; any other is left alone for a while.
if [[ $cached_bus =~ ^[0-9]+$ && $cached_maximum =~ ^[0-9]+$ ]]; then
Expand All @@ -119,7 +124,8 @@ percent=""
range_cached_at=""

if [[ -z $step ]]; then
read -r bus current maximum < <(read_brightness) || exit 1
brightness="$(read_brightness)" || exit $?
read -r bus current maximum <<<"$brightness"
[[ -n ${bus:-} && -n ${current:-} && -n ${maximum:-} ]] || exit 1
(( percent = (current * 100 + maximum / 2) / maximum ))
printf '%s\n' "$percent"
Expand All @@ -141,10 +147,12 @@ if [[ $step =~ ^([0-9]+)%$ ]]; then
fi

if (( ! range_cache_fresh )); then
read -r bus current maximum < <(read_brightness) || exit 1
brightness="$(read_brightness)" || exit $?
read -r bus current maximum <<<"$brightness"
fi
elif [[ $step =~ ^\+([0-9]+)%$ ]]; then
read -r bus current maximum < <(read_brightness) || exit 1
brightness="$(read_brightness)" || exit $?
read -r bus current maximum <<<"$brightness"
[[ -n ${bus:-} && -n ${current:-} && -n ${maximum:-} ]] || exit 1
(( percent = (current * 100 + maximum / 2) / maximum ))
amount="${BASH_REMATCH[1]}"
Expand All @@ -154,7 +162,8 @@ elif [[ $step =~ ^\+([0-9]+)%$ ]]; then
(( target = percent + amount ))
fi
elif [[ $step =~ ^([0-9]+)%-$ ]]; then
read -r bus current maximum < <(read_brightness) || exit 1
brightness="$(read_brightness)" || exit $?
read -r bus current maximum <<<"$brightness"
[[ -n ${bus:-} && -n ${current:-} && -n ${maximum:-} ]] || exit 1
(( percent = (current * 100 + maximum / 2) / maximum ))
amount="${BASH_REMATCH[1]}"
Expand Down
2 changes: 1 addition & 1 deletion bin/omarchy-hw-display
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ done < <(ls -1 "$backlight_path" 2>/dev/null)
# dual-GPU Macs the GPU's own PWM stops driving the panel once that GPU suspends. The
# platform's preferred backlights follow, then the GPU and ACPI ones.
# Glob the candidates in the loop list: [[ ]] does not do pathname expansion.
for candidate in "$backlight_path"/gmux_backlight "${prefers[@]/#/$backlight_path/}" "$backlight_path"/amdgpu_bl* "$backlight_path"/intel_backlight "$backlight_path"/acpi_video*; do
for candidate in "$backlight_path"/gmux_backlight "${prefers[@]/#/$backlight_path/}" "$backlight_path"/apple-panel-bl "$backlight_path"/amdgpu_bl* "$backlight_path"/intel_backlight "$backlight_path"/acpi_video*; do
if [[ -e $candidate ]] && usable "${candidate##*/}"; then
device="${candidate##*/}"
break
Expand Down
77 changes: 76 additions & 1 deletion test/shell.d/brightness-display-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,26 @@ platform_root_copy "$ROOT/bin/omarchy-brightness-display" "$brightness_display"
sed -i "s|/sys/class/drm|$test_tmp/drm|g" "$brightness_display"
grep -qF "$test_tmp/drm/card" "$brightness_display" || fail "omarchy-brightness-display reads DRM connectors from the fixture"

# Machine shape, as the kernel presents it. A laptop owns its backlight through
# an eDP connector; an all-in-one wires its built-in panel to DisplayPort and has
# no internal-named connector at all. The all-in-one gets both topologies, alone
# and with a second display attached, so each case runs against the DRM set it
# actually describes.
drm_laptop="$test_tmp/drm-laptop"
drm_aio="$test_tmp/drm-aio"
drm_aio_docked="$test_tmp/drm-aio-docked"
mkdir -p "$drm_laptop/card1-eDP-1" "$drm_laptop/card1-DP-2" \
"$drm_aio/card1-DP-1" "$drm_aio/card1-DP-2" \
"$drm_aio_docked/card1-DP-1" "$drm_aio_docked/card1-DP-2"
printf 'connected\n' >"$drm_laptop/card1-eDP-1/status"
printf 'connected\n' >"$drm_laptop/card1-DP-2/status"
printf 'connected\n' >"$drm_aio/card1-DP-1/status"
printf 'disconnected\n' >"$drm_aio/card1-DP-2/status"
printf 'connected\n' >"$drm_aio_docked/card1-DP-1/status"
printf 'connected\n' >"$drm_aio_docked/card1-DP-2/status"

run_brightness() {
CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" \
CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" OMARCHY_DRM_PATH="${DRM_PATH:-$drm_laptop}" \
PATH="$mock_bin:$ROOT/bin:$PATH" "$brightness_display" "$@"
}

Expand Down Expand Up @@ -220,3 +238,60 @@ for conf in "" "ddc-require-connector-ddc extra" "ddc-future"; do
done
rm -f "$platform_root/displays.conf"
pass "every external monitor is probed without the directive"

# An all-in-one's built-in panel is named DP-1 and answers no DDC display, and
# the machine has no eDP/LVDS/DSI connector that could own the kernel backlight
# instead -- so the backlight is the panel behind that connector.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
brightness=$(DRM_PATH="$drm_aio" DDC_CONNECTOR=DP-9 run_brightness --monitor DP-1) ||
fail "all-in-one panel falls back to the kernel backlight" "the command exited nonzero"
[[ $brightness == "40" ]] || fail "all-in-one panel falls back to the kernel backlight" "actual: $brightness"
pass "a DisplayPort panel with no DDC display uses the kernel backlight"

rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
DRM_PATH="$drm_aio" DDC_CONNECTOR=DP-9 run_brightness --no-osd --monitor DP-1 60%
grep -F 'brightnessctl -d mock_backlight set 60%' "$call_log" >/dev/null ||
fail "all-in-one panel sets brightness through the kernel backlight"
pass "a DisplayPort panel with no DDC display is adjusted through the kernel backlight"

# The same all-in-one with a real external monitor attached and connected: that
# one answers DDC, so it keeps using it. Only the connector without a DDC display
# falls back.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
brightness=$(DRM_PATH="$drm_aio_docked" DDC_CONNECTOR=DP-2 run_brightness --monitor DP-2) ||
fail "an external monitor on an all-in-one still uses DDC" "the command exited nonzero"
[[ $brightness == "50" ]] || fail "an external monitor on an all-in-one still uses DDC" "actual: $brightness"
pass "an external monitor that answers DDC still uses DDC"

# The built-in panel of that same docked all-in-one still has to reach the
# backlight. A second connected display is not an internal panel, so it cannot
# be what the backlight belongs to.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
brightness=$(DRM_PATH="$drm_aio_docked" DDC_CONNECTOR=DP-2 run_brightness --monitor DP-1) ||
fail "a docked all-in-one still drives its own panel" "the command exited nonzero"
[[ $brightness == "40" ]] || fail "a docked all-in-one still drives its own panel" "actual: $brightness"
pass "an all-in-one panel keeps the kernel backlight with a second display connected"

# Two connected outputs, neither listed by detect: only the unique unddc case
# may use the backlight. A second DDC-less external must not move the built-in.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
if DRM_PATH="$drm_aio_docked" DDC_CONNECTOR=DP-9 run_brightness --monitor DP-2 >/dev/null 2>&1; then
fail "a second DDC-less display on an all-in-one does not move the built-in backlight"
fi
pass "a second DDC-less display on an all-in-one does not fall back"

# A laptop's DDC-less external monitor must keep failing. Falling back would
# move the laptop's own panel while the user is asking for the other one.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
if DRM_PATH="$drm_laptop" DDC_CONNECTOR=DP-9 run_brightness --monitor DP-2 >/dev/null 2>&1; then
fail "a laptop's DDC-less external monitor does not move the internal backlight"
fi
pass "an external monitor on a machine with an internal panel does not fall back"

# A transient DDC read failure is not "no display on this connector", so it must
# not fall back either -- the monitor is there and will answer on the next try.
rm -rf "$runtime_dir/omarchy-brightness-display-ddc"
if DRM_PATH="$drm_aio" DDC_READ_FAIL=1 run_brightness --monitor DP-1 >/dev/null 2>&1; then
fail "a transient DDC read failure does not fall back to the backlight"
fi
pass "a transient DDC read failure is not treated as a missing DDC display"
20 changes: 20 additions & 0 deletions test/shell.d/hw-display-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ device=$(hw_display)
[[ $device == "gmux_backlight" ]] || fail "gmux outranks the GPU backlights on dual-GPU Macs" "actual: $device"
pass "gmux outranks the GPU backlights on dual-GPU Macs"

write_backlights 228600000.dsi.0 apple-panel-bl
device=$(hw_display)
[[ $device == "apple-panel-bl" ]] || fail "apple-panel-bl beats the alphabetical DSI fallback" "actual: $device"
pass "apple-panel-bl beats the alphabetical DSI fallback"

write_backlights apple-panel-bl
device=$(hw_display)
[[ $device == "apple-panel-bl" ]] || fail "apple-panel-bl is used when it is the only backlight" "actual: $device"
pass "apple-panel-bl is used when it is the only backlight"

write_backlights apple-panel-bl gmux_backlight
device=$(hw_display)
[[ $device == "gmux_backlight" ]] || fail "gmux outranks apple-panel-bl on T2 Macs" "actual: $device"
pass "gmux outranks apple-panel-bl on T2 Macs"

write_backlights 228600000.dsi.0 appletb_backlight
device=$(hw_display)
[[ $device == "228600000.dsi.0" ]] || fail "the Touch Bar is skipped even when the fallback is a DSI backlight" "actual: $device"
pass "the Touch Bar is skipped even when the fallback is a DSI backlight"

write_backlights
if hw_display >/dev/null 2>&1; then
fail "no backlight device reports failure"
Expand Down
Loading