-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibbacklight.sh
More file actions
executable file
·141 lines (130 loc) · 3.91 KB
/
Copy pathlibbacklight.sh
File metadata and controls
executable file
·141 lines (130 loc) · 3.91 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
#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
if [ -z "$HAS_UTILS" ]; then
. ./libutils.sh
fi
# Making this work with external displays requires the usage of the
# ddcci-driver-linux kernel module
sysfs_path="/sys/class/backlight/*/brightness"
# type: int const
# def: min_brightness=0
# description:
# The lowest brightness value.
min_brightness=0
# type: int const
# def: 255
# description:
# The maximum range for brightness.
# All brightness adjustments are done in the range between min_brightness
# and this value, meaning a range of 0 to 255 inclusive.
# The actual value written to each backlight device is scaled according to
# the device's max_brightness range.
max_brightness=255
# return type: int
# usage: get_scaled brightness_file
# description:
# will return the scaled brightness value from the given brightness_file
get_scaled () {
brightness_file="$1"
brightness_path="${brightness_file%/*}"
brighntess_max_file="${brightness_path}/max_brightness"
max_val=$(cat "$brighntess_max_file")
# remove float part if any
max_val="${max_val%.*}"
value=$(cat "$brightness_file")
# remove float part if any
value="${value%.*}"
scaled_value=$(unscale_val "$value" "$max_val" "$max_brightness" )
printf '%d' "$scaled_value"
}
# return type: int
# usage: get_brightness
# description:
# will return the current brightness value
# brightness values are from 0 to 255
get_brightness() {
c=0
# get brightness just from the first screen we can find
for screen_path in $sysfs_path; do
out=$(get_scaled "$screen_path")
c=$(( c + 1 ))
[ "$c" -gt 0 ] && break
done
printf '%d\n' "$out"
}
# return type: void
# usage: set_scaled brightness_file value
# description:
# will write the scaled brightness value onto the brightness_file
set_scaled () {
brightness_file="$1"
brightness_path="${brightness_file%/*}"
brighntess_max_file="${brightness_path}/max_brightness"
if [ -r "$brighntess_max_file" ]; then
max_val=$(cat "$brighntess_max_file")
else
max_val="$max_brightness"
fi
# remove float part if any
max_val="${max_val%.*}"
value="$2"
scaled_value=$(scale_val "$value" "$max_brightness" "$max_val")
if [ -w "$brightness_file" ]; then
printf '%s' "$scaled_value" > "$brightness_file"
if [ -n "$dbgOUT" ] || [ -n "$VERB" ]; then
printf '[%s: %6d] ' "actual value" "$scaled_value"
fi
fi
}
# return type: void
# usage: set_brightness num
# description:
# will set the brightness to the passed number
# brightness values are from 0 to 255
set_brightness() {
val="$1"
perc_val="$(int_to_perc "$val" "$max_brightness")"
if [ -n "$dbgOUT" ] || [ -n "$VERB" ]; then
printf '%s %3d (%s) ' "brightness level:" "$val" "${perc_val}%"
fi
# set brightness for every screen we can find
for screen_path in $sysfs_path; do
if [ -n "$dbgOUT" ]; then
scp_t="${screen_path%/*}"
scp_t="${scp_t##*/}"
printf '%s ' "$scp_t"
fi
set_scaled "$screen_path" "$val"
done
if [ -n "$dbgOUT" ] || [ -n "$VERB" ]; then
printf '\n'
fi
}
# return type: void
# usage: inc_brightness num
# description:
# will increase the brightness by the passed number
# brightness values are from 0 to 255
inc_brightness() {
delta="$1"
cur_br=$(get_brightness)
new_br=$(( cur_br + delta ))
if [ "$max_brightness" -lt "$new_br" ]; then
new_br="$max_brightness"
fi
set_brightness "$new_br"
}
# return type: void
# usage: dec_brightness num
# description:
# will decrease the brightness by the passed number
# brightness values are from 0 to 255
dec_brightness() {
delta="$1"
cur_br=$(get_brightness)
new_br=$(( cur_br - delta ))
if [ "$min_brightness" -gt "$new_br" ]; then
new_br="$min_brightness"
fi
set_brightness "$new_br"
}