122 lines
4.1 KiB
Go
122 lines
4.1 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"runtime"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/appwindow"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
)
|
|
|
|
func (a *App) openAppWindow(command context.Context, sessions *appwindow.Sessions, bridge *guesttools.Server, service builtin.ComputerUseService, name string) error {
|
|
a.mu.Lock()
|
|
locale := i18n.EffectiveLocale(a.cfg.Locale)
|
|
tmp := a.cfg.TempDir()
|
|
a.mu.Unlock()
|
|
coded := func(code, key string, cause error) error {
|
|
return i18n.NewCodedError(locale, code, key, i18n.Vars{"name": name}, cause)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
return coded("E_APP_WINDOW_UNAVAILABLE", "err.app_window_unavailable", nil)
|
|
}
|
|
vm, ok := a.polledVMForDriver(driver.BuiltinReimsMacOSDriverID, name)
|
|
if !ok || vm.State != "running" || vm.Dir == "" {
|
|
return coded("E_VM_NOT_RUNNING", "err.vm_not_running", nil)
|
|
}
|
|
err := sessions.Start(command, func(ctx context.Context) (func(), error) {
|
|
// A previous activation may still be clearing its input when Windows
|
|
// takes longer than the plugin's shutdown deadline.
|
|
if !a.appWindowGate.TryLock() {
|
|
return nil, appwindow.ErrWindowOpen
|
|
}
|
|
run, err := a.prepareAppWindow(ctx, bridge, service, vm, tmp, locale, coded)
|
|
if err != nil {
|
|
a.appWindowGate.Unlock()
|
|
return nil, err
|
|
}
|
|
return func() {
|
|
defer a.appWindowGate.Unlock()
|
|
run()
|
|
}, nil
|
|
})
|
|
switch {
|
|
case errors.Is(err, appwindow.ErrWindowOpen):
|
|
return coded("E_APP_WINDOW_OPEN", "err.app_window_open", nil)
|
|
case errors.Is(err, appwindow.ErrSessionsClosed), errors.Is(err, context.Canceled):
|
|
return coded("E_APP_WINDOW_DISABLED", "err.app_window_disabled", nil)
|
|
default:
|
|
return err
|
|
}
|
|
}
|
|
|
|
func (a *App) prepareAppWindow(ctx context.Context, bridge *guesttools.Server, service builtin.ComputerUseService, vm driver.VMListItem, tmp string, locale i18n.Locale, coded func(string, string, error) error) (func(), error) {
|
|
name := vm.Name
|
|
status := bridge.Status(name, 15*time.Second)
|
|
if !status.Connected {
|
|
return nil, coded("E_APP_WINDOW_GUEST_TOOLS", "err.app_window_guest_tools", nil)
|
|
}
|
|
if status.AgentVersion < guesttools.PasteReadyAgentVersion {
|
|
return nil, coded("E_APP_WINDOW_GUEST_TOOLS_UPDATE", "err.app_window_guest_tools_update", nil)
|
|
}
|
|
probe, stopProbe := context.WithTimeout(ctx, 2*time.Second)
|
|
pointer, err := service.PointerAvailable(probe, vm.Dir)
|
|
stopProbe()
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if err != nil || !pointer {
|
|
return nil, coded("E_APP_WINDOW_POINTER", "err.app_window_pointer", err)
|
|
}
|
|
if !bridge.SetWindowReporting(name, true) {
|
|
return nil, coded("E_APP_WINDOW_GUEST_TOOLS", "err.app_window_guest_tools", nil)
|
|
}
|
|
clearReporting := sync.OnceFunc(func() { bridge.SetWindowReporting(name, false) })
|
|
stopReporting := context.AfterFunc(ctx, clearReporting)
|
|
finishReporting := func() {
|
|
stopReporting()
|
|
clearReporting()
|
|
}
|
|
show, err := appwindow.Prepare(ctx, appwindow.SessionOptions{
|
|
VMDir: vm.Dir,
|
|
TmpDir: tmp,
|
|
Console: service,
|
|
Window: func(maxAge time.Duration) (guesttools.ActiveWindow, bool) {
|
|
return bridge.ActiveWindow(name, maxAge)
|
|
},
|
|
PrepareClipboard: func(wait context.Context) (func(), error) {
|
|
return bridge.PrepareGuestClipboardPaste(wait, name)
|
|
},
|
|
OnClipboardError: func(err error) {
|
|
a.emitVMLogForDriver(vm.DriverID, name, "App window paste: "+err.Error())
|
|
a.mu.Lock()
|
|
manager := a.trayMgr
|
|
a.mu.Unlock()
|
|
if manager != nil {
|
|
manager.Notify(i18n.T(locale, "tray.notify.app_window_paste_retry"), name, true)
|
|
}
|
|
},
|
|
})
|
|
if err != nil {
|
|
finishReporting()
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if errors.Is(err, appwindow.ErrNoWindow) {
|
|
return nil, coded("E_APP_WINDOW_NO_WINDOW", "err.app_window_no_window", nil)
|
|
}
|
|
return nil, coded("E_APP_WINDOW_CAPTURE", "err.app_window_capture", err)
|
|
}
|
|
return func() {
|
|
defer finishReporting()
|
|
if err := show(); err != nil && ctx.Err() == nil {
|
|
a.emitVMLogForDriver(vm.DriverID, name, "App window closed: "+err.Error())
|
|
}
|
|
}, nil
|
|
}
|