Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.50 KB | None | 0 0
  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. ## @file supplementary/runcommand/runcommand.sh
  13. ## @brief runcommand launching script
  14. ## @copyright GPLv3
  15. ## @details
  16. ## @par Usage
  17. ##
  18. ## `runcommand.sh VIDEO_MODE COMMAND SAVE_NAME`
  19. ##
  20. ## or
  21. ##
  22. ## `runcommand.sh VIDEO_MODE _SYS_/_PORT_ SYSTEM ROM`
  23. ##
  24. ## Video mode switching is only supported on the Raspberry Pi
  25. ##
  26. ## Automatic video mode selection:
  27. ##
  28. ## * VIDEO_MODE = 0: use the current video mode
  29. ## * VIDEO_MODE = 1: set video mode to 640x480 (4:3) or 720x480 (16:9) @60hz
  30. ## * VIDEO_MODE = 4: set video mode to 1024x768 (4:3) or 1280x720 (16:9) @60hz
  31. ##
  32. ## Manual video mode selection
  33. ##
  34. ## * VIDEO_MODE = "CEA-#": set video mode to CEA mode #
  35. ## * VIDEO_MODE = "DMT-#": set video mode to DMT mode #
  36. ## * VIDEO_MODE = "PAL/NTSC-RATIO": set mode to SD output with RATIO of 4:3 / 16:10 or 16:9
  37. ##
  38. ## @note
  39. ## Video mode switching only happens if the monitor reports the modes as available
  40. ## (via tvservice) and the requested mode differs from the currently active mode
  41. ##
  42. ## If `_SYS_` or `_PORT_` is provided for the second parameter, the commandline
  43. ## will be extracted from `/opt/retropie/configs/SYSTEM/emulators.cfg` with
  44. ## `%ROM%` `%BASENAME%` being replaced with the ROM parameter. This is the
  45. ## default mode used when launching in RetroPie so the user can switch emulator
  46. ## used as well as other options from the runcommand GUI.
  47. ##
  48. ## If SAVE_NAME is included, that is used for loading and saving of video output
  49. ## modes as well as SDL1 dispmanx settings for the current COMMAND. If omitted,
  50. ## the binary name is used as a key for the loading and saving. The savename is
  51. ## also displayed in the video output menu (detailed below), so for our purposes
  52. ## we send the emulator module id, which is somewhat descriptive yet short.
  53. ##
  54. ## On launch this script waits for 2 second for a key or joystick press. If
  55. ## pressed the GUI is shown, where a user can set video modes, default emulators
  56. ## and other options (depending what is being launched).
  57.  
  58. ROOTDIR="/opt/retropie"
  59. CONFIGDIR="$ROOTDIR/configs"
  60. LOG="/dev/shm/runcommand.log"
  61.  
  62. RUNCOMMAND_CONF="$CONFIGDIR/all/runcommand.cfg"
  63. VIDEO_CONF="$CONFIGDIR/all/videomodes.cfg"
  64. EMU_CONF="$CONFIGDIR/all/emulators.cfg"
  65. DISPMANX_CONF="$CONFIGDIR/all/dispmanx.cfg"
  66. RETRONETPLAY_CONF="$CONFIGDIR/all/retronetplay.cfg"
  67.  
  68. TVSERVICE="/opt/vc/bin/tvservice"
  69.  
  70. source "$ROOTDIR/lib/inifuncs.sh"
  71.  
  72. function get_config() {
  73. declare -Ag MODE_MAP
  74.  
  75. MODE_MAP[1-CEA-4:3]="CEA-1"
  76. MODE_MAP[1-DMT-4:3]="DMT-4"
  77. MODE_MAP[1-CEA-16:9]="CEA-1"
  78.  
  79. MODE_MAP[4-CEA-4:3]="DMT-16"
  80. MODE_MAP[4-DMT-4:3]="DMT-16"
  81. MODE_MAP[4-CEA-16:9]="CEA-4"
  82.  
  83. if [[ -f "$RUNCOMMAND_CONF" ]]; then
  84. iniConfig " = " '"' "$RUNCOMMAND_CONF"
  85. iniGet "governor"
  86. GOVERNOR="$ini_value"
  87. iniGet "use_art"
  88. USE_ART="$ini_value"
  89. iniGet "disable_joystick"
  90. DISABLE_JOYSTICK="$ini_value"
  91. iniGet "disable_menu"
  92. DISABLE_MENU="$ini_value"
  93. [[ "$DISABLE_MENU" -eq 1 ]] && DISABLE_JOYSTICK=1
  94. iniGet "image_delay"
  95. IMAGE_DELAY="$ini_value"
  96. [[ -z "$IMAGE_DELAY" ]] && IMAGE_DELAY=2
  97. fi
  98.  
  99. if [[ -f "$TVSERVICE" ]]; then
  100. HAS_TVS=1
  101. else
  102. HAS_TVS=0
  103. fi
  104. }
  105.  
  106. function start_joy2key() {
  107. [[ "$DISABLE_JOYSTICK" -eq 1 ]] && return
  108. # get the first joystick device (if not already set)
  109. if [[ -c "$__joy2key_dev" ]]; then
  110. JOY2KEY_DEV="$__joy2key_dev"
  111. else
  112. JOY2KEY_DEV="/dev/input/jsX"
  113. fi
  114. # if joy2key.py is installed run it with cursor keys for axis, and enter + tab for buttons 0 and 1
  115. if [[ -f "$ROOTDIR/supplementary/runcommand/joy2key.py" && -n "$JOY2KEY_DEV" ]] && ! pgrep -f joy2key.py >/dev/null; then
  116.  
  117. # call joy2key.py: arguments are curses capability names or hex values starting with '0x'
  118. # see: http://pubs.opengroup.org/onlinepubs/7908799/xcurses/terminfo.html
  119. "$ROOTDIR/supplementary/runcommand/joy2key.py" "$JOY2KEY_DEV" kcub1 kcuf1 kcuu1 kcud1 0x0a 0x09 &
  120. JOY2KEY_PID=$!
  121. fi
  122. }
  123.  
  124. function stop_joy2key() {
  125. if [[ -n "$JOY2KEY_PID" ]]; then
  126. kill -USR1 "$JOY2KEY_PID"
  127. wait "$JOY2KEY_PID" 2>/dev/null
  128. fi
  129. }
  130.  
  131. function get_params() {
  132. MODE_REQ="$1"
  133. COMMAND="$2"
  134.  
  135. [[ -z "$MODE_REQ" || -z "$COMMAND" ]] && return 1
  136.  
  137. CONSOLE_OUT=0
  138. # if the COMMAND is _SYS_, or _PORT_ arg 3 should be system name, and arg 4 rom/game, and we look up the configured system for that combination
  139. if [[ "$COMMAND" == "_SYS_" || "$COMMAND" == "_PORT_" ]]; then
  140. # if the rom is actually a special +Start System.sh script, we should launch the script directly.
  141. if [[ "$4" =~ \/\+Start\ (.+)\.sh$ ]]; then
  142. # extract emulator from the name (and lowercase it)
  143. EMULATOR=${BASH_REMATCH[1],,}
  144. IS_SYS=0
  145. COMMAND="bash \"$4\""
  146. SYSTEM="$3"
  147. [[ -z "$SYSTEM" ]] && return 1
  148. else
  149. IS_SYS=1
  150. SYSTEM="$3"
  151. ROM="$4"
  152. ROM_BN_EXT="${ROM##*/}"
  153. ROM_BN="${ROM_BN_EXT%.*}"
  154. if [[ "$COMMAND" == "_PORT_" ]]; then
  155. CONF_ROOT="$CONFIGDIR/ports/$SYSTEM"
  156. EMU_SYS_CONF="$CONF_ROOT/emulators.cfg"
  157. IS_PORT=1
  158. else
  159. CONF_ROOT="$CONFIGDIR/$SYSTEM"
  160. EMU_SYS_CONF="$CONF_ROOT/emulators.cfg"
  161. IS_PORT=0
  162. fi
  163. SYS_SAVE_ROM_OLD="a$(echo "$SYSTEM$ROM" | md5sum | cut -d" " -f1)"
  164. SYS_SAVE_ROM="$(clean_name "${SYSTEM}_${ROM_BN}")"
  165. [[ -z "$SYSTEM" ]] && return 1
  166. get_sys_command
  167. fi
  168. else
  169. IS_SYS=0
  170. CONSOLE_OUT=1
  171. EMULATOR="$3"
  172. # if we have an emulator name (such as module_id) we use that for storing/loading parameters for video output/dispmanx
  173. # if the parameter is empty we use the name of the binary (to avoid breakage with out of date emulationstation configs)
  174. [[ -z "$EMULATOR" ]] && EMULATOR="${COMMAND/% */}"
  175. fi
  176.  
  177. NETPLAY=0
  178. return 0
  179. }
  180.  
  181. function clean_name() {
  182. local name="$1"
  183. name="${name//\//_}"
  184. name="${name//[^a-zA-Z0-9_\-]/}"
  185. echo "$name"
  186. }
  187.  
  188. function set_save_vars() {
  189. # convert emulator name / binary to a names usable as variables in our config files
  190. SAVE_EMU="$(clean_name "$EMULATOR")"
  191. SAVE_ROM_OLD=r$(echo "$COMMAND" | md5sum | cut -d" " -f1)
  192. if [[ "$IS_SYS" -eq 1 ]]; then
  193. SAVE_ROM="${SAVE_EMU}_$(clean_name "$ROM_BN")"
  194. else
  195. SAVE_ROM="$SAVE_EMU"
  196. fi
  197. }
  198.  
  199. function get_all_modes() {
  200. declare -Ag MODE
  201. local group
  202. for group in CEA DMT; do
  203. while read -r line; do
  204. local id="$(echo "$line" | grep -oE "mode [0-9]*" | cut -d" " -f2)"
  205. local info="$(echo "$line" | cut -d":" -f2-)"
  206. info=${info/ /}
  207. if [[ -n "$id" ]]; then
  208. MODE_ID+=($group-$id)
  209. MODE[$group-$id]="$info"
  210. fi
  211. done < <($TVSERVICE -m $group)
  212. done
  213. local aspect
  214. for group in "NTSC" "PAL"; do
  215. for aspect in "4:3" "16:10" "16:9"; do
  216. MODE_ID+=($group-$aspect)
  217. MODE[$group-$aspect]="SDTV - $group-$aspect"
  218. done
  219. done
  220. }
  221.  
  222. function get_mode_info() {
  223. local status="$($TVSERVICE -s)"
  224. local temp
  225. local mode_info=()
  226.  
  227. # get mode type / id
  228. if [[ "$status" =~ (PAL|NTSC) ]]; then
  229. temp=($(echo "$status" | grep -oE "(PAL|NTSC) (4:3|16:10|16:9)"))
  230. else
  231. temp=($(echo "$status" | grep -oE "(CEA|DMT) \([0-9]+\)"))
  232. fi
  233. mode_info[0]="${temp[0]}"
  234. mode_info[1]="${temp[1]//[()]/}"
  235.  
  236. # get mode resolution
  237. temp=$(echo "$status" | cut -d"," -f2 | grep -oE "[0-9]+x[0-9]+")
  238. temp=(${temp/x/ })
  239. mode_info[2]="${temp[0]}"
  240. mode_info[3]="${temp[1]}"
  241.  
  242. # get aspect ratio
  243. temp=$(echo "$status" | grep -oE "([0-9]+:[0-9]+)")
  244. mode_info[4]="$temp"
  245.  
  246. # get refresh rate
  247. temp=$(echo "$status" | grep -oE "[0-9\.]+Hz" | cut -d"." -f1)
  248. mode_info[5]="$temp"
  249.  
  250. echo "${mode_info[@]}"
  251. }
  252.  
  253. function default_process() {
  254. local config="$1"
  255. local mode="$2"
  256. local key="$3"
  257. local value="$4"
  258.  
  259. iniConfig " = " '"' "$config"
  260. case "$mode" in
  261. get)
  262. iniGet "$key"
  263. echo "$ini_value"
  264. ;;
  265. set)
  266. iniSet "$key" "$value"
  267. ;;
  268. del)
  269. iniDel "$key"
  270. ;;
  271. esac
  272. }
  273.  
  274. function default_mode() {
  275. local mode="$1"
  276. local type="$2"
  277. local value="$3"
  278.  
  279. local key
  280. case "$type" in
  281. vid_emu)
  282. key="$SAVE_EMU"
  283. ;;
  284. vid_rom_old)
  285. key="$SAVE_ROM_OLD"
  286. ;;
  287. vid_rom)
  288. key="$SAVE_ROM"
  289. ;;
  290. fb_emu)
  291. key="${SAVE_EMU}_fb"
  292. ;;
  293. fb_rom_old)
  294. key="${SAVE_ROM_OLD}_fb"
  295. ;;
  296. fb_rom)
  297. key="${SAVE_ROM}_fb"
  298. ;;
  299. render)
  300. key="${SAVE_EMU}_render"
  301. ;;
  302. esac
  303. default_process "$CONFIGDIR/all/videomodes.cfg" "$mode" "$key" "$value"
  304. }
  305.  
  306. function default_emulator() {
  307. local mode="$1"
  308. local type="$2"
  309. local value="$3"
  310.  
  311. local key
  312. local config="$EMU_SYS_CONF"
  313.  
  314. case "$type" in
  315. emu_sys)
  316. key="default"
  317. ;;
  318. emu_cmd)
  319. key="$EMULATOR"
  320. ;;
  321. emu_rom_old)
  322. key="$SYS_SAVE_ROM_OLD"
  323. config="$EMU_CONF"
  324. ;;
  325. emu_rom)
  326. key="$SYS_SAVE_ROM"
  327. config="$EMU_CONF"
  328. ;;
  329. esac
  330. default_process "$config" "$mode" "$key" "$value"
  331. }
  332.  
  333. function load_mode_defaults() {
  334. local temp
  335. MODE_ORIG=()
  336.  
  337. if [[ "$HAS_TVS" -eq 1 ]]; then
  338. # get current mode / aspect ratio
  339. MODE_ORIG=($(get_mode_info))
  340. MODE_CUR=("${MODE_ORIG[@]}")
  341. MODE_ORIG_ID="${MODE_ORIG[0]}-${MODE_ORIG[1]}"
  342.  
  343. # get default mode for requested mode of 1 or 4
  344. if [[ "$MODE_REQ" == "0" ]]; then
  345. MODE_REQ_ID="$MODE_ORIG_ID"
  346. elif [[ "$MODE_REQ" =~ (1|4) ]]; then
  347. # if current aspect is anything else like 5:4 / 10:9 just choose a 4:3 mode
  348. local aspect="${MODE_ORIG[4]}"
  349. [[ "$aspect" =~ (4:3|16:9) ]] || aspect="4:3"
  350. temp="${MODE_REQ}-${MODE_ORIG[0]}-$aspect"
  351. MODE_REQ_ID="${MODE_MAP[$temp]}"
  352. else
  353. MODE_REQ_ID="$MODE_REQ"
  354. fi
  355. fi
  356.  
  357. # get default fb_res (if not running on X)
  358. FB_ORIG=()
  359. if [[ -z "$DISPLAY" ]]; then
  360. local status=($(fbset | tr -s '\n'))
  361. FB_ORIG[0]="${status[3]}"
  362. FB_ORIG[1]="${status[4]}"
  363. FB_ORIG[2]="${status[7]}"
  364. fi
  365.  
  366. # default retroarch render res to config file
  367. RENDER_RES="config"
  368.  
  369. local mode
  370. if [[ -f "$VIDEO_CONF" ]]; then
  371. # load default video mode for emulator / rom
  372. mode="$(default_mode get vid_emu)"
  373. [[ -n "$mode" ]] && MODE_REQ_ID="$mode"
  374.  
  375. # get default mode for system + rom combination
  376. # try the old key first and convert to the new key if found
  377. mode="$(default_mode get vid_rom_old)"
  378. if [[ -n "$mode" ]]; then
  379. default_mode del vid_rom_old
  380. default_mode set vid_rom "$mode"
  381. MODE_REQ_ID="$mode"
  382. else
  383. mode="$(default_mode get vid_rom)"
  384. [[ -n "$mode" ]] && MODE_REQ_ID="$mode"
  385. fi
  386.  
  387. if [[ -z "$DISPLAY" ]]; then
  388. # load default framebuffer res for emulator / rom
  389. mode="$(default_mode get fb_emu)"
  390. [[ -n "$mode" ]] && FB_NEW="$mode"
  391.  
  392. # get default fb mode for system + rom combination
  393. # try the old key first and convert to the new key if found
  394. mode="$(default_mode get fb_rom_old)"
  395. if [[ -n "$mode" ]]; then
  396. default_mode del fb_rom_old
  397. default_mode set fb_rom "$mode"
  398. FB_NEW="$mode"
  399. else
  400. mode="$(default_mode get fb_rom)"
  401. [[ -n "$mode" ]] && FB_NEW="$mode"
  402. fi
  403. fi
  404.  
  405. # get default retroarch render resolution for emulator
  406. mode="$(default_mode get render)"
  407. [[ -n "$mode" ]] && RENDER_RES="$mode"
  408. fi
  409. }
  410.  
  411. function main_menu() {
  412. local save
  413. local cmd
  414. local choice
  415.  
  416. local user_menu=0
  417. [[ -d "$CONFIGDIR/all/runcommand-menu" && -n "$(find "$CONFIGDIR/all/runcommand-menu" -maxdepth 1 -name "*.sh")" ]] && user_menu=1
  418.  
  419. [[ -z "$ROM_BN" ]] && ROM_BN="game/rom"
  420. [[ -z "$SYSTEM" ]] && SYSTEM="emulator/port"
  421.  
  422. while true; do
  423.  
  424. local options=()
  425. if [[ "$IS_SYS" -eq 1 ]]; then
  426. local emu_sys="$(default_emulator get emu_sys)"
  427. local emu_rom="$(default_emulator get emu_rom)"
  428. options+=(
  429. 1 "Select default emulator for $SYSTEM ($emu_sys)"
  430. 2 "Select emulator for ROM ($emu_rom)"
  431. )
  432. [[ -n "$emu_rom" ]] && options+=(3 "Remove emulator choice for ROM")
  433. fi
  434.  
  435. if [[ "$HAS_TVS" -eq 1 ]]; then
  436. local vid_emu="$(default_mode get vid_emu)"
  437. local vid_rom="$(default_mode get vid_rom)"
  438. options+=(
  439. 4 "Select default video mode for $EMULATOR ($vid_emu)"
  440. 5 "Select video mode for $EMULATOR + rom ($vid_rom)"
  441. )
  442. [[ -n "$vid_emu" ]] && options+=(6 "Remove video mode choice for $EMULATOR")
  443. [[ -n "$vid_rom" ]] && options+=(7 "Remove video mode choice for $EMULATOR + ROM")
  444. fi
  445.  
  446. if [[ "$COMMAND" =~ retroarch ]]; then
  447. options+=(
  448. 8 "Select RetroArch render res for $EMULATOR ($RENDER_RES)"
  449. 9 "Edit custom RetroArch config for this ROM"
  450. )
  451. elif [[ -z "$DISPLAY" ]]; then
  452. local fb_emu="$(default_mode get fb_emu)"
  453. local fb_rom="$(default_mode get fb_rom)"
  454. options+=(
  455. 10 "Select framebuffer res for $EMULATOR ($fb_emu)"
  456. 11 "Select framebuffer res for $EMULATOR + ROM ($fb_rom)"
  457. )
  458. [[ -n "$fb_emu" ]] && options+=(12 "Remove framebuffer res choice for $EMULATOR")
  459. [[ -n "$fb_rom" ]] && options+=(13 "Remove framebuffer res choice for $EMULATOR + ROM")
  460. fi
  461.  
  462. options+=(X "Launch")
  463.  
  464. if [[ "$COMMAND" =~ retroarch ]]; then
  465. options+=(L "Launch with verbose logging")
  466. options+=(Z "Launch with netplay enabled")
  467. fi
  468.  
  469. if [[ "$user_menu" -eq 1 ]]; then
  470. options+=(U "User Menu")
  471. fi
  472.  
  473. options+=(Q "Exit (without launching)")
  474.  
  475. local temp_mode
  476. if [[ "$HAS_TVS" -eq 1 ]]; then
  477. temp_mode="${MODE[$MODE_REQ_ID]}"
  478. else
  479. temp_mode="n/a"
  480. fi
  481. cmd=(dialog --nocancel --menu "System: $SYSTEM\nEmulator: $EMULATOR\nVideo Mode: $temp_mode\nROM: $ROM_BN" 22 76 16 )
  482. choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  483. case "$choice" in
  484. 1)
  485. choose_emulator "emu_sys" "$emu_sys"
  486. ;;
  487. 2)
  488. choose_emulator "emu_rom" "$emu_rom"
  489. ;;
  490. 3)
  491. default_emulator "del" "emu_rom"
  492. get_sys_command
  493. set_save_vars
  494. load_mode_defaults
  495. ;;
  496. 4)
  497. choose_mode "vid_emu" "$vid_emu"
  498. ;;
  499. 5)
  500. choose_mode "vid_rom" "$vid_rom"
  501. ;;
  502. 6)
  503. default_mode "del" "vid_emu"
  504. load_mode_defaults
  505. ;;
  506. 7)
  507. default_mode "del" "vid_rom"
  508. load_mode_defaults
  509. ;;
  510. 8)
  511. choose_render_res "render" "$RENDER_RES"
  512. ;;
  513. 9)
  514. touch "$ROM.cfg"
  515. cmd=(dialog --editbox "$ROM.cfg" 22 76)
  516. choice=$("${cmd[@]}" 2>&1 >/dev/tty)
  517. [[ -n "$choice" ]] && echo "$choice" >"$ROM.cfg"
  518. [[ ! -s "$ROM.cfg" ]] && rm "$ROM.cfg"
  519. ;;
  520. 10)
  521. choose_fb_res "fb_emu" "$fb_emu"
  522. ;;
  523. 11)
  524. choose_fb_res "fb_rom" "$fb_rom"
  525. ;;
  526. 12)
  527. default_mode "del" "fb_emu"
  528. load_mode_defaults
  529. ;;
  530. 13)
  531. default_mode "del" "fb_rom"
  532. load_mode_defaults
  533. ;;
  534. Z)
  535. NETPLAY=1
  536. break
  537. ;;
  538. X)
  539. return 0
  540. ;;
  541. L)
  542. COMMAND+=" --verbose"
  543. return 0
  544. ;;
  545. U)
  546. user_menu
  547. local ret="$?"
  548. [[ "$ret" -eq 1 ]] && return 1
  549. [[ "$ret" -eq 2 ]] && return 0
  550. ;;
  551. Q)
  552. return 1
  553. ;;
  554. esac
  555. done
  556. return 0
  557. }
  558.  
  559. function choose_mode() {
  560. local mode="$1"
  561. local default="$2"
  562.  
  563. local options=()
  564. local key
  565. for key in "${MODE_ID[@]}"; do
  566. options+=("$key" "${MODE[$key]}")
  567. done
  568. local cmd=(dialog --default-item "$default" --menu "Choose video output mode" 22 76 16 )
  569. local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  570. [[ -z "$choice" ]] && return
  571.  
  572. default_mode set "$mode" "$choice"
  573. load_mode_defaults
  574. }
  575.  
  576. function choose_emulator() {
  577. local mode="$1"
  578. local default="$2"
  579. local cancel="$3"
  580.  
  581. local default
  582. local default_id
  583.  
  584. local options=()
  585. local i=1
  586. while read line; do
  587. # convert key=value to array
  588. local line=(${line/=/ })
  589. local id=${line[0]}
  590. [[ "$id" == "default" ]] && continue
  591. local apps[$i]="$id"
  592. if [[ "$id" == "$default" ]]; then
  593. default_id="$i"
  594. fi
  595. options+=($i "$id")
  596. ((i++))
  597. done < <(sort "$EMU_SYS_CONF")
  598. if [[ -z "${options[*]}" ]]; then
  599. dialog --msgbox "No emulator options found for $SYSTEM - have you installed any snes emulators yet? Do you have a valid $EMU_SYS_CONF ?" 20 60 >/dev/tty
  600. exit 1
  601. fi
  602. local cmd=(dialog $cancel --default-item "$default_id" --menu "Choose default emulator" 22 76 16 )
  603. local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  604. [[ -z "$choice" ]] && return
  605.  
  606. default_emulator set "$mode" "${apps[$choice]}"
  607. get_sys_command
  608. set_save_vars
  609. load_mode_defaults
  610. }
  611.  
  612. function get_resolutions() {
  613. local res=(
  614. "320x224"
  615. "320x240"
  616. "400x240"
  617. "480x320"
  618. "640x480"
  619. "720x480"
  620. "720x576"
  621. "800x480"
  622. "800x600"
  623. "960x720"
  624. "1024x600"
  625. "1024x768"
  626. "1024x800"
  627. "1280x720"
  628. "1280x800"
  629. "1280x960"
  630. "1280x1024"
  631. "1920x1080"
  632. )
  633. echo "${res[@]}"
  634. }
  635.  
  636. function choose_render_res() {
  637. local mode="$1"
  638. local default="$2"
  639.  
  640. local res=($(get_resolutions))
  641. local i=1
  642. local item
  643. local options=()
  644. for item in "${res[@]}"; do
  645. [[ "$item" == "$default" ]] && default="$i"
  646. options+=($i "$item")
  647. ((i++))
  648. done
  649. options+=(
  650. O "Use video output resolution"
  651. C "Use config file resolution"
  652. )
  653. [[ "$default" == "output" ]] && default="O"
  654. [[ "$default" == "config" ]] && default="C"
  655. local cmd=(dialog --default-item "$default" --menu "Choose RetroArch render resolution" 22 76 16 )
  656. local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  657. [[ -z "$choice" ]] && return
  658. case "$choice" in
  659. O)
  660. choice="output"
  661. ;;
  662. C)
  663. choice="config"
  664. ;;
  665. *)
  666. choice="${res[$choice-1]}"
  667. ;;
  668. esac
  669.  
  670. default_mode set "$mode" "$choice"
  671. load_mode_defaults
  672. }
  673.  
  674. function choose_fb_res() {
  675. local mode="$1"
  676. local default="$2"
  677.  
  678. local res=($(get_resolutions))
  679. local i=1
  680. local item
  681. local options=()
  682. for item in "${res[@]}"; do
  683. options+=($i "$item")
  684. ((i++))
  685. done
  686. local cmd=(dialog --default-item "$default" --menu "Choose framebuffer resolution (Useful for X and console apps)" 22 76 16 )
  687. local choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  688. [[ -z "$choice" ]] && return
  689. choice="${res[$choice-1]}"
  690.  
  691. default_mode set "$mode" "$choice"
  692. load_mode_defaults
  693. }
  694.  
  695. function user_menu() {
  696. local default
  697. local options=()
  698. local script
  699. local i=1
  700. while read -r script; do
  701. script="${script##*/}"
  702. script="${script%.*}"
  703. options+=($i "$script")
  704. ((i++))
  705. done < <(find "$CONFIGDIR/all/runcommand-menu" -type f -name "*.sh" | sort)
  706. local default
  707. local cmd
  708. local choice
  709. local ret
  710. while true; do
  711. cmd=(dialog --default-item "$default" --cancel-label "Back" --menu "Choose option" 22 76 16)
  712. choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
  713. [[ -z "$choice" ]] && return 0
  714. default="$choice"
  715. script="runcommand-menu/${options[choice*2-1]}.sh"
  716. user_script "$script"
  717. ret="$?"
  718. [[ "$ret" -eq 1 || "$ret" -eq 2 ]] && return "$ret"
  719. done
  720. }
  721.  
  722. function switch_fb_res() {
  723. local res=(${1/x/ })
  724. local res_x="${res[0]}"
  725. local res_y="${res[1]}"
  726. local depth="$2"
  727. [[ -z "$depth" ]] && depth="${FB_ORIG[2]}"
  728.  
  729. if [[ -z "$res_x" || -z "$res_y" ]]; then
  730. fbset --all -depth 8
  731. fbset --all -depth $depth
  732. else
  733. fbset --all -depth 8
  734. fbset --all --geometry $res_x $res_y $res_x $res_y $depth
  735. fi
  736. }
  737.  
  738. function mode_switch() {
  739. local mode_id="$1"
  740.  
  741. [[ "$HAS_TVS" -eq 0 ]] && return 1
  742.  
  743. # if the requested mode is the same as the current mode don't switch
  744. [[ "$mode_id" == "${MODE_CUR[0]}-${MODE_CUR[1]}" ]] && return 1
  745.  
  746. local mode_id=(${mode_id/-/ })
  747.  
  748. if [[ "${mode_id[0]}" == "PAL" ]] || [[ "${mode_id[0]}" == "NTSC" ]]; then
  749. $TVSERVICE -c "${mode_id[*]}" >/dev/null
  750. else
  751. $TVSERVICE -e "${mode_id[*]}" >/dev/null
  752. fi
  753.  
  754. # if we have switched mode, switch the framebuffer resolution also
  755. if [[ "$?" -eq 0 ]]; then
  756. sleep 1
  757. MODE_CUR=($(get_mode_info))
  758. [[ -z "$FB_NEW" ]] && FB_NEW="${MODE_CUR[2]}x${MODE_CUR[3]}"
  759. return 0
  760. fi
  761.  
  762. return 1
  763. }
  764.  
  765. function restore_fb() {
  766. sleep 1
  767. switch_fb_res "${FB_ORIG[0]}x${FB_ORIG[1]}" "${FB_ORIG[2]}"
  768. }
  769.  
  770. function config_dispmanx() {
  771. local name="$1"
  772. # if we have a dispmanx conf file and $name is in it (as a variable) and set to 1,
  773. # change the library path to load dispmanx sdl first
  774. if [[ -f "$DISPMANX_CONF" ]]; then
  775. iniConfig " = " '"' "$DISPMANX_CONF"
  776. iniGet "$name"
  777. [[ "$ini_value" == "1" ]] && COMMAND="SDL1_VIDEODRIVER=dispmanx $COMMAND"
  778. fi
  779. }
  780.  
  781. function retroarch_append_config() {
  782. # only for retroarch emulators
  783. [[ ! "$COMMAND" =~ "retroarch" ]] && return
  784.  
  785. # make sure tmp folder exists for unpacking archives
  786. mkdir -p "/tmp/retroarch"
  787.  
  788. local conf="/dev/shm/retroarch.cfg"
  789. rm -f "$conf"
  790. touch "$conf"
  791. if [[ "$HAS_TVS" -eq 1 && "${MODE_CUR[5]}" -gt 0 ]]; then
  792. # set video_refresh_rate in our config to the same as the screen refresh
  793. [[ -n "${MODE_CUR[5]}" ]] && echo "video_refresh_rate = ${MODE_CUR[5]}" >>"$conf"
  794. fi
  795.  
  796. local dim
  797. # if our render resolution is "config", then we don't set anything (use the value in the retroarch.cfg)
  798. if [[ "$RENDER_RES" != "config" ]]; then
  799. if [[ "$RENDER_RES" == "output" ]]; then
  800. dim=(0 0)
  801. else
  802. dim=(${RENDER_RES/x/ })
  803. fi
  804. echo "video_fullscreen_x = ${dim[0]}" >>"$conf"
  805. echo "video_fullscreen_y = ${dim[1]}" >>"$conf"
  806. fi
  807.  
  808. # if the ROM has a custom configuration then append that too
  809. if [[ -f "$ROM.cfg" ]]; then
  810. conf+="'|'\"$ROM.cfg\""
  811. fi
  812.  
  813. # if we already have an existing appendconfig parameter, we need to add our configs to that
  814. if [[ "$COMMAND" =~ "--appendconfig" ]]; then
  815. COMMAND=$(echo "$COMMAND" | sed "s#\(--appendconfig *[^ $]*\)#\1'|'$conf#")
  816. else
  817. COMMAND+=" --appendconfig $conf"
  818. fi
  819.  
  820. # append any NETPLAY configuration
  821. if [[ "$NETPLAY" -eq 1 ]] && [[ -f "$RETRONETPLAY_CONF" ]]; then
  822. source "$RETRONETPLAY_CONF"
  823. COMMAND+=" -$__netplaymode $__netplayhostip_cfile --port $__netplayport --nick $__netplaynickname"
  824. fi
  825. }
  826.  
  827. function set_governor() {
  828. governor_old=()
  829. # we save the previous states first, as setting any cpuX on the RPI will also set the value for the other cores
  830. # which would cause us to save the wrong state for cpu1/2/3 after setting cpu0. On the RPI we could just process
  831. # cpu0, but this code needs to work on other platforms that do support a "per core" CPU governor.
  832. for cpu in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_governor; do
  833. governor_old+=($(<$cpu))
  834. done
  835. for cpu in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_governor; do
  836. echo "$1" | sudo tee "$cpu" >/dev/null
  837. done
  838. }
  839.  
  840. function restore_governor() {
  841. local i=0
  842. for cpu in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_governor; do
  843. echo "${governor_old[$i]}" | sudo tee "$cpu" >/dev/null
  844. ((i++))
  845. done
  846. }
  847.  
  848. function get_sys_command() {
  849. if [[ ! -f "$EMU_SYS_CONF" ]]; then
  850. echo "No config found for system $SYSTEM"
  851. exit 1
  852. fi
  853.  
  854. # get system & rom specific emulator if set
  855. local emulator="$(default_emulator get emu_sys)"
  856. if [[ -z "$emulator" ]]; then
  857. echo "No default emulator found for system $SYSTEM"
  858. start_joy2key
  859. choose_emulator "emu_sys" "" "--nocancel"
  860. stop_joy2key
  861. get_sys_command "$SYSTEM" "$ROM"
  862. return
  863. fi
  864. EMULATOR="$emulator"
  865.  
  866. # get default emulator for system + rom combination
  867. # try the old key first and convert to the new key if found
  868. emulator="$(default_emulator get emu_rom_old)"
  869.  
  870. if [[ -n "$emulator" ]]; then
  871. default_emulator del emu_rom_old
  872. default_emulator set emu_rom "$emulator"
  873. EMULATOR="$emulator"
  874. else
  875. emulator="$(default_emulator get emu_rom)"
  876. [[ -n "$emulator" ]] && EMULATOR="$emulator"
  877. fi
  878.  
  879. COMMAND="$(default_emulator get emu_cmd)"
  880.  
  881. # replace tokens
  882. COMMAND="${COMMAND//\%ROM\%/\"$ROM\"}"
  883. COMMAND="${COMMAND//\%BASENAME\%/\"$ROM_BN\"}"
  884.  
  885. # special case to get the last 2 folders for quake games for the -game parameter
  886. # remove everything up to /quake/
  887. local quake_dir="${ROM##*/quake/}"
  888. # remove filename
  889. quake_dir="${quake_dir%/*}"
  890. COMMAND="${COMMAND//\%QUAKEDIR\%/\"$quake_dir\"}"
  891.  
  892. # if it starts with CON: it is a console application (so we don't redirect stdout later)
  893. if [[ "$COMMAND" == CON:* ]]; then
  894. # remove CON:
  895. COMMAND="${COMMAND:4}"
  896. CONSOLE_OUT=1
  897. fi
  898.  
  899. # workaround for launching xserver on correct/user owned tty
  900. # see https://github.com/RetroPie/RetroPie-Setup/issues/1805
  901. if [[ -n "$TTY" && "$COMMAND" =~ ^(startx|xinit) ]]; then
  902. COMMAND+=" -- vt$TTY -keeptty"
  903. fi
  904. }
  905.  
  906. function show_launch() {
  907. local images=()
  908.  
  909. if [[ "$IS_SYS" -eq 1 && "$USE_ART" -eq 1 ]]; then
  910. # if using art look for images in paths for es art.
  911. images+=(
  912. "$HOME/RetroPie/roms/$SYSTEM/images/${ROM_BN}-image"
  913. "$HOME/.emulationstation/downloaded_images/$SYSTEM/${ROM_BN}-image"
  914. )
  915. fi
  916.  
  917. # look for custom launching images
  918. if [[ "$IS_SYS" -eq 1 ]]; then
  919. images+=(
  920. "$HOME/RetroPie/roms/$SYSTEM/images/${ROM_BN}-launching"
  921. "$CONF_ROOT/launching"
  922. )
  923. fi
  924. [[ "$IS_PORT" -eq 1 ]] && images+=("$CONFIGDIR/ports/launching")
  925. images+=("$CONFIGDIR/all/launching")
  926.  
  927. local image
  928. local path
  929. local ext
  930. for path in "${images[@]}"; do
  931. for ext in jpg png; do
  932. if [[ -f "$path.$ext" ]]; then
  933. image="$path.$ext"
  934. break 2
  935. fi
  936. done
  937. done
  938.  
  939. if [[ -n "$image" ]]; then
  940. # if we are running under X use feh otherwise try and use fbi
  941. if [[ -n "$DISPLAY" ]]; then
  942. feh -F -N -Z -Y -q "$image" & &>/dev/null
  943. IMG_PID=$!
  944. sleep "$IMAGE_DELAY"
  945. else
  946. fbi -1 -t "$IMAGE_DELAY" -noverbose -a "$image" </dev/tty &>/dev/null
  947. fi
  948. elif [[ "$DISABLE_MENU" -ne 1 && "$USE_ART" -ne 1 ]]; then
  949. local launch_name
  950. if [[ -n "$ROM_BN" ]]; then
  951. launch_name="$ROM_BN ($EMULATOR)"
  952. else
  953. launch_name="$EMULATOR"
  954. fi
  955. DIALOGRC="$CONFIGDIR/all/runcommand-launch-dialog.cfg" dialog --infobox "\nLaunching $launch_name ...\n\nPress a button to configure\n\nErrors are logged to $LOG" 9 60
  956. fi
  957. }
  958.  
  959. function check_menu() {
  960. local dont_launch=0
  961. start_joy2key
  962. # check for key pressed to enter configuration
  963. IFS= read -s -t 2 -N 1 key </dev/tty
  964. if [[ -n "$key" ]]; then
  965. [[ -n "$IMG_PID" ]] && kill -SIGINT "$IMG_PID"
  966. if [[ "$HAS_TVS" -eq 1 ]]; then
  967. get_all_modes
  968. fi
  969. tput cnorm
  970. main_menu
  971. dont_launch=$?
  972. tput civis
  973. clear
  974. fi
  975. stop_joy2key
  976. return $dont_launch
  977. }
  978.  
  979. # calls script with parameters SYSTEM, EMULATOR, ROM, and commandline
  980. function user_script() {
  981. local script="$CONFIGDIR/all/$1"
  982. if [[ -f "$script" ]]; then
  983. bash "$script" "$SYSTEM" "$EMULATOR" "$ROM" "$COMMAND" </dev/tty 2>>"$LOG"
  984. fi
  985. }
  986.  
  987. function restore_cursor_and_exit() {
  988. # if we are not being run from emulationstation (get parent of parent), turn the cursor back on.
  989. if [[ "$(ps -o comm= -p $(ps -o ppid= -p $PPID))" != "emulationstatio" ]]; then
  990. tput cnorm
  991. fi
  992.  
  993. exit 0
  994. }
  995.  
  996. function launch_command() {
  997. local ret
  998. # launch the command
  999. echo -e "Parameters: $@\nExecuting: $COMMAND" >>"$LOG"
  1000. if [[ "$CONSOLE_OUT" -eq 1 ]]; then
  1001. # turn cursor on
  1002. tput cnorm
  1003. eval $COMMAND </dev/tty 2>>"$LOG"
  1004. ret=$?
  1005. tput civis
  1006. else
  1007. eval $COMMAND </dev/tty &>>"$LOG"
  1008. ret=$?
  1009. fi
  1010. return $ret
  1011. }
  1012.  
  1013. function runcommand() {
  1014. get_config
  1015.  
  1016. if ! get_params "$@"; then
  1017. echo "$0 MODE COMMAND [SAVENAME]"
  1018. echo "$0 MODE _SYS_/_PORT_ SYSTEM ROM"
  1019. exit 1
  1020. fi
  1021.  
  1022. # turn off cursor and clear screen
  1023. tput civis
  1024. clear
  1025.  
  1026. rm -f "$LOG"
  1027. echo -e "$SYSTEM\n$EMULATOR\n$ROM\n$COMMAND" >/dev/shm/runcommand.info
  1028. user_script "runcommand-onstart.sh"
  1029.  
  1030. set_save_vars
  1031.  
  1032. load_mode_defaults
  1033.  
  1034. show_launch
  1035.  
  1036. if [[ "$DISABLE_MENU" -ne 1 ]]; then
  1037. if ! check_menu; then
  1038. clear
  1039. restore_cursor_and_exit 0
  1040. fi
  1041. fi
  1042.  
  1043. mode_switch "$MODE_REQ_ID"
  1044.  
  1045. [[ -n "$FB_NEW" ]] && switch_fb_res $FB_NEW
  1046.  
  1047. config_dispmanx "$SAVE_EMU"
  1048.  
  1049. # switch to configured cpu scaling governor
  1050. [[ -n "$GOVERNOR" ]] && set_governor "$GOVERNOR"
  1051.  
  1052. retroarch_append_config
  1053.  
  1054. local ret
  1055. launch_command
  1056. ret=$?
  1057.  
  1058. [[ -n "$IMG_PID" ]] && kill -SIGINT "$IMG_PID"
  1059.  
  1060. clear
  1061.  
  1062. # remove tmp folder for unpacked archives if it exists
  1063. rm -rf "/tmp/retroarch"
  1064.  
  1065. # restore default cpu scaling governor
  1066. [[ -n "$GOVERNOR" ]] && restore_governor
  1067.  
  1068. # if we switched mode - restore preferred mode
  1069. mode_switch "$MODE_ORIG_ID"
  1070.  
  1071. # reset/restore framebuffer res (if it was changed)
  1072. [[ -n "$FB_NEW" ]] && restore_fb
  1073.  
  1074. [[ "$COMMAND" =~ retroarch ]] && retroarchIncludeToEnd "$CONF_ROOT/retroarch.cfg"
  1075.  
  1076. user_script "runcommand-onend.sh"
  1077.  
  1078. restore_cursor_and_exit "$ret"
  1079. }
  1080.  
  1081. runcommand "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement