2022-01-14 17:50:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
|
|
|
#
|
|
|
|
# This file is part of input-remapper.
|
|
|
|
#
|
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2022-11-12 16:45:32 +00:00
|
|
|
|
|
|
|
from typing import Dict, Union, Tuple
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
import evdev
|
2022-03-17 09:32:22 +00:00
|
|
|
|
2022-01-14 17:50:57 +00:00
|
|
|
import inputremapper.exceptions
|
2022-07-23 08:53:41 +00:00
|
|
|
import inputremapper.utils
|
2022-01-14 17:50:57 +00:00
|
|
|
from inputremapper.logger import logger
|
|
|
|
|
2022-11-01 11:07:12 +00:00
|
|
|
MIN_ABS = -(2**15) # -32768
|
|
|
|
MAX_ABS = 2**15 # 32768
|
2022-01-14 17:50:57 +00:00
|
|
|
DEV_NAME = "input-remapper"
|
|
|
|
DEFAULT_UINPUTS = {
|
|
|
|
# for event codes see linux/input-event-codes.h
|
|
|
|
"keyboard": {
|
|
|
|
evdev.ecodes.EV_KEY: list(evdev.ecodes.KEY.keys() & evdev.ecodes.keys.keys())
|
|
|
|
},
|
|
|
|
"gamepad": {
|
|
|
|
evdev.ecodes.EV_KEY: [*range(0x130, 0x13F)], # BTN_SOUTH - BTN_THUMBR
|
|
|
|
evdev.ecodes.EV_ABS: [
|
2022-07-25 08:55:26 +00:00
|
|
|
*(
|
|
|
|
(i, evdev.AbsInfo(0, MIN_ABS, MAX_ABS, 0, 0, 0))
|
|
|
|
for i in range(0x00, 0x06)
|
|
|
|
),
|
|
|
|
*((i, evdev.AbsInfo(0, -1, 1, 0, 0, 0)) for i in range(0x10, 0x12)),
|
2022-01-14 17:50:57 +00:00
|
|
|
], # 6-axis and 1 hat switch
|
|
|
|
},
|
|
|
|
"mouse": {
|
|
|
|
evdev.ecodes.EV_KEY: [*range(0x110, 0x118)], # BTN_LEFT - BTN_TASK
|
2022-04-17 10:19:23 +00:00
|
|
|
evdev.ecodes.EV_REL: [*range(0x00, 0x0D)], # all REL axis
|
2022-01-14 17:50:57 +00:00
|
|
|
},
|
|
|
|
}
|
2022-06-25 10:23:56 +00:00
|
|
|
DEFAULT_UINPUTS["keyboard + mouse"] = {
|
|
|
|
evdev.ecodes.EV_KEY: [
|
|
|
|
*DEFAULT_UINPUTS["keyboard"][evdev.ecodes.EV_KEY],
|
|
|
|
*DEFAULT_UINPUTS["mouse"][evdev.ecodes.EV_KEY],
|
|
|
|
],
|
|
|
|
evdev.ecodes.EV_REL: [
|
|
|
|
*DEFAULT_UINPUTS["mouse"][evdev.ecodes.EV_REL],
|
|
|
|
],
|
|
|
|
}
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UInput(evdev.UInput):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-10-16 12:56:21 +00:00
|
|
|
name = kwargs["name"]
|
2022-11-12 16:45:32 +00:00
|
|
|
logger.debug('creating UInput device: "%s"', name)
|
2022-01-14 17:50:57 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2023-01-11 20:03:49 +00:00
|
|
|
# this will never change, so we cache it since evdev runs an expensive loop to
|
|
|
|
# gather the capabilities. (can_emit is called regularly)
|
|
|
|
self._capabilities_cache = self.capabilities(absinfo=False)
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def can_emit(self, event: Tuple[int, int, int]):
|
|
|
|
"""Check if an event can be emitted by the UIinput.
|
2022-01-14 17:50:57 +00:00
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
Wrong events might be injected if the group mappings are wrong,
|
2022-01-14 17:50:57 +00:00
|
|
|
"""
|
2023-01-11 20:03:49 +00:00
|
|
|
return event[1] in self._capabilities_cache.get(event[0], [])
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FrontendUInput:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Uinput which can not actually send events, for use in the frontend."""
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, events=None, name="py-evdev-uinput", **kwargs):
|
2022-11-12 16:45:32 +00:00
|
|
|
# see https://python-evdev.readthedocs.io/en/latest/apidoc.html#module-evdev.uinput # noqa pylint: disable=line-too-long
|
2022-01-14 17:50:57 +00:00
|
|
|
self.events = events
|
|
|
|
self.name = name
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
logger.debug('creating fake UInput device: "%s"', self.name)
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
def capabilities(self):
|
|
|
|
return self.events
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalUInputs:
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Manages all UInputs that are shared between all injection processes."""
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2022-07-23 08:53:41 +00:00
|
|
|
self.devices: Dict[str, Union[UInput, FrontendUInput]] = {}
|
2022-01-14 17:50:57 +00:00
|
|
|
self._uinput_factory = None
|
2022-03-17 09:32:22 +00:00
|
|
|
self.is_service = inputremapper.utils.is_service()
|
2022-01-14 17:50:57 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(uinput for _, uinput in self.devices.items())
|
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
def ensure_uinput_factory_set(self):
|
|
|
|
if self._uinput_factory is not None:
|
|
|
|
return
|
2022-01-14 17:50:57 +00:00
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
if self.is_service:
|
2022-01-14 17:50:57 +00:00
|
|
|
self._uinput_factory = UInput
|
|
|
|
else:
|
|
|
|
self._uinput_factory = FrontendUInput
|
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
def prepare_all(self):
|
2022-11-12 16:45:32 +00:00
|
|
|
"""Generate UInputs."""
|
2022-03-17 09:32:22 +00:00
|
|
|
self.ensure_uinput_factory_set()
|
|
|
|
|
2022-01-14 17:50:57 +00:00
|
|
|
for name, events in DEFAULT_UINPUTS.items():
|
|
|
|
if name in self.devices.keys():
|
|
|
|
continue
|
2022-03-17 09:32:22 +00:00
|
|
|
|
2022-01-14 17:50:57 +00:00
|
|
|
self.devices[name] = self._uinput_factory(
|
|
|
|
name=f"{DEV_NAME} {name}",
|
|
|
|
phys=DEV_NAME,
|
|
|
|
events=events,
|
|
|
|
)
|
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
def prepare_single(self, name: str):
|
|
|
|
"""Generate a single uinput.
|
|
|
|
|
|
|
|
This has to be done in the main process before injections that use it start.
|
|
|
|
"""
|
|
|
|
self.ensure_uinput_factory_set()
|
|
|
|
|
|
|
|
if name not in DEFAULT_UINPUTS:
|
|
|
|
raise KeyError("Could not find a matching uinput to generate.")
|
|
|
|
|
|
|
|
if name in self.devices:
|
|
|
|
logger.debug('Target "%s" already exists', name)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.devices[name] = self._uinput_factory(
|
|
|
|
name=f"{DEV_NAME} {name}",
|
|
|
|
phys=DEV_NAME,
|
|
|
|
events=DEFAULT_UINPUTS[name],
|
|
|
|
)
|
|
|
|
|
2022-11-12 16:45:32 +00:00
|
|
|
def write(self, event: Tuple[int, int, int], target_uinput):
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Write event to target uinput."""
|
2022-01-14 17:50:57 +00:00
|
|
|
uinput = self.get_uinput(target_uinput)
|
|
|
|
if not uinput:
|
|
|
|
raise inputremapper.exceptions.UinputNotAvailable(target_uinput)
|
|
|
|
|
|
|
|
if not uinput.can_emit(event):
|
|
|
|
raise inputremapper.exceptions.EventNotHandled(event)
|
|
|
|
|
|
|
|
uinput.write(*event)
|
|
|
|
uinput.syn()
|
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
def get_uinput(self, name: str):
|
2022-01-14 17:50:57 +00:00
|
|
|
"""UInput with name
|
|
|
|
|
|
|
|
Or None if there is no uinput with this name.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-11-12 16:45:32 +00:00
|
|
|
name
|
|
|
|
uniqe name of the uinput device
|
2022-01-14 17:50:57 +00:00
|
|
|
"""
|
|
|
|
if name in self.devices.keys():
|
|
|
|
return self.devices[name]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
global_uinputs = GlobalUInputs()
|