-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_keyboard_backlight
More file actions
executable file
·170 lines (146 loc) · 3.81 KB
/
change_keyboard_backlight
File metadata and controls
executable file
·170 lines (146 loc) · 3.81 KB
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
#!/bin/bash
#
# Increase/decrease keyboard backlight.
#
# Dependencies: coreutils, dbus, upower, util-linux
set -o nounset -o pipefail
readonly DBUS_CONNECTION="org.freedesktop.UPower"
readonly DBUS_INTERFACE="${DBUS_CONNECTION}.KbdBacklight"
readonly DBUS_OBJECT_PATH="/org/freedesktop/UPower/KbdBacklight"
readonly L_OPTS="help,version"
readonly OPTS="hVv"
readonly CMD="${0##*/}"
readonly USAGE="${CMD} [OPTION]... +|-"
readonly USAGE_ERR_MSG=$(
cat <<END
Usage: ${USAGE}
Try '${CMD} --help' for more information.
END
)
readonly VERSION_INFO=$(
cat <<END
${CMD} (scripts) 1.0.0
This is free and unencumbered software released into the public domain.
For more information, please refer to <https://unlicense.org/>.
END
)
readonly HELP=$(
cat <<END
SYNOPSIS
${USAGE}
DESCRIPTION
Increase (+) or decrease (-) keyboard backlight.
OPTIONS
-h, --help
display this help and exit
-V, -v, --version
display version and exit
END
)
#######################################
# Get the integer value of kbd backlight via dbus
# Globals:
# DBUS_CONNECTION - name of the dbus connection to receive the message
# DBUS_INTERFACE - name of the D-Bus interface
# DBUS_OBJECT_PATH - path to the D-Bus object
# Arguments:
# method - dbus interface method (e.g., GetBrightness)
#######################################
function get_dbus_kbd_backlight() {
local -r method="$1"
dbus-send --print-reply="literal" --system \
--dest="${DBUS_CONNECTION}" \
"${DBUS_OBJECT_PATH}" "${DBUS_INTERFACE}.${method}" \
| cut --delimiter " " --fields 5
}
#######################################
# Set the integer value of kbd backlight via dbus
# Globals:
# DBUS_CONNECTION - name of the dbus connection to receive the message
# DBUS_INTERFACE - name of the D-Bus interface
# DBUS_OBJECT_PATH - path to the D-Bus object
# Arguments:
# brightness - int32 value to set kbd brightness to
#######################################
function set_dbus_kbd_backlight() {
local -ir brightness="$1"
dbus-send --system --type=method_call \
--dest="${DBUS_CONNECTION}" \
${DBUS_OBJECT_PATH} "${DBUS_INTERFACE}.SetBrightness" \
"int32:${brightness}"
}
#######################################
# Main function
# Globals:
# CMD - script basename
# HELP - help message
# L_OPTS - long non-positional options (e.g. --help)
# OPTS - short non-positional options (e.g. --h)
# USAGE_ERR_MSG - usage error message
#######################################
function main() {
local opts
opts=$(
getopt \
--options=${OPTS} --longoptions=${L_OPTS} \
--name "${CMD}" \
-- "$@"
)
if (($? != 0)); then
echo "${USAGE_ERR_MSG}"
exit 2
fi
eval set -- "${opts}"
local need_help=false
local need_version=false
while true; do
case "$1" in
-h | --help)
need_help=true
shift
break
;;
-V | -v | --version)
need_version=true
shift
break
;;
--)
shift
break
;;
esac
done
if [[ $need_help == true ]]; then
echo "${HELP}"
exit 0
fi
if [[ $need_version == true ]]; then
echo "${VERSION_INFO}"
exit 0
fi
if (($# != 1)) || [[ $1 != "+" && $1 != "-" ]]; then
if (($# != 1)); then
echo "${CMD}: expected 1 argument; got $#"
else
echo "${CMD}: expected '+' or '-'; got $1"
fi
echo "${USAGE_ERR_MSG}"
exit 2
fi
local -ir min_brightness=0
local -ir max_brightness=$(get_dbus_kbd_backlight "GetMaxBrightness")
local -i brightness=$(get_dbus_kbd_backlight "GetBrightness")
if [[ $1 == "+" ]]; then
((brightness += 1))
else
((brightness -= 1))
fi
if ((brightness > max_brightness)); then
brightness=max_brightness
elif ((brightness < min_brightness)); then
brightness=min_brightness
fi
set_dbus_kbd_backlight "${brightness}"
}
main "$@"