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
192 changes: 174 additions & 18 deletions bin/omarchy-brightness-display
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Comment on lines +221 to +222

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.

P1 Connected monitors stay dim

The external-display check only runs when lowering brightness at the backlight floor. If a user dims the laptop and then connects an external monitor, nothing restores the saved gamma, so the external screen is dimmed too. Pressing brightness up on the laptop skips this check and keeps the external screen dimmed while gamma climbs back toward the baseline.

Restore owned gamma when an external output becomes active, and check that condition before either gamma-step direction.

Knowledge Base Used: Hyprland configuration and display control

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.

Tested on a Dell XPS 9320 (dev channel, laptop screen only), with the commit on top of current quattro. hyprsunset wasn't running beforehand.

Once I fixed the bug below, it works well: below 1% the keys keep dimming, the OSD shows the dim level, and brightness up lifts the dim first, then the backlight goes up from 1%.

The bug: the first time the script starts hyprsunset, hyprsunset inherits the lock fd and keeps omarchy-brightness-display.lock held for as long as it runs. After that, every press exits at flock -n and the keys stop working in both directions. I got stuck at 1% with gamma at 95. fuser showed hyprsunset holding the lock. The tests pass either way, so they don't catch it.

Closing the fd in the child fixed it for me:

-  setsid uwsm-app -- hyprsunset >/dev/null 2>&1 &
+  setsid uwsm-app -- hyprsunset {lock_fd}>&- >/dev/null 2>&1 &

With that, going down from 1% gave gamma 95 → 90 → 85, and going up went back to 100 and then the backlight rose to 2%. The lock stayed free between presses.

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.

You’re right—this is a real lock-lifetime bug. The background uwsm-app/hyprsunset process inherits lock_fd, so it keeps the flock held after the brightness script exits and causes later key presses to be dropped. Closing the descriptor on the launch command is the right fix:

setsid uwsm-app -- hyprsunset {lock_fd}>&- >/dev/null 2>&1 &

Thanks for catching and reproducing it; the existing tests don’t cover descriptor inheritance by the long-lived child.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

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
1 change: 1 addition & 0 deletions default/i18n/en/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -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>",
"browser.webappNeedsChromium": "Web apps need a Chromium-based browser",
Expand Down
2 changes: 2 additions & 0 deletions manual/33-monitors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions shell/services/BrightnessKeys.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion shell/services/BrightnessModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading