diff --git a/internal/driver/console.go b/internal/driver/console.go index 8084179..9e9c474 100644 --- a/internal/driver/console.go +++ b/internal/driver/console.go @@ -3,8 +3,15 @@ package driver import ( "context" "fmt" + "image" ) +// ConsoleFrameImages supplies host image operations to an in-process guest driver. +type ConsoleFrameImages struct { + Load func(path, format string) (image.Image, error) + Scale func(image.Image, int) image.Image +} + // 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. diff --git a/internal/macos/guest/stambha.go b/internal/macos/guest/stambha.go index b0a9b67..1b8620a 100644 --- a/internal/macos/guest/stambha.go +++ b/internal/macos/guest/stambha.go @@ -10,7 +10,7 @@ import ( ) func stambhaDefinition(ver string, cb builtin.HostCallbacks, desiredFn func(string, bool) bool) plugin.PluginDef { - drv := stambhadriver.NewStambhaDriver(cb.GetConfig) + drv := stambhadriver.NewStambhaDriver(cb.GetConfig, cb.ConsoleImages) pack := plugin.RuntimeKindContribution{ Kind: drv.RuntimeKind(), Name: plugin.LocalizedText{ZhCN: "Stambha 运行时", EnUS: "Stambha Runtime"}, RequiredFiles: stambhadriver.StambhaRuntimeFiles(), diff --git a/internal/macos/stambhadriver/shutdown_menu.go b/internal/macos/stambhadriver/shutdown_menu.go index cb52e17..62690aa 100644 --- a/internal/macos/stambhadriver/shutdown_menu.go +++ b/internal/macos/stambhadriver/shutdown_menu.go @@ -15,12 +15,14 @@ import ( "github.com/Nwflower/reims-go/internal/macos/autoinstall/ocr" "github.com/Nwflower/reims-go/internal/macos/autoinstall/vision" "github.com/Nwflower/reims-go/internal/qmp" - "github.com/Nwflower/reims-go/internal/thumb" ) // The login window handles its power menu even when it ignores the SMC // power-button shortcut. Read the native menu before selecting an action. -func requestLoginWindowShutdown(ctx context.Context, conn *qmp.Conn) (bool, error) { +func (d *StambhaDriver) requestLoginWindowShutdown(ctx context.Context, conn *qmp.Conn) (bool, error) { + if d.images.Load == nil || d.images.Scale == nil { + return false, nil + } tmp, err := os.MkdirTemp("", "reims-arm-shutdown-") if err != nil { return false, err @@ -35,7 +37,7 @@ func requestLoginWindowShutdown(ctx context.Context, conn *qmp.Conn) (bool, erro if err != nil { return nil, err } - return thumb.Load(pp, format) + return d.images.Load(pp, format) } frame, err := readFrame() @@ -56,7 +58,7 @@ func requestLoginWindowShutdown(ctx context.Context, conn *qmp.Conn) (bool, erro bounds := frame.Bounds() // A power-shaped status item alone can also belong to an ordinary desktop app. clockRegion := image.Rect(bounds.Dx()/4, bounds.Dy()/18, bounds.Dx()*3/4, bounds.Dy()/3).Add(bounds.Min) - clock := thumb.Scale(vision.Crop(frame, clockRegion), 512) + clock := d.images.Scale(vision.Crop(frame, clockRegion), 512) probe, cancel := context.WithTimeout(ctx, 2*time.Second) reading, err := engine.Recognize(probe, clock) cancel() diff --git a/internal/macos/stambhadriver/stambha.go b/internal/macos/stambhadriver/stambha.go index c7a812d..57fd07e 100644 --- a/internal/macos/stambhadriver/stambha.go +++ b/internal/macos/stambhadriver/stambha.go @@ -49,7 +49,10 @@ var ( stambhaPCIDeviceID = regexp.MustCompile(`^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$`) ) -type StambhaDriver struct{ config func() config.Config } +type StambhaDriver struct { + config func() config.Config + images driver.ConsoleFrameImages +} var ( _ driver.Driver = (*StambhaDriver)(nil) @@ -82,8 +85,10 @@ type stambhaPlan struct { Serial string `json:"serial_log"` } -func NewStambhaDriver(cfg func() config.Config) *StambhaDriver { return &StambhaDriver{config: cfg} } -func (*StambhaDriver) ID() string { return StambhaDriverID } +func NewStambhaDriver(cfg func() config.Config, images driver.ConsoleFrameImages) *StambhaDriver { + return &StambhaDriver{config: cfg, images: images} +} +func (*StambhaDriver) ID() string { return StambhaDriverID } func (*StambhaDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{ZhCN: "macOS ARM64(预览)", EnUS: "macOS ARM64 (Preview)"} } @@ -822,7 +827,7 @@ func (d *StambhaDriver) Stop(ctx context.Context, name string, force bool, _, _ if err != nil { return err } - menuHandled, err := requestLoginWindowShutdown(ctx, conn) + menuHandled, err := d.requestLoginWindowShutdown(ctx, conn) if err == nil && !menuHandled { err = requestStambhaShutdown(ctx, conn) } diff --git a/internal/macos/stambhadriver/stambha_test.go b/internal/macos/stambhadriver/stambha_test.go index 925985f..a468f9c 100644 --- a/internal/macos/stambhadriver/stambha_test.go +++ b/internal/macos/stambhadriver/stambha_test.go @@ -124,7 +124,7 @@ func TestStambhaSocketPathUsesBoundedPerUserFallback(t *testing.T) { func TestStambhaRejectsWritableDiskInStageBeforeCleanup(t *testing.T) { cfg := config.Config{DataRoot: t.TempDir()} - d := NewStambhaDriver(func() config.Config { return cfg }) + d := NewStambhaDriver(func() config.Config { return cfg }, driver.ConsoleFrameImages{}) vmDir, err := d.directory("arm") if err != nil { t.Fatal(err) @@ -396,7 +396,7 @@ func TestStambhaCreateSetShowHostedGPU(t *testing.T) { t.Fatal(err) } - drv := NewStambhaDriver(func() config.Config { return config.Config{DataRoot: dataRoot} }) + drv := NewStambhaDriver(func() config.Config { return config.Config{DataRoot: dataRoot} }, driver.ConsoleFrameImages{}) ctx := context.Background() err := drv.Create(ctx, driver.CreateRequest{ DriverID: StambhaDriverID, @@ -442,7 +442,7 @@ func TestStambhaCreateSetShowHostedGPU(t *testing.T) { } func TestStambhaCapabilities(t *testing.T) { - drv := NewStambhaDriver(nil) + drv := NewStambhaDriver(nil, driver.ConsoleFrameImages{}) caps := drv.Capabilities() for _, want := range []string{"guest.arm64", "serial_log", driver.CapAffinity} { if !slices.Contains(caps, want) { diff --git a/internal/plugin/builtin/bootstrap.go b/internal/plugin/builtin/bootstrap.go index e352aa5..6e235ee 100644 --- a/internal/plugin/builtin/bootstrap.go +++ b/internal/plugin/builtin/bootstrap.go @@ -7,6 +7,7 @@ import ( "github.com/Nwflower/reims-go/internal/host" "github.com/Nwflower/reims-go/internal/i18n" "github.com/Nwflower/reims-go/internal/plugin" + "github.com/Nwflower/reims-go/internal/thumb" "github.com/Nwflower/reims-go/internal/version" ) @@ -71,6 +72,12 @@ func BootstrapKernel(opts KernelBootstrapOptions) (*plugin.Manager, *driver.Rout mgr.Registry().SetBusyChecker(router.PluginBusyChecker()) cb := opts.Callbacks + if cb.ConsoleImages.Load == nil { + cb.ConsoleImages.Load = thumb.Load + } + if cb.ConsoleImages.Scale == nil { + cb.ConsoleImages.Scale = thumb.Scale + } if cb.Log == nil { cb.Log = logf } diff --git a/internal/plugin/builtin/builtin.go b/internal/plugin/builtin/builtin.go index 82eb73a..bf2f34e 100644 --- a/internal/plugin/builtin/builtin.go +++ b/internal/plugin/builtin/builtin.go @@ -49,6 +49,7 @@ const ( // HostCallbacks connects builtin extensions to host runtime services without // creating an import cycle with internal/app. type HostCallbacks struct { + ConsoleImages driver.ConsoleFrameImages GetConfig func() config.Config Log func(source, line string) EnsureGuestTools func() (*guesttools.Server, int, error) diff --git a/internal/plugin/builtin/builtin_test.go b/internal/plugin/builtin/builtin_test.go index 0a23e4b..6b6a251 100644 --- a/internal/plugin/builtin/builtin_test.go +++ b/internal/plugin/builtin/builtin_test.go @@ -3,6 +3,8 @@ package builtin import ( "context" "fmt" + "image" + "image/color" "os" "path/filepath" "strings" @@ -10,11 +12,46 @@ import ( "time" "github.com/Nwflower/reims-go/internal/config" + "github.com/Nwflower/reims-go/internal/driver" "github.com/Nwflower/reims-go/internal/guesttools" "github.com/Nwflower/reims-go/internal/host" "github.com/Nwflower/reims-go/internal/plugin" ) +func TestBootstrapSuppliesGuestConsoleImages(t *testing.T) { + var frames driver.ConsoleFrameImages + mgr, _, err := BootstrapKernel(KernelBootstrapOptions{ + DataRoot: t.TempDir(), + RegisterGuest: func(_ *plugin.Registry, cb HostCallbacks, _ func(string, bool) bool) error { + frames = cb.ConsoleImages + return nil + }, + }) + if err != nil { + t.Fatal(err) + } + defer mgr.Close() + if frames.Load == nil || frames.Scale == nil { + t.Fatal("guest registration did not receive console image operations") + } + path := filepath.Join(t.TempDir(), "screen.ppm") + data := append([]byte("P6\n2 1\n255\n"), 17, 23, 29, 43, 65, 87) + if err := os.WriteFile(path, data, 0600); err != nil { + t.Fatal(err) + } + full, err := frames.Load(path, "ppm") + if err != nil { + t.Fatal(err) + } + if full.Bounds() != image.Rect(0, 0, 2, 1) || full.At(1, 0) != (color.RGBA{43, 65, 87, 255}) { + t.Fatalf("decoded frame bounds %v, second pixel %v", full.Bounds(), full.At(1, 0)) + } + clock := frames.Scale(full, 1) + if clock.Bounds() != image.Rect(0, 0, 1, 1) || clock.At(0, 0) != (color.RGBA{30, 44, 58, 255}) { + t.Fatalf("scaled frame bounds %v, pixel %v", clock.Bounds(), clock.At(0, 0)) + } +} + func TestBuiltinClipboardAndInputLifecycleZeroResidue(t *testing.T) { reg := plugin.NewRegistry(nil) gtSrv := guesttools.NewTransportServer(nil)