2021-04-23 09:51:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2021-04-23 09:51:21 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2021-04-23 09:51:21 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2021-04-23 09:51:21 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2021-04-23 09:51:21 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
"""Find, classify and group devices.
|
|
|
|
|
|
|
|
Because usually connected devices pop up multiple times in /dev/input,
|
|
|
|
in order to provide multiple types of input devices (e.g. a keyboard and a
|
|
|
|
graphics-tablet at the same time)
|
|
|
|
|
|
|
|
Those groups are what is being displayed in the device dropdown, and
|
|
|
|
events are being read from all of the paths of an individual group in the gui
|
|
|
|
and the injector.
|
|
|
|
"""
|
2022-11-12 16:45:32 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
from __future__ import annotations
|
2022-11-12 16:45:32 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
import asyncio
|
2022-07-23 08:53:41 +00:00
|
|
|
import enum
|
2021-04-23 09:51:21 +00:00
|
|
|
import json
|
2022-07-23 08:53:41 +00:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import threading
|
|
|
|
from typing import List, Optional
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
import evdev
|
2022-11-12 16:45:32 +00:00
|
|
|
from evdev import InputDevice
|
2021-09-26 10:44:56 +00:00
|
|
|
from evdev.ecodes import (
|
|
|
|
EV_KEY,
|
|
|
|
EV_ABS,
|
|
|
|
KEY_CAMERA,
|
|
|
|
EV_REL,
|
|
|
|
BTN_STYLUS,
|
|
|
|
ABS_MT_POSITION_X,
|
|
|
|
REL_X,
|
|
|
|
KEY_A,
|
|
|
|
BTN_LEFT,
|
|
|
|
REL_Y,
|
|
|
|
REL_WHEEL,
|
|
|
|
)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.paths import get_preset_path
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.logger import logger
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
TABLET_KEYS = [
|
|
|
|
evdev.ecodes.BTN_STYLUS,
|
|
|
|
evdev.ecodes.BTN_TOOL_BRUSH,
|
|
|
|
evdev.ecodes.BTN_TOOL_PEN,
|
2021-09-26 10:44:56 +00:00
|
|
|
evdev.ecodes.BTN_TOOL_RUBBER,
|
2021-04-23 09:51:21 +00:00
|
|
|
]
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
|
|
|
|
class DeviceType(str, enum.Enum):
|
|
|
|
GAMEPAD = "gamepad"
|
|
|
|
KEYBOARD = "keyboard"
|
|
|
|
MOUSE = "mouse"
|
|
|
|
TOUCHPAD = "touchpad"
|
|
|
|
GRAPHICS_TABLET = "graphics-tablet"
|
|
|
|
CAMERA = "camera"
|
|
|
|
UNKNOWN = "unknown"
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
if not hasattr(evdev.InputDevice, "path"):
|
2021-04-23 09:51:21 +00:00
|
|
|
# for evdev < 1.0.0 patch the path property
|
|
|
|
@property
|
|
|
|
def path(device):
|
|
|
|
return device.fn
|
|
|
|
|
|
|
|
evdev.InputDevice.path = path
|
|
|
|
|
|
|
|
|
|
|
|
def _is_gamepad(capabilities):
|
2022-01-31 19:58:37 +00:00
|
|
|
"""Check if joystick movements are available for preset."""
|
2021-04-23 09:51:21 +00:00
|
|
|
# A few buttons that indicate a gamepad
|
|
|
|
buttons = {
|
|
|
|
evdev.ecodes.BTN_BASE,
|
|
|
|
evdev.ecodes.BTN_A,
|
|
|
|
evdev.ecodes.BTN_THUMB,
|
|
|
|
evdev.ecodes.BTN_TOP,
|
|
|
|
evdev.ecodes.BTN_DPAD_DOWN,
|
|
|
|
evdev.ecodes.BTN_GAMEPAD,
|
|
|
|
}
|
|
|
|
if not buttons.intersection(capabilities.get(EV_KEY, [])):
|
|
|
|
# no button is in the key capabilities
|
|
|
|
return False
|
|
|
|
|
|
|
|
# joysticks
|
|
|
|
abs_capabilities = capabilities.get(EV_ABS, [])
|
|
|
|
if evdev.ecodes.ABS_X not in abs_capabilities:
|
|
|
|
return False
|
|
|
|
if evdev.ecodes.ABS_Y not in abs_capabilities:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _is_mouse(capabilities):
|
|
|
|
"""Check if the capabilities represent those of a mouse."""
|
|
|
|
# Based on observation, those capabilities need to be present to get an
|
|
|
|
# UInput recognized as mouse
|
|
|
|
|
|
|
|
# mouse movements
|
|
|
|
if not REL_X in capabilities.get(EV_REL, []):
|
|
|
|
return False
|
|
|
|
if not REL_Y in capabilities.get(EV_REL, []):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# at least the vertical mouse wheel
|
|
|
|
if not REL_WHEEL in capabilities.get(EV_REL, []):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# and a mouse click button
|
|
|
|
if not BTN_LEFT in capabilities.get(EV_KEY, []):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _is_graphics_tablet(capabilities):
|
|
|
|
"""Check if the capabilities represent those of a graphics tablet."""
|
|
|
|
if BTN_STYLUS in capabilities.get(EV_KEY, []):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _is_touchpad(capabilities):
|
|
|
|
"""Check if the capabilities represent those of a touchpad."""
|
|
|
|
if ABS_MT_POSITION_X in capabilities.get(EV_ABS, []):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _is_keyboard(capabilities):
|
|
|
|
"""Check if the capabilities represent those of a keyboard."""
|
|
|
|
if KEY_A in capabilities.get(EV_KEY, []):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _is_camera(capabilities):
|
|
|
|
"""Check if the capabilities represent those of a camera."""
|
|
|
|
key_capa = capabilities.get(EV_KEY)
|
|
|
|
return key_capa and len(key_capa) == 1 and key_capa[0] == KEY_CAMERA
|
|
|
|
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def classify(device) -> DeviceType:
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Figure out what kind of device this is.
|
|
|
|
|
|
|
|
Use this instead of functions like _is_keyboard to avoid getting false
|
|
|
|
positives.
|
|
|
|
"""
|
|
|
|
capabilities = device.capabilities(absinfo=False)
|
|
|
|
|
|
|
|
if _is_graphics_tablet(capabilities):
|
|
|
|
# check this before is_gamepad to avoid classifying abs_x
|
|
|
|
# as joysticks when they are actually stylus positions
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.GRAPHICS_TABLET
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
if _is_touchpad(capabilities):
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.TOUCHPAD
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
if _is_gamepad(capabilities):
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.GAMEPAD
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
if _is_mouse(capabilities):
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.MOUSE
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
if _is_camera(capabilities):
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.CAMERA
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
if _is_keyboard(capabilities):
|
|
|
|
# very low in the chain to avoid classifying most devices
|
|
|
|
# as keyboard, because there are many with ev_key capabilities
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.KEYBOARD
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
return DeviceType.UNKNOWN
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
DENYLIST = [".*Yubico.*YubiKey.*", "Eee PC WMI hotkeys"]
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def is_denylisted(device: InputDevice):
|
2022-01-01 12:00:49 +00:00
|
|
|
"""Check if a device should not be used in input-remapper.
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-12 16:45:32 +00:00
|
|
|
device
|
2021-04-23 09:51:21 +00:00
|
|
|
"""
|
|
|
|
for name in DENYLIST:
|
|
|
|
if re.match(name, str(device.name), re.IGNORECASE):
|
|
|
|
return True
|
|
|
|
|
2021-11-23 18:59:28 +00:00
|
|
|
return False
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def get_unique_key(device: InputDevice):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Find a string key that is unique for a single hardware device.
|
|
|
|
|
|
|
|
All InputDevices in /dev/input that originate from the same physical
|
|
|
|
hardware device should return the same key via this function.
|
|
|
|
"""
|
|
|
|
# Keys that should not be used:
|
|
|
|
# - device.phys is empty sometimes and varies across virtual
|
|
|
|
# subdevices
|
|
|
|
# - device.version varies across subdevices
|
|
|
|
# - device.uniq is empty most of the time, I don't know what this is
|
|
|
|
# supposed to be
|
|
|
|
return (
|
|
|
|
# device.info bustype, vendor and product are unique for
|
|
|
|
# a product, but multiple similar device models would be grouped
|
|
|
|
# in the same group
|
2021-09-26 10:44:56 +00:00
|
|
|
f"{device.info.bustype}_"
|
|
|
|
f"{device.info.vendor}_"
|
|
|
|
f"{device.info.product}_"
|
2021-04-23 09:51:21 +00:00
|
|
|
# deivce.phys if "/input..." is removed from it, because the first
|
|
|
|
# chunk seems to be unique per hardware (if it's not completely empty)
|
|
|
|
f'{device.phys.split("/")[0] or "-"}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class _Group:
|
|
|
|
"""Groups multiple devnodes together.
|
|
|
|
|
|
|
|
For example, name could be "Logitech USB Keyboard", devices
|
|
|
|
might contain "Logitech USB Keyboard System Control" and "Logitech USB
|
|
|
|
Keyboard". paths is a list of files in /dev/input that belong to the
|
|
|
|
devices.
|
|
|
|
|
|
|
|
They are grouped by usb port.
|
|
|
|
|
|
|
|
Members
|
|
|
|
-------
|
|
|
|
name : str
|
|
|
|
A human readable name, generated from .names, that should always
|
|
|
|
look the same for a device model. It is used to generate the
|
|
|
|
presets folder structure
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
paths: List[os.PathLike],
|
|
|
|
names: List[str],
|
|
|
|
types: List[DeviceType | str],
|
|
|
|
key: str,
|
|
|
|
):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Specify a group
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-12 16:45:32 +00:00
|
|
|
paths
|
2021-04-23 09:51:21 +00:00
|
|
|
Paths in /dev/input of the grouped devices
|
2022-11-12 16:45:32 +00:00
|
|
|
names
|
2021-04-23 09:51:21 +00:00
|
|
|
Names of the grouped devices
|
2022-11-12 16:45:32 +00:00
|
|
|
types
|
2021-04-23 09:51:21 +00:00
|
|
|
Types of the grouped devices
|
2022-11-12 16:45:32 +00:00
|
|
|
key
|
2021-04-23 09:51:21 +00:00
|
|
|
Unique identifier of the group.
|
|
|
|
|
|
|
|
It should be human readable and if possible equal to group.name.
|
|
|
|
To avoid multiple groups having the same key, a number starting
|
|
|
|
with 2 followed by a whitespace should be added to it:
|
|
|
|
"key", "key 2", "key 3", ...
|
|
|
|
|
|
|
|
This is important for the autoloading configuration. If the key
|
|
|
|
changed over reboots, then autoloading would break.
|
|
|
|
"""
|
|
|
|
# There might be multiple groups with the same name here when two
|
|
|
|
# similar devices are connected to the computer.
|
2022-01-31 19:58:37 +00:00
|
|
|
self.name: str = sorted(names, key=len)[0]
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
self.paths = paths
|
|
|
|
self.names = names
|
2022-07-23 08:53:41 +00:00
|
|
|
self.types = [DeviceType(type_) for type_ in types]
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def get_preset_path(self, preset: Optional[str] = None):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Get a path to the stored preset, or to store a preset to.
|
|
|
|
|
|
|
|
This path is unique per device-model, not per group. Groups
|
|
|
|
of the same model share the same preset paths.
|
|
|
|
"""
|
|
|
|
return get_preset_path(self.name, preset)
|
|
|
|
|
2022-12-15 13:43:03 +00:00
|
|
|
def get_devices(self) -> List[evdev.InputDevice]:
|
|
|
|
devices: List[evdev.InputDevice] = []
|
|
|
|
for path in self.paths:
|
|
|
|
try:
|
|
|
|
devices.append(evdev.InputDevice(path))
|
|
|
|
except (FileNotFoundError, OSError):
|
|
|
|
logger.error('Could not find "%s"', path)
|
|
|
|
continue
|
|
|
|
return devices
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def dumps(self):
|
|
|
|
"""Return a string representing this object."""
|
2021-09-26 10:44:56 +00:00
|
|
|
return json.dumps(
|
2022-04-18 11:52:59 +00:00
|
|
|
dict(paths=self.paths, names=self.names, types=self.types, key=self.key),
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
@classmethod
|
2022-11-12 16:45:32 +00:00
|
|
|
def loads(cls, serialized: str):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Load a serialized representation."""
|
|
|
|
group = cls(**json.loads(serialized))
|
|
|
|
return group
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
return f"Group({self.key})"
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _FindGroups(threading.Thread):
|
|
|
|
"""Thread to get the devices that can be worked with.
|
|
|
|
|
|
|
|
Since InputDevice destructors take quite some time, do this
|
|
|
|
asynchronously so that they can take as much time as they want without
|
|
|
|
slowing down the initialization.
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def __init__(self, pipe: multiprocessing.Pipe):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Construct the process.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-12 16:45:32 +00:00
|
|
|
pipe
|
2021-04-23 09:51:21 +00:00
|
|
|
used to communicate the result
|
|
|
|
"""
|
|
|
|
self.pipe = pipe
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Do what get_groups describes."""
|
|
|
|
# evdev needs asyncio to work
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug("Discovering device paths")
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
# group them together by usb device because there could be stuff like
|
|
|
|
# "Logitech USB Keyboard" and "Logitech USB Keyboard Consumer Control"
|
|
|
|
grouped = {}
|
|
|
|
for path in evdev.list_devices():
|
2021-11-22 13:38:22 +00:00
|
|
|
try:
|
|
|
|
device = evdev.InputDevice(path)
|
|
|
|
except Exception as error:
|
|
|
|
# Observed exceptions in journalctl:
|
|
|
|
# - "SystemError: <built-in function ioctl_EVIOCGVERSION> returned NULL
|
|
|
|
# without setting an error"
|
|
|
|
# - "FileNotFoundError: [Errno 2] No such file or directory:
|
|
|
|
# '/dev/input/event12'"
|
|
|
|
logger.error("Failed to access %s: %s", path, str(error))
|
|
|
|
continue
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
if device.name == "Power Button":
|
2021-04-23 09:51:21 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
device_type = classify(device)
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
if device_type == DeviceType.CAMERA:
|
2021-04-23 09:51:21 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# https://www.kernel.org/doc/html/latest/input/event-codes.html
|
|
|
|
capabilities = device.capabilities(absinfo=False)
|
|
|
|
|
|
|
|
key_capa = capabilities.get(EV_KEY)
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
if key_capa is None and device_type != DeviceType.GAMEPAD:
|
2021-04-23 09:51:21 +00:00
|
|
|
# skip devices that don't provide buttons that can be mapped
|
|
|
|
continue
|
|
|
|
|
|
|
|
if is_denylisted(device):
|
|
|
|
continue
|
|
|
|
|
|
|
|
key = get_unique_key(device)
|
|
|
|
if grouped.get(key) is None:
|
|
|
|
grouped[key] = []
|
|
|
|
|
2022-01-18 20:20:42 +00:00
|
|
|
logger.debug(
|
2022-04-18 11:52:59 +00:00
|
|
|
'Found "%s", "%s", "%s", type: %s',
|
|
|
|
key,
|
|
|
|
path,
|
|
|
|
device.name,
|
|
|
|
device_type,
|
2021-04-23 09:51:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
grouped[key].append((device.name, path, device_type))
|
|
|
|
|
|
|
|
# now write down all the paths of that group
|
|
|
|
result = []
|
|
|
|
used_keys = set()
|
|
|
|
for group in grouped.values():
|
|
|
|
names = [entry[0] for entry in group]
|
|
|
|
devs = [entry[1] for entry in group]
|
|
|
|
|
|
|
|
# generate a human readable key
|
|
|
|
shortest_name = sorted(names, key=len)[0]
|
|
|
|
key = shortest_name
|
|
|
|
i = 2
|
|
|
|
while key in used_keys:
|
2021-09-26 10:44:56 +00:00
|
|
|
key = f"{shortest_name} {i}"
|
2021-04-23 09:51:21 +00:00
|
|
|
i += 1
|
|
|
|
used_keys.add(key)
|
|
|
|
|
|
|
|
group = _Group(
|
|
|
|
key=key,
|
|
|
|
paths=devs,
|
|
|
|
names=names,
|
2022-07-23 08:53:41 +00:00
|
|
|
types=sorted(
|
|
|
|
list({item[2] for item in group if item[2] != DeviceType.UNKNOWN})
|
|
|
|
),
|
2021-04-23 09:51:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result.append(group.dumps())
|
|
|
|
|
|
|
|
self.pipe.send(json.dumps(result))
|
2022-07-23 08:53:41 +00:00
|
|
|
loop.close() # avoid resource allocation warnings
|
2021-04-23 09:51:21 +00:00
|
|
|
# now that everything is sent via the pipe, the InputDevice
|
2022-07-23 08:53:41 +00:00
|
|
|
# destructors can go on and take ages to complete in the thread
|
2021-04-23 09:51:21 +00:00
|
|
|
# without blocking anything
|
|
|
|
|
|
|
|
|
|
|
|
class _Groups:
|
|
|
|
"""Contains and manages all groups."""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def __init__(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
self._groups: List[_Group] = None
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def __getattribute__(self, key: str):
|
2021-11-21 20:45:02 +00:00
|
|
|
"""To lazy load group info only when needed.
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
For example, this helps to keep logs of input-remapper-control clear when it
|
|
|
|
doesn't need it the information.
|
2021-04-23 09:51:21 +00:00
|
|
|
"""
|
2021-11-21 20:45:02 +00:00
|
|
|
if key == "_groups" and object.__getattribute__(self, "_groups") is None:
|
2022-07-23 08:53:41 +00:00
|
|
|
object.__setattr__(self, "_groups", [])
|
2021-11-21 20:45:02 +00:00
|
|
|
object.__getattribute__(self, "refresh")()
|
|
|
|
|
|
|
|
return object.__getattribute__(self, key)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2021-11-21 20:45:02 +00:00
|
|
|
def refresh(self):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Look for devices and group them together.
|
|
|
|
|
|
|
|
Since this needs to do some stuff with /dev and spawn processes the
|
|
|
|
result is cached. Use refresh_groups if you need up to date
|
|
|
|
devices.
|
|
|
|
"""
|
|
|
|
pipe = multiprocessing.Pipe()
|
|
|
|
_FindGroups(pipe[1]).start()
|
|
|
|
# block until groups are available
|
|
|
|
self.loads(pipe[0].recv())
|
|
|
|
|
|
|
|
if len(self._groups) == 0:
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug("Did not find any input device")
|
2021-04-23 09:51:21 +00:00
|
|
|
else:
|
|
|
|
keys = [f'"{group.key}"' for group in self._groups]
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.info("Found %s", ", ".join(keys))
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def filter(self, include_inputremapper: bool = False) -> List[_Group]:
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Filter groups."""
|
|
|
|
result = []
|
|
|
|
for group in self._groups:
|
|
|
|
name = group.name
|
2022-01-01 12:00:49 +00:00
|
|
|
if not include_inputremapper and name.startswith("input-remapper"):
|
2021-04-23 09:51:21 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
result.append(group)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def set_groups(self, new_groups: List[_Group]):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Overwrite all groups."""
|
2022-07-23 08:53:41 +00:00
|
|
|
logger.debug("overwriting groups with %s", new_groups)
|
2021-04-23 09:51:21 +00:00
|
|
|
self._groups = new_groups
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
def list_group_names(self) -> List[str]:
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Return a list of all 'name' properties of the groups."""
|
|
|
|
return [
|
2021-09-26 10:44:56 +00:00
|
|
|
group.name
|
|
|
|
for group in self._groups
|
2022-01-01 12:00:49 +00:00
|
|
|
if not group.name.startswith("input-remapper")
|
2021-04-23 09:51:21 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._groups)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._groups)
|
|
|
|
|
|
|
|
def dumps(self):
|
|
|
|
"""Create a deserializable string representation."""
|
|
|
|
return json.dumps([group.dumps() for group in self._groups])
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def loads(self, dump: str):
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Load a serialized representation created via dumps."""
|
|
|
|
self._groups = [_Group.loads(group) for group in json.loads(dump)]
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def find(
|
|
|
|
self,
|
2022-11-12 16:45:32 +00:00
|
|
|
name: Optional[str] = None,
|
|
|
|
key: Optional[str] = None,
|
|
|
|
path: Optional[str] = None,
|
2022-04-17 10:19:23 +00:00
|
|
|
include_inputremapper: bool = False,
|
2022-07-23 08:53:41 +00:00
|
|
|
) -> Optional[_Group]:
|
2021-04-23 09:51:21 +00:00
|
|
|
"""Find a group that matches the provided parameters.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-12 16:45:32 +00:00
|
|
|
name
|
2021-04-23 09:51:21 +00:00
|
|
|
"USB Keyboard"
|
|
|
|
Not unique, will return the first group that matches.
|
2022-11-12 16:45:32 +00:00
|
|
|
key
|
2021-04-23 09:51:21 +00:00
|
|
|
"USB Keyboard", "USB Keyboard 2", ...
|
2022-11-12 16:45:32 +00:00
|
|
|
path
|
2021-04-23 09:51:21 +00:00
|
|
|
"/dev/input/event3"
|
|
|
|
"""
|
|
|
|
for group in self._groups:
|
2022-01-01 12:00:49 +00:00
|
|
|
if not include_inputremapper and group.name.startswith("input-remapper"):
|
2021-11-22 22:19:51 +00:00
|
|
|
continue
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
if name and group.name != name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if key and group.key != key:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if path and path not in group.paths:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
groups = _Groups()
|