Files
Reims-Go/internal/host/audio_windows.go
T
Nwflower b2f7f036d8 feat(m5): add macOS prev-lang preset, disk resize/compact, audio diagnostics, and VM thumbnails
- 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
2026-11-28 10:17:54 -07:00

87 lines
2.2 KiB
Go

//go:build windows
package host
import (
"strings"
"syscall"
"unicode/utf16"
"unsafe"
)
var (
winmm = syscall.NewLazyDLL("winmm.dll")
procWaveOutGetNumDevs = winmm.NewProc("waveOutGetNumDevs")
procWaveOutGetDevCaps = winmm.NewProc("waveOutGetDevCapsW")
procWaveInGetNumDevs = winmm.NewProc("waveInGetNumDevs")
procWaveInGetDevCaps = winmm.NewProc("waveInGetDevCapsW")
)
// ProbeAudio enumerates Windows playback and recording devices via winmm
// (waveOutGetNumDevs/waveInGetNumDevs + wave*GetDevCapsW), matching upstream
// reims-ctl's sound_devices() check (crates/reims-ctl/src/os/windows.rs:699-708).
func ProbeAudio() AudioInfo {
info := AudioInfo{DefaultBackend: "dsound"}
if n, _, _ := procWaveOutGetNumDevs.Call(); n > 0 {
count := int(n)
info.OutputCount = count
limit := count
if limit > 16 {
limit = 16
}
for i := 0; i < limit; i++ {
// WAVEOUTCAPSW is 84 bytes; szPname[32] is at bytes 8..72.
var buf [84]byte
r, _, _ := procWaveOutGetDevCaps.Call(uintptr(i), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
if r == 0 {
if name := decodeUTF16Z(buf[8:72]); name != "" {
info.OutputDevices = append(info.OutputDevices, name)
}
}
}
}
if n, _, _ := procWaveInGetNumDevs.Call(); n > 0 {
count := int(n)
info.InputCount = count
limit := count
if limit > 16 {
limit = 16
}
for i := 0; i < limit; i++ {
// WAVEINCAPSW is 80 bytes; szPname[32] is at bytes 8..72.
var buf [80]byte
r, _, _ := procWaveInGetDevCaps.Call(uintptr(i), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
if r == 0 {
if name := decodeUTF16Z(buf[8:72]); name != "" {
info.InputDevices = append(info.InputDevices, name)
}
}
}
}
return info
}
func decodeUTF16Z(raw []byte) string {
u16 := make([]uint16, 0, len(raw)/2)
for i := 0; i+1 < len(raw); i += 2 {
v := uint16(raw[i]) | uint16(raw[i+1])<<8
if v == 0 {
break
}
u16 = append(u16, v)
}
return strings.TrimSpace(string(utf16.Decode(u16)))
}
// FreeDiskBytesAt returns free bytes available to the current user on the volume containing dir.
func FreeDiskBytesAt(dir string) (uint64, bool) {
gb, ok := probeFreeDiskGB(dir)
if !ok {
return 0, false
}
return uint64(gb * (1 << 30)), true
}