#!/usr/bin/env python3 # made for samsung galaxy note 4 # only works with stylus # open phone app, dial *#0*#, choose “Black” # requirements: # * python-libxdo import argparse import subprocess from xdo import Xdo 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 phone’s screen resolution)", ) args = parser.parse_args() 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"))) limits = [[0, 0], [0, 0]] 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] ) def real_value(axis, value): if axis == 1: value = phone_res[1] - value # https://stackoverflow.com/a/929107 return int( ((value - limits[axis][0]) * screen_res[axis]) / (limits[axis][1] - limits[axis][0]) ) update = 0 x = 0 y = 0 pressure = 0 xdo = Xdo() process = subprocess.Popen( ["adb", "shell", "getevent", "-q", "-l"], stdout=subprocess.PIPE ) for line in process.stdout: line = line.decode("utf-8") line = line.split() if line[1] != "EV_ABS": continue event = line[2] value = int("0x" + line[3], 16) if event == "ABS_PRESSURE": 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) elif event == "ABS_Y": old_x = x x = real_value(0, value) if old_x != x: update += 1 elif event == "ABS_X": 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])