-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkeyboard-backlight.sh
executable file
·89 lines (78 loc) · 1.98 KB
/
keyboard-backlight.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
#!/bin/sh
#
# Originally written by Francisco Diéguez Souto ([email protected])
# This script is licensed under MIT License.
#
# This program just modifies the value of backlight keyboard for Apple Laptops
# You must run it as root user or via sudo.
# As a shortcut you could allow to admin users to run via sudo without password
# prompt. To do this you must add sudoers file the next contents:
#
# Cmnd_Alias CMDS = /usr/local/bin/keyboard-backlight.sh
# %admin ALL = (ALL) NOPASSWD: CMDS
#
# After this you can use this script as follows:
#
# Increase backlight keyboard:
# $ sudo keyboard-backlight.sh up
# Decrease backlight keyboard:
# $ sudo keyboard-backlight.sh down
# Increase to total value backlight keyboard:
# $ sudo keyboard-backlight.sh total
# Turn off backlight keyboard:
# $ sudo keyboard-backlight.sh off
#
# You can customize the amount of backlight by step by changing the INCREMENT
# variable as you want it.
#
BACKLIGHT="/sys/class/leds/smc::kbd_backlight/brightness" # TBD
BRIGHTNESS=$(cat ${BACKLIGHT})
INCREMENT=20
if [ "$(id -ru)" -ne 0 ]; then
echo "Please run this program as superuser!"
exit 1
fi
die() {
echo "Brightness is already $1"
exit 1
}
case $1 in
up)
# BRIGHTNESS will be capped at 255 anyway
if [ "${BRIGHTNESS}" -lt 255 ]; then
echo $(( BRIGHTNESS + INCREMENT )) > ${BACKLIGHT}
else
die "${BRIGHTNESS}"
fi
;;
down)
if [ "${BRIGHTNESS}" -gt 0 ]; then
VALUE=$(( BRIGHTNESS - INCREMENT ))
# BRIGHTNESS cannot be negative
[ "$VALUE" -lt 0 ] && VALUE=0
echo $VALUE > ${BACKLIGHT}
else
die "${BRIGHTNESS}"
fi
;;
total)
echo 255 > ${BACKLIGHT}
;;
off)
echo 0 > ${BACKLIGHT}
;;
[0-9]*)
VALUE=$1
if [ "$VALUE" -ge 0 ] && [ "$VALUE" -le 255 ]; then
echo "$VALUE" > ${BACKLIGHT}
else
echo "Invalid argument ($VALUE). Please provide a value from 0 to 255!"
exit 1
fi
;;
*)
echo "Use: $(basename "$0") [up|down|total|off|value]"
exit 1
;;
esac
echo "Brightness set to $(cat ${BACKLIGHT})"