diff --git a/huion-tablet-x11.py b/huion-tablet-x11.py new file mode 100755 index 0000000..15b953c --- /dev/null +++ b/huion-tablet-x11.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# Set Huion H430P parameters from saved profiles using xsetwacom +# Driver: https://github.com/DIGImend/digimend-kernel-drivers +# +# Write your config into ~/.config/huion-tablet-yml +# Example: +# +# presets: +# my-preset: +# screen: DP2-2 +# area: 0 0 24384 15240 +# buttons: +# pad: +# 1: key Ctrl Z +# 2: key Ctrl Shift Z +# 3: key + +# 8: key - +# then run huion-tablet.py my-preset + +from subprocess import run +import argparse +import os.path +import yaml + + +def set_parameter(device, *args): + if device == "stylus": + device = "HUION Huion Tablet Pen stylus" + elif device == "pad": + device = "HUION Huion Tablet Pad pad" + args = map(str, args) + run(["xsetwacom", "set", device, *args], check=True) + + +parser = argparse.ArgumentParser(description="setup huion h430p tablet") +parser.add_argument("--area", type=str, default="") +parser.add_argument("--screen", type=str, default="") +parser.add_argument("preset", metavar="PRESET", type=str, nargs="?", help="a preset") +args = parser.parse_args() + +area = "0 0 24384 15240" +screen = "" + +if args.preset is not None: + with open(os.path.expanduser("~/.config/huion-tablet.yml"), "r") as f: + config = yaml.load(f, Loader=yaml.SafeLoader) + preset = config["presets"][args.preset] + area = preset["area"] + screen = preset["screen"] + if "buttons" in preset: + for device in preset["buttons"]: + for button, mapping in preset["buttons"][device].items(): + set_parameter(device, "button", button, *str(mapping).split(" ")) + +if args.area != "": + area = args.area +if args.screen != "": + screen = args.screen + +area = tuple(map(int, area.split(" "))) + +set_parameter("stylus", "Area", " ".join(map(str, area))) +set_parameter("stylus", "MapToOutput", screen) diff --git a/huion-tablet.py b/huion-tablet.py index 15b953c..b2eb273 100755 --- a/huion-tablet.py +++ b/huion-tablet.py @@ -1,63 +1,50 @@ -#!/usr/bin/env python3 -# Set Huion H430P parameters from saved profiles using xsetwacom -# Driver: https://github.com/DIGImend/digimend-kernel-drivers +#!/usr/bin/env nix-shell +#!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.pyyaml ])" +# Set Huion H430P parameters from saved profiles using swaymsg # -# Write your config into ~/.config/huion-tablet-yml +# This currently only works for setting the screen and area but not for setting +# the buttons. +# +# Write your config into ~/.config/huion-tablet.yml # Example: # # presets: # my-preset: -# screen: DP2-2 -# area: 0 0 24384 15240 -# buttons: -# pad: -# 1: key Ctrl Z -# 2: key Ctrl Shift Z -# 3: key + -# 8: key - +# screen: DP-4 +# area: 0 0 1 1 +# # then run huion-tablet.py my-preset - from subprocess import run -import argparse import os.path +import sys import yaml -def set_parameter(device, *args): - if device == "stylus": - device = "HUION Huion Tablet Pen stylus" - elif device == "pad": - device = "HUION Huion Tablet Pad pad" +def _set_parameter(parameter, *args): args = map(str, args) - run(["xsetwacom", "set", device, *args], check=True) + run( + ["swaymsg", "input", "9580:109:HUION_Huion_Tablet_Pen", parameter, *args], + check=True, + ) -parser = argparse.ArgumentParser(description="setup huion h430p tablet") -parser.add_argument("--area", type=str, default="") -parser.add_argument("--screen", type=str, default="") -parser.add_argument("preset", metavar="PRESET", type=str, nargs="?", help="a preset") -args = parser.parse_args() +def map_to_output(identifier): + _set_parameter("map_to_output", identifier) -area = "0 0 24384 15240" -screen = "" -if args.preset is not None: - with open(os.path.expanduser("~/.config/huion-tablet.yml"), "r") as f: - config = yaml.load(f, Loader=yaml.SafeLoader) - preset = config["presets"][args.preset] - area = preset["area"] - screen = preset["screen"] - if "buttons" in preset: - for device in preset["buttons"]: - for button, mapping in preset["buttons"][device].items(): - set_parameter(device, "button", button, *str(mapping).split(" ")) +def map_from_region(x1, y1, x2, y2): + _set_parameter("map_from_region", f"{x1}x{y1}", f"{x2}x{y2}") -if args.area != "": - area = args.area -if args.screen != "": - screen = args.screen -area = tuple(map(int, area.split(" "))) +preset = sys.argv[1] -set_parameter("stylus", "Area", " ".join(map(str, area))) -set_parameter("stylus", "MapToOutput", screen) +with open(os.path.expanduser("~/.config/huion-tablet.yml"), "r") as f: + config = yaml.load(f, Loader=yaml.SafeLoader) + preset = config["presets"][preset] + +area = list(map(float, preset["area"].split(" "))) +assert len(area) == 4 +screen = preset["screen"] + +map_to_output(screen) +map_from_region(*area)