#!/data/data/com.termux/files/usr/bin/env python3

import sys
import threading

try:
	import termuxgui as tg
	import termuxgui.msg as tgmsg
except ModuleNotFoundError:
	sys.exit("termuxgui module not found. Please install the Termux:GUI python bindings: https://github.com/tareksander/termux-gui-python-bindings")


with tg.Connection() as c:
    
    def handleEvents():
        global event
        while True:
            msg = tgmsg.read_msg(c._event)
            print(json.dumps(msg), file=sys.stderr, flush=True)
    
    
    def handleOutput():
        global main
        while True:
            msg = tgmsg.read_msg(c._main)
            print(json.dumps(msg), flush=True)
    
    
    eventt = threading.Thread(target=handleEvents,daemon=True)
    outt = threading.Thread(target=handleOutput,daemon=True)
    eventt.start()
    outt.start()
    
    
    while True:
        tgmsg.send_msg(c._main, sys.stdin.readline())
