From da0a4c180faf38cf768235c8875f3ef8b3129cd3 Mon Sep 17 00:00:00 2001 From: Ali Hatami Tajik Date: Mon, 27 Feb 2023 11:26:53 +0330 Subject: [PATCH] Add get_pointers Because: + we need this functionality to list pointers and operate on them later + this list can be filtered later with less string computations --- src/scripts/python/xutil/pointer.py | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/scripts/python/xutil/pointer.py b/src/scripts/python/xutil/pointer.py index cea1f90..9ae405b 100644 --- a/src/scripts/python/xutil/pointer.py +++ b/src/scripts/python/xutil/pointer.py @@ -36,3 +36,56 @@ class Pointer(XInput): def __init__(self, name, id, is_master: bool) -> None: super().__init__(name, id, is_master) +def get_short_pointer(id) -> Pointer: + """Generates Pointer object corresponding to id (short attrs) + + Args: + id (int): pointer id + + Returns: + Pointer: pointer object with name, id and is_master props + + Rises: + ValueError: if id is not reistered with xinput + ValueError: if id is not a pointer id + """ + desc = com.get_list_short(id) + name, props = desc.rsplit('id=', 1) + if "pointer" in props: + is_master = "master" in props + return Pointer( + name.strip(), + props.split(maxsplit=1), + is_master) + else: + raise TypeError(f'id[{id}] is not a pointer id') + + +def get_pointers(is_short=True): + """Wraps pointers in `xinput --list` in Pointer class + + Creation of the pointer is done by getting the list description of + each id. if the is_short arg is True, then short list description will be + used which will provide the class `name`, `is_master` and `id` values. + + Getting this pointers is done by first calling `xinput --list --id-only` to + get ids and then execute `xinput --list {id}` to get the description with + less-complicated output compare to iterating over `xinput --list --short` + line by line (--short option has some special characters that cause overhead + to the system for processing them individually and per-case). + """ + pointers = [] + ids = com.get_ids() + for id in ids: + if is_short: + try: + pointers.append(get_short_pointer(id)) + except TypeError as e: + # ignore if the id is not pointer + pass + except e: + # TODO: logging + pass + else: + pass + return pointers