Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ makeself*

!bin/suspend
!bin/shutdown
!bin/launch
!bin/launch
!bin/game-tracker
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,38 @@ Currently, the app supports the following features:
## Usage

```bash
minui-power-control <emulator> &
minui-power-control [OPTIONS]
```

Before starting `minui-power-control`, make sure the emulator is already running. Replace `<emulator>` 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 <emulator_binary>` Specify the emulator binary to monitor.
- `-r`, `--rom <rom_path>` 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 <emulator_binary> &
```

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 <emulator_binary> -r <rom_path> &
```

To see all available options, run:

```bash
minui-power-control --help
```

## Building

Expand Down
47 changes: 47 additions & 0 deletions bin/game-tracker
Original file line number Diff line number Diff line change
@@ -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 <start|stop>"
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 "$@"
67 changes: 60 additions & 7 deletions bin/launch
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ fi
export PATH="$BIN_DIR/$architecture:$BIN_DIR/$PLATFORM:$BIN_DIR:$PATH"

print_usage() {
echo "Usage: $0 <emulator-binary>"
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -e, --emulator <emulator_binary> Specify the emulator binary to monitor."
echo " -r, --rom <rom_path> Specify the ROM path."
#echo " -s, --save-key <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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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=$!
Expand Down
5 changes: 4 additions & 1 deletion bin/shutdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ show_message() {
shutdown_system() {
echo "Shutting down system..."
show_message "Powering off" forever
game-tracker stop
sync
poweroff
while :; do
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions bin/suspend
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ suspend_system() {

if [ -n "$PID" ]; then
echo "Suspending emulator process with PID: $PID"
game-tracker stop
kill -STOP "$PID" || true
fi

"$SCRIPT"

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."
Expand All @@ -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."
Expand Down
8 changes: 6 additions & 2 deletions src/button-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}