scripts/tabletcfg.py

51 lines
1.1 KiB
Python
Raw Permalink Normal View History

2021-07-11 10:47:36 +02:00
#!/usr/bin/env cached-nix-shell
#!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.pyyaml ])"
# Set Digitizer parameters from saved profiles using swaymsg
2020-01-16 19:25:19 +01:00
#
# This currently only works for setting the screen and area but not for setting
# the buttons.
#
# Write your config into ~/.config/tabletcfg.yml
2020-01-16 19:25:19 +01:00
# Example:
#
# presets:
# my-preset:
# screen: DP-4
# area: 0 0 1 1
#
# then run tabletcfg.py my-preset
2020-01-16 19:25:19 +01:00
from subprocess import run
import os.path
import sys
2020-01-16 19:25:19 +01:00
import yaml
2020-02-26 00:37:11 +01:00
def _set_parameter(parameter, *args):
2020-01-16 19:25:19 +01:00
args = map(str, args)
run(
["swaymsg", "input", "type:tablet_tool", 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/tabletcfg.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)