Set default volume and brightness at login
Add three `systemd` user units that reset volume, screen brightness, and keyboard backlight, so that every session starts from a known state. Values come from ~/.config/environment.d/50-defaults.conf, and can be overridden per machine via an untracked 90-local.conf
This commit is contained in:
parent
2c61af02a4
commit
67c14ddb25
9 changed files with 158 additions and 0 deletions
19
.config/environment.d/50-defaults.conf
Normal file
19
.config/environment.d/50-defaults.conf
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Session defaults, read by `systemd --user` at login
|
||||
#
|
||||
# Applied by:
|
||||
# - set-brightness-default.service
|
||||
# - set-keyboard-brightness-default.service
|
||||
# - set-volume-default.service
|
||||
#
|
||||
# For machine-local overrides create an untracked
|
||||
# ~/.config/environment.d/90-local.conf instead
|
||||
# and repeat only the keys you want to change
|
||||
#
|
||||
# Apply with: `systemctl --user daemon-reload`,
|
||||
# then log out and back in
|
||||
#
|
||||
# Verify with: `systemctl --user show-environment`
|
||||
|
||||
DEFAULT_BRIGHTNESS=30
|
||||
DEFAULT_KEYBOARD_BRIGHTNESS=50
|
||||
DEFAULT_VOLUME=30
|
||||
6
.config/environment.d/README.md
Normal file
6
.config/environment.d/README.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# Session Environment Defaults
|
||||
|
||||
This folder contains environment variables
|
||||
read by `systemd --user` at login.
|
||||
|
||||
Machine-local overrides go into an untracked `90-local.conf`.
|
||||
13
.config/systemd/user/set-brightness-default.service
Normal file
13
.config/systemd/user/set-brightness-default.service
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[Unit]
|
||||
Description=Set default screen brightness
|
||||
After=graphical-session.target
|
||||
ConditionPathExistsGlob=/sys/class/backlight/*
|
||||
PartOf=graphical-session.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=%h/.local/bin/set-brightness-default
|
||||
TimeoutStartSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=graphical-session.target
|
||||
12
.config/systemd/user/set-keyboard-brightness-default.service
Normal file
12
.config/systemd/user/set-keyboard-brightness-default.service
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[Unit]
|
||||
Description=Set default keyboard backlight brightness
|
||||
After=graphical-session.target
|
||||
PartOf=graphical-session.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=%h/.local/bin/set-keyboard-brightness-default
|
||||
TimeoutStartSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=graphical-session.target
|
||||
13
.config/systemd/user/set-volume-default.service
Normal file
13
.config/systemd/user/set-volume-default.service
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[Unit]
|
||||
Description=Set default output volume
|
||||
After=wireplumber.service
|
||||
PartOf=graphical-session.target
|
||||
Wants=wireplumber.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=%h/.local/bin/set-volume-default
|
||||
TimeoutStartSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=graphical-session.target
|
||||
|
|
@ -176,6 +176,9 @@ fi
|
|||
if systemctl --user daemon-reload 2>/dev/null; then
|
||||
systemctl --user enable gnome-restore.service
|
||||
systemctl --user enable night-light-reset.timer
|
||||
systemctl --user enable set-brightness-default.service
|
||||
systemctl --user enable set-keyboard-brightness-default.service
|
||||
systemctl --user enable set-volume-default.service
|
||||
systemctl --user enable xcape.service
|
||||
else
|
||||
echo "WARN: no systemd user session, units not enabled" >&2
|
||||
|
|
|
|||
20
.local/bin/set-brightness-default
Executable file
20
.local/bin/set-brightness-default
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
|
||||
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
|
||||
BRIGHTNESS="${DEFAULT_BRIGHTNESS:?unset, check ~/.config/environment.d/}"
|
||||
|
||||
|
||||
gdbus wait --session --timeout 10 org.gnome.SettingsDaemon.Power
|
||||
|
||||
gdbus call --session \
|
||||
--dest org.gnome.SettingsDaemon.Power \
|
||||
--object-path /org/gnome/SettingsDaemon/Power \
|
||||
--method org.freedesktop.DBus.Properties.Set \
|
||||
org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 $BRIGHTNESS>" \
|
||||
>/dev/null
|
||||
|
||||
|
||||
echo "Set screen brightness to $BRIGHTNESS"
|
||||
47
.local/bin/set-keyboard-brightness-default
Executable file
47
.local/bin/set-keyboard-brightness-default
Executable file
|
|
@ -0,0 +1,47 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
|
||||
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
|
||||
BRIGHTNESS="${DEFAULT_KEYBOARD_BRIGHTNESS:?unset, check ~/.config/environment.d/}"
|
||||
|
||||
|
||||
# Nothing to do on machines without a keyboard backlight
|
||||
KBD_DEVICE=
|
||||
for led in /sys/class/leds/*kbd_backlight*; do
|
||||
[ -e "$led" ] || break
|
||||
KBD_DEVICE=$led
|
||||
break
|
||||
done
|
||||
|
||||
if [ -z "$KBD_DEVICE" ]; then
|
||||
echo "No keyboard backlight, skipping"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
||||
kbd_set() {
|
||||
gdbus call --session \
|
||||
--dest org.gnome.SettingsDaemon.Power \
|
||||
--object-path /org/gnome/SettingsDaemon/Power \
|
||||
--method org.freedesktop.DBus.Properties.Set \
|
||||
org.gnome.SettingsDaemon.Power.Keyboard Brightness "<int32 $BRIGHTNESS>" \
|
||||
>/dev/null
|
||||
}
|
||||
|
||||
|
||||
gdbus wait --session --timeout 10 org.gnome.SettingsDaemon.Power
|
||||
|
||||
|
||||
# `gsd-power` owns its bus name before it has probed the keyboard
|
||||
# backlight, so at login the first write may silently be discarded
|
||||
kbd_set
|
||||
|
||||
if [ "$(cat "$KBD_DEVICE/brightness")" = "0" ]; then
|
||||
sleep 3
|
||||
kbd_set
|
||||
fi
|
||||
|
||||
|
||||
echo "Set keyboard brightness to $BRIGHTNESS"
|
||||
25
.local/bin/set-volume-default
Executable file
25
.local/bin/set-volume-default
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
|
||||
# Loaded from ~/.config/environment.d/50-defaults.conf or a local override
|
||||
VOLUME="${DEFAULT_VOLUME:?unset, check ~/.config/environment.d/}"
|
||||
|
||||
|
||||
i=0
|
||||
while ! wpctl status >/dev/null 2>&1; do
|
||||
i=$((i + 1))
|
||||
if [ "$i" -ge 20 ]; then
|
||||
echo "no PipeWire sink after 10s" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
|
||||
wpctl set-volume @DEFAULT_AUDIO_SINK@ "$VOLUME%"
|
||||
wpctl set-mute @DEFAULT_AUDIO_SINK@ 0
|
||||
|
||||
|
||||
echo "Set volume to $VOLUME%"
|
||||
Loading…
Reference in a new issue