import sys, os, time, signal, subprocess
sys.path.insert(0, "/home/ledger/aura_face")
import puck_kbd_app as A, puck_kbd_native as K
from PIL import Image

print("dj_pids():", A.dj_pids())
pids = [int(x) for x in subprocess.check_output(["pgrep", "-f", "aura_face/dj_face.py"]).split()]
print("pgrep pids:", pids)
# show one raw cmdline so we see what the match is up against
if pids:
    cl = open("/proc/%d/cmdline" % pids[0], "rb").read()
    print("sample cmdline repr:", repr(cl))
    print("'aura_face/dj_face.py' in decoded:", "aura_face/dj_face.py" in cl.decode("utf-8", "replace"))

for p in pids:
    try: os.kill(p, signal.SIGSTOP)
    except Exception as e: print("stop", p, e)
A.blit(K.render_keyboard("test typing", cands=["test", "the", "this"]))
time.sleep(0.3)
d = open("/dev/fb0", "rb").read(); px = bytearray()
for y in range(1080):
    px += d[y*8192:y*8192+1080*4]
im = Image.frombytes("RGBA", (1080, 1080), bytes(px)); b0, b1, b2, b3 = im.split()
Image.merge("RGB", (b2, b1, b0)).save("/tmp/kbd_live.png")
print("captured")
for p in pids:
    try: os.kill(p, signal.SIGCONT)
    except Exception: pass
print("resumed")
