fensterkennung nahm das groesste Fenster des Prozesses - im Klon ist die Lobby groesser als der Tisch, das Bild zeigte also die Lobby. Jetzt wird der Rahmen aus hand/fenster.py verglichen; ohne Rahmen bleibt es beim groessten (echter Tisch). Der Kettentest prueft beides: andere Kennung als das groesste Fenster und ein Bild im Seitenverhaeltnis des Tisches.
97 lines
3.8 KiB
Python
97 lines
3.8 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, "hand")):
|
|
_w = _os.path.dirname(_w)
|
|
WURZEL = _w
|
|
for _u in ("hand", "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
|
|
|
|
CF = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")
|
|
CG = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")
|
|
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()
|
|
|
|
|
|
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 echte 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
|