From 5c7c429c70113b288d0a6238f8dc53f3c0ca5e48 Mon Sep 17 00:00:00 2001 From: Trit0 Date: Thu, 3 Jul 2025 19:55:34 -0400 Subject: [PATCH] try to support both controllers --- frontend/src/models/controller-state.ts | 1 + lib/inputs/joystick.go | 83 +++++++++---------------- 2 files changed, 29 insertions(+), 55 deletions(-) diff --git a/frontend/src/models/controller-state.ts b/frontend/src/models/controller-state.ts index 441d3f4..2154d7f 100644 --- a/frontend/src/models/controller-state.ts +++ b/frontend/src/models/controller-state.ts @@ -1,4 +1,5 @@ export interface ControllerState { + id?: number, joystick: { x: number, y: number, diff --git a/lib/inputs/joystick.go b/lib/inputs/joystick.go index 375e3dc..f3b123e 100644 --- a/lib/inputs/joystick.go +++ b/lib/inputs/joystick.go @@ -47,6 +47,7 @@ type Vec2B struct { } type ControllerState struct { + Id int `json:"id"` Joystick Vec2B `json:"joystick"` Buttons byte `json:"buttons"` } @@ -107,74 +108,46 @@ func Start(onStateChange func(ControllerState)) { const vendorID = 0x0079 const productID = 0x0006 - // Enumerate all matching devices devices := hid.Enumerate(vendorID, productID) if len(devices) == 0 { - fmt.Printf("Device with VID:PID %04X:%04X not found", vendorID, productID) + fmt.Printf("Device with VID:PID %04X:%04X not found\n", vendorID, productID) return } - for _, d := range devices { - fmt.Printf("Found: %s - VID:%04X PID:%04X\n", d.Product, d.VendorID, d.ProductID) - } - - // Open the first matching device - - for i, deviceDetected := range devices { - - fmt.Printf("device %d detected\n", i) - device, err := deviceDetected.Open() - if err != nil { - fmt.Printf("Failed to open device: %v", err) - return - } - defer device.Close() - - controller := Controller{Device: device} - - fmt.Println("Reading data... Press Ctrl+C to exit") - - buf := make([]byte, 32) // Adjust size if needed - for { - state, err := controller.ReadState(buf) + for i, d := range devices { + fmt.Printf("Found device %d: %s - VID:%04X PID:%04X\n", i, d.Product, d.VendorID, d.ProductID) + go func(i int, d hid.DeviceInfo) { + device, err := d.Open() if err != nil { - fmt.Printf("Read error: %v", err) + fmt.Printf("Failed to open device %d: %v\n", i, err) return } + defer device.Close() - if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 { - fmt.Printf("State changed on device %d\n", i) - onStateChange(*state) + controller := Controller{Device: device} + buf := make([]byte, 32) + + fmt.Printf("Reading data from device %d... Press Ctrl+C to exit\n", i) + for { + state, err := controller.ReadState(buf) + if err != nil { + fmt.Printf("Read error on device %d: %v\n", i, err) + return + } + + state.Id = i + + if state.Buttons != 0 || state.Joystick.X != 127 || state.Joystick.Y != 127 { + fmt.Printf("State changed on device %d\n", i) + onStateChange(*state) + } } - } + }(i, d) } - fmt.Println("Out") - - // Open the joystick device file - // file, err := os.Open("/dev/input/js0") - // if err != nil { - // fmt.Println("Error opening joystick:", err) - // return - // } - // defer file.Close() - - // // Continuously read joystick events - // for { - // var e JoystickEvent - // err := binary.Read(file, binary.LittleEndian, &e) - // if err != nil { - // fmt.Println("Error reading joystick event:", err) - // return - // } - - // // Handle the event - // handleJoystickEvent(e) - - // // Sleep to avoid flooding output - // time.Sleep(10 * time.Millisecond) - // } + // Prevent the function from exiting + select {} } // handleJoystickEvent processes joystick events.