scripts/huion-tablet.py

51 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.pyyaml ])"
# Set Huion H430P parameters from saved profiles using swaymsg
#
# 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: DP-4
# area: 0 0 1 1
#
# then run huion-tablet.py my-preset
from subprocess import run
import os.path
import sys
import yaml
def _set_parameter(parameter, *args):
args = map(str, args)
run(
["swaymsg", "input", "9580:109:HUION_Huion_Tablet_Pen", parameter, *args],
check=True,
)
def map_to_output(identifier):
_set_parameter("map_to_output", identifier)
def map_from_region(x1, y1, x2, y2):
_set_parameter("map_from_region", f"{x1}x{y1}", f"{x2}x{y2}")
preset = sys.argv[1]
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)