2022-01-31 19:58:37 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- 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/>.
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
import enum
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
import evdev
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2022-04-17 10:19:23 +00:00
|
|
|
from typing import Tuple, Union, Sequence, Callable
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
from inputremapper.exceptions import InputEventCreationError
|
|
|
|
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
InputEventValidationType = Union[
|
|
|
|
str,
|
|
|
|
Tuple[int, int, int],
|
|
|
|
evdev.InputEvent,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class EventActions(enum.Enum):
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Additional information a InputEvent can send through the event pipeline"""
|
2022-04-17 10:19:23 +00:00
|
|
|
|
|
|
|
as_key = enum.auto()
|
|
|
|
recenter = enum.auto()
|
|
|
|
none = enum.auto()
|
|
|
|
|
|
|
|
|
|
|
|
# Todo: add slots=True as soon as python 3.10 is in common distros
|
|
|
|
@dataclass(frozen=True)
|
2022-01-31 19:58:37 +00:00
|
|
|
class InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""The evnet used by inputremapper
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
as a drop in replacement for evdev.InputEvent
|
|
|
|
"""
|
|
|
|
|
|
|
|
sec: int
|
|
|
|
usec: int
|
|
|
|
type: int
|
|
|
|
code: int
|
|
|
|
value: int
|
2022-04-17 10:19:23 +00:00
|
|
|
action: EventActions = EventActions.none
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.type, self.code, self.value))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, InputEvent) or isinstance(other, evdev.InputEvent):
|
|
|
|
return self.event_tuple == (other.type, other.code, other.value)
|
|
|
|
if isinstance(other, tuple):
|
|
|
|
return self.event_tuple == other
|
|
|
|
return False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __get_validators__(cls):
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Used by pydantic and EventCombination to create InputEvent objects."""
|
2022-04-17 10:19:23 +00:00
|
|
|
yield cls.validate
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate(cls, init_arg: InputEventValidationType) -> InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Try all the different methods, and raise an error if none succeed."""
|
2022-04-17 10:19:23 +00:00
|
|
|
if isinstance(init_arg, InputEvent):
|
|
|
|
return init_arg
|
|
|
|
|
|
|
|
event = None
|
|
|
|
validators: Sequence[Callable[..., InputEvent]] = (
|
|
|
|
cls.from_event,
|
|
|
|
cls.from_string,
|
|
|
|
cls.from_tuple,
|
|
|
|
)
|
|
|
|
for validator in validators:
|
|
|
|
try:
|
|
|
|
event = validator(init_arg)
|
|
|
|
break
|
|
|
|
except InputEventCreationError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if event:
|
|
|
|
return event
|
|
|
|
|
|
|
|
raise ValueError(f"failed to create InputEvent with {init_arg = }")
|
2022-01-31 19:58:37 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_event(cls, event: evdev.InputEvent) -> InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Create a InputEvent from another InputEvent or evdev.InputEvent."""
|
2022-01-31 19:58:37 +00:00
|
|
|
try:
|
|
|
|
return cls(event.sec, event.usec, event.type, event.code, event.value)
|
|
|
|
except AttributeError:
|
|
|
|
raise InputEventCreationError(
|
|
|
|
f"failed to create InputEvent from {event = }"
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_string(cls, string: str) -> InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Create a InputEvent from a string like 'type, code, value'."""
|
2022-01-31 19:58:37 +00:00
|
|
|
try:
|
|
|
|
t, c, v = string.split(",")
|
|
|
|
return cls(0, 0, int(t), int(c), int(v))
|
|
|
|
except (ValueError, AttributeError):
|
|
|
|
raise InputEventCreationError(
|
|
|
|
f"failed to create InputEvent from {string = !r}"
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_tuple(cls, event_tuple: Tuple[int, int, int]) -> InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Create a InputEvent from a (type, code, value) tuple."""
|
2022-01-31 19:58:37 +00:00
|
|
|
try:
|
|
|
|
if len(event_tuple) != 3:
|
|
|
|
raise InputEventCreationError(
|
|
|
|
f"failed to create InputEvent {event_tuple = }"
|
|
|
|
f" must have length 3"
|
|
|
|
)
|
|
|
|
return cls(
|
2022-04-18 11:52:59 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
int(event_tuple[0]),
|
|
|
|
int(event_tuple[1]),
|
|
|
|
int(event_tuple[2]),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
raise InputEventCreationError(
|
|
|
|
f"failed to create InputEvent from {event_tuple = }"
|
|
|
|
)
|
|
|
|
except TypeError:
|
|
|
|
raise InputEventCreationError(
|
|
|
|
f"failed to create InputEvent from {type(event_tuple) = }"
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def btn_left(cls):
|
|
|
|
return cls(0, 0, evdev.ecodes.EV_KEY, evdev.ecodes.BTN_LEFT, 1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type_and_code(self) -> Tuple[int, int]:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Event type, code."""
|
2022-01-31 19:58:37 +00:00
|
|
|
return self.type, self.code
|
|
|
|
|
|
|
|
@property
|
|
|
|
def event_tuple(self) -> Tuple[int, int, int]:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Event type, code, value."""
|
2022-01-31 19:58:37 +00:00
|
|
|
return self.type, self.code, self.value
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
@property
|
|
|
|
def is_key_event(self) -> bool:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Whether this is interpreted as a key event."""
|
2022-04-17 10:19:23 +00:00
|
|
|
return self.type == evdev.ecodes.EV_KEY or self.action == EventActions.as_key
|
|
|
|
|
2022-02-27 14:43:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
if self.type == evdev.ecodes.EV_KEY:
|
2022-02-27 15:11:07 +00:00
|
|
|
key_name = evdev.ecodes.bytype[self.type].get(self.code, "unknown")
|
2022-02-27 14:43:01 +00:00
|
|
|
action = "down" if self.value == 1 else "up"
|
2022-02-27 15:11:07 +00:00
|
|
|
return f"<InputEvent {key_name} ({self.code}) {action}>"
|
2022-02-27 14:43:01 +00:00
|
|
|
|
|
|
|
return f"<InputEvent {self.event_tuple}>"
|
|
|
|
|
|
|
|
def timestamp(self):
|
|
|
|
"""Return the unix timestamp of when the event was seen."""
|
|
|
|
return self.sec + self.usec / 1000000
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
def modify(
|
|
|
|
self,
|
|
|
|
sec: int = None,
|
|
|
|
usec: int = None,
|
|
|
|
type: int = None,
|
|
|
|
code: int = None,
|
|
|
|
value: int = None,
|
2022-04-17 10:19:23 +00:00
|
|
|
action: EventActions = EventActions.none,
|
2022-01-31 19:58:37 +00:00
|
|
|
) -> InputEvent:
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Return a new modified event."""
|
2022-01-31 19:58:37 +00:00
|
|
|
return InputEvent(
|
|
|
|
sec if sec is not None else self.sec,
|
|
|
|
usec if usec is not None else self.usec,
|
|
|
|
type if type is not None else self.type,
|
|
|
|
code if code is not None else self.code,
|
|
|
|
value if value is not None else self.value,
|
2022-04-17 10:19:23 +00:00
|
|
|
action if action is not EventActions.none else self.action,
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def json_str(self) -> str:
|
|
|
|
return ",".join([str(self.type), str(self.code), str(self.value)])
|