-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreen_brightness_ctl.sh
More file actions
executable file
·61 lines (52 loc) · 1.53 KB
/
screen_brightness_ctl.sh
File metadata and controls
executable file
·61 lines (52 loc) · 1.53 KB
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
#!/bin/sh
### Sets the brightness of external monitors.
### Just pass the number and it sets the same value on all detected monitors.
### The previous brightness value for each monitor is stored on a cache file and
### can be restored by the `restore` command. The `lower` command sets all monitors
### to a pre-defined low value (default 10).
# cache file
LOCK_FILE="/tmp/screen_brightness.lock"
CACHE_FILE="$HOME/.cache/ddcutil_brightness_restore"
LOW_BRIGHTNESS="10"
set_brightness_task() {
newBrightness="$1"
monitorId="$2"
currentBrightness="$(ddcutil getvcp 10 -d "$monitorId" | grep -o 'current value = *[0-9]*' | grep -o '[0-9]*')"
# save brightness
echo "$monitorId-$currentBrightness" >>"$CACHE_FILE"
# lower brightness
ddcutil setvcp 10 "$newBrightness" -d "$monitorId"
}
set_brightness() {
newBrightness="$1"
# clear cache
printf "" >"$CACHE_FILE"
ddcutil detect | grep Display | cut -d' ' -f2 | while read -r monitorId; do
set_brightness_task "$newBrightness" "$monitorId"
done
}
# wait pending jobs (acquire lock)
while ! mkdir -- "$LOCK_FILE" >/dev/null 2>&1; do
sleep 1
done
case "$1" in
"lower")
set_brightness "$LOW_BRIGHTNESS"
;;
"restore")
while read -r line; do
monitorId="$(echo "$line" | cut -d'-' -f1)"
brightness="$(echo "$line" | cut -d'-' -f2)"
# restore brightness
ddcutil setvcp 10 "$brightness" -d "$monitorId"
done <"$CACHE_FILE"
;;
[0-9] | [0-9][0-9] | [0-9][0-9][0-9])
set_brightness "$1"
;;
*)
echo "Unknown command: $1"
;;
esac
# release lock
rmdir "$LOCK_FILE"