Files
pokerfreak/hand/foto.py
T
mmintelandClaude Opus 4.8 cc7148f8b8 Windows-Port: Leerlauf-Takt entstockt + nativer Prozessleser + frische Sitz-Reserve (WIP)
- winproc: Kommandozeilen nativ (Toolhelp32 + NtQueryInformationProcess/PEB)
  statt WMI/PowerShell - ~43 ms statt ~10 s je Abfrage (100x), WMI nur noch
  als Rueckfall. Das war die Hauptquelle des 35-53-s-Zyklus.
- tischkanal/runner: echo=False (feuern-und-vergessen) fuer die OHNE_ECHO-
  Zustandsbefehle (play_next/auto_post_bb/aussetzen/aufstehen/nachkauf) - kein
  1,5-s-Leerwarten mehr je Leerlauf-Takt (offline bewiesen 1519 ms -> 0 ms).
- runner: Sitz-TTL-Rennen - unmittelbar vor take_Seat frisch reservieren und
  den vom Server bestaetigten Sitz nehmen; Flut-Deckel ueber Prozesszahl statt
  Fensterzahl. Live bewiesen (Held sitzt, spielt, gewinnt).
- hand/foto,mittler,notaus,press,sitzwache + werkzeuge/hover: Windows-Rand.
- Tests: auffuellen_kappe_spec, sitz_reserve_frisch_spec (neu) + Anpassungen.

WIP: Rest-Tail ~18% der Zuege >1 s Maschinenzeit noch offen; 12-Tisch-Last
noch nicht gemessen.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-12 16:08:41 +01:00

101 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""Bild eines Fensters - auch wenn ein anderes Fenster davor liegt.
Regionsaufnahmen zeigen, was vorne liegt. Über die Fensterkennung holt
`screencapture -l` dagegen den Inhalt genau dieses Fensters.
from foto import foto
"""
import ctypes
import os as _os, sys as _sys
_w = _os.path.dirname(_os.path.abspath(__file__))
while _w != "/" and not _os.path.isdir(_os.path.join(_w, "lesen")):
_w = _os.path.dirname(_w)
WURZEL = _w
for _u in ("hand", "client", "lesen"):
_q = _os.path.join(_w, _u)
if _os.path.isdir(_q) and _q not in _sys.path:
_sys.path.insert(0, _q)
import subprocess
import fenster
# Mac-Backend (CoreGraphics-Fensteraufnahme). Unter Windows nichts laden - sonst stirbt der Import
# (die Framework-Dateien fehlen); der Screenshot laeuft dort ueber einen anderen Weg. Wie hand/click.py.
_MAC = _sys.platform == "darwin"
CF = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation") if _MAC else None
CG = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics") if _MAC else None
if CF is not None and CG is not None:
CG.CGWindowListCopyWindowInfo.restype = ctypes.c_void_p
CG.CGWindowListCopyWindowInfo.argtypes = [ctypes.c_uint32, ctypes.c_uint32]
CF.CFArrayGetCount.restype = ctypes.c_long
CF.CFArrayGetCount.argtypes = [ctypes.c_void_p]
CF.CFArrayGetValueAtIndex.restype = ctypes.c_void_p
CF.CFArrayGetValueAtIndex.argtypes = [ctypes.c_void_p, ctypes.c_long]
CF.CFDictionaryGetValue.restype = ctypes.c_void_p
CF.CFDictionaryGetValue.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
CF.CFGetTypeID.restype = ctypes.c_ulong
CF.CFGetTypeID.argtypes = [ctypes.c_void_p]
CF.CFNumberGetTypeID.restype = ctypes.c_ulong
CF.CFNumberGetValue.restype = ctypes.c_bool
CF.CFNumberGetValue.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_void_p]
ZAHL = CF.CFNumberGetTypeID() if CF is not None else None
def _schluessel(name):
return ctypes.c_void_p.in_dll(CG, name).value
def _zahl(dikt, name):
wert = CF.CFDictionaryGetValue(dikt, _schluessel(name))
if not wert or CF.CFGetTypeID(wert) != ZAHL:
return None
aus = ctypes.c_int32()
return aus.value if CF.CFNumberGetValue(wert, 3, ctypes.byref(aus)) else None
def fensterkennung(pid, rahmen=None):
"""Die Fensterkennung des Fensters mit diesem Rahmen - sonst die des grössten.
Ohne Rahmen genügt das grösste (der Tisch ist das grösste Fenster seines
Prozesses); im Klon ist die Lobby grösser als der Tisch, deshalb wird dort der
Rahmen verglichen (hand/fenster.py liefert ihn).
"""
liste = CG.CGWindowListCopyWindowInfo(1, 0)
groesste, beste, passend = 0, None, None
class CGRect(ctypes.Structure):
_fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double),
("w", ctypes.c_double), ("h", ctypes.c_double)]
CG.CGRectMakeWithDictionaryRepresentation.restype = ctypes.c_bool
CG.CGRectMakeWithDictionaryRepresentation.argtypes = [ctypes.c_void_p,
ctypes.POINTER(CGRect)]
for i in range(CF.CFArrayGetCount(liste)):
d = CF.CFArrayGetValueAtIndex(liste, i)
if _zahl(d, "kCGWindowOwnerPID") != pid:
continue
grenzen = CF.CFDictionaryGetValue(d, _schluessel("kCGWindowBounds"))
r = CGRect()
if grenzen:
CG.CGRectMakeWithDictionaryRepresentation(grenzen, ctypes.byref(r))
if rahmen and all(abs(a - b) <= 2 for a, b in zip((r.x, r.y, r.w, r.h), rahmen)):
passend = passend or _zahl(d, "kCGWindowNumber")
if r.w * r.h > groesste:
groesste, beste = r.w * r.h, _zahl(d, "kCGWindowNumber")
return passend or beste
def foto(pfad, raum=None):
"""Schreibt ein Bild des Tischfensters. Gibt den Pfad zurück oder None."""
f = fenster.fenster_fuer(raum) if raum else (fenster.tischfenster() or [None])[0]
if not f:
return None
kennung = fensterkennung(f["pid"], f["rect"])
if not kennung:
return None
pfad = _os.path.abspath(pfad)
subprocess.run(["screencapture", "-o", "-x", "-l", str(kennung), pfad],
capture_output=True)
return pfad if _os.path.exists(pfad) else None