#!/usr/bin/python3
import os

# Touch mapper for Xorg/XDOTool >> Kobos
file = open("evtest-log","r")
coordinates = file.read()
file.close()
# Prevent other exit status than 0 if evtest log file is empty
if len(coordinates) <= 10:
    exit()
else:
    c1 = coordinates.split("\n")

    # Mapping x
    for item in c1:
        if "ABS_MT_POSITION_X" in item:
            c2 = item.split(" ")
            c3 = c2[-1]
    x = c3
    print("registered native x " + x)

    # Mapping y
    for item in c1:
        if "ABS_MT_POSITION_Y" in item:
            c2 = item.split(" ")
            c3 = c2[-1]
    y = c3
    print("registered native y " + y)

    # Dividing for lower resolutions
    # Originally made for Kobo Libra -> 1680x1264 / 2 = 840x632
    x = float(x)
    y = float(y)
    x2 = x
    y2 = y
    print("converted native x to %s" % x2)
    print("converted native y to %s" % y2)

    # Passing x + y to xdotool
    os.system("DISPLAY=:0 xdotool mousemove {0} {1}".format(x2, y2))
    # Clicking mouse button
    os.system("DISPLAY=:0 xdotool click 1")

    # Done! Emptying the file
    os.system("> evtest-log")