-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.py
85 lines (70 loc) · 2.57 KB
/
inputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from os import device_encoding
import subprocess
from dataclasses import dataclass
from typing import List
import re
import logging
# General logging settings
FORMAT = '%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s - %(message)s'
logging.basicConfig(level=logging.ERROR, format=FORMAT)
# logging for this file
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
class ExecError(Exception):
"""Error when execution on a command fails
"""
pass
class BadResult(Exception):
"""Error when the output of the execution of a command is bad
"""
pass
@dataclass
class HuionDevice:
"""Represents on Input HUION device
"""
name: str
xinput_id: int
input_type: str
def get_huion_pointer_devices(device_name_match:str) -> List[HuionDevice]:
"""Gets the list of huion devices that are pointers connected to the system
Args:
device_name_match (str): String for matching with the devices names returned by xinput. Empty string means all devices.
Raises:
ExecError: if getting devices fails
BadResult: if the result is bad
Returns:
List[HuionDevice]: List of Huion pointer devices
"""
xinput_cmd = ['xinput', 'list']
try:
input_devices = subprocess.check_output(xinput_cmd)
except Exception as e:
raise ExecError(e)
try:
input_devices = input_devices.decode().splitlines()
input_devices = [input_device.strip() for input_device in input_devices]
ret = []
pattern = re.compile('(?:.*)↳\s+(.+)id=(\d+)\s+\[(.+)\]')
for input_device in input_devices:
data = pattern.search(input_device)
if data is None:
continue
if len(data.groups()) != 3:
raise BadResult(f'xinput returned wrong formated output. {input_device}')
device_name = data.group(1).strip()
device_id = int(data.group(2))
input_type = data.group(3)
if device_name_match.lower() in device_name.lower() and "pointer" in input_type.lower():
disp = HuionDevice(device_name, device_id, input_type)
ret.append(disp)
_logger.debug(device_name)
except Exception as e:
raise BadResult(e)
return ret
def map_input_device_to_output(device_id: int, output_display: str) -> None:
xinput_map_cmd = ['xinput', 'map-to-output', f'{device_id}', output_display]
_logger.debug(xinput_map_cmd)
try:
subprocess.check_output(xinput_map_cmd)
except Exception as e:
raise ExecError(e)