-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrightness
executable file
·439 lines (366 loc) · 11.7 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
#
# i3-kb-backlight
#
# Keyboard backlight control and notifications for i3wm
#
# Dependencies:
# awk (POSIX compatible)
# bc
# upower
#
# Copyright (c) 2018 Beau Hastings. All rights reserved.
# License: GNU General Public License v2
#
# Author: Beau Hastings <beau@saweet.net>
# URL: https://github.com/hastinbe/i3-kb-brightness
get_brightness() {
backlight GetBrightness | awk '{print $2}'
}
get_max_brightness() {
backlight GetMaxBrightness | awk '{print $2}'
}
set_brightness() {
backlight SetBrightness "$(clamp "$1" 0 "$(get_max_brightness)")"
}
increase_brightness() {
local -r step=$1
local -r current=$(get_brightness)
local -r max=$(get_max_brightness)
local -r new=$(min "$max" $(( current + step )) )
set_brightness "$new"
}
decrease_brightness() {
local -r step=$1
local -r current=$(get_brightness)
local -r new=$(max 0 $(( current - step )) )
set_brightness "$new"
}
backlight() {
local -r method=$1
case "$method" in
GetBrightness)
dbus-send --type=method_call --system --print-reply=literal \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
"org.freedesktop.UPower.KbdBacklight.${method}"
;;
GetMaxBrightness)
dbus-send --type=method_call --system --print-reply=literal \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
"org.freedesktop.UPower.KbdBacklight.${method}"
;;
SetBrightness)
local value="$2"
dbus-send --type=method_call --system --print-reply=literal \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
"org.freedesktop.UPower.KbdBacklight.${method}" \
"int32:${value}"
;;
esac
}
min() {
echo "if ($1 < $2) $1 else $2" | bc
}
max() {
echo "if ($1 > $2) $1 else $2" | bc
}
clamp() {
min "$(max "$1" "$2")" "$3"
}
empty() {
[[ -z $1 ]]
}
command_exists() {
command -v "$1" >/dev/null 2>&1;
}
error() {
echo "$COLOR_RED$*$COLOR_RESET"
}
has_color() {
(( $(tput colors 2>/dev/null || echo 0) >= 8 )) && [ -t 1 ]
}
has_capability() {
[[ "${NOTIFY_CAPS[*]}" =~ $1 ]]
}
# Generates a progress bar for the provided value.
#
# Arguments:
# Percentage (integer) Percentage of progress.
# Maximum (integer) Maximum percentage. (default: 100)
# Divisor (integer) For calculating the ratio of blocks to progress (default: 5)
#
# Returns:
# The progress bar.
progress_bar() {
local -r percent=${1:?$(error 'Percentage is required')}
local -r max_percent=${2:-100}
local -r divisor=${3:-5}
local -r progress=$(((percent > max_percent ? max_percent : percent) / divisor))
printf -v bar "%*s" $progress ""
echo "${bar// /█}"
}
notify_brightness() {
local -r max=$(get_max_brightness)
local -r brightness=$(get_brightness)
local -r percent=$(echo "\
define pcnt(x,y) { \
auto s, var; \
s = scale; \
scale = 1; \
var = x / y * 100; \
scale = s; \
return (var); \
} scale = 0; pcnt($brightness,$max)/1" | bc -l)
local text="Brightness: ${percent}%"
if $SHOW_BRIGHTNESS_PROGRESS; then
local -r progress=$(progress_bar "$percent")
text="$text $progress"
fi
case "$NOTIFICATION_METHOD" in
xosd ) notify_brightness_xosd "$percent" "$text" ;;
herbe ) notify_brightness_herbe "$text" ;;
* ) notify_brightness_libnotify "$percent" "$ICON" "$text" ;;
esac
}
list_notification_methods() {
awk -W posix 'match($0,/^notify_brightness_([[:alnum:]]+)/) {print substr($0, 19, RLENGTH-18)}' "${BASH_SOURCE[0]}" || exit "$EX_USAGE"
exit "$EX_OK"
}
# Send notifcation for libnotify-compatible notification daemons.
#
# Arguments:
# Percent (integer) An integer indicating the brightness.
# Icon (string) Icon to display.
# Text (string) Notification text.
notify_brightness_libnotify() {
local -r percent=$1
local -r icon=${2:-$ICON}
local -r text=${*:3}
local -a args=(
-t "$EXPIRES"
)
local -a hints=(
# Replaces previous notification in some notification servers
string:synchronous:brightness
# Replaces previous notification in NotifyOSD
string:x-canonical-private-synchronous:brightness
)
# If we're not drawing our own progress bar, allow the notification daemon to draw its own (if supported)
if ! $SHOW_BRIGHTNESS_PROGRESS; then
hints+=(int:value:"$percent")
fi
(( ${#NOTIFY_CAPS[@]} < 1 )) && load_notify_server_caps
if has_capability icon-static; then
args+=(-i "$icon")
# haskell-notification-daemon (aka deadd-notification-center) does not support -i|--icon
hints+=(string:image-path:"$icon")
fi
if $USE_DUNSTIFY; then
args+=(-r 1000)
# Transient notifications will bypass the idle_threshold setting.
# Should be boolean, but Notify-OSD doesn't support boolean yet. Dunst checks
# for int and bool with transient so lets play nice with both servers.
hints+=(int:transient:1)
read -ra hints <<< "${hints[@]/#/-h }"
"${DUNSTIFY_PATH:+${DUNSTIFY_PATH%/}/}dunstify" "${hints[@]}" "${args[@]}" "$text"
else
read -ra hints <<< "${hints[@]/#/-h }"
"${NOTIFY_SEND_PATH:+${NOTIFY_SEND_PATH%/}/}notify-send" "${hints[@]}" "${args[@]}" "$text"
fi
}
# Send notification to XOSD.
#
# Arguments:
# Percent (integer) An integer indicating the brightness.
# Text (string) Notification text.
notify_brightness_xosd() {
local -r percent=$1
local -r text=${*:2}
local -r delay=$(ms_to_secs "$EXPIRES")
"${XOSD_PATH}osd_cat" --align center -b percentage -P "$percent" -d "$delay" -p top -A center -c "$COLOR_XOSD_TEXT" -T "$text" -O 2 -u "$COLOR_XOSD_OUTLINE" & disown
}
# Send notification to herbe.
#
# Arguments:
# Text (string) Notification text.
#
# Note: a patch with a notify-send script for herbe, not in the current version at this
# time but would make this irrelevant. See https://github.com/dudik/herbe/pull/10
notify_brightness_herbe() {
local -r text=$*
# Dismiss existing/pending notifications to prevent queuing
pkill -SIGUSR1 herbe
"${HERBE_PATH}herbe" "$text" & disown
}
# Rearrange all options to place flags first
# Author: greycat
# URL: https://mywiki.wooledge.org/ComplexOptionParsing
arrange_opts() {
local flags args optstr=$1
shift
while (($#)); do
case $1 in
--)
args+=("$@")
break;
;;
-*)
flags+=("$1")
if [[ $optstr == *"${1: -1}:"* ]]; then
flags+=("$2")
shift
fi
;;
*)
args+=("$1")
;;
esac
shift
done
OPTARR=("${flags[@]}" "${args[@]}")
}
parse_opts() {
local optstring=e:i:nN:phy
arrange_opts "$optstring" "$@"
set -- "${OPTARR[@]}"
OPTIND=1
while getopts "$optstring" opt; do
case "$opt" in
e ) EXPIRES=$OPTARG ;;
i ) ICON=$OPTARG ;;
n ) DISPLAY_NOTIFICATIONS=true ;;
N ) NOTIFICATION_METHOD=$OPTARG ;;
p ) SHOW_BRIGHTNESS_PROGRESS=true ;;
y ) USE_DUNSTIFY=true ;;
h | *) usage ;;
esac
done
read -ra CMDARGS <<< "${OPTARR[@]:$((OPTIND-1))}"
}
exec_command() {
IFS=' ' read -ra ARGS <<< "$1"
set -- "${ARGS[@]}"
COMMAND=${1:?$(error 'A command is required')}
shift
case "$COMMAND" in
increase)
increase_brightness "${1:-1}"
;;
decrease)
decrease_brightness "${1:-1}"
;;
notifications)
list_notification_methods
;;
*)
usage
;;
esac
}
# Converts milliseconds to seconds with rounding up
#
# Arguments:
# milliseconds (integer) An integer in milliseconds
ms_to_secs() {
echo $(( ($1 + (1000 - 1)) / 1000 ))
}
bootstrap() {
setup_cli_colors
}
setup_cli_colors() {
if has_color; then
COLOR_RESET=$'\033[0m'
COLOR_RED=$'\033[0;31m'
COLOR_GREEN=$'\033[0;32m'
COLOR_YELLOW=$'\033[0;33m'
COLOR_MAGENTA=$'\033[0;35m'
fi
}
usage() {
cat <<- EOF 1>&2
${COLOR_YELLOW}Usage:${COLOR_RESET} $0 [<options>] <command> [<args>]
Control keyboard backlight brightness.
${COLOR_YELLOW}Commands:${COLOR_RESET}
${COLOR_GREEN}increase <value>${COLOR_RESET} increase brightness
${COLOR_GREEN}decrease <value>${COLOR_RESET} decrease brightness
${COLOR_GREEN}notifications${COLOR_RESET} show available notification methods
${COLOR_GREEN}help${COLOR_RESET} display help
${COLOR_YELLOW}Options:${COLOR_RESET}
${COLOR_GREEN}-e <expires>${COLOR_RESET} expiration time of notifications in ms
${COLOR_GREEN}-i <icon>${COLOR_RESET} name of keyboard brightness icon
${COLOR_GREEN}-n${COLOR_RESET} enable notifications
${COLOR_GREEN}-N <method>${COLOR_RESET} notification method (${COLOR_MAGENTA}default: libnotify${COLOR_RESET})
${COLOR_GREEN}-p${COLOR_RESET} enable progress bar
${COLOR_GREEN}-h${COLOR_RESET} display help
EOF
exit "$EX_USAGE"
}
show_brightness_notification() {
$DISPLAY_NOTIFICATIONS || return
if empty "$NOTIFICATION_METHOD"; then
load_notify_server_info
NOTIFICATION_METHOD=$NOTIFY_SERVER
fi
notify_brightness
}
# Loads notification system information via DBus
load_notify_server_info() {
command_exists dbus-send || return
IFS=$'\t' read -r NOTIFY_SERVER _ _ _ < <(dbus-send --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.GetServerInformation | awk 'BEGIN { ORS="\t" }; match($0, /^ string ".*"/) {print substr($0, RSTART+11, RLENGTH-12)}')
}
# Load notification system capabilities via DBus
load_notify_server_caps() {
command_exists dbus-send || return
IFS= read -r -d '' -a NOTIFY_CAPS < <(dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_FDN}.GetCapabilities" | awk 'RS=" " { if (NR > 2) print $1 }')
}
post_command_hook() {
show_brightness_notification
}
main() {
# Getopt parsing variables
declare OPTIND
declare -a OPTARR CMDARGS
# Exit codes
declare -ir \
EX_OK=0 \
EX_USAGE=64
# DBUS constants
declare -r \
DBUS_NAME=org.freedesktop.Notifications \
DBUS_PATH=/org/freedesktop/Notifications \
DBUS_IFAC_FDN=org.freedesktop.Notifications
# Notification server information
declare \
NOTIFY_SERVER
# NOTIFY_VENDOR \
# NOTIFY_VERSION \
# NOTIFY_SPEC_VERSION
# Notification capabilities
declare -a NOTIFY_CAPS=()
declare \
COLOR_RESET \
COLOR_RED \
COLOR_GREEN \
COLOR_YELLOW \
COLOR_MAGENTA
# Notification colors
declare -r \
COLOR_XOSD_TEXT=${COLOR_OTHER:-#FFFFFF} \
COLOR_XOSD_OUTLINE=${COLOR_XOSD_OUTLINE:-#222222}
declare -l NOTIFICATION_METHOD
declare \
COMMAND \
ICON=keyboard-brightness
DISPLAY_NOTIFICATIONS=false \
SHOW_BRIGHTNESS_PROGRESS=false \
USE_DUNSTIFY=false
declare -i EXPIRES=1500
bootstrap
parse_opts "$@"
exec_command "${CMDARGS[*]}" && post_command_hook
exit "${EXITCODE:-$EX_OK}"
}
main "$@"