Ein Klick an den Prozess (CGEventPostToPid) erreicht den Unity-Tisch, einen Chrome-Klon überhaupt nicht - belegt mit zwei Mithörern in der Seite: kein Ereignis kam an. Ein globaler Klick erreicht beide, trifft aber nur, was oben liegt: hand/click.nach_vorn hebt das Zielfenster an und holt die App nach vorn. Dazu im Kettentest: die Uhr eines Zuges ist --clock plus die 30 s Zusatzzeit aus der Aufnahme (genau wie tests/lagen_spec.py rechnet), und der Buy-in wird je Tisch bestaetigt, bevor der naechste drankommt - sonst bleibt der Dialog offen.
143 lines
4.6 KiB
Python
143 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Klickt die Aktionsknöpfe des Tisches an - Positionen aus dem Fensterrahmen.
|
|
|
|
Die Knöpfe liegen immer an derselben Stelle im Fenster, deshalb werden die
|
|
Positionen als Anteil der Fenstergröße berechnet. Verschieben und Größe ändern
|
|
macht also nichts.
|
|
|
|
python3 click.py --where # Positionen anzeigen
|
|
python3 click.py fold # Fold klicken
|
|
python3 click.py call # Call klicken
|
|
python3 click.py raise # Raise klicken
|
|
python3 click.py size 33 # Einsatz-Voreinstellung 33% wählen
|
|
"""
|
|
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 ("lesen", "entscheiden", "hand", "lobby", "korpus", "betrieb"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import ctypes
|
|
import subprocess
|
|
import sys
|
|
import time as _time
|
|
|
|
FW = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices"
|
|
AS = ctypes.cdll.LoadLibrary(FW)
|
|
|
|
|
|
class CGPoint(ctypes.Structure):
|
|
_fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)]
|
|
|
|
|
|
AS.CGEventCreateMouseEvent.restype = ctypes.c_void_p
|
|
AS.CGEventCreateMouseEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint32, CGPoint,
|
|
ctypes.c_uint32]
|
|
AS.CGEventPost.argtypes = [ctypes.c_uint32, ctypes.c_void_p]
|
|
AS.CGEventPostToPid.argtypes = [ctypes.c_int, ctypes.c_void_p]
|
|
MOVED, DOWN, UP = 5, 1, 2
|
|
|
|
# Anteil der Fensterbreite/-höhe (aus einem Bild vom Tisch abgelesen)
|
|
SPOTS = {
|
|
"fold": (0.659, 0.943),
|
|
"call": (0.796, 0.943),
|
|
"raise": (0.933, 0.943),
|
|
"size33": (0.616, 0.858),
|
|
"size50": (0.677, 0.858),
|
|
"size75": (0.735, 0.858),
|
|
"max": (0.784, 0.858),
|
|
"amount": (0.873, 0.855),
|
|
}
|
|
|
|
|
|
def osa(script):
|
|
r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
|
|
return (r.stdout or r.stderr).strip()
|
|
|
|
|
|
def window_rect():
|
|
pids = subprocess.run(["pgrep", "-f", "CoinPoker Game"], capture_output=True,
|
|
text=True).stdout.split()
|
|
if not pids:
|
|
return None
|
|
proc = f'(first process whose unix id is {pids[0]})'
|
|
pos = osa(f'tell application "System Events" to tell {proc} to get position of window 1')
|
|
size = osa(f'tell application "System Events" to tell {proc} to get size of window 1')
|
|
try:
|
|
x, y = [float(v) for v in pos.replace(" ", "").split(",")[:2]]
|
|
w, h = [float(v) for v in size.replace(" ", "").split(",")[:2]]
|
|
except Exception:
|
|
return None
|
|
return x, y, w, h
|
|
|
|
|
|
def point_for(name, rect):
|
|
if name not in SPOTS:
|
|
return None
|
|
x, y, w, h = rect
|
|
fx, fy = SPOTS[name]
|
|
return round(x + w * fx), round(y + h * fy)
|
|
|
|
|
|
def nach_vorn(fenster):
|
|
"""Fenster anheben und die App nach vorn holen.
|
|
|
|
Ein Klick an den Prozess (CGEventPostToPid) erreicht den Unity-Tisch, einen
|
|
Chrome-Klon aber gar nicht; ein globaler Klick erreicht beide. Ein globaler
|
|
trifft nur, was oben liegt - deshalb muss das Zielfenster vorher nach vorn.
|
|
"""
|
|
if not fenster:
|
|
return
|
|
pid = fenster.get("pid")
|
|
titel = (fenster.get("titel") or "").replace('"', '\\"')
|
|
proc = f'(first process whose unix id is {pid})'
|
|
if titel:
|
|
osa(f'tell application "System Events" to perform action "AXRaise" of '
|
|
f'(first window whose name is "{titel}") of {proc}')
|
|
osa(f'tell application "System Events" to set frontmost of {proc} to true')
|
|
|
|
|
|
def click(px, py, pid=None, fenster=None):
|
|
"""Klickt global. Mit `fenster` (aus hand/fenster.py) wird es vorher angehoben."""
|
|
if fenster:
|
|
pid = fenster.get("pid")
|
|
nach_vorn(fenster)
|
|
for kind in (MOVED, DOWN, UP):
|
|
event = AS.CGEventCreateMouseEvent(None, kind, CGPoint(px, py), 0)
|
|
AS.CGEventPost(0, event)
|
|
_time.sleep(0.03)
|
|
|
|
|
|
def main():
|
|
args = sys.argv[1:]
|
|
rect = window_rect()
|
|
if not rect:
|
|
print("kein Tischfenster")
|
|
return 1
|
|
x, y, w, h = rect
|
|
print(f"Tischfenster: {int(x)},{int(y)} {int(w)}x{int(h)}")
|
|
|
|
if not args or args[0] == "--where":
|
|
for name in SPOTS:
|
|
px, py = point_for(name, rect)
|
|
print(f" {name:<7} -> {px},{py}")
|
|
return 0
|
|
|
|
key = args[0]
|
|
if key == "size":
|
|
key = "size" + args[1]
|
|
px, py = point_for(key, rect)
|
|
if px is None:
|
|
print(f"unbekannt: {key}; bekannt: {', '.join(SPOTS)}")
|
|
return 1
|
|
click(px, py)
|
|
print(f"Klick auf {key} bei {px},{py}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|