From 8a5de26ca3f9017d7ca956209493623d0562e1fb Mon Sep 17 00:00:00 2001 From: Trit0 Date: Tue, 1 Jul 2025 16:30:23 -0400 Subject: [PATCH] test --- app.go | 7 +- frontend/src/utils/use-keyboard-navigation.ts | 2 + lib/inputs/joystick.go | 89 ++++++++++++++++++- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index 5588f2d..0eb4ea0 100644 --- a/app.go +++ b/app.go @@ -6,6 +6,7 @@ import ( "conjure-os/lib/provider" "context" "fmt" + "github.com/wailsapp/wails/v2/pkg/runtime" "os" "os/exec" ) @@ -26,10 +27,14 @@ func NewApp() *App { // so we can call the runtime methods func (a *App) startup(ctx context.Context) { a.ctx = ctx - inputs.Start() + inputs.Start(a.onControllerChange) provider.Update() } +func (a *App) onControllerChange(data any) { + runtime.EventsEmit(a.ctx, "controller_change", data) +} + func (a *App) StartGame(id string) { for _, game := range games { if game.Id == id { diff --git a/frontend/src/utils/use-keyboard-navigation.ts b/frontend/src/utils/use-keyboard-navigation.ts index 8de2fbf..fe7f3ae 100644 --- a/frontend/src/utils/use-keyboard-navigation.ts +++ b/frontend/src/utils/use-keyboard-navigation.ts @@ -1,8 +1,10 @@ import { onMounted, onBeforeUnmount } from 'vue'; import { KeyboardManager } from "./keyboard-manager"; +import { EventsOn } from "../../wailsjs/runtime"; export function useKeyboardNavigation(): void { onMounted(() => { + EventsOn("controller_change", (data) => console.log(data)); window.addEventListener('keydown', KeyboardManager.handle.bind(KeyboardManager)); }); diff --git a/lib/inputs/joystick.go b/lib/inputs/joystick.go index 04f1271..d73c8d4 100644 --- a/lib/inputs/joystick.go +++ b/lib/inputs/joystick.go @@ -19,8 +19,82 @@ const ( JS_EVENT_INIT = 0x80 // Initial state of device ) -func Start() { - return +type Controller struct { + Device *hid.Device +} + +func (c *Controller) ReadState() error { + buf := make([]byte, 64) + _, err := c.Device.Read(buf) + if err != nil { + return err + } + + fmt.Printf("Joystick: X=%d Y=%d Buttons=%08b\n", buf[0], buf[1], buf[6]) + return nil +} + +type Vec2B struct { + X, Y byte +} + +type ControllerState struct { + joystick Vec2B + buttons byte +} + +type ConjureControllerButton int + +const ( + ButtonA ConjureControllerButton = iota //0 + ButtonB + ButtonC + + Button1 + Button2 + Button3 + + ButtonStart + ButtonPower // 7 +) + +var ConjureControllerButtons = []ConjureControllerButton{ + ButtonA, + ButtonB, + ButtonC, + + Button1, + Button2, + Button3, + + ButtonStart, + ButtonPower, +} + +func (s ConjureControllerButton) String() string { + switch s { + case ButtonA: + return "ButtonA" + case ButtonB: + return "ButtonB" + case ButtonC: + return "ButtonC" + case Button1: + return "Button1" + case Button2: + return "Button2" + case Button3: + return "Button3" + case ButtonStart: + return "ButtonStart" + case ButtonPower: + return "ButtonPower" + default: + return "Unknown" + } +} + +func Start(onStateChange func(any)) { fmt.Println("Opening devices") const vendorID = 0x0079 const productID = 0x0006 @@ -61,14 +135,25 @@ func Start() { x := buf[0] // Horizontal axis (0–255) y := buf[1] // Vertical axis (0–255) buttons := buf[6] // Buttons as bitfield + state := ControllerState{ + joystick: Vec2B{x, y}, + buttons: buttons, + } // fmt.Printf("Joystick X: %d, Y: %d, Buttons: %08b\n", x, y, buttons) if buttons != 0 { + for _, button := range ConjureControllerButtons { + if buttons&(1<