forked from atar-axis/xpadneo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.sh
executable file
·215 lines (174 loc) · 4.59 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
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
#!/bin/bash
# Written by CodeCanna, refined by atar-axis
set -o posix
NAME="$0"
# Check if ran as root
if [[ "$EUID" -ne 0 ]];
then
echo "$NAME: This script must be ran as root!"
exit 1
fi
# Define Variables
VERSION="$(cat ./VERSION)"
INSTALLED_VERSION="$(dkms status 2>/dev/null | sed -nE 's/^hid-xpadneo, ([0-9]+.[0-9]+.[0-9]+).*installed/\1/p')"
MODULE="/sys/module/hid_xpadneo/"
PARAMS="/sys/module/hid_xpadneo/parameters"
CONF_FILE=$(find /etc/modprobe.d/ -mindepth 1 -maxdepth 1 -type f -name "*xpadneo*")
# Use getopt NOT getopts to parse arguments.
OPTS=$(getopt -n "$NAME" -o hz:d:f:v:r: -l help,version,combined-z-axis:,debug-level:,disable-ff:,trigger-rumble-damping: -- "$@")
## Print Help ##
function display_help {
cat ./docs/config_help
}
## Print Version ##
function display_version {
echo "$NAME: Installed xpadneo version: $INSTALLED_VERSION"
}
## Parameter Validation ##
function check_param {
key=$1
value=$2
case $key in
"debug_level")
if [[ "$value" -gt 3 ]] || [[ "$value" -lt 0 ]];
then
echo "$NAME: $key: Invalid value! Value must be between 0 and 3."
exit 1
fi
;;
"disable_ff")
if [[ "$value" -gt 3 ]] || [[ "$value" -lt 0 ]];
then
echo "$NAME: $key: Invalid value! Value must be between 0 and 3."
exit 1
fi
;;
"trigger_rumble_damping")
if [[ "$value" -gt 256 ]] || [[ "$value" -lt 1 ]];
then
echo "$NAME: $key: Invalid value! Value must be between 1 and 256."
exit 1
fi
;;
"combined_z_axis")
if [[ "$value" != "y" ]] && [[ "$value" != "n" ]];
then
echo "$NAME: $key: Invalid value! Value must be 'y' or 'n'."
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 {
LINE_EXISTS=$(grep 'options hid_xpadneo' "$CONF_FILE")
if [[ -z "$LINE_EXISTS" ]];
then
# If line doesn't exist echo all of the defaults.
echo "options hid_xpadneo debug_level=0 disable_ff=0 trigger_rumble_damping=4 fake_dev_version=4400 combined_z_axis=n" >> "$CONF_FILE"
fi
if [[ $1 == "" ]];
then
display_help
exit 0
fi
eval set -- "$OPTS"
while true;
do
case "$1" in
-h | --help)
display_help
shift
;;
--version)
display_version
shift
;;
-d | --debug-level)
key='debug_level'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
-f | --disable-ff)
key='disable_ff'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
-r | --trigger-rumble-damping)
key='trigger_rumble_damping'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
-z | --combined-z-axis)
key='combined_z_axis'
value="${2#*=}"
set_param "$key" "$value"
shift 2
;;
--)
shift
break
;;
*)
echo "$NAME: Invalid option"
display_help
exit 1
;;
esac
done
}
### Main Entry Point ###
PARAMETERS=( "$@" )
# Check if xpadneo is installed
if [[ -z "$INSTALLED_VERSION" ]];
then
echo "$NAME: Installation not found. Did you run ./install.sh?"
exit 1
fi
# Check if the correct version is installed
if [[ "$VERSION" != "$INSTALLED_VERSION" ]];
then
echo "$NAME: Your version of xpadneo seems to be out of date."
echo "$NAME: Please run ./update.sh from the git directory to update to the latest version."
echo "$NAME: Installed version is $INSTALLED_VERSION, but this script is for version $VERSION"
exit 2
fi
parse_args "${PARAMETERS[@]}"