-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacklight
More file actions
executable file
·133 lines (114 loc) · 2.75 KB
/
Copy pathbacklight
File metadata and controls
executable file
·133 lines (114 loc) · 2.75 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
#!/bin/bash
#
# Dank general Backlight Utility
#
#
# Notes: Use a flag to call displayCurrentValue instead of as part of arg parsing
# to avoid argument ordering issues.
#
# CMD Macro
argument_error() { echo "Unrecognized argument: $1"; echo "Try passing --help."; exit 1; };
# File and Directory aliases
BRIGHTNESS_FILE="/sys/class/backlight/intel_backlight/brightness";
# Data
ACTUAL_BRIGHTNESS_RANGE=(0 6000);
EASY_BRIGHTNES_RANGE=(0 10);
TARGET_VALUE=;
# Flags
USE_ACTUAL_VALUE=false;
function throwError() {
local ERROR_TYPE="$1"
case "$ERROR_TYPE" in
argument_count)
echo "No argument supplied. Try --help.";
exit 2;;
non_integer_argument)
echo "Non integer argument supplied.";
exit 2;;
zero_brightness)
echo "Brightness cannot be set to zero.";
exit 5;;
brightness_above_range)
echo "Simple brightness value must be 1 - 10.";
exit 5;;
*)
echo "Unknown Error.";
exit 1;;
esac;
}
function displayHelp() {
echo displayHelp called.;
exit "$1";
};
function assertIntegerArgument() {
local arg_value="$1";
if [[ "$arg_value" == +([0-9]) ]]; then
return 0;
fi;
echo "$arg_value did not pass integer assertion."
return 1;
};
function convertSimpleValue() {
local desired_brightness="$TARGET_VALUE";
if [ "$desired_brightness" == 0 ]; then
throwError "zero_brightness";
elif [ "$desired_brightness" -gt 10 ]; then
echo $desired_brightness
throwError "brightness_above_range";
else
TARGET_VALUE=$(( $desired_brightness * 600 ));
fi;
};
function setBrightnessValue() {
if [ -z "$TARGET_VALUE" ]; then
echo No brightness value supplied.;
displayHelp "1";
exit 1;
fi;
echo "$TARGET_VALUE" > "$BRIGHTNESS_FILE";
return 0;
};
function convertActualValue() {
local actual_value=$1
echo $(( "$actual_value" / 600 ));
return 0;
}
function displayCurrentValue() {
local brightness_value=$(cat $BRIGHTNESS_FILE);
local simple_value=$(convertActualValue "$brightness_value");
printf "Current backlight value: ";
if [ "$USE_ACTUAL_VALUE" == true ]; then
printf "$brightness_value\n";
return 0;
fi;
printf "$simple_value\n";
return 0;
};
function main() {
# Determine argument count
if [ ${#@} -eq 0 ]; then throwError "argument_count"; fi;
# Parse arguments
for argument in ${@}; do
case $argument in
-a|--actual-value)
USE_ACTUAL_VALUE=true;;
-c|--current-value)
displayCurrentValue;
exit 0;;
--help)
displayHelp;
exit 1;;
*)
assertIntegerArgument "$argument" && \
TARGET_VALUE="$argument" || \
throwError "non_integer_argument";;
esac;
done;
# Determine value type to use (actual or 'easy')
[ "$USE_ACTUAL_VALUE" == false ] && convertSimpleValue;
# Set brightness value
setBrightnessValue "$TARGET_VALUE";
return 0;
};
main ${@};
exit 0;