-
Notifications
You must be signed in to change notification settings - Fork 9
/
lux.sh
executable file
·261 lines (231 loc) · 8.12 KB
/
lux.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh
#
# Copyright (c) 2020 Thomas "Ventto" Venriès <thomas.venries@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
usage() {
echo 'Usage: lux OPERATION [-c CONTROLLER_NAME] [-m MIN] [-M MAX]
Brightness option values are positive integers.
Percent mode, add "%" after values (operation options only).
Without option, it prints controller name and brightness info.
Information:
-h Prints this help and exits
-v Prints version info and exits
Thresholds (must be used in conjunction with an operation):
-m MIN
Set the brightness MIN (raw value)
-M MAX
Set the brightness MAX (raw value)
Operations (with percent mode):
-a VALUE[%]
Increase the brightness VALUE
-s VALUE[%]
Subtract the brightness VALUE
-S VALUE[%]
Set the brightness VALUE (thresholds will be ignored)
-g Print the current brightness raw value
-G Print the current brightness percentage
Controllers:
-c CONTROLLER_NAME
Set the controller to use.
Use any CONTROLLER_NAME in /sys/class/backlight.
'
}
version() {
echo 'Lux 1.21
Copyright (C) 2020 Thomas "Ventto" Venriès.
License MIT
<https://opensource.org/licenses/MIT>.
'
}
no_conjunction() {
while [ "$#" -ne 0 ]; do $1 && return 1; shift; done
}
arg_err() {
usage; exit 2
}
is_positive_int() {
echo "${1}" | grep -E '^[0-9]+$' >/dev/null 2>&1 || return 1
}
is_percentage() {
echo "${1}" | grep -E '%$' >/dev/null 2>&1 || return 1
}
check_perm() {
_ctrl="${1}"
udev_rule="/etc/udev/rules.d/99-lux.rules"
if [ ! -w "${_ctrl}/brightness" ] ; then
if [ "$(id -u)" -ne 0 ]; then
if ! id -nG "${USER}" | grep video >/dev/null 2>&1; then
echo "Use sudo once to setup group permissions,"
echo "to access to controller's brightness from user."
else
echo "To setup the group permissions permanently, you need to logout/login."
fi
exit
fi
fi
if [ "$(id -u)" -eq 0 ]; then
if ! cut -d: -f1 /etc/group | grep video >/dev/null 2>&1; then
echo "Group ~video~ does not exist."
exit 1
fi
if [ -z "$(find "${_ctrl}" -name 'brightness' -group video)" ]; then
if [ ! -f "${udev_rule}" ] ; then
echo "${udev_rule}: missing file."
exit 1
fi
udevadm control -R
udevadm trigger -c add -s backlight
fi
if ! id -nG "${SUDO_USER}" | grep video >/dev/null 2>&1; then
usermod -a -G video "${SUDO_USER}"
echo "User has been added to ~video~ group."
echo "To setup the group permissions permanently, you need to logout/login."
exit
fi
fi
}
main() {
gFlag=false
mFlag=false
MFlag=false
cFlag=false
aFlag=false
sFlag=false
SFlag=false
percent_mode=false
while getopts 'hvgGm:M:c:a:s:S:' opt; do
case $opt in
h) usage ; exit;;
v) version; exit;;
g|G) ! no_conjunction "${sFlag}" "${SFlag}" "${aFlag}" && arg_err
[ "$opt" = 'G' ] && percent_mode=true
gFlag=true
;;
m) ! is_positive_int "$OPTARG" && arg_err
mFlag=true
mArg="$OPTARG"
;;
M) ! is_positive_int "$OPTARG" && arg_err
MFlag=true
MArg="$OPTARG"
;;
c) controller_path="/sys/class/backlight/$OPTARG"
if [ ! -d "${controller_path}" ] ; then
echo "${controller_path}: controller not found."
exit
fi
[ "$#" -eq 2 ] && arg_err
cFlag=true
cArg="${controller_path}"
;;
a) ! no_conjunction "${sFlag}" "${SFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
fi
! is_positive_int "$OPTARG" && arg_err
aFlag=true
valArg="$OPTARG"
;;
s) ! no_conjunction "${aFlag}" "${SFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
fi
! is_positive_int "$OPTARG" && arg_err
sFlag=true
valArg="$OPTARG"
;;
S) ! no_conjunction "${aFlag}" "${sFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
fi
! is_positive_int "$OPTARG" && arg_err
SFlag=true
valArg="$OPTARG"
;;
\?) usage; exit 2;;
:) usage; exit 2;;
esac
done
if ${cFlag} ; then
best_controller=${cArg}
best_max=$(cat "${best_controller}/max_brightness")
# Try to find the best-max-value controller
else
best_max=-1
best_controller=''
for ctrl in /sys/class/backlight/*; do
[ ! -d "${ctrl}" ] && break
[ ! -r "${ctrl}/max_brightness" ] && break
max="$(cat "${ctrl}/max_brightness")"
if [ "${best_max}" -lt "${max}" ] ; then
best_max="${max}"
best_controller="${ctrl}"
fi
done
if [ -z "${best_controller}" ]; then
echo "No backlight controller detected"
exit
fi
fi
check_perm "${best_controller}"
file="${best_controller}/brightness"
brightness=$(cat "${file}")
best_max=$((best_max - 1))
# Display controller information if no argument
if [ "$#" -eq 0 ] ; then
echo "${best_controller} 0;${brightness};${best_max}"
exit
fi
if ${gFlag}; then
${percent_mode} && { echo "$((brightness * 100 / best_max))%"; exit; }
echo "$brightness"
exit
fi
if ${SFlag} ; then
${percent_mode} && valArg=$(( best_max * valArg / 100 ))
[ "$valArg" -lt 0 ] && valArg=0
[ "$valArg" -gt "$best_max" ] && valArg="${best_max}"
echo "${valArg}" | tee "${file}"
exit
fi
${mFlag} && own_min="${mArg}" || own_min=0
${MFlag} && own_max="${MArg}" || own_max="${best_max}"
if [ $(( own_max - own_min )) -le 0 ] || \
[ $(( own_max - own_min )) -gt "${best_max}" ] ; then
arg_err
fi
[ "$brightness" -lt "$own_min" ] && brightness="${own_min}"
[ "$brightness" -gt "$own_max" ] && brightness="${own_max}"
${percent_mode} && valArg=$(( (own_max - own_min) * valArg / 100 ))
if ${aFlag} ; then
value=$(( brightness + valArg ))
[ "$value" -gt "$own_max" ] && value="${own_max}"
echo "${value}" | tee "${file}"
elif ${sFlag} ; then
value=$(( brightness - valArg ))
[ "$value" -lt "$own_min" ] && value="${own_min}"
echo "${value}" | tee "${file}"
else
arg_err
fi
}
main "$@"