Files
pokerfreak-shell/hand/foto.py
T

90 lines
3.4 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):
"""Die Fensterkennung des grössten Fensters dieses Prozesses."""
liste = CG.CGWindowListCopyWindowInfo(1, 0)
groesste, beste = 0, 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 r.w * r.h > groesste:
groesste, beste = r.w * r.h, _zahl(d, "kCGWindowNumber")
return 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"])
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