#!/usr/bin/env -S python3 """ #!/usr/bin/python3 """ import subprocess import re import sys # configure you devices: display_id_wayland = "XWAYLAND0" device_names_wayland = ["xwayland-touch", "xwayland-tablet stylus", "xwayland-tablet eraser", "xwayland-tablet cursor"] display_id_x11 = "eDP-1" device_names_x11 = ["Wacom Pen and multitouch sensor Finger touch", "Wacom Pen and multitouch sensor Pen stylus", "Wacom Pen and multitouch sensor Pen eraser"] # end configure verbose = False def is_wayland(): """check sessiontype (Wayland or X11)""" ret_value = cmd("echo $XDG_SESSION_TYPE").lower() return ret_value == "wayland\n" def cmd(c): """run shell command and return output, if verbose print command and output""" print(c) if verbose else None res = subprocess.check_output(c, shell=True).decode() print(res) if verbose else None return res def get_device_id(device_name): """get xinput device id for device name""" id = re.search(r"id=(\d+)", [l for l in cmd("xinput").split("\n") if re.search(device_name, l)][0]).group(1) return int(id) if id else 0 def set_xinput_mapping(device_id, display_id): cmd(f"xinput map-to-output {device_id} {display_id}") if __name__ == "__main__": try: args = sys.argv[1:] if len(args) > 0 and ("-v" in args or "--verbose" in args): verbose = True else: print("Use -v or --verbose to see xinput output\nSet mappings:") is_wayland() if is_wayland(): display_id = display_id_wayland device_names = device_names_wayland else: display_id = display_id_x11 device_names = device_names_x11 name_length = max([len(n) for n in device_names]) for device_name in device_names: device_id = get_device_id(device_name) print(f" - Map {device_name:{name_length}} with Id={device_id:02d} to '{display_id}'") set_xinput_mapping(device_id, display_id) except Exception as e: print(e) print("") input("Press Enter to continue...")