109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
# Drive the REAL app's bridge SHM: open GUI ch=0, screenshot, pixel analysis.
|
|
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time, re
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
|
|
SHM_NAME = "SonicForge_DAW_IPC"
|
|
SIZE = 32768
|
|
LOG = os.path.join(os.path.dirname(os.path.abspath(__file__)), "probe_app.log")
|
|
|
|
user32 = ctypes.windll.user32
|
|
kernel32 = ctypes.windll.kernel32
|
|
kernel32.CreateFileMappingA.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_char_p]
|
|
kernel32.CreateFileMappingA.restype = ctypes.c_void_p
|
|
kernel32.MapViewOfFile.argtypes = [ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_size_t]
|
|
kernel32.MapViewOfFile.restype = ctypes.c_void_p
|
|
INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
|
|
PAGE_READWRITE = 0x04
|
|
FILE_MAP_ALL_ACCESS = 0xF001F
|
|
|
|
hMap = kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READWRITE, 0, SIZE, SHM_NAME.encode())
|
|
ptr = kernel32.MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0)
|
|
|
|
def w32(off, fmt, *args):
|
|
ctypes.memmove(ptr + off, struct.pack(fmt, *args), struct.calcsize(fmt))
|
|
|
|
def write_control(type_, arg0, arg1, ch, path):
|
|
for i in range(8):
|
|
off = 2836 + i * 1040
|
|
t = ctypes.c_uint32.from_address(ptr + off).value
|
|
if t == 0:
|
|
w32(off, 'IIII', type_, arg0, arg1, ch)
|
|
if path:
|
|
b = path.encode('utf-8')[:1023] + b'\x00'
|
|
ctypes.memmove(ptr + off + 16, b, len(b))
|
|
return i
|
|
raise RuntimeError("control queue full")
|
|
|
|
def push_controls(events):
|
|
for i in range(8):
|
|
w32(2836 + i * 1040, 'I', 0)
|
|
for (type_, arg0, arg1, ch, path) in events:
|
|
write_control(type_, arg0, arg1, ch, path)
|
|
w32(11156, 'I', len(events))
|
|
|
|
def wait_log(pattern, timeout):
|
|
t0 = time.time()
|
|
while time.time() - t0 < timeout:
|
|
try:
|
|
with open(LOG, 'r', encoding='utf-8', errors='replace') as f:
|
|
lines = f.readlines()
|
|
for i in range(len(lines)):
|
|
if pattern in lines[i]:
|
|
return lines[i].strip()
|
|
except FileNotFoundError:
|
|
pass
|
|
time.sleep(0.2)
|
|
return None
|
|
|
|
# copy app bridge.log to local log for wait_log
|
|
applog = os.path.join(os.environ.get('APPDATA', '.'), 'SonicForgeDAW', 'logs', 'bridge.log')
|
|
open(LOG, 'w', encoding='utf-8').write(open(applog, encoding='utf-8', errors='replace').read())
|
|
|
|
print("push OPEN_GUI ch=0...")
|
|
push_controls([(4, 0, 0, 0, "nexus")])
|
|
r = wait_log("GUI attached", 40)
|
|
print("[open]", r)
|
|
hwnd = None
|
|
if r:
|
|
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r)
|
|
if m:
|
|
hwnd = int(m.group(1), 16)
|
|
print("hwnd =", hex(hwnd) if hwnd else None)
|
|
time.sleep(6)
|
|
|
|
if hwnd:
|
|
rw = w.RECT()
|
|
user32.GetWindowRect(hwnd, ctypes.byref(rw))
|
|
print("window rect", rw.left, rw.top, rw.right, rw.bottom, "vis", user32.IsWindowVisible(hwnd))
|
|
wdt, hgt = rw.right - rw.left, rw.bottom - rw.top
|
|
ps = f"""
|
|
Add-Type -AssemblyName System.Drawing
|
|
$bmp = New-Object System.Drawing.Bitmap({wdt}, {hgt})
|
|
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
$g.CopyFromScreen({rw.left}, {rw.top}, 0, 0, $bmp.Size)
|
|
$bmp.Save('C:/Users/locpham/SonicForgeStudio/native_bridge/debug/probe_app.png')
|
|
$g.Dispose(); $bmp.Dispose()
|
|
"""
|
|
subprocess.run(["powershell", "-NoProfile", "-Command", ps], check=False)
|
|
print("screenshot saved probe_app.png")
|
|
else:
|
|
# dump all windows with class SonicForge_Native_VST3_Class
|
|
import ctypes
|
|
found = []
|
|
@ctypes.WINFUNCTYPE(w.BOOL, w.HWND, w.LPARAM)
|
|
def cb(h, lp):
|
|
buf = ctypes.create_unicode_buffer(64)
|
|
user32.GetClassNameW(h, buf, 64)
|
|
if 'SonicForge' in buf.value:
|
|
found.append((h, buf.value))
|
|
return True
|
|
user32.EnumWindows(cb, 0)
|
|
print("native vst windows:", [(hex(h), c) for h, c in found])
|
|
|
|
kernel32.UnmapViewOfFile.argtypes = [ctypes.c_void_p]
|
|
kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
|
|
kernel32.UnmapViewOfFile(ptr)
|
|
kernel32.CloseHandle(hMap)
|
|
print("done")
|