mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
using evdev instead of string constants
This commit is contained in:
@@ -29,6 +29,8 @@ from typing import Callable
|
||||
from typing import AsyncGenerator
|
||||
from typing import Any
|
||||
|
||||
from evdev import ecodes
|
||||
|
||||
from ...yamlconf import Option
|
||||
|
||||
from ...validators.basic import valid_bool
|
||||
@@ -37,7 +39,8 @@ from ...validators.basic import valid_string_list
|
||||
from ...validators.hid import valid_hid_key
|
||||
from ...validators.hid import valid_hid_mouse_move
|
||||
|
||||
from ...keyboard.mappings import WebModifiers
|
||||
from ...keyboard.mappings import WEB_TO_EVDEV
|
||||
from ...keyboard.mappings import EvdevModifiers
|
||||
from ...mouse import MouseRange
|
||||
|
||||
from .. import BasePlugin
|
||||
@@ -60,7 +63,7 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
|
||||
jiggler_interval: int,
|
||||
) -> None:
|
||||
|
||||
self.__ignore_keys = ignore_keys
|
||||
self.__ignore_keys = [WEB_TO_EVDEV[key] for key in ignore_keys]
|
||||
|
||||
self.__mouse_x_range = (mouse_x_min, mouse_x_max)
|
||||
self.__mouse_y_range = (mouse_y_min, mouse_y_max)
|
||||
@@ -142,7 +145,7 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
|
||||
|
||||
async def send_key_events(
|
||||
self,
|
||||
keys: Iterable[tuple[str, bool]],
|
||||
keys: Iterable[tuple[int, bool]],
|
||||
no_ignore_keys: bool=False,
|
||||
slow: bool=False,
|
||||
) -> None:
|
||||
@@ -153,24 +156,24 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
|
||||
await asyncio.sleep(0.02)
|
||||
self.send_key_event(key, state, False)
|
||||
|
||||
def send_key_event(self, key: str, state: bool, finish: bool) -> None:
|
||||
def send_key_event(self, key: int, state: bool, finish: bool) -> None:
|
||||
self._send_key_event(key, state)
|
||||
if state and finish and (key not in WebModifiers.ALL and key != "PrintScreen"):
|
||||
if state and finish and (key not in EvdevModifiers.ALL and key != ecodes.KEY_SYSRQ):
|
||||
# Считаем что PrintScreen это модификатор для Alt+SysRq+...
|
||||
# По-хорошему надо учитывать факт нажатия на Alt, но можно и забить.
|
||||
self._send_key_event(key, False)
|
||||
self.__bump_activity()
|
||||
|
||||
def _send_key_event(self, key: str, state: bool) -> None:
|
||||
def _send_key_event(self, key: int, state: bool) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
# =====
|
||||
|
||||
def send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
self._send_mouse_button_event(button, state)
|
||||
self.__bump_activity()
|
||||
|
||||
def _send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def _send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
# =====
|
||||
|
||||
@@ -285,10 +285,10 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
|
||||
def set_connected(self, connected: bool) -> None:
|
||||
self.__queue_event(SetConnectedEvent(connected), clear=True)
|
||||
|
||||
def _send_key_event(self, key: str, state: bool) -> None:
|
||||
def _send_key_event(self, key: int, state: bool) -> None:
|
||||
self.__queue_event(KeyEvent(key, state))
|
||||
|
||||
def _send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def _send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
self.__queue_event(MouseButtonEvent(button, state))
|
||||
|
||||
def _send_mouse_move_event(self, to_x: int, to_y: int) -> None:
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
import dataclasses
|
||||
import struct
|
||||
|
||||
from evdev import ecodes
|
||||
|
||||
from ....keyboard.mappings import KEYMAP
|
||||
|
||||
from ....mouse import MouseRange
|
||||
@@ -106,33 +108,36 @@ class ClearEvent(BaseEvent):
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class KeyEvent(BaseEvent):
|
||||
name: str
|
||||
code: int
|
||||
state: bool
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
assert self.name in KEYMAP
|
||||
assert self.code in KEYMAP
|
||||
|
||||
def make_request(self) -> bytes:
|
||||
code = KEYMAP[self.name].mcu.code
|
||||
code = KEYMAP[self.code].mcu.code
|
||||
return _make_request(struct.pack(">BBBxx", 0x11, code, int(self.state)))
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class MouseButtonEvent(BaseEvent):
|
||||
name: str
|
||||
code: int
|
||||
state: bool
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
assert self.name in ["left", "right", "middle", "up", "down"]
|
||||
assert self.code in [
|
||||
ecodes.BTN_LEFT, ecodes.BTN_RIGHT, ecodes.BTN_MIDDLE,
|
||||
ecodes.BTN_BACK, ecodes.BTN_FORWARD,
|
||||
]
|
||||
|
||||
def make_request(self) -> bytes:
|
||||
(code, state_pressed, is_main) = {
|
||||
"left": (0b10000000, 0b00001000, True),
|
||||
"right": (0b01000000, 0b00000100, True),
|
||||
"middle": (0b00100000, 0b00000010, True),
|
||||
"up": (0b10000000, 0b00001000, False), # Back
|
||||
"down": (0b01000000, 0b00000100, False), # Forward
|
||||
}[self.name]
|
||||
ecodes.BTN_LEFT: (0b10000000, 0b00001000, True),
|
||||
ecodes.BTN_RIGHT: (0b01000000, 0b00000100, True),
|
||||
ecodes.BTN_MIDDLE: (0b00100000, 0b00000010, True),
|
||||
ecodes.BTN_BACK: (0b10000000, 0b00001000, False), # Up
|
||||
ecodes.BTN_FORWARD: (0b01000000, 0b00000100, False), # Down
|
||||
}[self.code]
|
||||
if self.state:
|
||||
code |= state_pressed
|
||||
if is_main:
|
||||
|
||||
@@ -203,10 +203,10 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
|
||||
self._set_jiggler_active(jiggler)
|
||||
self.__notifier.notify()
|
||||
|
||||
def _send_key_event(self, key: str, state: bool) -> None:
|
||||
def _send_key_event(self, key: int, state: bool) -> None:
|
||||
self.__server.queue_event(make_keyboard_event(key, state))
|
||||
|
||||
def _send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def _send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
self.__server.queue_event(MouseButtonEvent(button, state))
|
||||
|
||||
def _send_mouse_relative_event(self, delta_x: int, delta_y: int) -> None:
|
||||
|
||||
@@ -168,10 +168,10 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
|
||||
self._set_jiggler_active(jiggler)
|
||||
self.__notifier.notify()
|
||||
|
||||
def _send_key_event(self, key: str, state: bool) -> None:
|
||||
def _send_key_event(self, key: int, state: bool) -> None:
|
||||
self.__queue_cmd(self.__keyboard.process_key(key, state))
|
||||
|
||||
def _send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def _send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
self.__queue_cmd(self.__mouse.process_button(button, state))
|
||||
|
||||
def _send_mouse_move_event(self, to_x: int, to_y: int) -> None:
|
||||
|
||||
@@ -46,7 +46,7 @@ class Keyboard:
|
||||
async def get_leds(self) -> dict[str, bool]:
|
||||
return (await self.__leds.get())
|
||||
|
||||
def process_key(self, key: str, state: bool) -> bytes:
|
||||
def process_key(self, key: int, state: bool) -> bytes:
|
||||
code = KEYMAP[key].usb.code
|
||||
is_modifier = KEYMAP[key].usb.is_modifier
|
||||
if state:
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
import math
|
||||
|
||||
from evdev import ecodes
|
||||
|
||||
from ....mouse import MouseRange
|
||||
from ....mouse import MouseDelta
|
||||
|
||||
@@ -43,18 +45,18 @@ class Mouse: # pylint: disable=too-many-instance-attributes
|
||||
def is_absolute(self) -> bool:
|
||||
return self.__absolute
|
||||
|
||||
def process_button(self, button: str, state: bool) -> bytes:
|
||||
def process_button(self, button: int, state: bool) -> bytes:
|
||||
code = 0x00
|
||||
match button:
|
||||
case "left":
|
||||
case ecodes.BTN_LEFT:
|
||||
code = 0x01
|
||||
case "right":
|
||||
case ecodes.BTN_RIGHT:
|
||||
code = 0x02
|
||||
case "middle":
|
||||
case ecodes.BTN_MIDDLE:
|
||||
code = 0x04
|
||||
case "up":
|
||||
case ecodes.BTN_BACK:
|
||||
code = 0x08
|
||||
case "down":
|
||||
case ecodes.BTN_FORWARD:
|
||||
code = 0x10
|
||||
if code:
|
||||
if state:
|
||||
|
||||
@@ -206,10 +206,10 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
|
||||
self._set_jiggler_active(jiggler)
|
||||
self.__notifier.notify()
|
||||
|
||||
def _send_key_event(self, key: str, state: bool) -> None:
|
||||
def _send_key_event(self, key: int, state: bool) -> None:
|
||||
self.__keyboard_proc.send_key_event(key, state)
|
||||
|
||||
def _send_mouse_button_event(self, button: str, state: bool) -> None:
|
||||
def _send_mouse_button_event(self, button: int, state: bool) -> None:
|
||||
self.__mouse_current.send_button_event(button, state)
|
||||
|
||||
def _send_mouse_move_event(self, to_x: int, to_y: int) -> None:
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
import struct
|
||||
import dataclasses
|
||||
|
||||
from evdev import ecodes
|
||||
|
||||
from ....keyboard.mappings import UsbKey
|
||||
from ....keyboard.mappings import KEYMAP
|
||||
|
||||
@@ -46,7 +48,7 @@ class ResetEvent(BaseEvent):
|
||||
# =====
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class KeyEvent(BaseEvent):
|
||||
key: UsbKey
|
||||
key: UsbKey
|
||||
state: bool
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
@@ -56,13 +58,13 @@ class KeyEvent(BaseEvent):
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class ModifierEvent(BaseEvent):
|
||||
modifier: UsbKey
|
||||
state: bool
|
||||
state: bool
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
assert self.modifier.is_modifier
|
||||
|
||||
|
||||
def make_keyboard_event(key: str, state: bool) -> (KeyEvent | ModifierEvent):
|
||||
def make_keyboard_event(key: int, state: bool) -> (KeyEvent | ModifierEvent):
|
||||
usb_key = KEYMAP[key].usb
|
||||
if usb_key.is_modifier:
|
||||
return ModifierEvent(usb_key, state)
|
||||
@@ -102,17 +104,17 @@ def make_keyboard_report(
|
||||
# =====
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class MouseButtonEvent(BaseEvent):
|
||||
button: str
|
||||
state: bool
|
||||
code: int = 0
|
||||
button: int
|
||||
state: bool
|
||||
code: int = 0
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
object.__setattr__(self, "code", {
|
||||
"left": 0x1,
|
||||
"right": 0x2,
|
||||
"middle": 0x4,
|
||||
"up": 0x8, # Back
|
||||
"down": 0x10, # Forward
|
||||
ecodes.BTN_LEFT: 0x1,
|
||||
ecodes.BTN_RIGHT: 0x2,
|
||||
ecodes.BTN_MIDDLE: 0x4,
|
||||
ecodes.BTN_BACK: 0x8, # Back/Up
|
||||
ecodes.BTN_FORWARD: 0x10, # Forward/Down
|
||||
}[self.button])
|
||||
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ class KeyboardProcess(BaseDeviceProcess):
|
||||
self._clear_queue()
|
||||
self._queue_event(ResetEvent())
|
||||
|
||||
def send_key_event(self, key: str, state: bool) -> None:
|
||||
def send_key_event(self, key: int, state: bool) -> None:
|
||||
self._queue_event(make_keyboard_event(key, state))
|
||||
|
||||
# =====
|
||||
|
||||
@@ -85,7 +85,7 @@ class MouseProcess(BaseDeviceProcess):
|
||||
self._clear_queue()
|
||||
self._queue_event(ResetEvent())
|
||||
|
||||
def send_button_event(self, button: str, state: bool) -> None:
|
||||
def send_button_event(self, button: int, state: bool) -> None:
|
||||
self._queue_event(MouseButtonEvent(button, state))
|
||||
|
||||
def send_move_event(self, to_x: int, to_y: int) -> None:
|
||||
|
||||
Reference in New Issue
Block a user