-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness
executable file
·105 lines (92 loc) · 2.59 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
#!/bin/zsh
zmodload zsh/mathfunc
# define some constants
HW_LOWEST=200; KEY_LOWEST='lowest'
HW_LOW=400; KEY_LOW='low'
HW_MEDIUM=1500; KEY_MEDIUM='Medium'
HW_HIGH=12000; KEY_HIGH='HIGH'
HW_MAX=24000; KEY_MAX='MAX'
# setup increase/decrease
KEY_INC='inc'
KEY_DEC='dec'
integer HW_STEP=$(( $HW_MAX * 0.1 ))
# output constants
if [[ $1 == 'keys' ]]; then
echo -e "$KEY_LOWEST\n$KEY_LOW\n$KEY_MEDIUM\n$KEY_HIGH\n$KEY_MAX"
exit 0
fi
# load hardware
source ~/.scripts/.display/.names
DEV='--device=intel_backlight'
# get inc/dec
HW_CUR=$(brightnessctl $DEV get)
if [[ $1 == $KEY_INC ]]; then
# treat low values
if [[ $HW_CUR -lt HW_LOWEST ]]; then
HW_REQ=$HW_LOWEST
elif [[ $HW_CUR -lt HW_LOW ]]; then
HW_REQ=$HW_LOW
elif [[ $HW_CUR -lt HW_MEDIUM ]]; then
HW_REQ=$HW_MEDIUM
# add 10%
else
integer HW_REQ=$(( floor(($HW_CUR + $HW_STEP) / $HW_STEP) * $HW_STEP ))
# do not exceed max
if [[ $HW_REQ -gt $HW_MAX ]]; then
HW_REQ=$HW_MAX
fi
fi
elif [[ $1 == $KEY_DEC ]]; then
# lower by 10%
if [[ $HW_CUR -gt $(( $HW_MEDIUM + $HW_STEP )) ]]; then
integer HW_REQ=$(( ceil(($HW_CUR - $HW_STEP * 1.0) / $HW_STEP) * $HW_STEP ))
# treat low values
elif [[ $HW_CUR -gt $HW_MEDIUM ]]; then
HW_REQ=$HW_MEDIUM
elif [[ $HW_CUR -gt $HW_LOW ]]; then
HW_REQ=$HW_LOW
else
HW_REQ=$HW_LOWEST
fi
# get requested value
elif [[ $1 == $KEY_LOWEST ]]; then
HW_REQ=$HW_LOWEST
elif [[ $1 == $KEY_LOW ]]; then
HW_REQ=$HW_LOW
elif [[ $1 == $KEY_MEDIUM ]]; then
HW_REQ=$HW_MEDIUM
elif [[ $1 == $KEY_HIGH ]]; then
HW_REQ=$HW_HIGH
elif [[ $1 == $KEY_MAX ]]; then
HW_REQ=$HW_MAX
fi
# get brightness percentage
if [[ $HW_REQ ]]; then # calculate from requested value
HW_BR=$HW_REQ
FRAC=$(( $HW_REQ / 24000.0 ))
integer BR=$(( rint( $FRAC * 100 ) ))
else # parse from input
BR=$(~/.scripts/.utils/parsepercent $1)
FRAC=$(($BR / 100.0))
integer HW_BR=$(( rint($FRAC * $HW_MAX) ))
fi
# if we can use laptop display backlight
if [[ $BR -ge 0 && $BR -le 100 ]]; then
# set xrandr to 100%
xrandr --output $M_LAPTOP --brightness 1
# use hardware
brightnessctl $DEV set $HW_BR
else
# set hardware to 100%
brightnessctl $DEV set $HW_MAX
# use xrandr
xrandr --output $M_LAPTOP --brightness $FRAC
fi
# external monitors (always using xrandr)
test $BR -le 100 && FRAC_EXT=$(( $FRAC ** 0.25 )) || FRAC_EXT=$FRAC
xrandr --output $M_HDMI --brightness $FRAC_EXT
xrandr --output $M_DL_LEFT --brightness $FRAC_EXT
xrandr --output $M_DL_RIGHT --brightness $FRAC_EXT
# notify
NOTIF_ID=42069 # arbitrary id (different for every kind of notification)
dunstify -u 0 -t 1500 -r $NOTIF_ID "Brightness $(~/.scripts/.utils/draw_bar $BR)"