Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
196 lines
5.4 KiB
Go
196 lines
5.4 KiB
Go
//go:build !ios
|
|
|
|
package gamepad
|
|
|
|
import (
|
|
"runtime"
|
|
"strconv"
|
|
"sync"
|
|
"unsafe"
|
|
|
|
"github.com/ebitengine/purego"
|
|
"github.com/ebitengine/purego/objc"
|
|
)
|
|
|
|
// macOS reads pads through GameController.framework: GCController's
|
|
// controllers with an extended gamepad profile, which Xbox, PlayStation,
|
|
// Switch Pro and MFi controllers have. The framework finds controllers
|
|
// on the main run loop, which a running MyGo app turns.
|
|
|
|
var (
|
|
gcOnce sync.Once
|
|
gcOK bool
|
|
|
|
poolPush, poolPop uintptr
|
|
sendFloat func(id objc.ID, sel objc.SEL) float32
|
|
|
|
selControllers, selCount, selObjectAt, selExtended, selVendor, selUTF8,
|
|
selValue, selXAxis, selYAxis, selRetain, selRelease, selResponds objc.SEL
|
|
buttonSels [ButtonCount]objc.SEL
|
|
selDpad, selLeftStick, selRightStick, selUp, selDown, selLeft, selRight objc.SEL
|
|
)
|
|
|
|
func loadGC() bool {
|
|
gcOnce.Do(func() {
|
|
if _, err := purego.Dlopen("/System/Library/Frameworks/GameController.framework/GameController", purego.RTLD_LAZY|purego.RTLD_GLOBAL); err != nil {
|
|
return
|
|
}
|
|
libobjc, err := purego.Dlopen("/usr/lib/libobjc.A.dylib", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if poolPush, err = purego.Dlsym(libobjc, "objc_autoreleasePoolPush"); err != nil {
|
|
return
|
|
}
|
|
if poolPop, err = purego.Dlsym(libobjc, "objc_autoreleasePoolPop"); err != nil {
|
|
return
|
|
}
|
|
msgSend, err := purego.Dlsym(libobjc, "objc_msgSend")
|
|
if err != nil {
|
|
return
|
|
}
|
|
purego.RegisterFunc(&sendFloat, msgSend)
|
|
sel := objc.RegisterName
|
|
selControllers, selCount, selObjectAt = sel("controllers"), sel("count"), sel("objectAtIndex:")
|
|
selExtended, selVendor, selUTF8 = sel("extendedGamepad"), sel("vendorName"), sel("UTF8String")
|
|
selValue, selXAxis, selYAxis = sel("value"), sel("xAxis"), sel("yAxis")
|
|
selRetain, selRelease, selResponds = sel("retain"), sel("release"), sel("respondsToSelector:")
|
|
selDpad, selLeftStick, selRightStick = sel("dpad"), sel("leftThumbstick"), sel("rightThumbstick")
|
|
selUp, selDown, selLeft, selRight = sel("up"), sel("down"), sel("left"), sel("right")
|
|
for b, name := range map[Button]string{
|
|
South: "buttonA", East: "buttonB", West: "buttonX", North: "buttonY",
|
|
LeftShoulder: "leftShoulder", RightShoulder: "rightShoulder",
|
|
LeftTrigger: "leftTrigger", RightTrigger: "rightTrigger",
|
|
Back: "buttonOptions", Start: "buttonMenu", Guide: "buttonHome",
|
|
LeftStickButton: "leftThumbstickButton", RightStickButton: "rightThumbstickButton",
|
|
} {
|
|
buttonSels[b] = sel(name)
|
|
}
|
|
gcOK = objc.GetClass("GCController") != 0
|
|
})
|
|
return gcOK
|
|
}
|
|
|
|
// withPool runs fn in an autorelease pool, for the objects the framework
|
|
// returns autoreleased. A pool belongs to its thread: the goroutine stays
|
|
// on one until the pool is popped.
|
|
func withPool(fn func()) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
p, _, _ := purego.SyscallN(poolPush)
|
|
defer purego.SyscallN(poolPop, p)
|
|
fn()
|
|
}
|
|
|
|
type gcPad struct {
|
|
ctl objc.ID // retained
|
|
title string
|
|
}
|
|
|
|
func controllers() []objc.ID {
|
|
list := objc.ID(objc.GetClass("GCController")).Send(selControllers)
|
|
if list == 0 {
|
|
return nil
|
|
}
|
|
n := int(objc.Send[uint](list, selCount))
|
|
ids := make([]objc.ID, n)
|
|
for i := range n {
|
|
ids[i] = list.Send(selObjectAt, uint(i))
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func scan(known map[string]device) []device {
|
|
if !loadGC() {
|
|
return nil
|
|
}
|
|
var found []device
|
|
withPool(func() {
|
|
for _, c := range controllers() {
|
|
if c.Send(selExtended) == 0 {
|
|
continue // a remote or a micro gamepad
|
|
}
|
|
k := strconv.FormatUint(uint64(c), 16)
|
|
if d, ok := known[k]; ok {
|
|
found = append(found, d)
|
|
continue
|
|
}
|
|
d := &gcPad{ctl: c.Send(selRetain)}
|
|
if v := c.Send(selVendor); v != 0 {
|
|
d.title = goString(objc.Send[uintptr](v, selUTF8))
|
|
}
|
|
found = append(found, d)
|
|
}
|
|
})
|
|
return found
|
|
}
|
|
|
|
func goString(p uintptr) string {
|
|
if p == 0 {
|
|
return ""
|
|
}
|
|
b := unsafe.Slice((*byte)(*(*unsafe.Pointer)(unsafe.Pointer(&p))), 256)
|
|
for i, c := range b {
|
|
if c == 0 {
|
|
return string(b[:i])
|
|
}
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func (d *gcPad) key() string { return strconv.FormatUint(uint64(d.ctl), 16) }
|
|
func (d *gcPad) name() string { return d.title }
|
|
func (d *gcPad) close() { d.ctl.Send(selRelease) }
|
|
|
|
// gcPresent reports whether a controller is still among the controllers;
|
|
// tests replace it.
|
|
var gcPresent = func(ctl objc.ID) bool {
|
|
for _, c := range controllers() {
|
|
if c == ctl {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func value(input objc.ID) float32 {
|
|
if input == 0 {
|
|
return 0
|
|
}
|
|
return sendFloat(input, selValue)
|
|
}
|
|
|
|
func (d *gcPad) read(s *State) bool {
|
|
ok := false
|
|
withPool(func() {
|
|
if ok = gcPresent(d.ctl); !ok {
|
|
return
|
|
}
|
|
gp := d.ctl.Send(selExtended)
|
|
if gp == 0 {
|
|
ok = false
|
|
return
|
|
}
|
|
for b, sel := range buttonSels {
|
|
// Older systems have no Home or Options button.
|
|
if sel != 0 && objc.Send[bool](gp, selResponds, sel) {
|
|
s.Buttons[b] = value(gp.Send(sel))
|
|
}
|
|
}
|
|
if pad := gp.Send(selDpad); pad != 0 {
|
|
s.Buttons[Up] = value(pad.Send(selUp))
|
|
s.Buttons[Down] = value(pad.Send(selDown))
|
|
s.Buttons[Left] = value(pad.Send(selLeft))
|
|
s.Buttons[Right] = value(pad.Send(selRight))
|
|
}
|
|
// GameController's y axes go up.
|
|
if st := gp.Send(selLeftStick); st != 0 {
|
|
s.Axes[LeftX], s.Axes[LeftY] = value(st.Send(selXAxis)), -value(st.Send(selYAxis))
|
|
}
|
|
if st := gp.Send(selRightStick); st != 0 {
|
|
s.Axes[RightX], s.Axes[RightY] = value(st.Send(selXAxis)), -value(st.Send(selYAxis))
|
|
}
|
|
})
|
|
return ok
|
|
}
|