Files
openchamber-android-native/third_party/mygo/plugins/game/gamepad/gamepad_js.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
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.
2026-11-18 13:22:59 +08:00

74 lines
1.8 KiB
Go

package gamepad
import (
"strconv"
"syscall/js"
)
// Browsers report pads through navigator.getGamepads(), each at an index
// for as long as it is connected. Those with the "standard" mapping have
// their buttons and axes in this package's order; others keep the order
// the browser gives, which fits the first buttons and axes of most.
type jsPad struct {
index int
id string
}
func pollPads() js.Value {
nav := js.Global().Get("navigator")
if !nav.Truthy() || !nav.Get("getGamepads").Truthy() {
return js.Undefined()
}
return nav.Call("getGamepads")
}
func scan(known map[string]device) []device {
list := pollPads()
if !list.Truthy() {
return nil
}
var found []device
for i := range list.Length() {
g := list.Index(i)
if !g.Truthy() || !g.Get("connected").Bool() {
continue
}
d := &jsPad{index: i, id: g.Get("id").String()}
if k, ok := known[d.key()]; ok {
found = append(found, k)
continue
}
found = append(found, d)
}
return found
}
func (d *jsPad) key() string { return strconv.Itoa(d.index) + "\x00" + d.id }
func (d *jsPad) name() string { return d.id }
func (d *jsPad) close() {}
func (d *jsPad) read(s *State) bool {
list := pollPads()
if !list.Truthy() || d.index >= list.Length() {
return false
}
g := list.Index(d.index)
if !g.Truthy() || !g.Get("connected").Bool() || g.Get("id").String() != d.id {
return false
}
buttons := g.Get("buttons")
for i := range min(buttons.Length(), int(ButtonCount)) {
b := buttons.Index(i)
s.Buttons[i] = float32(b.Get("value").Float())
if s.Buttons[i] == 0 && b.Get("pressed").Bool() {
s.Buttons[i] = 1 // a browser that reports only pressed
}
}
axes := g.Get("axes")
for i := range min(axes.Length(), int(AxisCount)) {
s.Axes[i] = float32(axes.Index(i).Float())
}
return true
}