-
Notifications
You must be signed in to change notification settings - Fork 114
/
configure.sh
executable file
·171 lines (139 loc) · 3.56 KB
/
configure.sh
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
#!/bin/bash
# Written by CodeCanna, refined by atar-axis
cd "$(dirname "$0")" || exit 1
set -o posix
NAME="$0"
# Define Variables
MODULE="/sys/module/hid_xpadneo/"
PARAMS="/sys/module/hid_xpadneo/parameters"
CONF_FILE=$(grep -sl '^options hid_xpadneo' /etc/modprobe.d/*.conf | tail -1)
: "${CONF_FILE:="/etc/modprobe.d/99-xpadneo-options.conf"}"
# Use getopt NOT getopts to parse arguments.
OPTS=$(getopt -n "$NAME" -o h:m:r:n: -l help,version,trigger-rumble-mode:,rumble-attenuation:,ff_connect_notify: -- "$@")
## Print Help ##
function display_help {
cat ./docs/config_help
}
## Parameter Validation ##
function check_param {
key=$1
value=$2
case $key in
"trigger_rumble_mode")
if [[ "$value" -gt 2 ]] || [[ "$value" -lt 0 ]];
then
echo "$NAME: $key: Invalid value! Value must be between 0 and 2."
exit 1
fi
;;
"rumble_attenuation")
if [[ "$value" -gt 100 ]] || [[ "$value" -lt 0 ]];
then
echo "$NAME: $key: Invalid value! Value must be between 0 and 100."
exit 1
fi
;;
"ff_connect_notify")
if [[ "$value" -gt 2 ]] || [[ "$value" -lt 0 ]];
then
echo "$NAME: $key: Invalid value! Value must be 0 or 1."
exit 1
fi
;;
*)
# key not known, should not be possible
exit 2
;;
esac
}
## Parameter Setting Helpers ##
function set_modprobe_param {
sed -i "/^[[:space:]]*options[[:space:]]\+hid_xpadneo/s/$1=[^[:space:]]*/$1=$2/g" "$CONF_FILE"
}
function set_sysfs_param {
echo "$2" > "$PARAMS/$1"
}
## Parameter Setting ##
function set_param {
key=$1
value=$2
# check for valid parameters first
check_param "$key" "$value"
# edit sysfs parameter if module is inserted
if [[ -d "$MODULE" ]];
then
echo "$NAME: Module inserted - writing to $PARAMS"
if ! set_sysfs_param "$key" "$value";
then
echo "$NAME: ERROR! Could not write to $PARAMS!"
exit 1
fi
fi
# edit modprobe config file
if ! set_modprobe_param "$key" "$value";
then
echo "$NAME: ERROR! Could not write to $CONF_FILE!"
exit 1
fi
echo "$NAME: $key: parameter written to $CONF_FILE"
}
## Argument Parsing ##
function parse_args {
if [[ $1 == "" ]];
then
display_help
exit 1
fi
if ! grep -sq 'options hid_xpadneo' "${CONF_FILE}";
then
# If line doesn't exist echo all of the defaults.
mkdir -p "$(dirname "${CONF_FILE}")"
touch "${CONF_FILE}"
echo "options hid_xpadneo disable_deadzones=0 rumble_attenuation=0 trigger_rumble_mode=0 ff_connect_notify=1" >> "$CONF_FILE"
fi
eval set -- "$OPTS"
while true;
do
case "$1" in
-h | --help)
display_help
shift
;;
-m | --trigger-rumble-mode)
key='trigger_rumble_mode'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
-r | --rumble-attenuation)
key='rumble_attenuation'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
-n | --ff_connect_notify)
key='ff_connect_notify'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
--)
shift
break
;;
*)
echo "$NAME: Invalid option"
display_help
exit 1
;;
esac
done
}
### Main Entry Point ###
PARAMETERS=( "$@" )
parse_args "${PARAMETERS[@]}"
# Check if xpadneo is installed
if lsmod | grep -vq 'hid-xpadneo';
then
>&2 echo -e "$0: WARNING: hid-xpadneo not loaded, configured anyways"
fi