- M5.5: add pure-Go internal/opencore reader/in-place patcher for OpenCore NVRAM prev-lang:kbd with sidecar persistence, automatic replay after update-opencore/graphics changes/CoW clones, wizard/settings/CLI (--guest-lang) support - M5.6: add stopped VM disk resize (qemu-img resize + dynamic macOS APFS expand command) and standalone qcow2 compaction (backing-file guard, free-space check, qemu-img check, atomic swap) in GUI Settings and CLI (vm resize/compact) - M5.9: add Win32 winmm audio output/input device probing, fix reims-ctl show audio_backend parsing, expose audio backend selector (auto/dsound/none) and latency/mic diagnostics in Settings & CLI - UI & thumbnails: add QMP screendump VM thumbnails (internal/thumb, thumbnail_flow.go), VMPage/CreateWizard layout polish, and docs/ui-audit.md - Update docs/roadmap.md for M5.5, M5.6, and M5.9
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package main
|
|
|
|
// VM screen thumbnails for the VM page (docs/ui-audit.md): while a VM runs,
|
|
// grab one frame over its QMP socket (`screendump`), shrink it to a small
|
|
// JPEG and keep it under <data root>\cache\thumbs, so a stopped VM can still
|
|
// show its last screen. Nothing here touches vm.toml or the guest.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/qmp"
|
|
"github.com/Nwflower/reims-go/internal/thumb"
|
|
)
|
|
|
|
// VMThumbnail is one frame of a VM's screen as a data URL.
|
|
type VMThumbnail struct {
|
|
DataURL string `json:"data_url"`
|
|
CapturedAt int64 `json:"captured_at"` // Unix milliseconds
|
|
Live bool `json:"live"` // captured just now from the running VM
|
|
}
|
|
|
|
const thumbWidth = 640
|
|
|
|
// thumbLocks keeps one capture per VM in flight: QMP serves a single client
|
|
// and the page polls on a timer.
|
|
var thumbLocks sync.Map // name → *sync.Mutex
|
|
|
|
func (a *App) thumbPath(name string) string {
|
|
a.mu.Lock()
|
|
dir := a.cfg.XDGCacheDir()
|
|
a.mu.Unlock()
|
|
return filepath.Join(dir, "thumbs", name+".jpg")
|
|
}
|
|
|
|
// GetVMThumbnail returns the VM's screen: a fresh frame while it runs, else
|
|
// the last one saved. An empty DataURL means there is no frame to show.
|
|
func (a *App) GetVMThumbnail(name string) (VMThumbnail, error) {
|
|
path := a.thumbPath(name)
|
|
l, _ := thumbLocks.LoadOrStore(name, &sync.Mutex{})
|
|
mu := l.(*sync.Mutex)
|
|
if mu.TryLock() {
|
|
defer mu.Unlock()
|
|
if t, ok := a.captureThumb(name, path); ok {
|
|
return t, nil
|
|
}
|
|
}
|
|
return readThumb(path, false), nil
|
|
}
|
|
|
|
func (a *App) captureThumb(name, path string) (VMThumbnail, bool) {
|
|
typing.mu.Lock()
|
|
busy := typing.busy[name]
|
|
typing.mu.Unlock()
|
|
if busy {
|
|
return VMThumbnail{}, false
|
|
}
|
|
d, err := a.GetVMDetail(name)
|
|
if err != nil || d.State != "running" || d.Directory == "" {
|
|
return VMThumbnail{}, false
|
|
}
|
|
a.mu.Lock()
|
|
tmp := a.cfg.TempDir()
|
|
a.mu.Unlock()
|
|
if err := os.MkdirAll(tmp, 0o755); err != nil {
|
|
return VMThumbnail{}, false
|
|
}
|
|
raw := filepath.Join(tmp, "screendump-"+name)
|
|
defer os.Remove(raw)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
|
defer cancel()
|
|
format, err := qmp.ScreendumpVM(ctx, d.Directory, raw)
|
|
if err != nil {
|
|
return VMThumbnail{}, false
|
|
}
|
|
img, err := thumb.Load(raw, format)
|
|
if err != nil || thumb.Blank(img) {
|
|
return VMThumbnail{}, false
|
|
}
|
|
jpg, err := thumb.JPEG(thumb.Scale(img, thumbWidth), 80)
|
|
if err != nil {
|
|
return VMThumbnail{}, false
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err == nil {
|
|
part := path + ".part"
|
|
if os.WriteFile(part, jpg, 0o644) == nil {
|
|
_ = os.Rename(part, path)
|
|
}
|
|
}
|
|
return VMThumbnail{
|
|
DataURL: "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(jpg),
|
|
CapturedAt: time.Now().UnixMilli(),
|
|
Live: true,
|
|
}, true
|
|
}
|
|
|
|
func readThumb(path string, live bool) VMThumbnail {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil || len(b) == 0 {
|
|
return VMThumbnail{}
|
|
}
|
|
var at int64
|
|
if st, err := os.Stat(path); err == nil {
|
|
at = st.ModTime().UnixMilli()
|
|
}
|
|
return VMThumbnail{
|
|
DataURL: "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(b),
|
|
CapturedAt: at,
|
|
Live: live,
|
|
}
|
|
}
|