Refactor tablet device search code

This commit is contained in:
Eric Jiang 2021-02-04 13:02:53 -08:00
parent bd22057c30
commit 13e40d0025

View File

@ -6,6 +6,11 @@ from _xdo_cffi import ffi, lib
CONFIG_FILE_PATH = os.path.expanduser('~/.config/huion_keys.conf') CONFIG_FILE_PATH = os.path.expanduser('~/.config/huion_keys.conf')
TABLET_MODELS = {
"Kamvas Pro (2019)": "256c:006e",
"Q620M": "256c:006d",
}
BUTTON_BINDINGS = {} BUTTON_BINDINGS = {}
def main(): def main():
@ -19,9 +24,12 @@ def main():
return 1 return 1
prev_button = None prev_button = None
while True: while True:
hidraw_path = get_tablet_hidraw('256c', '006e') hidraw_path = None
if hidraw_path is None: # search for a known tablet device
hidraw_path = get_tablet_hidraw('256c', '006d') # Q620M for device_name, device_id in TABLET_MODELS.items():
hidraw_path = get_tablet_hidraw(device_id)
if hidraw_path is not None:
break
if hidraw_path is None: if hidraw_path is None:
print("Could not find tablet hidraw device") print("Could not find tablet hidraw device")
time.sleep(2) time.sleep(2)
@ -54,13 +62,13 @@ def main():
xdo, lib.CURRENTWINDOW, BUTTON_BINDINGS[btn], 1000) xdo, lib.CURRENTWINDOW, BUTTON_BINDINGS[btn], 1000)
def get_tablet_hidraw(vendor_id, product_id): def get_tablet_hidraw(device_id):
"""Finds the /dev/hidrawX file that belongs to the given vendor and product ID.""" """Finds the /dev/hidrawX file that belongs to the given device ID (in xxxx:xxxx format)."""
# TODO: is this too fragile? # TODO: is this too fragile?
hidraws = os.listdir('/sys/class/hidraw') hidraws = os.listdir('/sys/class/hidraw')
for h in hidraws: for h in hidraws:
device_path = os.readlink(os.path.join('/sys/class/hidraw', h, 'device')) device_path = os.readlink(os.path.join('/sys/class/hidraw', h, 'device'))
if ("%s:%s" % (vendor_id.upper(), product_id.upper())) in device_path: if device_id.upper() in device_path:
# need to confirm that there's "input" because there are two hidraw # need to confirm that there's "input" because there are two hidraw
# files listed for the tablet, but only one of them carries the # files listed for the tablet, but only one of them carries the
# mouse/keyboard input # mouse/keyboard input