-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjsfuncs.sh
687 lines (566 loc) · 20.1 KB
/
jsfuncs.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#!/usr/bin/env bash
# jsfuncs.sh
############
#
# These functions were originally part of joystick_selection.sh but as
# they became useful for runcommand-on{start,end}.sh, then I decided to
# put them in a separate file.
#
# Most of the functions have comments about what it does.
#
user="$SUDO_USER"
[[ -z "$user" ]] && user=$(id -un)
readonly rootdir="/opt/retropie"
readonly configdir="$rootdir/configs"
readonly global_jscfg="$configdir/all/joystick-selection.cfg"
readonly jslist_exe="$rootdir/supplementary/joystick-selection/jslist"
readonly jslist_file=$(mktemp /tmp/jslist.XXXX)
readonly temp_file=$(mktemp /tmp/deleteme.XXXX)
# getting some usefull functions from RetroPie
source "$rootdir/lib/inifuncs.sh"
# BYNAME is a flag to indicate the using of "joystick selection by name" method.
# The "ON" string means that the selection by name method is on, any string
# different from "ON" means "off"
BYNAME="OFF"
byname_msg=
# borrowed code from runcommand.sh
function start_joy2key() {
local __joy2key_dev
# get the first joystick device (if not already set)
[[ -z "$__joy2key_dev" ]] && __joy2key_dev="$(ls -1 /dev/input/js* 2>/dev/null | head -n1)"
# if joy2key.py is installed run it with cursor keys for axis, and enter + tab for buttons 0 and 1
if [[ -f "$rootdir/supplementary/runcommand/joy2key.py" && -n "$__joy2key_dev" ]] && ! pgrep -f joy2key.py >/dev/null; then
"$rootdir/supplementary/runcommand/joy2key.py" "$__joy2key_dev" 1b5b44 1b5b43 1b5b41 1b5b42 0a 09 &
__joy2key_pid=$!
fi
}
# borrowed code from runcommand.sh
function stop_joy2key() {
if [[ -n "$__joy2key_pid" ]]; then
kill -INT "$__joy2key_pid" 2> /dev/null
fi
}
# use this instead of exit to stop_joy2key
function safe_exit() {
stop_joy2key
rm -f "$jslist_file" "$temp_file"
exit $1
}
function fatalError() {
echo "Error: $1" 1>&2
safe_exit 1
}
# check if we are able to use the joystick selection by name method.
function check_byname_is_ok() {
local okcount=0
# checking if we have runcommand-onstart.sh feature...
grep 'runcommand-onstart\.sh' "$rootdir/supplementary/runcommand/runcommand.sh" \
| grep -qv '#.*runcommand-onstart\.sh'
if [[ $? -ne 0 ]]; then
dialog \
--title "Joystick selection by name error!" \
--yesno \
"It seems that your runcommand is outdated!
The joystick selection by name method depends on an updated version of RetroPie's runcommand.
You must update it via retropie_setup.sh to use joystick selection by name method.
Short way: execute retropie_setup.sh and choose \"Update RetroPie-Setup script\". And then go to
\"Manage Packages\" -> \"Manage core packages\" -> \"runcommand\" -> \"Update from binary\"
Detailed instructions can be found here:
https://github.com/RetroPie/RetroPie-Setup/wiki/Updating-RetroPie
Choose \"Yes\" to exit or \"No\" to continue using the joystick selection by index method." \
0 0 >/dev/tty || return $?
safe_exit 0
fi
okcount=$((okcount + 1)) # runcommand is updated - CHECKED!
local jsonstart="$rootdir/supplementary/joystick-selection/js-onstart.sh"
local rconstart="$configdir/all/runcommand-onstart.sh"
# checking runcommand-onstart.sh
if ! grep -q "^bash \"$jsonstart\" \"\$@\"" "$rconstart" 2> /dev/null; then
sudo cat >> "$rconstart" << _EoF_
# the line below is needed to use the joystick selection by name method
bash "$jsonstart" "\$@"
_EoF_
check_byname_is_ok
return $?
fi
okcount=$((okcount + 1)) # runcommand-onstart calls js-onstart.sh - CHECKED!
# checking if js-onstart.sh exists
if ! [[ -f "$jsonstart" ]]; then
cat > "/tmp/jsonstart.tmp" << _EoF_
#!/bin/bash
# this file is needed to use the joystick selection by name method.
# Do NOT edit it unless you are absolutely right of what you are doing.
[[ "\$4" != *retroarch* ]] && exit 0
source "$rootdir/supplementary/joystick-selection/jsfuncs.sh"
system="\$1"
get_configs
# if not using joystick selection by name method, there's nothing to do.
if [[ "\$BYNAME" != "ON" ]]; then
exit 0
fi
echo "--- start of joystick-selection log" >&2
echo "joystick selection by name is ON!" >&2
js_to_retroarchcfg "\$system" && echo "joystick indexes for \"\$system\" was configured" >&2
js_to_retroarchcfg all && echo "joystick indexes for \"all\" was configured" >&2
echo "--- end of joystick-selection log" >&2
_EoF_
sudo mv "/tmp/jsonstart.tmp" "$jsonstart"
sudo chown "$user"."$user" "$jsonstart"
check_byname_is_ok
return $?
fi
okcount=$((okcount + 1)) # js-onstart is OK - CHECKED!
if [[ $okcount -lt 3 ]]; then
return 1
fi
return 0
}
# get the initial configs
# [improvement]: maybe I'll add a way to define __joy2key_dev
function get_configs() {
iniConfig ' = ' '"' '' 1
iniGet joystick_selection_by_name "$global_jscfg"
if [[ "$ini_value" =~ ^[Tt][Rr][Uu][Ee]$ ]]; then
if check_byname_is_ok; then
BYNAME="ON"
else
iniSet "joystick_selection_by_name" "false" "$global_jscfg"
BYNAME="OFF"
fi
else
BYNAME="OFF"
fi
byname_msg="Selection by name is: [$BYNAME]\n\n"
}
###############################################################################
# Fills the jslist_file with the available joysticks and their indexes.
#
# Globals:
# jslist_exe
# jslist_file
#
# Arguments:
# None
#
# Returns:
# 1 if no joystick found.
# 0 otherwise
function fill_jslist_file() {
# the jslist returns a non-zero value if it doesn't find any joystick
"$jslist_exe" > "$temp_file" 2>/dev/null || return 1
# This obscure command searches for duplicated joystick names and puts
# a sequential number at the end of the repeated ones
# credit goes to fedorqui (http://stackoverflow.com/users/1983854/fedorqui)
awk -F: 'FNR==NR {count[$2]++; next}
count[$2]>0 {$0=$0 OFS "#"++times[$2]}
1' "$temp_file" "$temp_file" > "$jslist_file"
}
###############################################################################
# Get a joystick name and print its index. If the name is an integer, print
# this integer.
# OBS.: jslist_file MUST be filled.
#
# Globals:
# jslist_file
#
# Arguments:
# $1 Joystick name.
#
# Returns:
# 0 on success
# non-zero otherwise
function js_name2index() {
[[ "$1" ]] || return -1
if [[ "$1" =~ ^[0-9]+$ ]]; then
echo "$1"
return 0
fi
local js_name
local js_index
js_name="$1"
js_index=$(grep "^[0-9]\+:$js_name" "$jslist_file" | cut -d: -f1)
[[ -z "$js_index" ]] && return 1
echo "$js_index"
}
###############################################################################
# Get a joystick index and print its name. If there is no joystick with the
# given index, print "(NOT CONNECTED)" and return a non-zero value.
#
# Globals:
# jslist_file
#
# Arguments:
# $1 : Joystick index (must be an integer).
#
# Returns:
# 0 on success
# non-zero otherwise
function js_index2name() {
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "*** erro: js_index2name: argument must be an integer" >&2
return -1
fi
local js_index="$1"
local js_name
local return_val=0
js_name=$(grep "^$js_index:" "$jslist_file" | cut -d: -f2)
if [[ -z "$js_name" ]]; then
js_name="(NOT CONNECTED)"
return_val=1
fi
echo "$js_name"
return $return_val
}
###############################################################################
# Get a joystick index or name and check if it is connected. If the argument
# is an integer, it is considered an index, otherwise it is considered a
# joystick name.
# If the joystick is connected, return 0.
# If the joystick isn't connected, print "(NOT CONNECTED)" and return non-zero.
#
# Globals:
# jslist_file
#
# Arguments:
# $1 : Joystick index or name
#
# Returns:
# 0 if the joystick is connected
# non-zero otherwise
function js_is_connected() {
[[ "$1" ]] || fatalError "js_is_connected: missing arguments!"
# if it's an integer, than it's an index
if [[ "$1" =~ ^[0-9]+$ ]]; then
if grep -q "^$1:" "$jslist_file"; then
return 0
else
echo "(NOT CONNECTED)"
return 1
fi
else
if grep -q "^[0-9]\+:$1" "$jslist_file"; then
return 0
else
echo "(NOT CONNECTED)"
return 1
fi
fi
}
###############################################################################
# Make the retroarch.cfg configs match the joystick-selection.cfg. Only works
# if the BYNAME is on.
# Needs the system as an argument.
#
# Globals:
# None
#
# Arguments:
# $1 the system to configure
#
# Returns:
# 1 if BYNAME is off
# 0 otherwise
function js_to_retroarchcfg() {
[[ "$BYNAME" != "ON" ]] && return 1
[[ "$1" ]] && [[ -d "$configdir/$1" ]] || fatalError "js_to_retroarchcfg: the argument must be a valid system!"
local jscfg="$configdir/$1/joystick-selection.cfg"
local retroarchcfg="$configdir/$1/retroarch.cfg"
[[ -f "$jscfg" ]] || return 1
fill_jslist_file
local js_index
for i in 1 2 3 4; do
iniGet "input_player${i}_joypad_index" "$jscfg"
js_index=$(js_name2index "$ini_value")
if [[ -z "$js_index" ]]; then
iniUnset "input_player${i}_joypad_index" "$((i-1))" "$retroarchcfg"
else
iniSet "input_player${i}_joypad_index" "$js_index" "$retroarchcfg"
fi
done
}
###############################################################################
# Create a joystick-selection.cfg for the system given as argument.
# The created file will be filled with the input_player[1-4]_joypad_index
# values from the respective retroarch.cfg.
#
# Globals:
# None
#
# Arguments:
# $1 : The joystick-selection.cfg file.
#
# Returns:
# 0
function retroarch_to_jscfg() {
[[ "$1" ]] && [[ -d "$configdir/$1" ]] || fatalError "retroarch_to_jscfg: the argument must be a valid system!"
local temp=$(mktemp /tmp/temp.XXX)
local jscfg="$configdir/$1/joystick-selection.cfg"
local retroarchcfg="$configdir/$1/retroarch.cfg"
local jsname=
[[ -f "$retroarchcfg" ]] || fatalError "retroarch_to_jscfg: \"$retroarchcfg\" not found!"
fill_jslist_file
cat > "$temp" << _EoF_
# This file was created by the joystick-selection tool.
# It's recommended to NOT edit it manually.
# The format is pretty simmilar to a retroarch.cfg file, but it contains only
# input_player[1-4]_joypad_index, and accepts "strings" as a value.
# Example:
# input_playerN_joypad_index = "joystick name"
# If "joystick name" is an integer, then the real joystick index is used.
_EoF_
sudo mv "$temp" "$jscfg"
sudo chown "$user"."$user" "$jscfg"
if [[ "$jscfg" = "$global_jscfg" ]]; then
if [[ "$BYNAME" = "ON" ]]; then
iniSet "joystick_selection_by_name" "true" "$jscfg"
else
iniSet "joystick_selection_by_name" "false" "$jscfg"
fi
fi
for i in 1 2 3 4; do
iniGet "input_player${i}_joypad_index" "$retroarchcfg"
if [[ -z "$ini_value" ]]; then
iniUnset "input_player${i}_joypad_index" "$((i-1))" "$jscfg"
else
jsname=$(js_index2name "$ini_value")
if [[ "$jsname" = "(NOT CONNECTED)" ]]; then
iniUnset "input_player${i}_joypad_index" "$((i-1))" "$jscfg"
else
iniSet "input_player${i}_joypad_index" "$jsname" "$jscfg"
fi
fi
done
} # end of retroarch_to_jscfg()
###############################################################################
# Toggle the joystick selection method (by name or by index).
#
# If toggle from "by name" to "by index", just set the
# "joystick_selection_by_name" to "false" in global joystick-selection.cfg.
#
# If toggle from "by index" to "by name", make all existent system's
# joystick-selection.cfg match the current config from the
# respective retroarch.cfg.
#
# Globals:
# BYNAME
# byname_msg
#
# Arguments:
# None
#
# Returns:
# 0
function toggle_byname() {
if [[ "$BYNAME" = "ON" ]]; then
dialog \
--title "Toggle \"selection by name\" capability" \
--yesno "Are you sure you want to turn OFF the joystick selection by name method?" \
0 0 >/dev/tty || return 1
iniSet "joystick_selection_by_name" "false" "$global_jscfg"
BYNAME="OFF"
else
check_byname_is_ok || return 1
dialog \
--title "Toggle \"selection by name\" capability" \
--yesno "If you turn ON the joystick selection by name method, probably your current joystick selection will be lost (but you can easily reconfigure it using this tool).\n\nAre you sure you want to turn ON the joystick selection by name method?" \
0 0 >/dev/tty || return 1
dialog --title "Joystick Selection" --infobox "Please wait..." 0 0
BYNAME="ON"
local file
local system
# get all the systems *joypad_index configs and convert to an
# equivalent joystick-selection.cfg
for file in $(find "$configdir" -name retroarch.cfg 2>/dev/null); do
if grep -q "^[[:space:]#]*input_player[1-4]_joypad_index[[:space:]]*=" "$file"; then
system=${file%/*}
system=${system//$configdir\//}
retroarch_to_jscfg "$system"
fi
done
fi
byname_msg="Selection by name is: [$BYNAME]\n\n"
} # end of toggle_byname()
# Choose between global config, system specific config, toggle byname
function main_menu() {
while true; do
cmd=(dialog \
--title " Joystick Selection Main Menu " \
--menu "${byname_msg}This is a tool to let you choose which controller to use for RetroArch players 1-4" 19 80 12)
options=(
1 "Global joystick selection."
2 "System specific joystick selection."
3 "Toggle the joystick selection \"by-name\" method."
)
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [[ -n "$choice" ]]; then
case $choice in
1) system_js_select_menu "all"
;;
2) systems_menu
;;
3) toggle_byname
;;
esac
else
break
fi
done
} # end of main_menu()
###############################################################################
# Show a menu to let the user configure the input for a given system.
# At the end of this function, if the BYNAME flag is ON, the retroarch.cfg
# system file will match the configs in joystick-selection.cfg system file.
#
# Globals:
# js_list_file
#
# Arguments:
# $1 : The system to be configured
#
# Returns:
# 0
function system_js_select_menu() {
[[ "$1" ]] || fatalError "js_select: missing argument: \"system\""
fill_jslist_file
local system="$1"
local js_name_p=()
local retroarchcfg="$configdir/$system/retroarch.cfg"
local jscfg="$configdir/$system/joystick-selection.cfg"
while true; do
# the 'if' outside the 'for' is for a performance reason
if [[ "$BYNAME" = "ON" ]]; then
for i in 1 2 3 4; do
iniGet "input_player${i}_joypad_index" "$jscfg"
if [[ -z "$ini_value" ]]; then
js_name_p[$i]="** UNSET **"
elif [[ "$ini_value" == "16" ]]; then # 16 means disabled
js_name_p[$i]="** DISABLED **"
else
js_name_p[$i]="$ini_value $(js_is_connected "$ini_value")"
fi
done
else
for i in 1 2 3 4; do
iniGet "input_player${i}_joypad_index" "$retroarchcfg"
if [[ -z "$ini_value" ]]; then
js_name_p[$i]="** UNSET **"
elif [[ "$ini_value" == "16" ]]; then # 16 means disabled
js_name_p[$i]="** DISABLED **"
else
js_name_p[$i]="$ini_value:$(js_index2name "$ini_value")"
fi
done
fi
cmd=(dialog \
--title " Joystick Selection for \"$system\" " \
--menu "${byname_msg}OBS: UNSET/NOT CONNECTED joysticks are set to RetroArch defaults when launching a game.\n\nChoose the player you want to configure:" \
19 80 12
)
options=(
1 "${js_name_p[1]}"
2 "${js_name_p[2]}"
3 "${js_name_p[3]}"
4 "${js_name_p[4]}"
)
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [[ -n "$choice" ]]; then
player_js_select_menu "$choice" "$system"
else
break
fi
done
# making retroarch.cfg match the configs in joystick-selection.cfg
# to avoid user confusion when looking at the files.
js_to_retroarchcfg "$system"
# if this function was invoked by "runcommand-onmenu"...
[[ "$ON_MENU" -eq 1 ]] && return 0
# if the system is not "all", go back to the systems_menu
[[ "$system" != "all" ]] && systems_menu
} # end of system_js_select_menu()
###############################################################################
# Show the available joysticks and let the user choose which one to use
# for the player given as a parameter.
#
# Globals:
# None
#
# Arguments:
# $1 an integer [1-4] that represents the player.
# $2 the system to configure
#
# Returns:
# 0
function player_js_select_menu() {
[[ "$1" =~ ^[1-4]$ ]] || fatalError "player_js_select_menu: invalid argument!"
[[ "$2" ]] && [[ -d "$configdir/$2" ]] || fatalError "player_js_select_menu: arg2 must be a valid system!"
local i="$1"
local jscfg="$configdir/$2/joystick-selection.cfg"
local retroarchcfg="$configdir/$2/retroarch.cfg"
fill_jslist_file
options="U \"Unset\""
# The sed below obtain the joystick list with the format
# index "Joystick Name"
# to use as dialog menu options
options+=" $(sed 's/:\(.*\)/ "\1"/' "$jslist_file")"
# do not give the option to disable input for player1
[[ "$i" -ne 1 ]] && options+=" D \"Disable input for player$i\""
choice=$(
echo "$options" \
| xargs dialog \
--title " Joystick Selection for \"$system\" " \
--menu "${byname_msg}Choose the joystick for player$i" \
19 80 12 2>&1 >/dev/tty
)
if [[ -n "$choice" ]]; then
case $choice in
U)
iniUnset "input_player${i}_joypad_index" "$((i-1))" "$jscfg"
iniUnset "input_player${i}_joypad_index" "$((i-1))" "$retroarchcfg"
;;
D)
# index 16 is an ugly workaround to disable joypad input
if [[ "$BYNAME" = "ON" ]]; then
iniSet "input_player${i}_joypad_index" "16" "$jscfg"
else
iniSet "input_player${i}_joypad_index" "16" "$retroarchcfg"
fi
;;
*)
if [[ "$BYNAME" = "ON" ]]; then
iniSet "input_player${i}_joypad_index" "$(js_index2name "$choice")" "$jscfg"
else
iniSet "input_player${i}_joypad_index" "$choice" "$retroarchcfg"
fi
;;
esac
fi
} # end of player_js_select_menu()
###############################################################################
# Show the available systems to configure.
#
# Globals:
# None
#
# Arguments:
# None
#
# Returns:
# 0
function systems_menu() {
local file
local system_list
local system
for file in $(find "$configdir" -name retroarch.cfg 2>/dev/null); do
system=${file%/*}
system=${system//$configdir\//}
system_list+="$system\n"
done
system_list=$(echo -e "$system_list" | grep -v "^all$" | sort | nl )
cmd=(dialog \
--title "Joystick Selection" \
--menu "What system do you want to configure?" 25 80 20)
options=( $system_list )
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty) || return 1
system=$(echo -e "$system_list" | grep "^[[:blank:]]*$choice[[:blank:]]\+" | cut -f2)
system_js_select_menu "$system"
}