Files
Reims-Go/internal/app/computer_use_held.go
T
千川白浪 d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

245 lines
7.1 KiB
Go

package app
import (
"context"
"sort"
"time"
"github.com/Nwflower/reims-go/internal/driver"
)
// heldInputIdleTimeout bounds how long a plugin may leave a key or a pointer
// button held in a guest without touching that console again. A plugin that
// stops calling without releasing must not leave the guest stuck with a
// modifier down, so the record carries its own timer instead of waiting for the
// plugin's next call.
var heldInputIdleTimeout = 2 * time.Minute
// heldReleaseTimeout bounds one fallback release, which runs while the plugin
// that held the input may already be gone.
const heldReleaseTimeout = 5 * time.Second
// heldInputs is what one plugin currently holds in one guest. QEMU and a
// driver's own console keep a pressed key or button down in the guest until a
// release arrives, and that state does not depend on the connection, so the
// host has to remember what it pressed and lift it when the plugin can no
// longer do so.
type heldInputs struct {
keys map[string]bool
buttons map[string]bool
touched time.Time
timer *time.Timer
}
// noteHeldKey records or forgets one key transition. code must already be a
// QKeyCode.
func (a *App) noteHeldKey(pluginID, vmKey, code string, down bool) {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
held := a.heldLocked(pluginID, vmKey)
if down {
held.keys[code] = true
} else {
delete(held.keys, code)
}
a.touchHeldLocked(pluginID, vmKey, held)
}
// noteHeldButton records or forgets one pointer button transition.
func (a *App) noteHeldButton(pluginID, vmKey, button string, down bool) {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
held := a.heldLocked(pluginID, vmKey)
if down {
held.buttons[button] = true
} else {
delete(held.buttons, button)
}
a.touchHeldLocked(pluginID, vmKey, held)
}
// heldLocked returns the record for one plugin and VM, creating it when
// needed. The caller holds computerUseHeldMu.
func (a *App) heldLocked(pluginID, vmKey string) *heldInputs {
if a.computerUseHeld == nil {
a.computerUseHeld = map[string]map[string]*heldInputs{}
}
byVM := a.computerUseHeld[pluginID]
if byVM == nil {
byVM = map[string]*heldInputs{}
a.computerUseHeld[pluginID] = byVM
}
held := byVM[vmKey]
if held == nil {
held = &heldInputs{keys: map[string]bool{}, buttons: map[string]bool{}}
byVM[vmKey] = held
}
return held
}
// touchHeldLocked drops a record that holds nothing and rearms the idle timer
// of one that still does. The caller holds computerUseHeldMu.
func (a *App) touchHeldLocked(pluginID, vmKey string, held *heldInputs) {
held.touched = time.Now()
if held.timer != nil {
held.timer.Stop()
held.timer = nil
}
if len(held.keys)+len(held.buttons) == 0 {
a.pruneHeldLocked(pluginID, vmKey)
return
}
held.timer = time.AfterFunc(heldInputIdleTimeout, func() {
a.releaseHeldForVM(pluginID, vmKey)
})
}
// pruneHeldLocked drops a record that holds nothing, so an idle plugin leaves
// no state behind. The caller holds computerUseHeldMu.
func (a *App) pruneHeldLocked(pluginID, vmKey string) {
byVM := a.computerUseHeld[pluginID]
if byVM == nil {
return
}
if held := byVM[vmKey]; held != nil {
if held.timer != nil {
held.timer.Stop()
held.timer = nil
}
delete(byVM, vmKey)
}
if len(byVM) == 0 {
delete(a.computerUseHeld, pluginID)
}
}
// heldState returns what one plugin holds in one VM, sorted.
func (a *App) heldState(pluginID, vmKey string) (keys, buttons []string) {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
byVM := a.computerUseHeld[pluginID]
if byVM == nil {
return nil, nil
}
held := byVM[vmKey]
if held == nil {
return nil, nil
}
for code := range held.keys {
keys = append(keys, code)
}
for button := range held.buttons {
buttons = append(buttons, button)
}
sort.Strings(keys)
sort.Strings(buttons)
return keys, buttons
}
// heldVMs lists the VMs one plugin currently holds input in.
func (a *App) heldVMs(pluginID string) map[string]bool {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
out := map[string]bool{}
for vmKey := range a.computerUseHeld[pluginID] {
out[vmKey] = true
}
return out
}
// dropHeld forgets the named keys and buttons of one plugin and VM. It is
// called only after the release reached the guest.
func (a *App) dropHeld(pluginID, vmKey string, keys, buttons []string) {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
byVM := a.computerUseHeld[pluginID]
if byVM == nil {
return
}
held := byVM[vmKey]
if held == nil {
return
}
for _, code := range keys {
delete(held.keys, code)
}
for _, button := range buttons {
delete(held.buttons, button)
}
a.touchHeldLocked(pluginID, vmKey, held)
}
// forgetHeld drops the record of one plugin and VM without touching the
// guest. It is for a VM that is not running: nothing in a powered-off guest
// can still be held, and a release sent later could lift a key the user is
// pressing after the VM starts again.
func (a *App) forgetHeld(pluginID, vmKey string) {
a.computerUseHeldMu.Lock()
defer a.computerUseHeldMu.Unlock()
a.pruneHeldLocked(pluginID, vmKey)
}
// heldConsoleRoute resolves the console a held release has to go through. It
// reports false when the VM is gone or not running, because then nothing can
// be held in it.
func (a *App) heldConsoleRoute(vmKey string) (consoleRoute, bool) {
ref, ok := driver.ParseVMRef(vmKey)
if !ok {
return consoleRoute{}, false
}
vm, err := a.resolveVMInfo(ref.DriverID, ref.Name)
if err != nil {
return consoleRoute{}, false
}
current, ok := a.polledVMForDriver(vm.DriverID, vm.Name)
if !ok || current.State != "running" {
return consoleRoute{}, false
}
if vm.Directory == "" {
vm.Directory = current.Dir
}
route, err := a.resolveConsoleRoute(vm)
if err != nil {
return consoleRoute{}, false
}
route.vm = vm
return route, true
}
// releaseHeldForVM lifts everything one plugin still holds in one VM. It is the
// fallback for a plugin that can no longer release: its process exited, its
// scope was disposed, or it stopped calling. A VM that is not running has
// nothing held, so its record is dropped instead of retried; a release that
// fails on a running VM keeps its record for the next attempt.
func (a *App) releaseHeldForVM(pluginID, vmKey string) {
keys, buttons := a.heldState(pluginID, vmKey)
if len(keys)+len(buttons) == 0 {
a.forgetHeld(pluginID, vmKey)
return
}
route, ok := a.heldConsoleRoute(vmKey)
if !ok {
a.forgetHeld(pluginID, vmKey)
return
}
ctx, cancel := context.WithTimeout(context.Background(), heldReleaseTimeout)
defer cancel()
if err := a.consoleRelease(ctx, route, keys, buttons); err != nil {
// The VM may have stopped while the release was in flight.
if _, stillRunning := a.heldConsoleRoute(vmKey); !stillRunning {
a.forgetHeld(pluginID, vmKey)
return
}
a.emitLog("plugin", "Could not release input held by "+pluginID+" in "+vmKey+": "+err.Error())
return
}
a.dropHeld(pluginID, vmKey, keys, buttons)
}
// releaseHeldForPlugin lifts everything one plugin holds in any VM.
func (a *App) releaseHeldForPlugin(pluginID string) {
for vmKey := range a.heldVMs(pluginID) {
a.releaseHeldForVM(pluginID, vmKey)
}
}