Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 28 additions & 5 deletions bin/omarchy-brightness-display
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,35 @@ monitor_is_internal() {
[[ $monitor =~ ^(eDP|LVDS|DSI)- ]]
}

# Hyprland monitor names are DRM connector names, so a backlight on the monitor's connected external
# connector is its own (another card can expose a disconnected connector with the same name). Internal
# panels stay on omarchy-hw-display: GPU drivers can register an eDP backlight that doesn't drive the panel.
connector_backlight() {
local subsystem

if [[ -z $monitor ]] || monitor_is_internal; then
return 1
fi

for subsystem in "${OMARCHY_DRM_PATH:-/sys/class/drm}"/card*-"$monitor"/*/subsystem; do
if [[ $(<"${subsystem%/*/subsystem}/status") == "connected" && $(readlink "$subsystem") == */class/backlight ]]; then
subsystem="${subsystem%/subsystem}"
printf '%s\n' "${subsystem##*/}"
return 0
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
fi
done

return 1
}

device="$(connector_backlight)"
Comment thread
greptile-apps[bot] marked this conversation as resolved.

use_apple_display() {
omarchy-hyprland-monitor-focused-apple "$monitor"
[[ -z $device ]] && omarchy-hyprland-monitor-focused-apple "$monitor"
}

use_ddc_display() {
[[ -n $monitor ]] && ! monitor_is_internal
[[ -z $device && -n $monitor ]] && ! monitor_is_internal
}

if (( $# == 0 )); then
Expand All @@ -52,7 +75,7 @@ if (( $# == 0 )); then
exit
fi

device="$(omarchy-hw-display)" || exit 1
[[ -n $device ]] || device="$(omarchy-hw-display)" || exit 1
backlight_brightness "$device"
exit
fi
Expand Down Expand Up @@ -86,8 +109,8 @@ elif use_ddc_display; then
brightness="$(omarchy-brightness-display-ddc "$monitor" "$step")" || exit 1
(( no_osd )) || omarchy-osd -i brightness -p "$brightness"
else
# Current device highlighted
device="$(omarchy-hw-display)" || exit 1
# The focused connector's backlight, else the most likely panel backlight
[[ -n $device ]] || device="$(omarchy-hw-display)" || exit 1

# Current brightness percentage
current=$(backlight_brightness "$device") || exit 1
Expand Down
59 changes: 57 additions & 2 deletions test/shell.d/brightness-display-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,36 @@ trap 'rm -rf "$test_tmp"' EXIT
mock_bin="$test_tmp/bin"
call_log="$test_tmp/calls"
runtime_dir="$test_tmp/runtime"
drm_dir="$test_tmp/drm"
mkdir -p "$mock_bin" "$runtime_dir"

# DP-3 has a connector backlight and DP-1 only a non-backlight child. eDP-1's i915-style backlight must
# not override omarchy-hw-display, and card1's disconnected DP-3 backlight must be ignored.
mkdir -p "$drm_dir/card2-DP-3/apple-DP-3-bl" "$drm_dir/card2-DP-1/power" "$drm_dir/card0-eDP-1/intel_backlight" \
"$drm_dir/card1-DP-3/other-DP-3-bl"
ln -s ../../../../class/backlight "$drm_dir/card2-DP-3/apple-DP-3-bl/subsystem"
ln -s ../../../../class/backlight "$drm_dir/card0-eDP-1/intel_backlight/subsystem"
ln -s ../../../../bus/platform "$drm_dir/card2-DP-1/power/subsystem"
ln -s ../../../../class/backlight "$drm_dir/card1-DP-3/other-DP-3-bl/subsystem"
for connector in card2-DP-3 card2-DP-1 card0-eDP-1; do
printf 'connected\n' >"$drm_dir/$connector/status"
done
printf 'disconnected\n' >"$drm_dir/card1-DP-3/status"

cat >"$mock_bin/omarchy-hyprland-monitor-focused-apple" <<'SH'
#!/bin/bash
exit 1
printf 'omarchy-hyprland-monitor-focused-apple %s\n' "$*" >>"$CALL_LOG"
[[ ${APPLE_DISPLAY:-0} == "1" ]]
SH

cat >"$mock_bin/omarchy-brightness-display-apple" <<'SH'
#!/bin/bash
printf 'omarchy-brightness-display-apple %s\n' "$*" >>"$CALL_LOG"
SH

cat >"$mock_bin/omarchy-osd" <<'SH'
#!/bin/bash
printf 'omarchy-osd %s\n' "$*" >>"$CALL_LOG"
SH

cat >"$mock_bin/omarchy-hyprland-monitor-focused" <<'SH'
Expand Down Expand Up @@ -54,7 +79,7 @@ SH
chmod +x "$mock_bin"/*

run_brightness() {
CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" PATH="$mock_bin:$ROOT/bin:$PATH" \
CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" OMARCHY_DRM_PATH="$drm_dir" PATH="$mock_bin:$ROOT/bin:$PATH" \
"$ROOT/bin/omarchy-brightness-display" "$@"
}

Expand Down Expand Up @@ -129,6 +154,36 @@ grep -F 'ddcutil --bus 7 --skip-ddc-checks --noverify setvcp 10 5' "$call_log" >
fail "external low brightness writes the one-percent target"
pass "external low brightness uses a one-percent step"

: >"$call_log"
brightness=$(APPLE_DISPLAY=1 run_brightness --monitor DP-3)
[[ $brightness == "40" ]] || fail "connector backlight reports brightness" "actual: $brightness"
grep -F 'brightnessctl -d apple-DP-3-bl -m' "$call_log" >/dev/null || \
fail "connector backlight is queried through brightnessctl"
pass "connector backlight reports brightness"

: >"$call_log"
APPLE_DISPLAY=1 run_brightness --monitor DP-3 +5%
grep -Fx 'brightnessctl -d apple-DP-3-bl set 45%' "$call_log" >/dev/null || \
fail "connector backlight steps through brightnessctl" "$(cat "$call_log")"
grep -Fx 'omarchy-osd -i brightness -p 40' "$call_log" >/dev/null || \
fail "connector backlight shows the OSD"
if grep -E 'omarchy-brightness-display-apple|omarchy-hyprland-monitor-focused-apple|ddcutil' "$call_log"; then
fail "connector backlight skips the Apple and DDC backends"
fi
pass "connector backlight wins over the Apple and DDC backends"

: >"$call_log"
FOCUSED_MONITOR=DP-3 run_brightness --no-osd 1%-
grep -Fx 'brightnessctl -d apple-DP-3-bl set 1%-' "$call_log" >/dev/null || \
fail "focused monitor's connector backlight takes precise steps" "$(cat "$call_log")"
pass "focused monitor's connector backlight takes precise steps"

: >"$call_log"
APPLE_DISPLAY=1 run_brightness --no-osd --monitor DP-1 +5%
grep -Fx 'omarchy-brightness-display-apple --no-osd +5%' "$call_log" >/dev/null || \
fail "Apple display without a connector backlight keeps asdcontrol" "$(cat "$call_log")"
pass "Apple display without a connector backlight keeps asdcontrol"

cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf '%s\n' '[
Expand Down