Browse Source

Refactor pointer generation

pull/2/head
Ali Hatami Tajik 2 years ago
parent
commit
2d83018fcb
  1. 85
      src/scripts/python/util/pointer.py

85
src/scripts/python/util/pointer.py

@ -1,5 +1,10 @@
import x as xutil import x as xutil
import re import re
from enum import Enum
class XInputState(Enum):
SLAVE, MASTER, FLOATING = range(3)
class XInput: class XInput:
@ -11,7 +16,7 @@ class XInput:
is_master (bool): True if device is master 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 """Initializes the class with name, id and master status
Args: Args:
@ -21,7 +26,7 @@ class XInput:
""" """
self.name = name self.name = name
self.id = id self.id = id
self.is_master = is_master self.state = state
class Pointer(XInput): class Pointer(XInput):
@ -34,12 +39,19 @@ class Pointer(XInput):
is_master (bool): True if the pointer is a master pointer else False is_master (bool): True if the pointer is a master pointer else False
""" """
def __init__(self, name, id, is_master: bool) -> None: def __init__(self, name, id, state) -> None:
super().__init__(name, id, is_master) super().__init__(name, id, state)
@property @property
def slave(self): 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: def get_short_pointer(id) -> Pointer:
@ -58,31 +70,41 @@ def get_short_pointer(id) -> Pointer:
desc = xutil.get_list_short(id) desc = xutil.get_list_short(id)
name, props = desc.rsplit("id=", 1) name, props = desc.rsplit("id=", 1)
if "pointer" in props: if "pointer" in props:
is_master = "master" in props state = XInputState.FLOATING
return Pointer(name.strip(), props.split(maxsplit=1)[0], is_master) 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: else:
raise TypeError(f"id[{id}] is not a pointer id") raise TypeError(f"id[{id}] is not a pointer id")
def get_pointers(is_short=True): def get_ids_iter():
"""Wraps pointers in `xinput --list` in Pointer class """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 Yields:
get ids and then execute `xinput --list {id}` to get the description with int: id of xinput devices
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 = xutil.get_ids() ids = xutil.get_ids()
for id in 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: if is_short:
try: try:
pointers.append(get_short_pointer(id)) yield get_short_pointer(id)
except TypeError as e: except TypeError as e:
# ignore if the id is not pointer # ignore if the id is not pointer
pass pass
@ -90,14 +112,23 @@ def get_pointers(is_short=True):
# TODO: logging # TODO: logging
pass pass
else: else:
pass pass # TODO
return pointers
def get_touch_master(pointers): def get_pointers(is_short=True):
"""returns Pointer of the master touch pointer """Wraps pointers in `xinput --list` in Pointer class
Args: Creation of the pointer is done by getting the list description of
pointers (List[Pointes]): list of pointers queried 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

Loading…
Cancel
Save