Differences

This shows you the differences between two versions of the page.

Link to this comparison view

launch_a_script_legacy [2023/10/21 03:26] – created atarilaunch_a_script_legacy [2023/10/21 04:43] (current) – remove redundancy atari
Line 84: Line 84:
 ===== Watch for a game start/stop event ===== ===== Watch for a game start/stop event =====
  
-Batocera **5.23** and higher supports running a script right before a game launches and/or after a game is exited. Here are some examples: +<WRAP center round info
-  * Automatic controller setup +This still functions the same in current version of Batocera[[:launch_a_script#watch_for_a_game_start_stop_event|Refer to the current page for its info.]]
-  * Automatic scraping for new games +
-  * Change screen resolution (though it might be better to use switchres for this) +
-  * Sync savestates to an external/network device +
-  * ... give me your idea here +
- +
-Place an executable script (can have any filename) with the correct [[https://en.wikipedia.org/wiki/Shebang_(Unix)|setted shebang]] (first line!) or an executable binary into directory ''/userdata/system/scripts/''+
- +
-<WRAP center round important+
-Game start/stop event scripts must be marked as executable with the ''chmod +x'' commandFor example: +
- +
-<code> +
-Open up a text editor to create your script: +
-nano /userdata/system/scripts/first_script.sh +
-# Write in your script, save and exit. +
-# Then to add the executable bit to it: +
-chmod +x /userdata/system/scripts/first_script.sh +
-</code> +
- +
-This means that if your userdata partition is of the NTFS, exFAT or older file system that are missing the executable bit functionality, it will fail to execute.+
 </WRAP> </WRAP>
- 
-You can add as many subfolders as you want, every script placed there will be executed. To distinguish between **START** or **STOP** condition the first argument parsed in the script have either of these flags: 
- 
-  * ''gameStart'' is passed to your scripts if you select a game from EmulationStation (Game Starts!) 
-  * ''gameStop'' is passed to your scripts if you are going back from a game to EmulationStation (Game Stops!) 
- 
-**If you do not set cases for first argument, then the script is executed on every start and on every end of a game.** 
- 
-==== What is parsed ==== 
- 
-Table of parsed arguments in correct order and their functions: 
- 
-^ Arg no. ^ Parsed Parameter              ^ Usage ^ 
-|       | ''gameStart'' or ''gameStop'' | To distinguish between START or STOP condition | 
-|       | ''systemName''                | The system shortname as is in ''es_system.cfg'', eg. **atari2600** | 
-|       | ''system.config['emulator']'' | The emulator settings, eg. **libretro** | 
-|       | ''system.config['core']''     | The emulator core you have chosen, eg. **stella** | 
-|       | ''args.rom''                  | The full rom path, eg. **/userdata/roms/atari2600/Mysterious Thief, A (USA).zip** | 
- 
-==== Simple game start/stop script as an example ==== 
- 
-At first create the directory where the scripts need to be set up. [[access_the_batocera_via_ssh|Connect through SSH]] and type ''mkdir /userdata/system/scripts''. After this we can set up our first script by typing ''nano /userdata/system/scripts/first_script.sh''. The filename can be anything readable by bash. 
- 
-Here's the template script: 
- 
-<code bash| first_script.sh> 
-#!/bin/bash 
-# This is an example file of how gameStart and gameStop events can be used. 
- 
-# It's good practice to set all constants before anything else. 
-logfile=/tmp/scriptlog.txt 
- 
-# Case selection for first parameter parsed, our event. 
-case $1 in 
-    gameStart) 
-        # Commands in here will be executed on the start of any game. 
-        echo "START" > $logfile 
-        echo "$@" >> $logfile 
-    ;; 
-    gameStop) 
-        # Commands here will be executed on the stop of every game. 
-        echo "END" >> $logfile 
-    ;; 
-esac 
-</code> 
- 
-Now you can see a log file created ''/tmp/scriptlog.txt'', that parsed all arguments in this file. This is just a small test of course. You can check out [[:access_the_batocera_via_ssh|the SSH page]] and [[:usage_of_batocera-settings|the usage of batocera-settings]] page for general and Batocera-specific commands. 
  
 ===== EmulationStation scripting ===== ===== EmulationStation scripting =====
  
-In case Batocera's provided scripting functionality is not sufficientES itself also triggers scripts of its own volition. Extra scripts can be created at ''/userdata/system/configs/emulationstation/scripts/<event name>/<script>.sh''; unlike regular Batocera scripts the event named will trigger all scripts in the respective event folder. For instance, to have a script execute at every ''game-start'' event, it must be placed in ''/userdata/system/configs/emulationstation/scripts/game-start/''.+EmulationStation scripting has not changed yetso it is still on [[:launch_a_script|the current page]].
  
-<WRAP center round important> +===== Real use cases =====
-All EmulationStation scripts must be marked as executable with the ''chmod +x'' command. For example:+
  
-<code+<WRAP center round tip
-# Open up a text editor to create your script: +Only the older script examples are herefor more examples [[:launch_a_script|check the current page]].
-nano /userdata/system/configs/emulationstation/scripts/game-start/first_script.sh +
-# Write in your script, save and exit. +
-# Then to add the executable bit to it: +
-chmod +x /userdata/system/configs/emulationstation/scripts/game-start/first_script.sh +
-</code> +
- +
-This means that if your userdata partition is of the NTFS, exFAT or older file system that are missing the executable bit functionality, it will fail to execute.+
 </WRAP> </WRAP>
- 
-^ Event name           ^ Arguments ^ Notes ^ 
-| ''game-start''       | ROM name ''rom'', ROM path ''basename'' | When a game starts. Nearly identical to Batocera's ''gameStart'', however this doesn't include as much information and only triggers when it's ES itself that starts it. | 
-| ''game-end''         | N/A | When a game ends. Nearly identical to Batocera's ''gameStop'', however this only triggers when it's ES itself that ends it. | 
-| ''game-selected''    | ''getSourceFileData()->getSystem()->getName()'', ''getPath()'', ''getName()'' | New to Batocera **v33**. Whichever game is currently being hovered over. Includes games shown during the screensaver. | 
-| ''system-selected''  | System ''getSelected()->getName()'' | New to Batocera **v33**. Whichever system is currently being hovered over in the system list. | 
-| ''theme-changed''    | Theme being switched to ''theme_set->getSelected()'', Previous theme ''oldTheme'' | When a different theme is selected. Mostly used for theme/element reloading. | 
-| ''settings-changed'' | N/A | When a system setting is saved. | 
-| ''controls-changed'' | N/A | When a controller mapping is saved from the **CONTROLLER MAPPING** menu. | 
-| ''config-changed''   | N/A | Whenever any configuration, be it system settings or controller mappings, is changed. | 
-| ''quit''             | N/A | When the system is told to do a regular shutdown. | 
-| ''reboot''           | N/A | When the system is told to do a reboot. | 
-| ''shutdown''         | N/A | When the system is told to do a fast shutdown. | 
-| ''sleep''            | N/A | When the system is told to sleep. | 
-| ''wake''             | N/A | When the system is told to wake from sleep. | 
- 
-EmulationStation sends different arguments to these scripts based on the event type. For game-related events: 
- 
-^ Arg no. ^ Parsed parameter ^ Usage ^ 
-| 1       | ''system''       | The current system of the game. | 
-| 2       | ''rom''          | The filename of the current game. | 
-| 3       | ''romname''      | The name of the game as specified in its metadata. | 
- 
-<WRAP center round todo> 
-All events need to be added here. 
-</WRAP> 
- 
- 
-==== Simple EmulationStation script example ==== 
- 
-This script is an example of an EmulationStation ''game-selected'' script for controlling the Pixelcade display.  More robust results with the Pixelcade may be achieved with the [[https://pixelcade.org/batocera/|Pixelcade Batocera scripts]]. 
- 
-<file bash pixelcade.sh> 
-#!/bin/bash 
- 
-# Save the arguments into variables. 
-system="${1}" 
-rom="${2}" 
-romname="${3}" 
- 
-# Convert an argument into another value. 
-if [[ "${system}" == "fbneo" ]]; then 
-        system="mame" 
-fi 
- 
-# Switch case for certain systems. 
-case ${system} in 
-  fbneo) 
-    system="mame" 
-    ;; 
-  scummvm) 
-    rom="${rom%.*}" 
-    ;; 
-esac 
- 
-# Execute this part every time this event triggers. 
-curl -G \ 
-        --data-urlencode "t=${romname}" \ 
-        http://127.0.0.1:8080/arcade/stream/${system}/`basename ${rom}` 
-</file> 
- 
-===== Real use cases ===== 
- 
-==== Batocera after boot scripts ==== 
- 
-  * [[https://forum.batocera.org/d/7974-add-a-cronjob-to-turn-off-device-at-a-given-time/5|Shutdown Batocera at a given time.]] 
-<file bash custom.sh> 
-#!/bin/bash 
-while true; do 
-    currenthour=$(date +%k%M) 
-    if [ $currenthour -eq 2355 ]; then 
-        shutdown -h now 
-    fi 
-    sleep 30 
-done 
-</file> 
- 
-  * Disable a specific network interface (e.g. ''eth0'', ''eth1'', etc.): 
-<file bash custom.sh> 
-#!/bin/bash 
-ifconfig eth1 down 
-</file> 
  
 ==== Batocera event watcher scripts ==== ==== Batocera event watcher scripts ====
  
-  * [[https://github.com/batocera-linux/batocera.linux/pull/757#issuecomment-500219341|Output text to a file]] 
-<file bash outputsomething.sh> 
-#!/bin/bash 
-# by cyperghost for batocera 
  
-testfile="/userdata/system/scripts/output.txt" 
-echo "END Script!" >> $testfile 
-echo "Parameter: $@" >> $testfile 
-echo "Systemname: $1" >> $testfile 
-echo "Emulatorcore: $2" >> $testfile 
-echo "ROM: $(basename "$3")" >> $testfile  
-[[ "$4" == "libretro" ]] && echo "You are free!" >> $testfile || echo "No hotkey is pure hell eh??" >> $testfile 
-</file> 
  
   * [[https://wiki.batocera.org/doku.php?id=method_to_not_lose_srm_states_and_other_meta-_data|Method to not lose SRM and other meta-data]]   * [[https://wiki.batocera.org/doku.php?id=method_to_not_lose_srm_states_and_other_meta-_data|Method to not lose SRM and other meta-data]]
Line 510: Line 342:
 exit 1   exit 1  
  
-</file> 
- 
-  * [[https://forum.batocera.org/d/3243-installer-un-script|Change LED colors while running games, and shutdown on button press]] 
-<file bash shutdownsimple.sh> 
-#!/bin/bash 
-LED1=22 
-LED2=27 
-SHUTDOWN=3  
- 
-case "$1" in 
-start) 
-    # LED init 
-    echo "$LED1" > /sys/class/gpio/export 
-    echo "$LED2" > /sys/class/gpio/export 
-    echo out > /sys/class/gpio/gpio$LED1/direction 
-    echo 0 > /sys/class/gpio/gpio$LED1/value 
-    echo out > /sys/class/gpio/gpio$LED2/direction 
-    echo 1 > /sys/class/gpio/gpio$LED2/value 
- 
-    #Button init 
-    echo "$SHUTDOWN" > /sys/class/gpio/export  
-    echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction  
-    #This loop continuously checks if the shutdown button was pressed  
-    #It sleeps as long as that has not happened.  
-    buttonstate1=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 
-    shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 
-    while [ $shutdownSignal = $buttonstate1 ]; do  
-        shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 
-        sleep 0.5 
-    done 
-    shutdown -h now  
-;; 
-stop) 
-      #unexport all GPIOs 
-       #echo "$SHUTDOWN" > /sys/class/gpio/unexport  
-       echo "$LED1" > /sys/class/gpio/unexport  
-       echo "$LED2" > /sys/class/gpio/unexport  
-;; 
-esac 
-exit $? 
- 
-</file> 
- 
-  * [[https://forum.batocera.org/d/4428-script-load-latest-created-savegame|Load latest created savestate automatically]] 
-<file bash loadlatestsave.sh> 
-#!/bin/bash 
- 
-# disable auto load/save state inside ES 
-# add 'global.retroarch.savestate_auto_load=true' 
-# to your batocera.conf 
-# by cyperghost aka lala for BATOCERA 
- 
-# download this script to '/userdata/system/scripts' and set executable bit 
- 
- 
-rom_no_ext="$(basename "${5%.*}")" 
-sav_path="/userdata/saves/$2" 
- 
-[[ "$(batocera-settings get global.autosave)" -eq 1 ]] && exit 
-[[ "$(batocera-settings get global.retroarch.savestate_auto_load)" == "true" ]] || exit 
- 
-if [[ $1 == "gameStart" ]]; then 
-    file="$(/bin/ls "$sav_path/$rom_no_ext."* -turR1A | tail -1)" 
-    [[ -n "$file" ]] || exit 
-    [[ "${file##*.}" == "png" ]] && file="${file%.*}" 
-    [[ -f "$file" ]] || exit 
-    cp -f "$file" "$sav_path/$rom_no_ext.state.auto" 
-fi 
- 
-if [[ $1 == "gameStop" ]]; then 
-    rm -f  "$sav_path/$rom_no_ext.state.auto" 
-fi 
- 
-</file> 
- 
-  * Faff about with audio settings. 
-<file bash audio-shenanigans.sh> 
-case "$1" in 
- 
-    start) 
-        # Get the current audio profile. 
-        defaultprofile="$(batocera-audio get-profile)" 
-        # Get the current audio device. 
-        defaultaudio="$(batocera-audio get)" 
-        # Set the audio device to HDMI-2. 
-        batocera-audio set HDMI-2 
-        # Set the default "auto" audio profile. 
-        batocera-audio set-profile auto 
-         
-        # Unmute the audio device. 
-        batocera-audio setSystemVolume unmute 
-        # Set audio level to 69. 
-        batocera-audio setSystemVolume 69 
-        # Test the audio device with Mallet.wav 
-        batocera-audio test 
-    ;; 
-    stop) 
-        # Toggle the audio mute. 
-        batocera-audio setSystemVolume mute-toggle 
-</file> 
- 
-==== Batocera boot scripts ==== 
- 
-=== Delay Syncthing until after everything else === 
- 
-<file bash boot-custom.sh> 
-#!/bin/bash 
-# Delay Syncthing until other boot stuff has completed. 
- 
-if [[ "$1" != "start" ]] && exit 0; then 
-  if mv /etc/init.d/S27syncthing /etc/init.d/S99syncthing; then 
-    echo "Successfully delayed Syncthing." 
-  elif ls /etc/init.d/S99syncthing; then 
-    echo "Command has already run!" 
-    exit 0 
-  else 
-    echo "Syncthing failed to be delayed." 
-    touch /userdata/check-boot-custom-sh 
-    shutdown -h now 
-    exit 1 
-  fi 
-fi 
- 
-exit $? 
-</file> 
- 
-==== EmulationStation scripts ==== 
- 
-=== Play a video on a second screen === 
- 
-[[https://forum.batocera.org/d/7129-dynamic-marquee-for-pi4-working|Original forum post with a deeper explanation.]] Artwork needs to be sourced and placed in the appropriate ''Marquee'' and ''roms/Marquee'' folders first. 
- 
-Place ''game.sh'' into ''system/configs/emulationstation/scripts/game-selected'' 
- 
-<file bash game.sh> 
-#!/bin/bash 
-System=$1 #system name 
-Romname=${2%.*} #romname 
-rom=${Romname##*/} 
-/userdata/marquee.sh Gameselected $System "$rom" 
-</file> 
- 
-Place ''system.sh'' into ''system/configs/emulationstation/scripts/system-selected'' 
- 
-<file bash system.sh> 
-#!/bin/bash 
-System=$1 #System name 
-/userdata/marquee.sh Systemselected $System & 
-</file> 
- 
-Place ''marquee.sh'' in ''/userdata'' 
- 
-<file bash marquee.sh> 
-#!/bin/bash 
- 
-case $1 in 
-Start) 
-Romname=$3 
-Gamepath=$2 
-marqueeimage=$Gamepath/images/$romname-marquee.png 
-if [ -f "/userdata/roms/Marquee/videos/$Romname.mp4" ] 
-then 
-ffmpeg -i /userdata/roms/Marquee/videos/$Romname.mp4 -vf scale=1280:720 -sws_flags bilinear -pix_fmt rgb565le -f fbdev /dev/fb0 
- 
-fi 
- 
-if [ -f "/userdata/roms/Marquee/hires/$Romname.jpg" ] 
-then 
-fbv /userdata/roms/Marquee/hires/$Romname.jpg -fer 
-elif [ -f "$marqueeimage" ] 
-then 
-fbv $marqueeimage -fer 
-else 
-fbv /userdata/roms/mame/images/mame.png -fer 
-fi 
- 
-;; 
-Gameselected) 
-System=$2 #system name 
-Romname=$3 #romname 
- 
-if [ -f "/userdata/roms/Marquee/$Romname.png" ] 
-then 
-fbv /userdata/roms/Marquee/$Romname.png -fer 
-elif [ -f "/userdata/roms/$System/images/$Romname-marquee.png" ] 
-then 
-fbv "/userdata/roms/$System/images/$Romname-marquee.png" -fer 
-else 
-fbv /userdata/roms/Marquee/mame.png -fer 
-fi 
- 
-;; 
- 
-Systemselected) 
-imagepath="/userdata/roms/sysimages/$2" 
-if [ -f "$imagepath.png" ] 
-then 
-fbv "$imagepath.png" -fer 
-else 
-fbv /userdata/roms/mame/images/mame.png -fer 
-fi 
- 
-;; 
- 
-esac 
-</file> 
- 
-Place ''script.sh'' in ''system/scripts'' 
- 
-<file bash script.sh> 
-#!/bin/bash 
- 
-case $1 in 
-gameStart) 
- 
-gamepath=${5%/*} 
-romname=${5##*/} 
-/userdata/marquee.sh Start $gamepath ${romname%.*} & 
-;; 
- 
-gameStop) 
-killall ffmpeg 
-;; 
-esac 
 </file> </file>
  
  • launch_a_script_legacy.1697858785.txt.gz
  • Last modified: 3 years ago
  • by atari