126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package fakedriver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"unicode/utf8"
|
|
|
|
"github.com/Nwflower/reims-go/pkg/pluginsdk"
|
|
)
|
|
|
|
var consoleMu sync.Mutex
|
|
|
|
// ConsoleLogName is the file, in the driver's VM root, that lists every input
|
|
// call the driver console received, one per line.
|
|
const ConsoleLogName = "console.log"
|
|
|
|
func (e *engine) record(format string, args ...any) {
|
|
if e.root == "" {
|
|
return
|
|
}
|
|
consoleMu.Lock()
|
|
defer consoleMu.Unlock()
|
|
f, err := os.OpenFile(filepath.Join(e.root, ConsoleLogName), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close()
|
|
fmt.Fprintf(f, format+"\n", args...)
|
|
}
|
|
|
|
// serveConsole answers the driver console RPCs of the "hosted-console" mode.
|
|
// Screenshots are real PNG files written to the path the host chose; every
|
|
// input call is appended to ConsoleLogName in the driver's VM root, so a test
|
|
// can tell what reached the driver.
|
|
func (e *engine) serveConsole(srv *pluginsdk.Server) {
|
|
running := func(raw json.RawMessage) (string, error) {
|
|
var r struct {
|
|
Name string `json:"name"`
|
|
}
|
|
_ = json.Unmarshal(raw, &r)
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
v, err := e.get(r.Name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if v.state != "running" {
|
|
return "", fmt.Errorf("VM %q is not running", r.Name)
|
|
}
|
|
return r.Name, nil
|
|
}
|
|
srv.Handle("driver/console.screenshot", func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
if _, err := running(raw); err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Path string `json:"path"`
|
|
}
|
|
_ = json.Unmarshal(raw, &r)
|
|
if r.Path == "" {
|
|
return nil, fmt.Errorf("no frame path")
|
|
}
|
|
img := image.NewRGBA(image.Rect(0, 0, 64, 48))
|
|
for y := 0; y < 48; y++ {
|
|
for x := 0; x < 64; x++ {
|
|
img.Set(x, y, color.RGBA{R: uint8(x * 4), G: uint8(y * 5), B: 128, A: 255})
|
|
}
|
|
}
|
|
f, err := os.Create(r.Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
if err := png.Encode(f, img); err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"format": "png", "width": 64, "height": 48, "source_width": 64, "source_height": 48}, nil
|
|
})
|
|
srv.Handle("driver/console.pointer", func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
name, err := running(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Action string `json:"action"`
|
|
X, Y int
|
|
Button string `json:"button"`
|
|
}
|
|
_ = json.Unmarshal(raw, &r)
|
|
e.record("pointer %s %s %s", name, r.Action, r.Button)
|
|
return map[string]any{}, nil
|
|
})
|
|
srv.Handle("driver/console.key", func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
name, err := running(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Key string `json:"key"`
|
|
Modifiers []string `json:"modifiers"`
|
|
}
|
|
_ = json.Unmarshal(raw, &r)
|
|
e.record("key %s %s %v", name, r.Key, r.Modifiers)
|
|
return map[string]any{}, nil
|
|
})
|
|
srv.Handle("driver/console.type", func(_ context.Context, raw json.RawMessage) (any, error) {
|
|
name, err := running(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Text string `json:"text"`
|
|
}
|
|
_ = json.Unmarshal(raw, &r)
|
|
e.record("type %s %s", name, r.Text)
|
|
return map[string]any{"characters": utf8.RuneCountInString(r.Text)}, nil
|
|
})
|
|
}
|