conjure-os/app.go
2025-07-19 19:58:57 +00:00

119 lines
2.6 KiB
Go

package main
import (
"conjure-os/lib/inputs"
"conjure-os/lib/models"
"conjure-os/lib/provider"
"context"
"fmt"
"os"
"os/exec"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
var (
games []models.Metadata
lastEmitTimestamp = time.Now().Add(-10 * time.Second)
emitInterval = 150 * time.Millisecond
gameIsOpen = false
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// Startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
inputs.Start(a.onControllerChange)
provider.Update()
}
func (a *App) onControllerChange(state inputs.ControllerState) {
now := time.Now()
if now.Sub(lastEmitTimestamp) >= emitInterval && !gameIsOpen {
if state.Buttons != 0 {
for _, button := range inputs.ConjureControllerButtons {
if state.Buttons&(1<<button) != 0 {
fmt.Printf("Button %s pressed\n", (button).String())
}
}
fmt.Printf("Button was pressed! %d\n", state.Buttons)
}
if state.Joystick.X != 127 || state.Joystick.Y != 127 {
fmt.Printf("Joystick moved! %d - %d\n", state.Joystick.X, state.Joystick.Y)
}
fmt.Printf("Joystick: X=%d Y=%d Buttons=%08b\n", state.Joystick.X, state.Joystick.Y, state.Buttons)
runtime.EventsEmit(a.ctx, "controller_change", state)
lastEmitTimestamp = now
}
}
func (a *App) StartGame(id string) {
found := false
fmt.Println(id)
for _, game := range games {
fmt.Println(game.Id)
if game.Id == id {
found = true
gamePath := provider.ExtractGame(game)
cmd := exec.Command(gamePath)
gameIsOpen = true
// Optional: attach current terminal's std streams
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
// Start the process
if err := cmd.Start(); err != nil {
fmt.Println("Failed to start:", err)
return
}
fmt.Printf("Process started with PID %d\n", cmd.Process.Pid)
// Optional: wait for it to finish
if err := cmd.Wait(); err != nil {
fmt.Println("Process exited with error:", err)
} else {
fmt.Println("Process exited successfully.")
}
gameIsOpen = false
}
}
fmt.Printf("Found %b", found)
fmt.Println()
}
func (a *App) LoadGames() []models.Metadata {
games = provider.GetConjureGameInfo()
return games
}
func (a *App) LoadGamesNewModel() []models.Game {
return []models.Game{}
}
func (a *App) LoadImage(gameId string, imageSrc string) provider.FileBlob {
blob := provider.LoadImage(gameId, imageSrc)
if blob == nil {
return provider.FileBlob{}
}
return *blob
}