From 2d83018fcb21e790a3bc03cbdcf68f9156f47fc9 Mon Sep 17 00:00:00 2001 From: Ali Hatami Tajik Date: Sat, 11 Mar 2023 15:57:38 +0330 Subject: [PATCH] Refactor pointer generation --- src/scripts/python/util/pointer.py | 85 ++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/src/scripts/python/util/pointer.py b/src/scripts/python/util/pointer.py index d645a4b..213aee1 100644 --- a/src/scripts/python/util/pointer.py +++ b/src/scripts/python/util/pointer.py @@ -1,5 +1,10 @@ import x as xutil import re +from enum import Enum + + +class XInputState(Enum): + SLAVE, MASTER, FLOATING = range(3) class XInput: @@ -11,7 +16,7 @@ class XInput: is_master (bool): True if device is master """ - def __init__(self, name, id, is_master) -> None: + def __init__(self, name, id, state) -> None: """Initializes the class with name, id and master status Args: @@ -21,7 +26,7 @@ class XInput: """ self.name = name self.id = id - self.is_master = is_master + self.state = state class Pointer(XInput): @@ -34,12 +39,19 @@ class Pointer(XInput): is_master (bool): True if the pointer is a master pointer else False """ - def __init__(self, name, id, is_master: bool) -> None: - super().__init__(name, id, is_master) + def __init__(self, name, id, state) -> None: + super().__init__(name, id, state) @property def slave(self): - return not self.is_master + return self.state == XInputState.SLAVE + + @property + def master(self): + return self.state == XInputState.MASTER + + def floating(self): + return self.state == XInputState.FLOATING def get_short_pointer(id) -> Pointer: @@ -58,31 +70,41 @@ def get_short_pointer(id) -> Pointer: desc = xutil.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)[0], is_master) + state = XInputState.FLOATING + if "master" in props: + state = XInputState.MASTER + elif "slave" in props: + state = XInputState.SLAVE + return Pointer(name.strip(), props.split(maxsplit=1)[0], state) 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 +def get_ids_iter(): + """xinput id generator - 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). + Yields: + int: id of xinput devices """ - pointers = [] ids = xutil.get_ids() for id in ids: + yield id + + +def get_pointer_iter(is_short=True): + """xinput pointers generator + + Args: + is_short (bool, optional): if True generates short type pointers. + Defaults to True. + + Yields: + Pointer: xinput pointers + """ + for id in get_ids_iter(): if is_short: try: - pointers.append(get_short_pointer(id)) + yield get_short_pointer(id) except TypeError as e: # ignore if the id is not pointer pass @@ -90,14 +112,23 @@ def get_pointers(is_short=True): # TODO: logging pass else: - pass - return pointers + pass # TODO -def get_touch_master(pointers): - """returns Pointer of the master touch pointer +def get_pointers(is_short=True): + """Wraps pointers in `xinput --list` in Pointer class - Args: - pointers (List[Pointes]): list of pointers queried + 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). """ - pass + pointers = [] + for pointer in get_pointer_iter(is_short): + pointers.append(pointer) + return pointers