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
|
#!/bin/sh
# File: /usr/bin/hwset-thinkpad-p50s
# Location: stackrpms-thinkpad-p50s package
# Author: bgstack15
# SPDX-License-Identifier: GPL-3.0
# Startdate: 2017-11-10 19:41:58
# Title: Script that Adjusts Hardware Settings
# Package: stackrpms-thinkpad-p50s
# Purpose: Provide programmatic ways to adjust screen brightness, volume, etc.
# History:
# 2018-12-10 change directory
# 2019-12-26 adapted for Thinkpad P50s and add bright_set max/min/safe
# 2022-11-03 adapted for package
# 2024-04-29 adapted for Acer Chromebook C720
# Usage:
# configure your display manager to react to key combinations, like vol-up to execute: hwset vol up
# Reference: ftemplate.sh 2017-11-10a; framework.sh 2017-11-10a
# Improve:
# Provide better 'screen 0' detection. Right now it is hard-coded to use display LVDS.
# Dependencies:
# dep-devuan: bgscripts-core
fiversion="2017-11-10a"
hwsetversion="2024-04-29a" # for Acer Chromebook C720
usage() {
less -F >&2 <<ENDUSAGE
usage: ${0} [-duV] [-c conffile] PIECE ACTION VALUE
version ${hwsetversion}
-d debug Show debugging info, including parsed variables.
-u usage Show this usage block.
-V version Show script version number.
-c conf Read in this config file.
PIECE vol | bright
ACTION up | down | set | mute
VALUE [vol: 0-100] [bright: 0.0-1.0]
Use this tool to adjust volume or brightness.
Examples:
hwset vol up
hweset vol mute
hwset bright set 0.8
Return values:
0 Normal
1 Help or version info displayed
2 Count or type of flaglessvals is incorrect
3 Incorrect OS type
4 Unable to find dependency
5 Not run as root or sudo
ENDUSAGE
}
# DEFINE FUNCTIONS
vol_up() {
# call: vol_down "${HWSET_SND_INCREMENT}"
local increment="${1}"
mixer vol +${increment}
}
vol_down() {
# call: vol_down "${HWSET_SND_INCREMENT}"
local increment="${1}"
mixer vol -${increment}
}
vol_set() {
# call: vol_set value
local value="${1}"
mixer vol "${value}"
}
vol_mute() {
# this function toggles the mute state between volume 0 and whatever it was before
# will ignore any parameters as they are unneeded
# get saved volume from temp file
local saved_level="$( awk 'BEGIN{FS="=";} /VOL_REGULAR_LEVEL=/{print $2}' "${HWSET_SND_TEMP_FILE}" 2>/dev/null )"
local current_level="$( mixer vol | awk '{print $NF}' )"
debuglev 3 && ferror "saved: ${saved_level}\tcurrent: ${current_level}"
# logic: if current level is other than "0:0", save current level and set to 0
# logic: if current level is "0:0", set to saved level.
if echo "${current_level}" | grep -qE "^0:0$";
then
# the current level is zero
if test -z "${saved_level}" || echo "${saved_level}" | grep -qvE "[0-9]+:[0-9]+";
then
# nothing to revert to, so set to a basic level
mixer vol 70
else
mixer vol "${saved_level}"
fi
else
# current level is not zero
echo "VOL_REGULAR_LEVEL=${current_level}" > "${HWSET_SND_TEMP_FILE}"
mixer vol "0:0"
fi
}
bright_get() {
#local this_screen="$( xrandr --listactivemonitors | tail -n +2 | grep LVDS | awk '{print $NF}' )"
#xrandr --verbose --screen 0 | grep Brightness | awk '{print $NF}'
cat /sys/class/backlight/intel_backlight/brightness
}
bright_safe() {
# call: new_brightness="$( bright_safe "${new_brightness}" )"
# this function makes sure the new brightness is not lower than the min and not higher than the max
local requested_brightness="${1}"
local output="$( echo "${requested_brightness}" | grep -oE '^-?[0-9]+(\.[0-9]{0,3})?$' )"
if test -n "${output}";
then
# so it is a proper decimal number for a brightness value
local lt_min="$( printf '%f<%f\n' "${requested_brightness}" "${HWSET_BRIGHTNESS_MIN}" | bc )"
local gt_max="$( printf '%f>%f\n' "${requested_brightness}" "${HWSET_BRIGHTNESS_MAX}" | bc )"
test "${gt_max}" = "1" && output="${HWSET_BRIGHTNESS_MAX}"
test "${lt_min}" = "1" && output="${HWSET_BRIGHTNESS_MIN}"
#echo "lt=${lt} gt=${gt}"
echo "${output%%.*}"
else
# invalid input, so provide a safe number
echo "${HWSET_BRIGHTNESS_MAX:850}"
fi
}
bright_up() {
# call: bright_up "${HWSET_BRIGHTNESS_INCREASE_SIZE}"
local current_brightness="$( bright_get )"
local increment="${1}"
local new_brightness="$( printf '%0.2f' "$( printf 'scale=3;%0.2f+%0.2f\n' "${current_brightness}" "${increment}" | bc )" )"
new_brightness="$( bright_safe "${new_brightness}" )"
debuglev 1 && ferror "current brightness:\"${current_brightness}\" new:\"${new_brightness}\""
printf "${new_brightness}" | sudo tee /sys/class/backlight/intel_backlight/brightness 1>/dev/null
#xrandr --output LVDS1 --brightness "${new_brightness}"
}
bright_down() {
# call: bright_down "${HWSET_BRIGHTNESS_INCREASE_SIZE}"
local current_brightness="$( bright_get )"
local increment="${1}"
local new_brightness="$( printf '%0.2f' "$( printf 'scale=3;%0.2f-%0.2f\n' "${current_brightness}" "${increment}" | bc )" )"
new_brightness="$( bright_safe "${new_brightness}" )"
debuglev 1 && ferror "current brightness:\"${current_brightness}\" new:\"${new_brightness}\""
printf "${new_brightness}" | sudo tee /sys/class/backlight/intel_backlight/brightness 1>/dev/null
#xrandr --output LVDS1 --brightness "${new_brightness}"
}
bright_set() {
# call: bright_set "${value}"
local value="${1}"
#xrandr --output LVDS1 --brightness "${value}"
local value_evaluated="$( echo "${value}" | tr '[a-z]' '[A-Z]' )"
case "${value_evaluated}" in
MAX*) test -n "${HWSET_BRIGHTNESS_MAX}" && value="${HWSET_BRIGHTNESS_MAX}" ;;
MIN*) test -n "${HWSET_BRIGHTNESS_MIN}" && value="${HWSET_BRIGHTNESS_MIN}" ;;
SAFE) test -n "${HWSET_BRIGHTNESS_SAFE}" && value="${HWSET_BRIGHTNESS_SAFE}" ;;
esac
debuglev 2 && ferror "setting to ${value}"
printf "${value}" | sudo tee /sys/class/backlight/intel_backlight/brightness 1>/dev/null
}
# DEFINE TRAPS
clean_hwset() {
# use at end of entire script if you need to clean up tmpfiles
#rm -f ${tmpfile} 1>/dev/null 2>&1
:
}
CTRLC() {
# use with: trap "CTRLC" 2
# useful for controlling the ctrl+c keystroke
:
}
CTRLZ() {
# use with: trap "CTRLZ" 18
# useful for controlling the ctrl+z keystroke
:
}
parseFlag() {
flag="$1"
hasval=0
case ${flag} in
# INSERT FLAGS HERE
"d" | "debug" | "DEBUG" | "dd" ) setdebug; ferror "debug level ${debug}";;
"u" | "usage" | "help" | "h" ) usage; exit 1;;
"V" | "fcheck" | "version" ) ferror "${scriptfile} version ${hwsetversion}"; exit 1;;
#"i" | "infile" | "inputfile" ) getval; infile1=${tempval};;
"c" | "conf" | "conffile" | "config" ) getval; conffile="${tempval}";;
esac
debuglev 10 && { test ${hasval} -eq 1 && ferror "flag: ${flag} = ${tempval}" || ferror "flag: ${flag}"; }
}
# DETERMINE LOCATION OF FRAMEWORK
while read flocation; do if test -e ${flocation} && test "$( sh ${flocation} --fcheck 2>/dev/null )" -ge 20170608; then frameworkscript="${flocation}"; break; fi; done <<EOFLOCATIONS
./framework.sh
${scriptdir}/framework.sh
~/bin/bgscripts/framework.sh
~/bin/framework.sh
~/bgscripts/framework.sh
~/framework.sh
/usr/local/bin/bgscripts/framework.sh
/usr/local/bin/framework.sh
/usr/bin/bgscripts/framework.sh
/usr/bin/framework.sh
/bin/bgscripts/framework.sh
/usr/local/share/bgscripts/framework.sh
/usr/share/bgscripts/framework.sh
/usr/libexec/bgscripts/framework.sh
EOFLOCATIONS
test -z "${frameworkscript}" && echo "$0: framework not found. Aborted." 1>&2 && exit 4
# INITIALIZE VARIABLES
# variables set in framework:
# today server thistty scriptdir scriptfile scripttrim
# is_cronjob stdin_piped stdout_piped stderr_piped sendsh sendopts
. ${frameworkscript} || echo "$0: framework did not run properly. Continuing..." 1>&2
infile1=
outfile1=
logfile=${scriptdir}/${scripttrim}.${today}.out
define_if_new interestedparties "bgstack15@gmail.com"
# SIMPLECONF
define_if_new default_conffile "/etc/hwset.conf"
define_if_new defuser_conffile ~/.config/hwset/hwset.conf
# REACT TO OPERATING SYSTEM TYPE
case $( uname -s ) in
Linux) [ ];;
*) echo "${scriptfile}: 3. Indeterminate or unsupported OS: $( uname -s )" 1>&2 && exit 3;;
esac
## REACT TO ROOT STATUS
#case ${is_root} in
# 1) # proper root
# [ ] ;;
# sudo) # sudo to root
# [ ] ;;
# "") # not root at all
# #ferror "${scriptfile}: 5. Please run as root or sudo. Aborted."
# #exit 5
# [ ]
# ;;
#esac
# SET CUSTOM SCRIPT AND VALUES
#setval 1 sendsh sendopts<<EOFSENDSH # if $1="1" then setvalout="critical-fail" on failure
#/usr/share/bgscripts/send.sh -hs # setvalout maybe be "fail" otherwise
#/usr/local/bin/send.sh -hs # on success, setvalout="valid-sendsh"
#/usr/bin/mail -s
#EOFSENDSH
#test "${setvalout}" = "critical-fail" && ferror "${scriptfile}: 4. mailer not found. Aborted." && exit 4
# VALIDATE PARAMETERS
# objects before the dash are options, which get filled with the optvals
# to debug flags, use option DEBUG. Variables set in framework: fallopts
validateparams piece action value - "$@"
# CONFIRM TOTAL NUMBER OF FLAGLESSVALS IS CORRECT
#if test ${thiscount} -lt 2;
#then
# ferror "${scriptfile}: 2. Fewer than 2 flaglessvals. Aborted."
# exit 2
#fi
# LOAD CONFIG FROM SIMPLECONF
# This section follows a simple hierarchy of precedence, with first being used:
# 1. parameters and flags
# 2. environment
# 3. config file
# 4. default user config: ~/.config/script/script.conf
# 5. default config: /etc/script/script.conf
if test -f "${conffile}";
then
get_conf "${conffile}"
else
if test "${conffile}" = "${default_conffile}" || test "${conffile}" = "${defuser_conffile}"; then :; else test -n "${conffile}" && ferror "${scriptfile}: Ignoring conf file which is not found: ${conffile}."; fi
fi
test -f "${defuser_conffile}" && get_conf "${defuser_conffile}"
test -f "${default_conffile}" && get_conf "${default_conffile}"
# CONFIGURE VARIABLES AFTER PARAMETERS
define_if_new HWSET_SND_INCREMENT=8
define_if_new HWSET_SND_MUTE_LEVEL=0
define_if_new HWSET_SND_TEMP_FILE=/tmp/hwset/snd.level
define_if_new HWSET_BRIGHTNESS_INCREMENT=85
define_if_new HWSET_BRIGHTNESS_MAX=$( cat /sys/class/backlight/intel_backlight/max_brightness )
define_if_new HWSET_BRIGHTNESS_MIN=0
define_if_new HWSET_BRIGHTNESS_SAFE=$( cat /sys/class/backlight/intel_backlight/max_brightness )
## REACT TO BEING A CRONJOB
#if test ${is_cronjob} -eq 1;
#then
# [ ]
#else
# [ ]
#fi
# SET TRAPS
#trap "CTRLC" 2
#trap "CTRLZ" 18
#trap "clean_hwset" 0
# DEBUG SIMPLECONF
debuglev 5 && {
ferror "Using values"
# used values: EX_(OPT1|OPT2|VERBOSE)
set | grep -iE "^HWSET_" 1>&2
}
# make temp files
if test -n "${HWSET_SND_TEMP_FILE}"
then
tempdir="$( dirname "${HWSET_SND_TEMP_FILE}" )"
mkdir -p "${tempdir}" && touch "${HWSET_SND_TEMP_FILE}"
fi
# MAIN LOOP
#{
debuglev 2 && ferror "piece:\"${piece}\" action:\"${action}\" value:\"${value}\""
case "${piece}" in
vol|volume)
case "${action}" in
up|down|mute)
debuglev 1 && ferror vol_${action} "${value:-$HWSET_SND_INCREMENT}"
vol_${action} "${value:-$HWSET_SND_INCREMENT}"
;;
set)
if test -z "${value}";
then
# you ran hwset vol set, without a value
# do nothing, silently
debuglev 1 && ferror "Cannot set vol to \"\". Skipped."
else
debuglev 1 && ferror vol_${action} "${value}"
vol_${action} "${value}"
fi
;;
*)
ferror "${scripttrim}: piece ${piece} was given unknown action ${action}. Aborted."
exit 2
;;
esac
;;
bright|brightness)
case "${action}" in
up|down)
debuglev 1 && ferror bright_${action} "${value:-${HWSET_BRIGHTNESS_INCREMENT}}"
bright_${action} "${value:-${HWSET_BRIGHTNESS_INCREMENT}}"
;;
set)
if test -z "${value}";
then
# you ran hwset bright set, without a value
# do nothing, silently
debuglev 1 && ferror "Cannot set brightness to \"\". Skipped."
else
debuglev 1 && ferror bright_${action} "${value}"
bright_${action} "${value}"
fi
;;
*)
ferror "${scripttrim}: piece ${piece} was given unknown action ${action}. Aborted."
;;
esac
;;
*)
ferror "${scripttrim}: Unknown piece ${piece}. Aborted."
exit 2
;;
esac
#} | tee -a ${logfile}
# EMAIL LOGFILE
#${sendsh} ${sendopts} "${server} ${scriptfile} out" ${logfile} ${interestedparties}
## STOP THE READ CONFIG FILE
#exit 0
#fi; done; }
|