diff --git a/bin/omarchy-brightness-display b/bin/omarchy-brightness-display index 5a3e534e27d4..be2067092a9c 100755 --- a/bin/omarchy-brightness-display +++ b/bin/omarchy-brightness-display @@ -29,6 +29,87 @@ backlight_brightness() { brightnessctl -d "$1" -m 2>/dev/null | awk -F, '{ gsub("%", "", $4); print $4; found=1 } END{ exit !found }' } +# hyprsunset's gamma filter can keep dimming below the panel's own floor, but +# it applies to every output: only use it while the laptop panel is the sole +# active display, or an external monitor would dim along with it. +gamma_state_file="${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display.gamma" +gamma_default=100 +gamma_min=30 + +hyprsunset_pid() { + pgrep -u "$UID" -nx hyprsunset 2>/dev/null +} + +gamma_value() { + local gamma + gamma=$(hyprctl hyprsunset gamma 2>/dev/null) || return 1 + [[ $gamma =~ ^[0-9]+$ ]] || return 1 + printf '%s\n' "$gamma" +} + +ensure_hyprsunset() { + [[ -n $(hyprsunset_pid) ]] && return 0 + + setsid uwsm-app -- hyprsunset >/dev/null 2>&1 & + local attempt + for attempt in {1..20}; do + hyprctl hyprsunset gamma >/dev/null 2>&1 && return 0 + sleep 0.1 + done + return 1 +} + +# A gamma this script set on the hyprsunset process it set it on is ours to +# reset; one the user configured stays untouched. The state also records the +# gamma the user had before dimming, so resetting restores that instead of +# assuming a neutral 100. +gamma_owned() { + local pid baseline gamma + [[ -r $gamma_state_file ]] || return 1 + read -r pid baseline gamma <"$gamma_state_file" || return 1 + [[ $pid =~ ^[0-9]+$ && $baseline =~ ^[0-9]+$ && $gamma =~ ^[0-9]+$ ]] || return 1 + [[ $(hyprsunset_pid) == "$pid" ]] || return 1 + [[ $(gamma_value) == "$gamma" ]] +} + +gamma_baseline() { + local pid baseline gamma + [[ -r $gamma_state_file ]] || return 1 + read -r pid baseline gamma <"$gamma_state_file" || return 1 + [[ $baseline =~ ^[0-9]+$ ]] || return 1 + printf '%s\n' "$baseline" +} + +reset_gamma() { + local baseline + if gamma_owned; then + baseline=$(gamma_baseline) || baseline=$gamma_default + hyprctl hyprsunset gamma "$baseline" >/dev/null 2>&1 + fi + rm -f "$gamma_state_file" +} + +# gamma is the level to apply; baseline is the user's gamma before this script +# started dimming, and returning to it hands the filter back to them. +set_gamma() { + local gamma=$1 baseline=$2 pid + (( gamma < gamma_min )) && gamma=$gamma_min + (( gamma > baseline )) && gamma=$baseline + pid=$(hyprsunset_pid) || return 1 + hyprctl hyprsunset gamma "$gamma" >/dev/null || return 1 + if (( gamma >= baseline )); then + rm -f "$gamma_state_file" + else + printf '%s %s %s\n' "$pid" "$baseline" "$gamma" >"$gamma_state_file" + fi + printf '%s\n' "$gamma" +} + +gamma_dim_allowed() { + hyprctl monitors -j 2>/dev/null | + jq -e 'any(.[]; .disabled == false and (.name | test("^(eDP|LVDS|DSI)-") | not)) | not' >/dev/null +} + [[ -n $monitor ]] || monitor="$(omarchy-hyprland-monitor-focused 2>/dev/null || true)" monitor_is_internal() { @@ -102,12 +183,16 @@ exec {lock_fd}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-brightness-display.lock" flock -n "$lock_fd" || exit 0 if use_apple_display; then + # Adjusting another display lifts a gamma this script set, so it does not + # stay dim behind the user's back. + reset_gamma if (( no_osd )); then omarchy-brightness-display-apple --no-osd "$step" else omarchy-brightness-display-apple "$step" fi elif use_ddc_display; then + reset_gamma brightness="$(omarchy-brightness-display-ddc "$monitor" "$step")" || exit 1 (( no_osd )) || omarchy-osd -i brightness -p "$brightness" elif ! use_backlight_display; then @@ -119,31 +204,102 @@ else # Current brightness percentage current=$(backlight_brightness "$device") || exit 1 - # 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 )) + # Relative steps can drive the gamma range once the panel is at its floor. + delta=0 + if [[ $step == "+5%" || $step == "5%-" ]]; then + delta=5 + elif [[ $step == "+1%" || $step == "1%-" ]]; then + delta=1 + fi + [[ $step == "5%-" || $step == "1%-" ]] && (( delta = -delta )) + + gamma_step="" + gamma_handled=0 + new_brightness="" + baseline="" + + if (( delta < 0 )) && (( current <= 1 )); then + if gamma_dim_allowed; then + if ensure_hyprsunset; then + gamma="$(gamma_value)" || gamma=$gamma_default + if gamma_owned; then + baseline=$(gamma_baseline) || baseline=$gamma + else + baseline=$gamma + fi + if (( gamma <= gamma_min )); then + gamma_step=$gamma + else + (( gamma += delta )) + gamma_step="$(set_gamma "$gamma" "$baseline")" || gamma_step="" + fi + gamma_handled=1 + fi else - (( target = current + 5 )) + # An external display would dim along with the panel. + reset_gamma fi + elif (( delta > 0 )) && (( current <= 1 )) && gamma_owned; then + gamma="$(gamma_value)" || gamma=$gamma_default + baseline=$(gamma_baseline) || baseline=$gamma + (( gamma += delta )) + gamma_step="$(set_gamma "$gamma" "$baseline")" || gamma_step="" + if (( gamma_step >= baseline )); then + if (( baseline >= gamma_default )); then + gamma_step="" + else + gamma_step=$baseline + fi + fi + gamma_handled=1 + fi - (( target > 100 )) && target=100 - step="$target%" - elif [[ $step == "5%-" ]]; then - if (( current <= 5 )); then - (( target = current - 1 )) - else - (( target = current - 5 )) + if (( ! gamma_handled )); then + # 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 + + (( target > 100 )) && target=100 + step="$target%" + elif [[ $step == "5%-" ]]; then + if (( current <= 5 )); then + (( target = current - 1 )) + else + (( target = current - 5 )) + fi + + (( target < 1 )) && target=1 + step="$target%" + elif [[ $step == "1%-" ]] && (( current <= 1 )); then + # Keep the panel at its floor: some panels blank at raw 0. + step="1%" fi - (( target < 1 )) && target=1 - step="$target%" + # Set brightness of the display device. + brightnessctl -d "$device" set "$step" >/dev/null + + # A backlight above its floor means the gamma range is over. Only clear a + # gamma this script set; a user's own filter stays untouched. + new_brightness=$(backlight_brightness "$device") || exit 1 + if (( new_brightness > 1 )); then + reset_gamma + fi fi - # Set brightness of the display device. - brightnessctl -d "$device" set "$step" >/dev/null + # A filter this script still owns is the real level at the backlight floor. + if [[ -z $gamma_step ]] && gamma_owned; then + gamma_step="$(gamma_value)" || gamma_step="" + fi # Show the new brightness in OSD - (( no_osd )) || omarchy-osd -i brightness -p "$(backlight_brightness "$device")" + if [[ -n $gamma_step ]]; then + (( no_osd )) || omarchy-osd -i brightness -m "$(omarchy-i18n brightness.dim "$gamma_step")" + else + (( no_osd )) || omarchy-osd -i brightness -p "${new_brightness:-$(backlight_brightness "$device")}" + fi fi diff --git a/default/i18n/en/cli.json b/default/i18n/en/cli.json index 436982aa509d..7f76dee32729 100644 --- a/default/i18n/en/cli.json +++ b/default/i18n/en/cli.json @@ -63,6 +63,7 @@ "battlenet.wipePartial": "Wipe the partial prefix and start fresh?", "branding.pickAboutImage": "Pick PNG or SVG for About", "branding.pickScreensaverImage": "Pick PNG or SVG for screensaver", + "brightness.dim": "Dim %1%", "browser.notFound": "No default browser found", "browser.notFoundHint": "Install a browser or set one with: omarchy default browser ", "browser.webappNeedsChromium": "Web apps need a Chromium-based browser", diff --git a/manual/33-monitors.md b/manual/33-monitors.md index 744d81b36e12..5f7c163d92de 100644 --- a/manual/33-monitors.md +++ b/manual/33-monitors.md @@ -59,6 +59,8 @@ You can also checkout [Hyprmon](https://github.com/erans/hyprmon/), if you'd lik Monitor brightness is controlled by the dedicated function keys for brightness up/down. If you hold down shift while pressing these, you'll go to maximum or minimum brightness. The keys control the display you're focused on, so external monitors that speak DDC/CI are adjusted the same way as the laptop screen. +On a laptop panel, once the backlight reaches its 1% floor, pressing brightness down again keeps dimming the screen with a gamma filter, down to 30%. Pressing brightness up reverses it before the backlight rises. Because the filter applies to every output, an active external monitor turns this extra range off. It is perceived brightness only: it saves no power and slightly changes color accuracy. + ### Apple Displays If you're using an Apple display, the regular keyboard brightness keys will also automatically work, if you're focused on the Apple display. This is done through the `asdcontrol` command. diff --git a/shell/services/BrightnessKeys.qml b/shell/services/BrightnessKeys.qml index 0f9818f5cc49..50b6a31164f4 100644 --- a/shell/services/BrightnessKeys.qml +++ b/shell/services/BrightnessKeys.qml @@ -37,6 +37,9 @@ Item { var max = readNumber(maxFile) if (!(max > 0)) return false var current = Math.round(100 * readNumber(brightnessFile) / max) + // At the panel's floor the script drives the gamma range, so hand the + // press back to it. + if (BrightnessModel.atBacklightFloor(current)) return false setProc.command = ["brightnessctl", "-q", "-d", device, "set", BrightnessModel.brightnessKeyTarget(action, current) + "%"] setProc.running = true diff --git a/shell/services/BrightnessModel.js b/shell/services/BrightnessModel.js index 531d6e36e430..9e2767b119bb 100644 --- a/shell/services/BrightnessModel.js +++ b/shell/services/BrightnessModel.js @@ -5,8 +5,15 @@ function brightnessKeyTarget(action, current) { return Math.max(current <= 5 ? current - 1 : current - 5, 1) } +// The backlight's floor is where omarchy-brightness-display takes over to dim +// with hyprsunset gamma. +function atBacklightFloor(current) { + return current <= 1 +} + if (typeof module !== "undefined") { module.exports = { - brightnessKeyTarget: brightnessKeyTarget + brightnessKeyTarget: brightnessKeyTarget, + atBacklightFloor: atBacklightFloor } } diff --git a/test/shell.d/brightness-display-test.sh b/test/shell.d/brightness-display-test.sh index 22586c1fe67a..1921f02b4c28 100755 --- a/test/shell.d/brightness-display-test.sh +++ b/test/shell.d/brightness-display-test.sh @@ -64,7 +64,7 @@ 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" run_brightness() { - CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" \ + CALL_LOG="$call_log" XDG_RUNTIME_DIR="$runtime_dir" TEST_TMP="$test_tmp" \ PATH="$mock_bin:$ROOT/bin:$PATH" "$brightness_display" "$@" } @@ -220,3 +220,203 @@ 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" + +# --- Gamma dimming below the backlight floor ------------------------------ + +# The mocks below keep the backlight and hyprsunset gamma in files, so a step +# can be followed from one command to the next. hyprctl answers monitors and +# the hyprsunset IPC; pgrep and uwsm-app model the daemon's life cycle. +cat >"$mock_bin/brightnessctl" <<'SH' +#!/bin/bash +printf 'brightnessctl %s\n' "$*" >>"$CALL_LOG" +file="$TEST_TMP/backlight" +[[ -r $file ]] || printf '40\n' >"$file" + +if [[ $* == *" -m"* ]]; then + value=$(cat "$file") + printf 'mock_backlight,backlight,%s,%s%%\n' "$value" "$value" + exit 0 +fi + +if [[ $* == *" set "* ]]; then + arg=${!#} + current=$(cat "$file") + if [[ $arg == +* ]]; then + amount=${arg#+} + amount=${amount%\%} + (( value = current + amount )) + elif [[ $arg == *- ]]; then + amount=${arg%-} + amount=${amount%\%} + (( value = current - amount )) + else + value=${arg%\%} + fi + (( value < 1 )) && value=1 + (( value > 100 )) && value=100 + printf '%s\n' "$value" >"$file" +fi +SH + +cat >"$mock_bin/hyprctl" <<'SH' +#!/bin/bash +printf 'hyprctl %s\n' "$*" >>"$CALL_LOG" + +if [[ $1 == monitors && $2 == -j ]]; then + cat "$TEST_TMP/monitors.json" +elif [[ $1 == hyprsunset && $2 == gamma ]]; then + [[ -r $TEST_TMP/hyprsunset.pid ]] || exit 1 + if [[ -n ${3:-} ]]; then + printf '%s\n' "$3" >"$TEST_TMP/gamma" + else + cat "$TEST_TMP/gamma" + fi +else + exit 1 +fi +SH + +cat >"$mock_bin/pgrep" <<'SH' +#!/bin/bash +[[ -r $TEST_TMP/hyprsunset.pid ]] || exit 1 +cat "$TEST_TMP/hyprsunset.pid" +SH + +cat >"$mock_bin/uwsm-app" <<'SH' +#!/bin/bash +printf '4242\n' >"$TEST_TMP/hyprsunset.pid" +[[ -r $TEST_TMP/gamma ]] || printf '100\n' >"$TEST_TMP/gamma" +SH + +cat >"$mock_bin/omarchy-osd" <<'SH' +#!/bin/bash +printf 'osd %s\n' "$*" >>"$CALL_LOG" +SH + +chmod +x "$mock_bin"/* + +write_monitors() { + if [[ $1 == "external" ]]; then + printf '%s\n' '[{"name":"eDP-1","disabled":false},{"name":"DP-1","disabled":false}]' >"$test_tmp/monitors.json" + else + printf '%s\n' '[{"name":"eDP-1","disabled":false}]' >"$test_tmp/monitors.json" + fi +} + +gamma_state() { + cat "$runtime_dir/omarchy-brightness-display.gamma" 2>/dev/null +} + +write_monitors internal +printf '1\n' >"$test_tmp/backlight" +printf '100\n' >"$test_tmp/gamma" +printf '4242\n' >"$test_tmp/hyprsunset.pid" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +: >"$call_log" + +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 95 ]] || fail "the first step below the floor lowers gamma" "gamma: $(cat "$test_tmp/gamma")" +[[ $(cat "$test_tmp/backlight") == 1 ]] || fail "gamma dimming keeps the backlight at its floor" "backlight: $(cat "$test_tmp/backlight")" +[[ $(gamma_state) == "4242 100 95" ]] || fail "the script records the gamma it owns" "state: $(gamma_state)" +pass "below the backlight floor, down steps gamma" + +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 90 ]] || fail "another down step keeps lowering gamma" "gamma: $(cat "$test_tmp/gamma")" +pass "gamma keeps stepping down" + +run_brightness --no-osd --monitor eDP-1 1%- +[[ $(cat "$test_tmp/gamma") == 89 ]] || fail "precise down uses one gamma point" "gamma: $(cat "$test_tmp/gamma")" +pass "precise down uses one gamma point" + +run_brightness --no-osd --monitor eDP-1 +5% +[[ $(cat "$test_tmp/gamma") == 94 ]] || fail "up retraces the gamma range first" "gamma: $(cat "$test_tmp/gamma")" +pass "brightening retraces the gamma range first" + +run_brightness --no-osd --monitor eDP-1 +5% +run_brightness --no-osd --monitor eDP-1 +5% +[[ $(cat "$test_tmp/gamma") == 100 ]] || fail "gamma reaches neutral" "gamma: $(cat "$test_tmp/gamma")" +[[ ! -e "$runtime_dir/omarchy-brightness-display.gamma" ]] || fail "the owned state clears at neutral gamma" +run_brightness --no-osd --monitor eDP-1 +5% +[[ $(cat "$test_tmp/backlight") == 2 ]] || fail "the backlight rises after gamma reaches 100" "backlight: $(cat "$test_tmp/backlight")" +pass "the backlight rises after gamma reaches 100" + +printf '1\n' >"$test_tmp/backlight" +printf '30\n' >"$test_tmp/gamma" +printf '4242 100 30\n' >"$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 30 ]] || fail "gamma stops at its 30 percent floor" "gamma: $(cat "$test_tmp/gamma")" +pass "gamma stops at its floor" + +write_monitors external +printf '80\n' >"$test_tmp/gamma" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +: >"$call_log" +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 80 ]] || fail "an external display keeps gamma dimming off" "gamma: $(cat "$test_tmp/gamma")" +[[ $(cat "$test_tmp/backlight") == 1 ]] || fail "an external display leaves the backlight at its floor" +grep -q 'hyprsunset gamma 80' "$call_log" && fail "an external display does not write gamma" "$(<"$call_log")" +pass "an active external display keeps gamma dimming off" + +printf '1\n' >"$test_tmp/backlight" +printf '80\n' >"$test_tmp/gamma" +printf '4242 100 80\n' >"$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 100 ]] || fail "an external display lifts the script's own gamma" "gamma: $(cat "$test_tmp/gamma")" +[[ ! -e "$runtime_dir/omarchy-brightness-display.gamma" ]] || fail "lifting the gamma clears the owned state" +pass "an external display lifts the script's own gamma" + +write_monitors internal +printf '1\n' >"$test_tmp/backlight" +printf '90\n' >"$test_tmp/gamma" +printf '4242 100 90\n' >"$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 50% +[[ $(cat "$test_tmp/backlight") == 50 ]] || fail "an absolute level sets the backlight" "backlight: $(cat "$test_tmp/backlight")" +[[ $(cat "$test_tmp/gamma") == 100 ]] || fail "an absolute level above the floor clears the script's gamma" +[[ ! -e "$runtime_dir/omarchy-brightness-display.gamma" ]] || fail "an absolute level clears the owned state" +pass "an absolute level above the floor clears the script's gamma" + +printf '50\n' >"$test_tmp/backlight" +printf '80\n' >"$test_tmp/gamma" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 40% +[[ $(cat "$test_tmp/gamma") == 80 ]] || fail "a user's gamma is not reset" "gamma: $(cat "$test_tmp/gamma")" +pass "a user's gamma survives a brightness change" + +write_monitors internal +printf '1\n' >"$test_tmp/backlight" +printf '80\n' >"$test_tmp/gamma" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 5%- +[[ $(cat "$test_tmp/gamma") == 75 ]] || fail "dimming below the floor adopts the user's gamma" "gamma: $(cat "$test_tmp/gamma")" +[[ $(gamma_state) == "4242 80 75" ]] || fail "the state keeps the user's gamma as the baseline" "state: $(gamma_state)" +run_brightness --no-osd --monitor eDP-1 +5% +[[ $(cat "$test_tmp/gamma") == 80 ]] || fail "raising returns to the user's gamma" "gamma: $(cat "$test_tmp/gamma")" +[[ ! -e "$runtime_dir/omarchy-brightness-display.gamma" ]] || fail "returning to the baseline hands the filter back" +run_brightness --no-osd --monitor eDP-1 +5% +[[ $(cat "$test_tmp/gamma") == 80 ]] || fail "the user's gamma survives the backlight rising" "gamma: $(cat "$test_tmp/gamma")" +pass "returning to a user's gamma hands the filter back" + +printf '1\n' >"$test_tmp/backlight" +printf '100\n' >"$test_tmp/gamma" +rm -f "$test_tmp/hyprsunset.pid" "$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 5%- +[[ -r "$test_tmp/hyprsunset.pid" ]] || fail "hyprsunset starts on demand" +[[ $(cat "$test_tmp/gamma") == 95 ]] || fail "the started hyprsunset takes the first gamma step" "gamma: $(cat "$test_tmp/gamma")" +pass "hyprsunset starts when the gamma range is first needed" + +printf '1\n' >"$test_tmp/backlight" +printf '100\n' >"$test_tmp/gamma" +printf '4242\n' >"$test_tmp/hyprsunset.pid" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +: >"$call_log" +run_brightness --monitor eDP-1 5%- +grep -F 'osd -i brightness -m Dim 95%' "$call_log" >/dev/null || fail "the OSD names the gamma level" "$(<"$call_log")" +pass "the OSD shows the gamma level" + +write_monitors external +printf '1\n' >"$test_tmp/backlight" +printf '100\n' >"$test_tmp/gamma" +rm -f "$runtime_dir/omarchy-brightness-display.gamma" +run_brightness --no-osd --monitor eDP-1 1%- +[[ $(cat "$test_tmp/backlight") == 1 ]] || fail "precise down keeps the backlight floor" "backlight: $(cat "$test_tmp/backlight")" +pass "precise down does not blank the panel at the floor" diff --git a/test/shell.d/brightness-keys-test.sh b/test/shell.d/brightness-keys-test.sh index 6a560bf45794..df3edd778610 100644 --- a/test/shell.d/brightness-keys-test.sh +++ b/test/shell.d/brightness-keys-test.sh @@ -16,4 +16,9 @@ const steps = [ for (const [action, current, target] of steps) { assertEqual(model.brightnessKeyTarget(action, current), target, `brightness ${action} from ${current}% lands on ${target}%`) } + +// The script owns the step at the backlight floor, where gamma takes over. +for (const [current, floor] of [[0, true], [1, true], [2, false], [50, false]]) { + assertEqual(model.atBacklightFloor(current), floor, `backlight floor check at ${current}% is ${floor}`) +} JS