-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrightness
executable file
·125 lines (108 loc) · 3.52 KB
/
brightness
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env sh
#
# Saumon i3blocks scripts
# brightness (brightness control for laptops and external monitors)
# Dependencies: `brightnessctl` (setuid) for laptops
# `ddcutil` for desktop external monitors
# We could use brightnessctl with the ddcci kernel module, but it does not work on <6.3 as of 2023-05, so here we are
# with ddcutil calls. It might be faster anyway though.
#
# Chassis types: https://www.dmtf.org/standards/SMBIOS
# 3 is Desktop
if [ "$(cat /sys/class/dmi/id/chassis_type)" = "3" ]; then
if ! command -v ddcutil >/dev/null; then
echo " !dep "
exit 33
fi
# Initialise cache
[ -e "$XDG_CACHE_HOME/ddcutil_detect" ] || ddcutil --brief detect | grep -v "should not exist" > "$XDG_CACHE_HOME/ddcutil_detect"
# Match monitor name to I2C bus number
monitor_name=${desktop_monitor:-"Monitor"}
bus_number=$(grep -C2 "$monitor_name" "$XDG_CACHE_HOME/ddcutil_detect" | grep i2c | head -n1 | cut -d'-' -f2)
command="ddcutil --bus $bus_number"
case $BLOCK_BUTTON in
2) # middle click: toggle night/day brightness
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
if [ "$current" -le 20 ]; then
if [ "$BLOCK_INSTANCE" -eq 6 ]; then
new=100
else
new=60
fi
else
if [ "$BLOCK_INSTANCE" -eq 6 ]; then
new=20
else
new=0
fi
fi
;;
3) # right click: set min/full brightness depending on context
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
if [ "$current" -ge 50 ]; then
new=100
else
new=1
fi
;;
4) # scroll up: increase brightness by 20%
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
if [ "$current" -ge 80 ]; then
new=100
else
new=$(expr $current + 20)
fi
;;
5) # scroll down: decrease brightness by 20%
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
if [ "$current" -le 20 ]; then
new=0
else
new=$(expr $current - 20)
fi
;;
esac
if [ -n "$new" ]; then
printf " <span fallback='true'> </span>%s<span font_size='small'>%%</span> \n" "$new"
$command setvcp 10 "$new" --noverify > /dev/null &
exit 0
fi
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
# When ddcutil fails, it returns "DDC communication failed for monitor on bus /dev/i2c-X"
# In that case, retry
if [ "$current" = "for" ]; then
current=$($command getvcp 10 --brief | tail -n1 | awk '{print $4}')
fi
# If we still don't have anything, give up, but at least hide the "for"
if [ "$current" = "for" ]; then
current=""
fi
else
if ! command -v brightnessctl >/dev/null; then
echo " !dep "
exit 33
fi
command="brightnessctl -m"
case $BLOCK_BUTTON in
3) # right click: toggle min/full brightness
rounded=$($command | cut -d ',' -f4 | tr -d '%')
if [ "$rounded" -ge 50 ]; then
command="brightnessctl -m set 100%"
else
command="brightnessctl -m set 1"
fi
;;
4) # scroll up: increase brightness by 2%
command="brightnessctl -m set -m +2%"
;;
5) # scroll down: decrease brightness by 2%
command="brightnessctl -m set -m 2%-"
;;
esac
current=$($command | cut -d ',' -f4 | tr -d '%')
fi
if [ -z "$current" ]; then
printf " <span fallback='true'> </span>?? \n"
exit 0
fi
printf " <span fallback='true'> </span>$current<span font_size='small'>%%</span> \n"