Skip to content

Commit c64d94f

Browse files
committed
added joystick support
1 parent f4889ee commit c64d94f

File tree

2 files changed

+72
-39
lines changed

2 files changed

+72
-39
lines changed

input_selection.sh

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,67 @@
2121
# and let the user quit.
2222
# - [robustness] verify if the "#include ...input-selection.cfg" line
2323
# is before any input_playerN_joypad_index in the retroarch.cfg.
24-
# - if jslist is not in the PATH, download it from github, compile it,
25-
# and put it in $HOME/bin directory.
2624
#
27-
# meleu, 2016/05/21
25+
# meleu, 2016/05
2826

2927

30-
jsListFile="/tmp/jslist-$$"
31-
tempFile="${jsListFile}_d"
32-
retroarchcfg="/opt/retropie/configs/all/retroarch.cfg"
33-
inputcfg="/opt/retropie/configs/all/input-selection.cfg"
28+
rootdir="/opt/retropie"
29+
configdir="$rootdir/configs"
30+
31+
js_list_file="/tmp/jslist-$$"
32+
temp_file="${js_list_file}_d"
33+
retroarchcfg="$configdir/all/retroarch.cfg"
34+
inputcfg="$configdir/all/input-selection.cfg"
35+
36+
37+
# borrowed code from runcommand.sh ############################################
38+
# The joy2key.py aren't documented, so I don't know how to use it... :(
39+
function start_joy2key() {
40+
# if joy2key.py is installed run it with cursor keys for axis,
41+
# and enter + tab for buttons 0 and 1
42+
__joy2key_dev=$(ls -1 /dev/input/js* 2>/dev/null | head -n1)
43+
if [[ -f "$rootdir/supplementary/runcommand/joy2key.py" && -n "$__joy2key_dev" ]] && ! pgrep -f joy2key.py >/dev/null; then
44+
"$rootdir/supplementary/runcommand/joy2key.py" "$__joy2key_dev" 1b5b44 1b5b43 1b5b41 1b5b42 0a 09 &
45+
__joy2key_pid=$!
46+
fi
47+
}
48+
49+
function stop_joy2key() {
50+
if [[ -n "$__joy2key_pid" ]]; then
51+
kill -INT "$__joy2key_pid"
52+
fi
53+
}
54+
# end of the borrowed code from runcommand.sh #################################
55+
56+
57+
start_joy2key
3458

3559
# checking if the "#include ..." line is in the retroarch.cfg
36-
grep -q '^#include "/opt/retropie/configs/all/input-selection.cfg"$' $retroarchcfg || {
37-
dialog \
38-
--title "Error" \
39-
--yesno \
60+
grep -q \
61+
"^#include \"$configdir/all/input-selection.cfg\"$"\
62+
$retroarchcfg || {
63+
dialog \
64+
--title "Error" \
65+
--yesno \
4066
"Your retroarch.cfg isn't properly configured to work with this method of \
4167
input selection. You need to put the following line on your \"$retroarchcfg\" \
4268
(preferably at the beginning)\
43-
\n\n#include \"/opt/retropie/configs/all/input-selection.cfg\"\n\n\
69+
\n\n#include \"$configdir/all/input-selection.cfg\"\n\n\
4470
Do you want me to put it at the beginning of the retroarch.cfg now?\
4571
\n(if you choose \"No\", I will stop now)" \
46-
0 0 || exit 1;
72+
0 0 || {
73+
stop_joy2key
74+
exit 1;
75+
}
4776

48-
# Put the "#include ..." at the beginning line of retroarch.cfg
49-
sed -i "1i\
77+
# Putting the "#include ..." at the beginning line of retroarch.cfg
78+
sed -i "1i\
5079
# $(date +%Y-%m-%d): The following line was added to allow input selection\n\
51-
#include \"/opt/retropie/configs/all/input-selection.cfg\"\n" $retroarchcfg
52-
}
80+
#include \"$configdir/all/input-selection.cfg\"\n" $retroarchcfg
81+
} # end of failed grep
5382

5483
# if the input-selection.cfg doesn't exist, create it with default values
55-
[ -f "$inputcfg" ] || {
84+
[[ -f "$inputcfg" ]] || {
5685
cat << _EOF_ > $inputcfg
5786
# This file is used to choose what controller to use for each player.
5887
input_player1_joypad_index = "0"
@@ -62,51 +91,52 @@ input_player4_joypad_index = "3"
6291
_EOF_
6392
}
6493

65-
6694
# checking if jslist is on the PATH
6795
which jslist > /dev/null || {
68-
dialog --title 'Fail!' --msgbox '"jslist" not found!' 5 40
96+
dialog --title "Fail!" --msgbox "\"jslist\" not found!" 5 40
97+
stop_joy2key
6998
exit 1
7099
}
71100

72-
# the jslist returns a non-zero value if it fails or doesn't find any joystick
73-
jslist > $tempFile || {
101+
# the jslist returns a non-zero value if it doesn't find any joystick
102+
jslist > $temp_file || {
74103
dialog --title "Fail!" --msgbox "No joystick found. :(" 5 40
75-
rm -f $tempFile
104+
rm -f $temp_file
105+
stop_joy2key
76106
exit 1
77107
}
78108

79-
80109
# This obscure command searches for duplicated joystick names and puts
81110
# a sequential number at the end of the repeated ones
82111
# credit goes to fedorqui (http://stackoverflow.com/users/1983854/fedorqui)
83112
awk -F: 'FNR==NR {count[$2]++; next}
84113
count[$2]>1 {$0=$0 OFS "#"++times[$2]}
85-
1' $tempFile $tempFile > $jsListFile
114+
1' $temp_file $temp_file > $js_list_file
86115

87116
# No need for this file anymore
88-
rm -f $tempFile
117+
rm -f $temp_file
89118

90-
tempFile="/tmp/jstmp-$$"
119+
temp_file="/tmp/jstmp-$$"
91120

92121
for i in $(seq 1 4); do
93122
# Obtaining the joystick list with the format
94123
# index "Joystick Name"
95124
# to use as dialog menu options
96-
dialogOptions=$(sed 's/:\(.*\)/ "\1"/' $jsListFile)
125+
dialogOptions=$(sed 's/:\(.*\)/ "\1"/' $js_list_file)
97126

98127
echo "$dialogOptions" |
99128
xargs dialog \
100129
--title "Input selection" \
101-
--menu "Which controller you want to use for \"Player $i\"?" \
102-
0 0 0 2> $tempFile
130+
--menu "Which controller you want to use for Player $i?" \
131+
0 0 0 2> $temp_file
103132

104-
jsIndex=$(cat $tempFile)
133+
js_index=$(cat $temp_file)
105134

106135
# Here is the magic! Change the input_playerX_joypad_index in retroarch.cfg
107-
sed "s/^input_player${i}_joypad_index.*/input_player${i}_joypad_index = \"$jsIndex\"/" $inputcfg > $tempFile
136+
sed "s/^input_player${i}_joypad_index.*/input_player${i}_joypad_index = \"$js_index\"/" $inputcfg > $temp_file
108137

109-
mv $tempFile $inputcfg
138+
mv $temp_file $inputcfg
110139
done
111140

112-
rm -f $jsListFile
141+
stop_joy2key
142+
rm -f $js_list_file

install.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@
88
# - certifies that $HOME/bin is in your PATH.
99

1010
echo "Checking if \"$HOME/bin\" exists, creating it if doesn't..."
11-
[ -d "$HOME/bin" ] || mkdir "$HOME/bin";
11+
[[ -d "$HOME/bin" ]] || mkdir "$HOME/bin"
1212

1313
echo "Compiling \"jslist.c\" and putting it in \"$HOME/bin\"..."
1414
gcc jslist.c -o "$HOME/bin/jslist" $(sdl2-config --cflags --libs) || {
15-
echo "Something wrong with compilation process. Aborting..."
15+
echo -e "\nSomething wrong with the compilation process. Aborting..."
1616
exit 1
1717
}
1818

1919
echo "Putting \"input_selection.sh\" in \"$HOME/bin\"..."
20-
cp input_selection.sh "$HOME/bin/input_selection.sh"
20+
cp input_selection.sh "$HOME/bin/input_selection.sh" || {
21+
echo -e "\nUnable to put \"input_selection.sh\" in \"$HOME/bin\". Aborting."
22+
exit 1
23+
}
2124

2225
echo "Certifying that \"$HOME/bin\" is in your PATH..."
2326
echo "$PATH" | grep -q "$HOME/bin" || {
2427
export PATH="$HOME/bin:$PATH"
25-
grep -q '^ *PATH="$HOME/bin:$PATH"' "$HOME/.profile" ||
26-
echo PATH="$HOME/bin:$PATH" >> "$HOME/.profile";
28+
grep -q '^ *PATH="$HOME/bin:$PATH"' "$HOME/.profile" \
29+
|| echo PATH="$HOME/bin:$PATH" >> "$HOME/.profile";
2730

2831
}

0 commit comments

Comments
 (0)