configurable wheel for otg

This commit is contained in:
Devaev Maxim 2020-11-03 06:17:52 +03:00
parent 544f4b3fec
commit c31115051c
5 changed files with 160 additions and 150 deletions

View File

@ -42,9 +42,8 @@ from ... import env
from .. import init from .. import init
from .hid import Hid from .hid import Hid
from .hid.keyboard import KEYBOARD_HID from .hid.keyboard import make_keyboard_hid
from .hid.mouse import MOUSE_ABSOLUTE_HID from .hid.mouse import make_mouse_hid
from .hid.mouse import MOUSE_RELATIVE_HID
# ===== # =====
@ -203,11 +202,11 @@ def _cmd_start(config: Section) -> None:
if config.kvmd.hid.type == "otg": if config.kvmd.hid.type == "otg":
logger.info("===== Required HID =====") logger.info("===== Required HID =====")
_create_hid(gadget_path, config_path, 0, KEYBOARD_HID) _create_hid(gadget_path, config_path, 0, make_keyboard_hid())
if config.kvmd.hid.mouse.absolute: _create_hid(gadget_path, config_path, 1, make_mouse_hid(
_create_hid(gadget_path, config_path, 1, MOUSE_ABSOLUTE_HID) absolute=config.kvmd.hid.mouse.absolute,
else: horizontal_wheel=config.kvmd.hid.mouse.horizontal_wheel,
_create_hid(gadget_path, config_path, 1, MOUSE_RELATIVE_HID) ))
if config.kvmd.msd.type == "otg": if config.kvmd.msd.type == "otg":
logger.info("===== Required MSD =====") logger.info("===== Required MSD =====")

View File

@ -24,59 +24,60 @@ from . import Hid
# ===== # =====
KEYBOARD_HID = Hid( def make_keyboard_hid() -> Hid:
protocol=1, # Keyboard protocol return Hid(
subclass=1, # Boot interface subclass protocol=1, # Keyboard protocol
subclass=1, # Boot interface subclass
report_length=8, report_length=8,
report_descriptor=bytes([ report_descriptor=bytes([
# Logitech descriptor. It's very similar to https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt # Logitech descriptor. It's very similar to https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt
# Dumped using usbhid-dump; parsed using https://eleccelerator.com/usbdescreqparser # Dumped using usbhid-dump; parsed using https://eleccelerator.com/usbdescreqparser
# Keyboard # Keyboard
0x05, 0x01, # USAGE_PAGE (Generic Desktop) 0x05, 0x01, # USAGE_PAGE (Generic Desktop)
0x09, 0x06, # USAGE (Keyboard) 0x09, 0x06, # USAGE (Keyboard)
0xA1, 0x01, # COLLECTION (Application) 0xA1, 0x01, # COLLECTION (Application)
# Modifiers # Modifiers
0x05, 0x07, # USAGE_PAGE (Keyboard) 0x05, 0x07, # USAGE_PAGE (Keyboard)
0x19, 0xE0, # USAGE_MINIMUM (Keyboard LeftControl) 0x19, 0xE0, # USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xE7, # USAGE_MAXIMUM (Keyboard Right GUI) 0x29, 0xE7, # USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, # LOGICAL_MINIMUM (0) 0x15, 0x00, # LOGICAL_MINIMUM (0)
0x25, 0x01, # LOGICAL_MAXIMUM (1) 0x25, 0x01, # LOGICAL_MAXIMUM (1)
0x75, 0x01, # REPORT_SIZE (1) 0x75, 0x01, # REPORT_SIZE (1)
0x95, 0x08, # REPORT_COUNT (8) 0x95, 0x08, # REPORT_COUNT (8)
0x81, 0x02, # INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0x81, 0x02, # INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
# Reserved byte # Reserved byte
0x95, 0x01, # REPORT_COUNT (1) 0x95, 0x01, # REPORT_COUNT (1)
0x75, 0x08, # REPORT_SIZE (8) 0x75, 0x08, # REPORT_SIZE (8)
0x81, 0x01, # INPUT (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) 0x81, 0x01, # INPUT (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
# LEDs output # LEDs output
0x95, 0x05, # REPORT_COUNT (5) 0x95, 0x05, # REPORT_COUNT (5)
0x75, 0x01, # REPORT_SIZE (1) 0x75, 0x01, # REPORT_SIZE (1)
0x05, 0x08, # USAGE_PAGE (LEDs) 0x05, 0x08, # USAGE_PAGE (LEDs)
0x19, 0x01, # USAGE_MINIMUM (Num Lock) 0x19, 0x01, # USAGE_MINIMUM (Num Lock)
0x29, 0x05, # USAGE_MAXIMUM (Kana) 0x29, 0x05, # USAGE_MAXIMUM (Kana)
0x91, 0x02, # OUTPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) 0x91, 0x02, # OUTPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
# Reserved 3 bits in output # Reserved 3 bits in output
0x95, 0x01, # REPORT_COUNT (1) 0x95, 0x01, # REPORT_COUNT (1)
0x75, 0x03, # REPORT_SIZE (3) 0x75, 0x03, # REPORT_SIZE (3)
0x91, 0x01, # OUTPUT (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) 0x91, 0x01, # OUTPUT (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
# 6 keys # 6 keys
0x95, 0x06, # REPORT_COUNT (6) 0x95, 0x06, # REPORT_COUNT (6)
0x75, 0x08, # REPORT_SIZE (8) 0x75, 0x08, # REPORT_SIZE (8)
0x15, 0x00, # LOGICAL_MINIMUM (0) 0x15, 0x00, # LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x00, # LOGICAL_MAXIMUM (0xFF) 0x26, 0xFF, 0x00, # LOGICAL_MAXIMUM (0xFF)
0x05, 0x07, # USAGE_PAGE (Keyboard) 0x05, 0x07, # USAGE_PAGE (Keyboard)
0x19, 0x00, # USAGE_MINIMUM (Reserved) 0x19, 0x00, # USAGE_MINIMUM (Reserved)
0x2A, 0xFF, 0x00, # USAGE_MAXIMUM (0xFF) 0x2A, 0xFF, 0x00, # USAGE_MAXIMUM (0xFF)
0x81, 0x00, # INPUT (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) 0x81, 0x00, # INPUT (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, # END_COLLECTION 0xC0, # END_COLLECTION
]), ]),
) )

View File

@ -24,114 +24,119 @@ from . import Hid
# ===== # =====
MOUSE_ABSOLUTE_HID = Hid( def make_mouse_hid(absolute: bool, horizontal_wheel: bool) -> Hid:
protocol=0, # None protocol maker = (_make_absolute_hid if absolute else _make_relative_hid)
subclass=0, # No subclass return maker(horizontal_wheel)
report_length=7,
report_descriptor=bytes([ _HORIZONTAL_WHEEL = [
# https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/SingleAbsoluteMouse.cpp 0x05, 0x0C, # USAGE PAGE (Consumer Devices)
# Репорт взят отсюда ^^^, но изменен диапазон значений координат перемещений. 0x0A, 0x38, 0x02, # USAGE (AC Pan)
# Автор предлагает использовать -32768...32767, но семерка почему-то не хочет работать 0x15, 0x81, # LOGICAL_MINIMUM (-127)
# с отрицательными значениями координат, как не хочет хавать 65536 и 32768. 0x25, 0x7F, # LOGICAL_MAXIMUM (127)
# Так что мы ей скармливаем диапазон 0...32767, и передаем рукожопам из микрософта привет, 0x75, 0x08, # REPORT_SIZE (8)
# потому что линуксы прекрасно работают с любыми двухбайтовыми диапазонами. 0x95, 0x01, # REPORT_COUNT (1)
0x81, 0x06, # INPUT (Data,Var,Rel)
]
# Absolute mouse
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
0x09, 0x02, # USAGE (Mouse)
0xA1, 0x01, # COLLECTION (Application)
# 8 Buttons def _make_absolute_hid(horizontal_wheel: bool) -> Hid:
0x05, 0x09, # USAGE_PAGE (Button) return Hid(
0x19, 0x01, # USAGE_MINIMUM (Button 1) protocol=0, # None protocol
0x29, 0x08, # USAGE_MAXIMUM (Button 8) subclass=0, # No subclass
0x15, 0x00, # LOGICAL_MINIMUM (0)
0x25, 0x01, # LOGICAL_MAXIMUM (1)
0x95, 0x08, # REPORT_COUNT (8)
0x75, 0x01, # REPORT_SIZE (1)
0x81, 0x02, # INPUT (Data,Var,Abs)
# X, Y report_length=(7 if horizontal_wheel else 6),
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
0x09, 0x30, # USAGE (X)
0x09, 0x31, # USAGE (Y)
0x16, 0x00, 0x00, # LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x7F, # LOGICAL_MAXIMUM (32767)
0x75, 0x10, # REPORT_SIZE (16)
0x95, 0x02, # REPORT_COUNT (2)
0x81, 0x02, # INPUT (Data,Var,Abs)
# Wheel report_descriptor=bytes([
0x09, 0x38, # USAGE (Wheel) # https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/SingleAbsoluteMouse.cpp
0x15, 0x81, # LOGICAL_MINIMUM (-127) # Репорт взят отсюда ^^^, но изменен диапазон значений координат перемещений.
0x25, 0x7F, # LOGICAL_MAXIMUM (127) # Автор предлагает использовать -32768...32767, но семерка почему-то не хочет работать
0x75, 0x08, # REPORT_SIZE (8) # с отрицательными значениями координат, как не хочет хавать 65536 и 32768.
0x95, 0x01, # REPORT_COUNT (1) # Так что мы ей скармливаем диапазон 0...32767, и передаем рукожопам из микрософта привет,
0x81, 0x06, # INPUT (Data,Var,Rel) # потому что линуксы прекрасно работают с любыми двухбайтовыми диапазонами.
# Horizontal wheel # Absolute mouse
0x05, 0x0C, # USAGE PAGE (Consumer Devices) 0x05, 0x01, # USAGE_PAGE (Generic Desktop)
0x0A, 0x38, 0x02, # USAGE (AC Pan) 0x09, 0x02, # USAGE (Mouse)
0x15, 0x81, # LOGICAL_MINIMUM (-127) 0xA1, 0x01, # COLLECTION (Application)
0x25, 0x7F, # LOGICAL_MAXIMUM (127)
0x75, 0x08, # REPORT_SIZE (8)
0x95, 0x01, # REPORT_COUNT (1)
0x81, 0x06, # INPUT (Data,Var,Rel)
# End # 8 Buttons
0xC0, # END_COLLECTION 0x05, 0x09, # USAGE_PAGE (Button)
]), 0x19, 0x01, # USAGE_MINIMUM (Button 1)
) 0x29, 0x08, # USAGE_MAXIMUM (Button 8)
0x15, 0x00, # LOGICAL_MINIMUM (0)
0x25, 0x01, # LOGICAL_MAXIMUM (1)
0x95, 0x08, # REPORT_COUNT (8)
0x75, 0x01, # REPORT_SIZE (1)
0x81, 0x02, # INPUT (Data,Var,Abs)
MOUSE_RELATIVE_HID = Hid( # X, Y
protocol=2, # Mouse protocol 0x05, 0x01, # USAGE_PAGE (Generic Desktop)
subclass=1, # Boot interface subclass 0x09, 0x30, # USAGE (X)
0x09, 0x31, # USAGE (Y)
0x16, 0x00, 0x00, # LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x7F, # LOGICAL_MAXIMUM (32767)
0x75, 0x10, # REPORT_SIZE (16)
0x95, 0x02, # REPORT_COUNT (2)
0x81, 0x02, # INPUT (Data,Var,Abs)
report_length=5, # Wheel
0x09, 0x38, # USAGE (Wheel)
0x15, 0x81, # LOGICAL_MINIMUM (-127)
0x25, 0x7F, # LOGICAL_MAXIMUM (127)
0x75, 0x08, # REPORT_SIZE (8)
0x95, 0x01, # REPORT_COUNT (1)
0x81, 0x06, # INPUT (Data,Var,Rel)
report_descriptor=bytes([ *(_HORIZONTAL_WHEEL if horizontal_wheel else []),
# https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/BootMouse.cpp
# Relative mouse # End
0x05, 0x01, # USAGE_PAGE (Generic Desktop) 0xC0, # END_COLLECTION
0x09, 0x02, # USAGE (Mouse) ]),
0xA1, 0x01, # COLLECTION (Application) )
# 8 Buttons
0x05, 0x09, # USAGE_PAGE (Button)
0x19, 0x01, # USAGE_MINIMUM (Button 1)
0x29, 0x08, # USAGE_MAXIMUM (Button 8)
0x15, 0x00, # LOGICAL_MINIMUM (0)
0x25, 0x01, # LOGICAL_MAXIMUM (1)
0x95, 0x08, # REPORT_COUNT (8)
0x75, 0x01, # REPORT_SIZE (1)
0x81, 0x02, # INPUT (Data,Var,Abs)
# X, Y def _make_relative_hid(horizontal_wheel: bool) -> Hid:
0x05, 0x01, # USAGE_PAGE (Generic Desktop) return Hid(
0x09, 0x30, # USAGE (X) protocol=2, # Mouse protocol
0x09, 0x31, # USAGE (Y) subclass=1, # Boot interface subclass
# Wheel report_length=(5 if horizontal_wheel else 4),
0x09, 0x38, # USAGE (Wheel)
0x15, 0x81, # LOGICAL_MINIMUM (-127)
0x25, 0x7F, # LOGICAL_MAXIMUM (127)
0x75, 0x08, # REPORT_SIZE (8)
0x95, 0x03, # REPORT_COUNT (3)
0x81, 0x06, # INPUT (Data,Var,Rel)
# Horizontal wheel report_descriptor=bytes([
0x05, 0x0C, # USAGE PAGE (Consumer Devices) # https://github.com/NicoHood/HID/blob/0835e6a/src/SingleReport/BootMouse.cpp
0x0A, 0x38, 0x02, # USAGE (AC Pan)
0x15, 0x81, # LOGICAL_MINIMUM (-127)
0x25, 0x7F, # LOGICAL_MAXIMUM (127)
0x75, 0x08, # REPORT_SIZE (8)
0x95, 0x01, # REPORT_COUNT (1)
0x81, 0x06, # INPUT (Data,Var,Rel)
# End # Relative mouse
0xC0, # END_COLLECTION 0x05, 0x01, # USAGE_PAGE (Generic Desktop)
]), 0x09, 0x02, # USAGE (Mouse)
) 0xA1, 0x01, # COLLECTION (Application)
# 8 Buttons
0x05, 0x09, # USAGE_PAGE (Button)
0x19, 0x01, # USAGE_MINIMUM (Button 1)
0x29, 0x08, # USAGE_MAXIMUM (Button 8)
0x15, 0x00, # LOGICAL_MINIMUM (0)
0x25, 0x01, # LOGICAL_MAXIMUM (1)
0x95, 0x08, # REPORT_COUNT (8)
0x75, 0x01, # REPORT_SIZE (1)
0x81, 0x02, # INPUT (Data,Var,Abs)
# X, Y
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
0x09, 0x30, # USAGE (X)
0x09, 0x31, # USAGE (Y)
# Wheel
0x09, 0x38, # USAGE (Wheel)
0x15, 0x81, # LOGICAL_MINIMUM (-127)
0x25, 0x7F, # LOGICAL_MAXIMUM (127)
0x75, 0x08, # REPORT_SIZE (8)
0x95, 0x03, # REPORT_COUNT (3)
0x81, 0x06, # INPUT (Data,Var,Rel)
*(_HORIZONTAL_WHEEL if horizontal_wheel else []),
# End
0xC0, # END_COLLECTION
]),
)

View File

@ -76,6 +76,7 @@ class Plugin(BaseHid):
"write_retries_delay": Option(0.1, type=valid_float_f01), "write_retries_delay": Option(0.1, type=valid_float_f01),
"reopen_delay": Option(0.5, type=valid_float_f01), "reopen_delay": Option(0.5, type=valid_float_f01),
"absolute": Option(True, type=valid_bool), "absolute": Option(True, type=valid_bool),
"horizontal_wheel": Option(True, type=valid_bool),
}, },
"noop": Option(False, type=valid_bool), "noop": Option(False, type=valid_bool),
} }

View File

@ -69,6 +69,7 @@ class _WheelEvent(BaseEvent):
class MouseProcess(BaseDeviceProcess): class MouseProcess(BaseDeviceProcess):
def __init__(self, **kwargs: Any) -> None: def __init__(self, **kwargs: Any) -> None:
self.__absolute: bool = kwargs.pop("absolute") self.__absolute: bool = kwargs.pop("absolute")
self.__horizontal_wheel: bool = kwargs.pop("horizontal_wheel")
super().__init__( super().__init__(
name="mouse", name="mouse",
@ -212,4 +213,7 @@ class MouseProcess(BaseDeviceProcess):
def __make_report(self, buttons: int, move_x: int, move_y: int, wheel_x: int, wheel_y: int) -> bytes: def __make_report(self, buttons: int, move_x: int, move_y: int, wheel_x: int, wheel_y: int) -> bytes:
# XXX: Wheel Y before X: it's ok. # XXX: Wheel Y before X: it's ok.
# See /kvmd/apps/otg/hid/mouse.py for details # See /kvmd/apps/otg/hid/mouse.py for details
return struct.pack(("<BHHbb" if self.__absolute else "<Bbbbb"), buttons, move_x, move_y, wheel_y, wheel_x) if self.__horizontal_wheel:
return struct.pack(("<BHHbb" if self.__absolute else "<Bbbbb"), buttons, move_x, move_y, wheel_y, wheel_x)
else:
return struct.pack(("<BHHb" if self.__absolute else "<Bbbb"), buttons, move_x, move_y, wheel_y)