-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbrightness
112 lines (92 loc) · 3.06 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
#!/bin/bash
#===================================================================================
# BRIGHTNESS INC/DEC
# FILE: brightness
#
# DESCRIPTION: Inc/dec the brightness in small steps
#
# AUTHOR: Leonardo Marco
# VERSION: 1.0
# CREATED: 30.06.2018
# LICENSE: GNU General Public License v3.0
#===================================================================================
# CONFIG
bl_step=3 # Percent steps of inc/dec
bl_min=3 # Minium percent brightness
bl_default="30" # Default percent
bl_dir="" # Dir for control backlight
install_path="/usr/bin/brightness" # Installation dir
#=== FUNCTION ==================================================================
# NAME: help
# DESCRIPTION: Show command help
#===============================================================================
function help() {
echo -e 'Inc/dec the brightness
Usage: '$(basename $0)' -inc|-dec|-h|-I|-U
\e[1m-h\e[0m\tShow command help
\e[1m-def\e[0m\tSet brightness to '$bl_default'% (may be config in '$0')
\e[1m-inc\e[0m\tIncrease the brightness in '$bl_step'% (may be config in '$0')
\e[1m-dec\e[0m\tDecrease the brightness in '$bl_step'% (may be config in '$0')
\e[1m-I\e[0m\tInstall the script
'
exit 0
}
function install() {
[ "$(id -u)" -ne 0 ] && echo "Install must be run as root" && exit 1
[ "$install_path" = "$(readlink -f $0)" ] || sudo cp -v "$0" "$install_path"
chmod +x "$install_path"
addgroup video 2> /dev/null
echo 'ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="'$(basename "$bl_dir")'", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="'$(basename "$bl_dir")'", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"' | tee /etc/udev/rules.d/backlight.rules
echo "Add useres to video group for give brightness change permisions"
}
function set_brightness() {
[ "$1" -eq "$1" ] &> /dev/null || return
bl_max="$(cat "$bl_dir/max_brightness")"
bl_min="$(($bl_max*$bl_min/100))"
if [ "$bl_next" -ge "$bl_max" ]; then
bl_next="$bl_max"
elif [ "$bl_next" -lt "$bl_min" ]; then
bl_next="$bl_min"
fi
echo "$bl_next" > "$bl_dir/brightness"
}
function change_brightness() {
bl_current="$(cat "$bl_dir/brightness")"
bl_max="$(cat "$bl_dir/max_brightness")"
case "$1" in
"-def")
bl_next="$(($bl_max*$bl_default/100))"
;;
"-inc"|"-dec")
bl_step="$(($bl_max*$bl_step/100))"
[ "$1" = "-dec" ] && bl_step="-$bl_step"
bl_next="$(($bl_step+$bl_current))"
esac
set_brightness "$bl_next"
}
function check_bldir () {
[ ! -d "$bl_dir" ] && bl_dir=$(ls /sys/class/backlight)
if [ "$(echo "$bl_dir" | wc -l)" -gt 1 ]; then
echo "ERROR: too many directories in /sys/class/backlight"
echo "Please edit $0 and config bl_dir variable with brightness directory"
exit 1
elif [ ! -d "$bl_dir" ]; then
echo "ERROR: directory $bl_dir dont exist"
echo "Please edit $0 and config bl_dir variable with brightness directory"
exit 1
fi
}
case "$1" in
-I)
check_bldir
install
;;
-dec|-inc|-def)
check_bldir
change_brightness "$1"
;;
*)
help
;;
esac