diff --git a/.gitignore b/.gitignore index b74dd2f..b0b6d92 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ makeself* !bin/suspend !bin/shutdown -!bin/launch \ No newline at end of file +!bin/launch +!bin/game-tracker diff --git a/README.md b/README.md index 9692613..3d52b26 100644 --- a/README.md +++ b/README.md @@ -34,18 +34,38 @@ Currently, the app supports the following features: ## Usage ```bash -minui-power-control & +minui-power-control [OPTIONS] ``` -Before starting `minui-power-control`, make sure the emulator is already running. Replace `` with the actual name of the emulator’s binary. The app will run in the background, watching for power button events and carrying out the actions. Once the emulator closes, the app will automatically exit as well. To make things more convenient and reliable, it’s best to start the app in the emulator’s `launch.sh` script, placing it before the emulator command. This makes sure it launches alongside the emulator, removing the need to start it manually. +**Options:** -Alternatively, it is possible to run `minui-power-control` without specifying an emulator: +- `-e`, `--emulator ` Specify the emulator binary to monitor. +- `-r`, `--rom ` Specify the ROM path. +- `-h`, `--help` Show help message and exit. + +If no emulator binary is specified, a dummy PID file will be created at `/tmp/powercontrol-dummy-pid` and the app will continue running until that file is deleted. This is useful for testing or for scenarios where the name of the emulator is not known in advance. + +**How to use:** + +Before starting `minui-power-control`, make sure the emulator is already running. Use the `-e` or `--emulator` option to specify the emulator’s binary. The app will run in the background, watching for power button events and carrying out the actions. Once the emulator closes, the app will automatically exit as well. To make things more convenient and reliable, it’s best to start the app in the emulator’s `launch.sh` script, placing it before the emulator command. This ensures it launches alongside the emulator, removing the need to start it manually. + +Example: ```bash -minui-power-control & +minui-power-control -e & ``` -In this mode, the app creates a file at `/tmp/powercontrol-dummy-pid` and will continue running until that file is deleted. This is useful for testing or for scenarios where the name of the emulator is not known in advance. +You can also specify the ROM path if needed: + +```bash +minui-power-control -e -r & +``` + +To see all available options, run: + +```bash +minui-power-control --help +``` ## Building diff --git a/bin/game-tracker b/bin/game-tracker new file mode 100644 index 0000000..52ba306 --- /dev/null +++ b/bin/game-tracker @@ -0,0 +1,47 @@ +#!/bin/sh + +TRACKER_PATH="$SDCARD_PATH/Tools/$PLATFORM/Game Tracker.pak/gametimectl.elf" + +main() { + if [ "$#" -lt 1 ] || [ "$#" -gt 1 ]; then + echo "Invalid number of arguments. Usage: $0 " + exit 1 + fi + + if [ ! -f /tmp/powercontrol-rom-path ]; then + echo "ROM path file not found. Unable to $1 the Game Tracker." + exit + fi + + if [ ! -f "$TRACKER_PATH" ]; then + echo "Game tracker $TRACKER_PATH not found. Unable to $1 the Game Tracker." + exit + fi + + ACTION="$1" + ROM_PATH=$(cat /tmp/powercontrol-rom-path) + + if [ ! -f "$ROM_PATH" ]; then + echo "Invalid ROM path: $ROM_PATH" + exit 1 + fi + + case "$ACTION" in + start) + if [ -n "$ROM_PATH" ]; then + "$TRACKER_PATH" start "$ROM_PATH" + fi + ;; + stop) + if [ -n "$ROM_PATH" ]; then + "$TRACKER_PATH" stop "$ROM_PATH" + fi + ;; + *) + echo "Unknown action: $ACTION. Use 'start' or 'stop'." + exit 1 + ;; + esac +} + +main "$@" \ No newline at end of file diff --git a/bin/launch b/bin/launch index 3c62804..007b12c 100755 --- a/bin/launch +++ b/bin/launch @@ -9,8 +9,16 @@ fi export PATH="$BIN_DIR/$architecture:$BIN_DIR/$PLATFORM:$BIN_DIR:$PATH" print_usage() { - echo "Usage: $0 " + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Options:" + echo " -e, --emulator Specify the emulator binary to monitor." + echo " -r, --rom Specify the ROM path." + #echo " -s, --save-key Specify the save key." + echo " -h, --help Show this help message and exit." + echo "" echo "If no emulator binary is specified, a dummy PID file will be created." + exit 1 } cleanup() { @@ -22,6 +30,8 @@ cleanup() { fi rm -f /tmp/powercontrol-emulator-pid rm -f /tmp/powercontrol-dummy-pid + rm -f /tmp/powercontrol-rom-path + rm -f /tmp/powercontrol-suspend-active } main() { @@ -67,16 +77,57 @@ main() { exit 1 fi + if [ ! -f "$BIN_DIR/game-tracker" ]; then + echo "game-tracker script not found." + exit 1 + fi + + chmod +x "$BIN_DIR/$PLATFORM/minui-presenter" chmod +x "$BIN_DIR/$architecture/button-handler" chmod +x "$BIN_DIR/shutdown" chmod +x "$BIN_DIR/suspend" + chmod +x "$BIN_DIR/game-tracker" + + EMULATOR_BIN="" + ROM_PATH="" + #SAVE_KEY="" + + while [ $# -gt 0 ]; do + case "$1" in + -e|--emulator) + EMULATOR_BIN="$2" + shift 2 + ;; + -r|--rom) + ROM_PATH="$2" + shift 2 + ;; + # -s|--save-key) + # SAVE_KEY="$2" + # shift 2 + # ;; + -h|--help) + print_usage + ;; + -*) + echo "Unknown option: $1" + print_usage + ;; + *) + # Treat positional argument as -e (emulator binary) + EMULATOR_BIN="$1" + shift + ;; + esac + done + + if [ -n "$ROM_PATH" ]; then + echo "$ROM_PATH" > /tmp/powercontrol-rom-path + echo "ROM path: $ROM_PATH" + fi - if [ "$#" -eq 0 ]; then - echo "Emulator process not specified. Creating a dummy PID file." - touch /tmp/powercontrol-dummy-pid - elif [ "$#" -eq 1 ]; then - EMULATOR_BIN="$1" + if [ -n "$EMULATOR_BIN" ]; then EMULATOR_PROCESS_PID="" for _ in $(seq 1 10); do @@ -96,9 +147,11 @@ main() { echo "$EMULATOR_PROCESS_PID" > /tmp/powercontrol-emulator-pid echo "Emulator PID: $EMULATOR_PROCESS_PID" else - print_usage + echo "Creating dummy PID file..." + touch /tmp/powercontrol-dummy-pid fi + echo "Starting button-handler..." button-handler & HANDLER_PROCESS_PID=$! diff --git a/bin/shutdown b/bin/shutdown index 3cd9737..7698bfc 100755 --- a/bin/shutdown +++ b/bin/shutdown @@ -28,6 +28,7 @@ show_message() { shutdown_system() { echo "Shutting down system..." show_message "Powering off" forever + game-tracker stop sync poweroff while :; do @@ -36,8 +37,10 @@ shutdown_system() { } main() { + echo "Starting shutdown script..." + if [ -f /tmp/powercontrol-emulator-pid ]; then - PROCESS_PID=$(cat /tmp/emulator_pid) + PROCESS_PID=$(cat /tmp/powercontrol-emulator-pid) echo "Emulator PID file found. Killing the emulator process with PID: $PROCESS_PID" if [ -z "$PROCESS_PID" ] || ! kill -0 "$PROCESS_PID" 2>/dev/null; then diff --git a/bin/suspend b/bin/suspend index f6f6d55..a8eafc1 100755 --- a/bin/suspend +++ b/bin/suspend @@ -25,6 +25,7 @@ suspend_system() { if [ -n "$PID" ]; then echo "Suspending emulator process with PID: $PID" + game-tracker stop kill -STOP "$PID" || true fi @@ -32,13 +33,14 @@ suspend_system() { if [ -n "$PID" ]; then echo "Resuming emulator process with PID: $PID" + game-tracker start kill -CONT "$PID" || true fi } fallback_suspend() { PID="$1" - echo "Fallback suspend for emulator process with PID: $PID" + echo "Using fallback suspend method..." if [ -z "$PID" ]; then echo "No PID provided for fallback suspend." @@ -48,18 +50,22 @@ fallback_suspend() { if [ ! -f /tmp/powercontrol-suspend-active ]; then echo "Pausing emulator process with PID: $PID" kill -STOP "$PID" || true + game-tracker stop touch /tmp/powercontrol-suspend-active else echo "Resuming emulator process with PID: $PID" kill -CONT "$PID" || true + game-tracker start rm -f /tmp/powercontrol-suspend-active fi } main() { + echo "Starting suspend script..." + if [ -f /tmp/powercontrol-emulator-pid ]; then - PROCESS_PID=$(cat /tmp/emulator_pid) - echo "Emulator PID file found. Killing the emulator process with PID: $PROCESS_PID" + PROCESS_PID=$(cat /tmp/powercontrol-emulator-pid) + echo "Emulator PID file found with PID: $PROCESS_PID" if [ -z "$PROCESS_PID" ] || ! kill -0 "$PROCESS_PID" 2>/dev/null; then echo "Emulator process $PROCESS_PID not found." diff --git a/src/button-handler.go b/src/button-handler.go index bdd11b4..59b9c7b 100644 --- a/src/button-handler.go +++ b/src/button-handler.go @@ -74,7 +74,11 @@ func main() { func runScript(scriptPath string) { cmd := exec.Command(scriptPath) - if err := cmd.Run(); err != nil { - log.Printf("Failed to run %s script: %v", scriptPath, err) + output, err := cmd.CombinedOutput() + if err != nil { + log.Printf("Failed to run %s script:\n%v", scriptPath, err) + } + if len(output) > 0 { + log.Printf("Script %s executed successfully:\n%s", scriptPath, output) } }