Last active
July 10, 2021 10:27
-
-
Save jthistle/346bdfa63960b8972d0f08de604b7bf5 to your computer and use it in GitHub Desktop.
osu! tablet configuration script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import subprocess | |
import argparse | |
import re | |
Y_OFFSET = 0 | |
X_OFFSET = 0 | |
def run_and_return_stdout(*args): | |
res = subprocess.run(*args, stdout=subprocess.PIPE) | |
return res.stdout.decode("utf-8") | |
parser = argparse.ArgumentParser( | |
"Configure osu tablet" | |
) | |
parser.add_argument( | |
"percentage", | |
type=float, | |
help="Percentage of the width of the tablet to cover as a decimal. Must be < 1.0 ." | |
) | |
parser.add_argument( | |
"--resolution", | |
type=str, | |
default="1920x1080", | |
help="Resolution of your screen" | |
) | |
parser.add_argument( | |
"--flip", | |
action="store_true", | |
help="Rotate the orientation of your tablet by 180 degrees", | |
) | |
args = parser.parse_args() | |
# Get screen res | |
ptn = re.compile(r"^\s*(\d+)x(\d+)\s*$") | |
match = ptn.search(args.resolution) | |
if match is None: | |
raise Exception("Resolution must be in form of e.g. 1920x1080") | |
resolution = int(match.group(2)) / int(match.group(1)) | |
# Set percentage | |
percentage = args.percentage | |
# Find stylus | |
devices = run_and_return_stdout(["xsetwacom", "--list", "devices"]).strip().split("\n") | |
ptn = re.compile(r"id:\s*(\d+).*?STYLUS") | |
stylus = None | |
for dev in devices: | |
res = ptn.search(dev) | |
if res is None: | |
continue | |
stylus = res.group(1) | |
break | |
if stylus is None: | |
raise Exception("Could not find your tablet - check it is plugged in") | |
# Configure tablet | |
if args.flip: | |
subprocess.call(["xsetwacom", "--set", stylus, "Rotate", "half"]) | |
subprocess.call(["xsetwacom", "--set", stylus, "ResetArea"]) | |
stdout = run_and_return_stdout(["xsetwacom", "--get", stylus, "Area"]) | |
parts = [x.strip() for x in stdout.split(" ")] | |
width = int(parts[2]) | |
height = int(parts[3]) | |
new_width = int(width * percentage) | |
new_x_start = int((width - new_width) / 2) + int(width * X_OFFSET) | |
new_height = int(resolution * new_width) | |
new_y_start = int((height - new_height) / 2) + int(height * Y_OFFSET) | |
code = subprocess.call([ | |
"xsetwacom", "--set", stylus, "Area", | |
str(new_x_start), str(new_y_start), | |
str(new_x_start + new_width), str(new_y_start + new_height) | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment