scripts/android-touchpad.py

97 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-01-16 19:25:19 +01:00
#!/usr/bin/env python3
# made for samsung galaxy note 4
# only works with stylus
# open phone app, dial *#0*#, choose “Black”
2020-02-26 00:37:11 +01:00
# requirements:
# * python-libxdo
2020-01-16 19:25:19 +01:00
import argparse
import subprocess
from xdo import Xdo
2020-02-26 00:37:11 +01:00
parser = argparse.ArgumentParser(description="Use android phone stylus as input method")
parser.add_argument(
"--pos",
type=str,
default="100x100+0x0",
help="Position on phone to use in percent (e.g. 40x40+10x10)",
)
parser.add_argument(
"--screen", type=str, default="1920x1080+0x0", help="Screen resolution and offset"
)
parser.add_argument(
"--phone",
type=str,
default="12544x7056",
help="Phone resolution (not necessarily equal to the phones screen resolution)",
)
2020-01-16 19:25:19 +01:00
args = parser.parse_args()
2020-02-26 00:37:11 +01:00
screen_res = tuple(map(int, args.screen.split("+")[0].split("x")))
offset = tuple(map(int, args.screen.split("+")[1].split("x")))
phone_res = tuple(map(int, args.phone.split("x")))
2020-01-16 19:25:19 +01:00
limits = [[0, 0], [0, 0]]
2020-02-26 00:37:11 +01:00
limits[0][0] = int(args.pos.split("+")[1].split("x")[0]) / 100 * phone_res[0]
limits[0][1] = (
limits[0][0] + int(args.pos.split("+")[0].split("x")[0]) / 100 * phone_res[0]
)
limits[1][0] = int(args.pos.split("+")[1].split("x")[1]) / 100 * phone_res[1]
limits[1][1] = (
limits[1][0] + int(args.pos.split("+")[0].split("x")[1]) / 100 * phone_res[1]
)
2020-01-16 19:25:19 +01:00
def real_value(axis, value):
if axis == 1:
value = phone_res[1] - value
# https://stackoverflow.com/a/929107
2020-02-26 00:37:11 +01:00
return int(
((value - limits[axis][0]) * screen_res[axis])
/ (limits[axis][1] - limits[axis][0])
)
2020-01-16 19:25:19 +01:00
update = 0
x = 0
y = 0
pressure = 0
xdo = Xdo()
2020-02-26 00:37:11 +01:00
process = subprocess.Popen(
["adb", "shell", "getevent", "-q", "-l"], stdout=subprocess.PIPE
)
2020-01-16 19:25:19 +01:00
for line in process.stdout:
2020-02-26 00:37:11 +01:00
line = line.decode("utf-8")
2020-01-16 19:25:19 +01:00
line = line.split()
2020-02-26 00:37:11 +01:00
if line[1] != "EV_ABS":
2020-01-16 19:25:19 +01:00
continue
event = line[2]
2020-02-26 00:37:11 +01:00
value = int("0x" + line[3], 16)
2020-01-16 19:25:19 +01:00
2020-02-26 00:37:11 +01:00
if event == "ABS_PRESSURE":
2020-01-16 19:25:19 +01:00
if value == 0 and pressure != 0:
xdo.mouse_up(0, 1)
elif pressure == 0:
xdo.mouse_down(0, 1)
pressure = value
# Y and X flipped (landscape)
2020-02-26 00:37:11 +01:00
elif event == "ABS_Y":
2020-01-16 19:25:19 +01:00
old_x = x
x = real_value(0, value)
if old_x != x:
update += 1
2020-02-26 00:37:11 +01:00
elif event == "ABS_X":
2020-01-16 19:25:19 +01:00
old_y = y
y = real_value(1, value)
if old_y != y:
update += 1
if update == 2:
update = 0
if 0 <= x < screen_res[0] and 0 <= y < screen_res[1]:
xdo.move_mouse(x + offset[0], y + offset[1])