-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacklight.sh
executable file
·101 lines (87 loc) · 2.32 KB
/
backlight.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/backlight.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-10-28T06:58:26+0100
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# auth can be something like sudo -A, doas -- or nothing,
# depending on configuration requirements
auth="${EXEC_AS_USER:-sudo}"
message_title="Brightness"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to change backlight
Usage:
$script [-inc/-dec] [percent]
Settings:
[-inc] = increase in percent (0-100)
[-dec] = decrease in percent (0-100)
[percent] = how much percent to increase/decrease the brightness
Examples:
$script -inc 5
$script -dec 5"
backlight_dir=$( \
find /sys/class/backlight -type l \
| head -n1 \
)
notification() {
if [ "$1" -ge 100 ]; then
brightness=$(($1 / 100))
brightness=$(($2 / brightness))
else
brightness=$((100 / $1))
brightness=$(($2 * brightness))
fi
brightness=$((brightness /= $3))
brightness=$((brightness *= $3))
notify-send \
-t 2000 \
-u low \
"$message_title $brightness%" \
-h string:x-canonical-private-synchronous:"$message_title" \
-h int:value:"$brightness"
}
brightness() {
if [ "$1" -ge 0 ] \
&& [ "$1" -le 100 ]; then
max=$(cat "$backlight_dir/max_brightness")
actual=$(cat "$backlight_dir/actual_brightness")
percent=$((100 / $1))
factor=$((max / percent))
[ $factor -eq 0 ] \
&& factor=1
else
printf "%s\n" "$help"
exit 1
fi
}
set_brightness() {
$auth sh -c "printf \"%s\" \"$1\" > \"$backlight_dir/brightness\""
}
case "$1" in
-h | --help)
printf "%s\n" "$help"
;;
-inc)
brightness "$2"
value=$((actual + factor))
[ $value -ge "$max" ] \
&& value=$max
set_brightness "$value"
notification "$max" "$value" "$2"
;;
-dec)
brightness "$2"
value=$((actual - factor))
[ $value -le 0 ] \
&& value=0
set_brightness "$value"
notification "$max" "$value" "$2"
;;
*)
printf "%s\n" "$help"
exit 1
;;
esac