package driver import ( "context" "fmt" ) // ConsoleEndpointQMP and ConsoleEndpointDriver are the values of a driver's // `consoleEndpoint`. With "qmp" the host reaches the guest through its own QMP // console; with "driver" the driver serves the console itself. const ( ConsoleEndpointQMP = "qmp" ConsoleEndpointDriver = "driver" ) // ConsoleRegion limits a screenshot to part of the screen, in guest pixels. type ConsoleRegion struct { X int `json:"x"` Y int `json:"y"` Width int `json:"width"` Height int `json:"height"` } // ConsoleScreenshotRequest asks a driver for one frame of the guest screen. // The host picks Path (a fresh file in its temp folder) and the driver writes // the frame there, so the host never trusts a path the driver chose. type ConsoleScreenshotRequest struct { Path string `json:"path"` MaxEdge int `json:"max_edge,omitempty"` Region *ConsoleRegion `json:"region,omitempty"` } // ConsoleScreenshotResult describes the frame the driver wrote to the // requested path. type ConsoleScreenshotResult struct { Path string `json:"path,omitempty"` // empty or equal to the requested path Format string `json:"format,omitempty"` // "png" (default) or "ppm" Width int `json:"width,omitempty"` Height int `json:"height,omitempty"` SourceWidth int `json:"source_width,omitempty"` SourceHeight int `json:"source_height,omitempty"` } // ConsolePointerRequest is one pointer action: move, button, click or scroll. type ConsolePointerRequest struct { Action string `json:"action"` X int `json:"x,omitempty"` Y int `json:"y,omitempty"` Width int `json:"width,omitempty"` Height int `json:"height,omitempty"` Button string `json:"button,omitempty"` Down *bool `json:"down,omitempty"` Delta int `json:"delta,omitempty"` Horizontal bool `json:"horizontal,omitempty"` } // ConsoleKeyRequest presses one key with optional held modifiers. type ConsoleKeyRequest struct { Key string `json:"key"` Modifiers []string `json:"modifiers,omitempty"` } // ConsoleDriver is implemented by drivers that serve their own console. The // host only calls it when the driver declares `consoleEndpoint: "driver"` and // the `console` capability. type ConsoleDriver interface { ConsoleScreenshot(ctx context.Context, name string, req ConsoleScreenshotRequest) (ConsoleScreenshotResult, error) ConsolePointer(ctx context.Context, name string, req ConsolePointerRequest) error ConsoleKey(ctx context.Context, name string, req ConsoleKeyRequest) error ConsoleType(ctx context.Context, name, text string) (int, error) } // DriverServesConsole reports whether d serves its own console: it declares // the driver console endpoint and the `console` capability and implements // ConsoleDriver. func DriverServesConsole(d Driver) (ConsoleDriver, bool) { if d == nil { return nil, false } md, ok := d.(DriverMetadata) if !ok || md.ConsoleEndpoint() != ConsoleEndpointDriver || !HasCapability(d.Capabilities(), CapConsole) { return nil, false } cd, ok := d.(ConsoleDriver) return cd, ok } func (d *SidecarDriver) requireConsole() error { if d.ConsoleEndpoint() != ConsoleEndpointDriver || !HasCapability(d.contrib.Capabilities, CapConsole) { return fmt.Errorf("driver %q does not serve its own console", d.contrib.ID) } return nil } // ConsoleScreenshot implements ConsoleDriver through `driver/console.screenshot`. func (d *SidecarDriver) ConsoleScreenshot(ctx context.Context, name string, req ConsoleScreenshotRequest) (ConsoleScreenshotResult, error) { if err := d.requireConsole(); err != nil { return ConsoleScreenshotResult{}, err } var res ConsoleScreenshotResult err := d.sup.Call(ctx, "driver/console.screenshot", map[string]any{ "name": name, "ref": d.ref(name), "path": req.Path, "max_edge": req.MaxEdge, "region": req.Region, }, &res) return res, err } // ConsolePointer implements ConsoleDriver through `driver/console.pointer`. func (d *SidecarDriver) ConsolePointer(ctx context.Context, name string, req ConsolePointerRequest) error { if err := d.requireConsole(); err != nil { return err } return d.sup.Call(ctx, "driver/console.pointer", map[string]any{ "name": name, "ref": d.ref(name), "action": req.Action, "x": req.X, "y": req.Y, "width": req.Width, "height": req.Height, "button": req.Button, "down": req.Down, "delta": req.Delta, "horizontal": req.Horizontal, }, nil) } // ConsoleKey implements ConsoleDriver through `driver/console.key`. func (d *SidecarDriver) ConsoleKey(ctx context.Context, name string, req ConsoleKeyRequest) error { if err := d.requireConsole(); err != nil { return err } return d.sup.Call(ctx, "driver/console.key", map[string]any{"name": name, "ref": d.ref(name), "key": req.Key, "modifiers": req.Modifiers}, nil) } // ConsoleType implements ConsoleDriver through `driver/console.type`. func (d *SidecarDriver) ConsoleType(ctx context.Context, name, text string) (int, error) { if err := d.requireConsole(); err != nil { return 0, err } var resp struct { Characters int `json:"characters"` } if err := d.sup.Call(ctx, "driver/console.type", map[string]any{"name": name, "ref": d.ref(name), "text": text}, &resp); err != nil { return 0, err } return resp.Characters, nil } var _ ConsoleDriver = (*SidecarDriver)(nil)