Skip to content

Commit 50ed2e7

Browse files
committed
joy2key - split out into standalone module and rework helper code
Move much of the helpers.sh start/stop logic and default parameters to a joy2key wrapper script. Switch runcommand.sh to use new wrapper script Add tab button to "y" key for use in edit dialogs Remove runcommand $md_inst on update. If old joy2key is present in runcommand install, trigger joy2key module install. Remove system.sh python3-sdl2 dependency check - moved to joy2key_depends
1 parent bd58256 commit 50ed2e7

File tree

7 files changed

+100
-75
lines changed

7 files changed

+100
-75
lines changed

scriptmodules/admin/joy2key.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
3+
# This file is part of The RetroPie Project
4+
#
5+
# The RetroPie Project is the legal property of its developers, whose names are
6+
# too numerous to list here. Please refer to the COPYRIGHT.md file distributed with this source.
7+
#
8+
# See the LICENSE.md file at the top-level directory of this distribution and
9+
# at https://raw.githubusercontent.com/RetroPie/RetroPie-Setup/master/LICENSE.md
10+
#
11+
12+
rp_module_id="joy2key"
13+
rp_module_desc="Provides joystick to keyboard conversion for navigation of RetroPie dialog menus"
14+
rp_module_section="core"
15+
16+
function _update_hook_joy2key() {
17+
# make sure joy2key is always updated when updating retropie-setup
18+
rp_isInstalled "$md_id" && install_bin_joy2key
19+
}
20+
21+
function depends_joy2key() {
22+
local depends=()
23+
# 'python3-sdl2' might not be available
24+
# it's packaged in Debian starting with version 11 (Bullseye)
25+
local p_ver
26+
p_ver="$(apt-cache madison python3-sdl2 | cut -d" " -f3 | head -n1)"
27+
if [[ -n "$p_ver" ]]; then
28+
depends+=(python3-sdl2)
29+
fi
30+
31+
getDepends "${depends[@]}"
32+
}
33+
34+
function install_bin_joy2key() {
35+
local file
36+
for file in "joy2key.py" "joy2key_sdl.py"; do
37+
cp "$md_data/$file" "$md_inst/"
38+
chmod +x "$md_inst/$file"
39+
python3 -m compileall "$md_inst/$file"
40+
done
41+
42+
local wrapper="$md_inst/joy2key"
43+
cat >"$wrapper" <<_EOF_
44+
#!/bin/bash
45+
mode="\$1"
46+
[[ -z "\$mode" ]] && mode="start"
47+
shift
48+
49+
# allow overriding joystick device via __joy2key_dev env (by default will use /dev/input/jsX which will scan all)
50+
device="/dev/input/jsX"
51+
[[ -n "\$__joy2key_dev" ]] && device="\$__joy2key_dev"
52+
53+
params=("\$@")
54+
if [[ "\${#params[@]}" -eq 0 ]]; then
55+
# Default button-to-keyboard mappings:
56+
# * cursor keys for axis/dpad
57+
# * enter, space, esc and tab for buttons 'a', 'b', 'x' and 'y'
58+
# * page up/page down for buttons 5,6 (shoulder buttons)
59+
params=(kcub1 kcuf1 kcuu1 kcud1 0x0a 0x20 0x1b 0x09 kpp knp)
60+
fi
61+
62+
source "$rootdir/lib/inifuncs.sh"
63+
iniConfig " = " '"' "$configdir/all/runcommand.cfg"
64+
iniGet "joy2key_version"
65+
script="joy2key_sdl.py"
66+
[[ "\$ini_value" == "0" ]] && script="joy2key.py"
67+
! python3 -c "import sdl2" 2>/dev/null && script="joy2key.py"
68+
69+
case "\$mode" in
70+
start)
71+
if pgrep -f "\$script" &>/dev/null; then
72+
"\$0" stop
73+
fi
74+
"$md_inst/\$script" "\$device" "\${params[@]}" || exit 1
75+
;;
76+
stop)
77+
pkill -f "\$script"
78+
sleep 1
79+
;;
80+
esac
81+
exit 0
82+
_EOF_
83+
chmod +x "$wrapper"
84+
}
File renamed without changes.
File renamed without changes.

scriptmodules/helpers.sh

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,44 +1248,21 @@ function joy2keyStart() {
12481248
[[ "$(who -m)" == *\(* ]] && return
12491249
12501250
local params=("$@")
1251-
if [[ "${#params[@]}" -eq 0 ]]; then
1252-
# Default button-to-keyboard mappings:
1253-
# * cursor keys for axis/dpad
1254-
# * enter, space and esc for buttons 'a', 'b' and 'x'
1255-
# * page up/page down for buttons 5,6 (shoulder buttons)
1256-
params=(kcub1 kcuf1 kcuu1 kcud1 0x0a 0x20 0x1b 0x00 kpp knp)
1257-
fi
1258-
1259-
# Choose the joy2key implementation here, since `runcommand` may not be installed
1260-
local joy2key="joy2key.py"
1261-
if hasPackage "python3-sdl2"; then
1262-
iniConfig " =" '"' "$configdir/all/runcommand.cfg"
1263-
iniGet "joy2key_version"
1264-
[[ $ini_value != "0" ]] && joy2key="joy2key_sdl.py"
1265-
fi
1266-
1267-
# get the first joystick device (if not already set)
1268-
[[ -c "$__joy2key_dev" ]] || __joy2key_dev="/dev/input/jsX"
1269-
1270-
# if no joystick device, or joy2key is already running exit
1271-
[[ -z "$__joy2key_dev" ]] || pgrep -f "$joy2key" >/dev/null && return 1
12721251
12731252
# if joy2key is installed, run it
1274-
if "$scriptdir/scriptmodules/supplementary/runcommand/$joy2key" "$__joy2key_dev" "${params[@]}" 2>/dev/null; then
1275-
__joy2key_pid=$(pgrep -f "$joy2key")
1276-
return 0
1253+
if rp_isInstalled "joy2key"; then
1254+
"$(rp_getInstallPath joy2key)/joy2key" start "${params[@]}" 2>/dev/null && return 0
12771255
fi
12781256
12791257
return 1
12801258
}
12811259
12821260
## @fn joy2keyStop()
1283-
## @brief Stop previously started joy2key.py process.
1261+
## @brief Stop previously started joy2key process.
12841262
function joy2keyStop() {
1285-
if [[ -n $__joy2key_pid ]]; then
1286-
kill $__joy2key_pid 2>/dev/null
1287-
__joy2key_pid=""
1288-
sleep 1
1263+
# if joy2key is installed, stop it
1264+
if rp_isInstalled "joy2key"; then
1265+
"$(rp_getInstallPath joy2key)/joy2key" stop
12891266
fi
12901267
}
12911268

scriptmodules/supplementary/runcommand.sh

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rp_module_flags="nonet"
1717
function _update_hook_runcommand() {
1818
# make sure runcommand is always updated when updating retropie-setup
1919
rp_isInstalled "$md_id" && install_bin_runcommand
20+
[[ -f "$md_inst/joy2key.py" ]] && rp_callModule "joy2key"
2021
}
2122

2223
function depends_runcommand() {
@@ -28,11 +29,9 @@ function depends_runcommand() {
2829
}
2930

3031
function install_bin_runcommand() {
31-
for file in "runcommand.sh" "joy2key.py" "joy2key_sdl.py"; do
32-
cp "$md_data/$file" "$md_inst/"
33-
chmod +x "$md_inst/$file"
34-
done
35-
python3 -m compileall "$md_inst/joy2key.py" "$md_inst/joy2key_sdl.py"
32+
rm -rf "$md_inst"
33+
mkdir -p "$md_inst"
34+
cp "$md_data/runcommand.sh" "$md_inst/"
3635
if [[ ! -f "$configdir/all/runcommand.cfg" ]]; then
3736
mkUserDir "$configdir/all"
3837
iniConfig " = " '"' "$configdir/all/runcommand.cfg"
@@ -41,16 +40,8 @@ function install_bin_runcommand() {
4140
iniSet "governor" ""
4241
iniSet "disable_menu" "0"
4342
iniSet "image_delay" "2"
44-
if hasPackage "python3-sdl2"; then
45-
iniSet "joy2key_version" "1"
46-
fi
4743
chown $user:$user "$configdir/all/runcommand.cfg"
4844
fi
49-
# if PySDL2 is not installed, force the udev version of joy2key
50-
if ! hasPackage "python3-sdl2"; then
51-
iniConfig " = " '"' "$configdir/all/runcommand.cfg"
52-
iniSet "joy2key_version" "0"
53-
fi
5445
if [[ ! -f "$configdir/all/runcommand-launch-dialog.cfg" ]]; then
5546
dialog --create-rc "$configdir/all/runcommand-launch-dialog.cfg"
5647
chown $user:$user "$configdir/all/runcommand-launch-dialog.cfg"

scriptmodules/supplementary/runcommand/runcommand.sh

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ VIDEO_CONF="$CONFIGDIR/all/videomodes.cfg"
8686
EMU_CONF="$CONFIGDIR/all/emulators.cfg"
8787
BACKENDS_CONF="$CONFIGDIR/all/backends.cfg"
8888
RETRONETPLAY_CONF="$CONFIGDIR/all/retronetplay.cfg"
89-
JOY2KEY="joy2key_sdl.py"
89+
JOY2KEY="$ROOTDIR/admin/joy2key/joy2key"
9090

9191
# modesetting tools
9292
TVSERVICE="/opt/vc/bin/tvservice"
@@ -119,8 +119,6 @@ function get_config() {
119119
iniGet "image_delay"
120120
IMAGE_DELAY="$ini_value"
121121
[[ -z "$IMAGE_DELAY" ]] && IMAGE_DELAY=2
122-
iniGet "joy2key_version"
123-
[[ "$ini_value" == "0" ]] && JOY2KEY="joy2key.py"
124122
fi
125123

126124
if [[ -n "$DISPLAY" ]] && $XRANDR &>/dev/null; then
@@ -134,32 +132,15 @@ function get_config() {
134132
}
135133

136134
function start_joy2key() {
137-
[[ "$DISABLE_JOYSTICK" -eq 1 ]] && return
138-
# get the first joystick device (if not already set)
139-
if [[ -c "$__joy2key_dev" ]]; then
140-
JOY2KEY_DEV="$__joy2key_dev"
141-
else
142-
JOY2KEY_DEV="/dev/input/jsX"
143-
fi
144-
# if joy2key is installed run it with cursor keys for axis, and enter + tab for buttons 0 and 1
145-
if [[ -f "$ROOTDIR/supplementary/runcommand/$JOY2KEY" && -n "$JOY2KEY_DEV" ]] && ! pgrep -f "$JOY2KEY" >/dev/null; then
146-
147-
# call joy2key.py: arguments are curses capability names or hex values starting with '0x'
148-
# see: http://pubs.opengroup.org/onlinepubs/7908799/xcurses/terminfo.html
149-
"$ROOTDIR/supplementary/runcommand/$JOY2KEY" "$JOY2KEY_DEV" kcub1 kcuf1 kcuu1 kcud1 0x0a 0x09
150-
JOY2KEY_PID=$(pgrep -f "$JOY2KEY")
135+
# check if joystick support is enabled and joy2key is available
136+
[[ "$DISABLE_JOYSTICK" -eq 1 || ! -f "$JOY2KEY" ]] && return
151137

152-
# ensure coherency between on-screen prompts and actual button mapping functionality
153-
sleep 0.3
154-
fi
138+
"$JOY2KEY" start 2>/dev/null && return 0
155139
}
156140

157141
function stop_joy2key() {
158-
if [[ -n "$JOY2KEY_PID" ]]; then
159-
kill "$JOY2KEY_PID"
160-
JOY2KEY_PID=""
161-
sleep 1
162-
fi
142+
# if joy2key is installed, stop it
143+
[[ -f "$JOY2KEY" ]] && "$JOY2KEY" stop
163144
}
164145

165146
function get_params() {

scriptmodules/system.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,6 @@ function get_retropie_depends() {
304304

305305
[[ "$__use_ccache" -eq 1 ]] && depends+=(ccache)
306306

307-
# 'python3-sdl2' might not be available
308-
# it's packaged in Debian starting with version 11 (Bullseye)
309-
local p_ver
310-
p_ver="$(apt-cache madison python3-sdl2 | cut -d" " -f3 | head -n1)"
311-
if [[ -n "$p_ver" ]]; then
312-
depends+=(python3-sdl2)
313-
fi
314-
315307
if ! getDepends "${depends[@]}"; then
316308
fatalError "Unable to install packages required by $0 - ${md_ret_errors[@]}"
317309
fi

0 commit comments

Comments
 (0)