/
mupen64plus.sh
274 lines (260 loc) · 7.94 KB
/
mupen64plus.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
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
#!/usr/bin/env bash
# This file is part of The RetroPie Project
#
# The RetroPie Project is the legal property of its developers, whose names are
# too numerous to list here. Please refer to the COPYRIGHT.md file distributed with this source.
#
# See the LICENSE.md file at the top-level directory of this distribution and
# at https://raw.githubusercontent.com/RetroPie/RetroPie-Setup/master/LICENSE.md
#
function onstart_mupen64plus_joystick() {
# write temp file header
echo "; ${DEVICE_NAME}_START " > /tmp/mp64tempconfig.cfg
echo "[${DEVICE_NAME}]" >> /tmp/mp64tempconfig.cfg
iniConfig " = " "" "/tmp/mp64tempconfig.cfg"
iniSet "plugged" "True"
iniSet "plugin" "2"
iniSet "mouse" "False"
iniSet "AnalogDeadzone" "4096,4096"
iniSet "AnalogPeak" "32768,32768"
iniSet "Mempak switch" ""
iniSet "Rumblepak switch" ""
}
function map_mupen64plus_joystick() {
local input_name="$1"
local input_type="$2"
local input_id="$3"
local input_value="$4"
local keys
local dir
case "$input_name" in
up)
keys=("DPad U")
dir=("Up")
;;
down)
keys=("DPad D")
dir=("Down")
;;
left)
keys=("DPad L")
dir=("Left")
;;
right)
keys=("DPad R")
dir=("Right")
;;
b)
keys=("A Button")
;;
y)
keys=("B Button")
;;
a)
keys=("C Button D")
;;
x)
keys=("C Button U")
;;
leftbottom|leftshoulder)
keys=("L Trig")
;;
rightbottom|rightshoulder)
keys=("R Trig")
;;
lefttop|lefttrigger)
keys=("Z Trig")
;;
start)
keys=("Start")
;;
leftanalogleft)
keys=("X Axis")
dir=("Left")
;;
leftanalogright)
keys=("X Axis")
dir=("Right")
;;
leftanalogup)
keys=("Y Axis")
dir=("Up")
;;
leftanalogdown)
keys=("Y Axis")
dir=("Down")
;;
rightanalogleft)
keys=("C Button L")
dir=("Left")
;;
rightanalogright)
keys=("C Button R")
dir=("Right")
;;
rightanalogup)
keys=("C Button U")
dir=("Up")
;;
rightanalogdown)
keys=("C Button D")
dir=("Down")
;;
leftthumb)
keys=("Mempak switch")
;;
rightthumb)
keys=("Rumblepak switch")
;;
*)
return
;;
esac
local key
local value
#iniConfig " = " "" "/tmp/mp64keys.cfg"
for key in "${keys[@]}"; do
# read key value. Axis takes two key/axis values.
iniGet "$key"
case "$input_type" in
axis)
# key "X/Y Axis" needs different button naming
if [[ "$key" == *Axis* ]]; then
# if there is already a "-" axis add "+" axis value
if [[ "$ini_value" == *\(* ]]; then
value="${ini_value}${input_id}+)"
# if there is already a "+" axis add "-" axis value
elif [[ "$ini_value" == *\)* ]]; then
value="axis(${input_id}-,${ini_value}"
# if there is no ini_value add "+" axis value
elif [[ "$input_value" == "1" ]]; then
value="${input_id}+)"
else
value="axis(${input_id}-,"
fi
elif [[ "$input_value" == "1" ]]; then
value="axis(${input_id}+) ${ini_value}"
else
value="axis(${input_id}-) ${ini_value}"
fi
;;
hat)
if [[ "$key" == *Axis* ]]; then
if [[ "$ini_value" == *\(* ]]; then
value="${ini_value}${dir})"
elif [[ "$ini_value" == *\)* ]]; then
value="hat(${input_id} ${dir} ${ini_value}"
elif [[ "$dir" == "Up" || "$dir" == "Left" ]]; then
value="hat(${input_id} ${dir} "
elif [[ "$dir" == "Right" || "$dir" == "Down" ]]; then
value="${dir})"
fi
else
if [[ -n "$dir" ]]; then
value="hat(${input_id} ${dir}) ${ini_value}"
fi
fi
;;
*)
if [[ "$key" == *Axis* ]]; then
if [[ "$ini_value" == *\(* ]]; then
value="${ini_value}${input_id})"
elif [[ "$ini_value" == *\)* ]]; then
value="button(${input_id},${ini_value}"
elif [[ "$dir" == "Up" || "$dir" == "Left" ]]; then
value="button(${input_id},"
elif [[ "$dir" == "Right" || "$dir" == "Down" ]]; then
value="${input_id})"
fi
else
value="button(${input_id}) ${ini_value}"
fi
;;
esac
iniSet "$key" "$value"
done
}
function onend_mupen64plus_joystick() {
local bind
local axis
local axis_neg
local axis_pos
for axis in "X Axis" "Y Axis"; do
if [[ "$axis" == *X* ]]; then
axis_neg="DPad L"
axis_pos="DPad R"
else
axis_neg="DPad U"
axis_pos="DPad D"
fi
# analog stick sanity check
# replace Axis values with DPAD values if there is no Axis
# device setup
if ! grep -q "$axis" /tmp/mp64tempconfig.cfg ; then
iniGet "${axis_neg}"
bind=${ini_value//)/,}
iniGet "${axis_pos}"
ini_value=${ini_value//axis(/}
ini_value=${ini_value//hat(/}
ini_value=${ini_value//button(/}
bind="${bind}${ini_value}"
iniSet "$axis" "$bind"
iniDel "${axis_neg}"
iniDel "${axis_pos}"
fi
done
# If there is no Z Trig try to map the L shoulder
# button to it via copying over the existing L Trig
# value and deleting it (L Trig) after
if ! grep -q "Z Trig" /tmp/mp64tempconfig.cfg ; then
iniGet "L Trig"
iniSet "Z Trig" "${ini_value}"
iniDel "L Trig"
fi
echo "; ${DEVICE_NAME}_END " >> /tmp/mp64tempconfig.cfg
echo "" >> /tmp/mp64tempconfig.cfg
# abort if old device config cannot be deleted.
# keep original mupen64plus-input-sdl configs.
local file="$configdir/n64/InputAutoCfg.ini"
if [[ -f "$file" ]]; then
# backup current config file
cp "$file" "${file}.bak"
local escaped_device_name=$(echo "$DEVICE_NAME" | sed 's|[]\[^$.*/]|\\&|g')
sed -i /"${escaped_device_name}_START"/,/"${escaped_device_name}_END"/d "$file"
if grep -Fq "$DEVICE_NAME" "$file" ; then
rm /tmp/mp64tempconfig.cfg
return
fi
else
cat > "$file" << _EOF_
; InputAutoCfg.ini for Mupen64Plus SDL Input plugin
; Keyboard_START
[Keyboard]
plugged = True
plugin = 2
mouse = False
DPad R = key(100)
DPad L = key(97)
DPad D = key(115)
DPad U = key(119)
Start = key(13)
Z Trig = key(122)
B Button = key(306)
A Button = key(304)
C Button R = key(108)
C Button L = key(106)
C Button D = key(107)
C Button U = key(105)
R Trig = key(99)
L Trig = key(120)
Mempak switch = key(44)
Rumblepak switch = key(46)
X Axis = key(276,275)
Y Axis = key(273,274)
; Keyboard_END
_EOF_
fi
# append temp device configuration to InputAutoCfg.ini
cat /tmp/mp64tempconfig.cfg >> "$file"
rm /tmp/mp64tempconfig.cfg
}