Skip to content

Commit 37060de

Browse files
committed
dual mouse mode
1 parent a33efca commit 37060de

8 files changed

+73
-14
lines changed

configs/os/udev/v2-hdmi-rpi3.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", KERNELS=="3f801000.csi|3f801000.csi1", ATTR{name}=="unicam-image", GROUP="kvmd", SYMLINK+="kvmd-video", TAG+="systemd"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"

configs/os/udev/v2-hdmi-rpi4.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", KERNELS=="fe801000.csi|fe801000.csi1", ATTR{name}=="unicam-image", GROUP="kvmd", SYMLINK+="kvmd-video", TAG+="systemd"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"

configs/os/udev/v2-hdmi-zerow.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", KERNELS=="20801000.csi|20801000.csi1", ATTR{name}=="unicam-image", GROUP="kvmd", SYMLINK+="kvmd-video", TAG+="systemd"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"

configs/os/udev/v2-hdmiusb-generic.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTR{index}=="0", GROUP="kvmd", SYMLINK+="kvmd-video"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"

configs/os/udev/v2-hdmiusb-rpi4.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", PROGRAM="/usr/bin/kvmd-udev-hdmiusb-check rpi4 %b", ATTR{index}=="0", GROUP="kvmd", SYMLINK+="kvmd-video"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"

configs/os/udev/v3-hdmi-rpi4.rules

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", KERNELS=="fe801000.csi|fe801000.csi1", ATTR{name}=="unicam-image", GROUP="kvmd", SYMLINK+="kvmd-video", TAG+="systemd"
44
KERNEL=="hidg0", GROUP="kvmd", SYMLINK+="kvmd-hid-keyboard"
55
KERNEL=="hidg1", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse"
6+
KERNEL=="hidg2", GROUP="kvmd", SYMLINK+="kvmd-hid-mouse-alt"
67
KERNEL=="sd[a-z]", SUBSYSTEM=="block", KERNELS=="1-1.4:1.0", GROUP="kvmd", SYMLINK+="kvmd-msd-aum"

kvmd/apps/otg/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ def _cmd_start(config: Section) -> None:
212212
absolute=config.kvmd.hid.mouse.absolute,
213213
horizontal_wheel=config.kvmd.hid.mouse.horizontal_wheel,
214214
))
215+
if config.kvmd.hid.mouse_alt.device:
216+
logger.info("===== Required HID-Mouse ALT =====")
217+
_create_hid(gadget_path, config_path, 2, make_mouse_hid(
218+
absolute=(not config.kvmd.hid.mouse.absolute),
219+
horizontal_wheel=config.kvmd.hid.mouse_alt.horizontal_wheel,
220+
))
215221

216222
if config.kvmd.msd.type == "otg":
217223
logger.info("===== Required MSD =====")

kvmd/plugins/hid/otg/__init__.py

+61-14
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
from typing import Dict
2525
from typing import Iterable
2626
from typing import AsyncGenerator
27+
from typing import Optional
2728
from typing import Any
2829

30+
from .... import tools
2931
from .... import aiomulti
3032

3133
from ....yamlconf import Option
@@ -43,11 +45,12 @@
4345

4446

4547
# =====
46-
class Plugin(BaseHid):
48+
class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
4749
def __init__( # pylint: disable=super-init-not-called
4850
self,
4951
keyboard: Dict[str, Any],
5052
mouse: Dict[str, Any],
53+
mouse_alt: Dict[str, Any],
5154
noop: bool,
5255
udc: str, # XXX: Not from options, see /kvmd/apps/kvmd/__init__.py for details
5356
) -> None:
@@ -56,8 +59,25 @@ def __init__( # pylint: disable=super-init-not-called
5659

5760
self.__udc = UsbDeviceController(udc)
5861

59-
self.__keyboard_proc = KeyboardProcess(udc=self.__udc, noop=noop, notifier=self.__notifier, **keyboard)
60-
self.__mouse_proc = MouseProcess(udc=self.__udc, noop=noop, notifier=self.__notifier, **mouse)
62+
common = {
63+
"udc": self.__udc,
64+
"noop": noop,
65+
"notifier": self.__notifier,
66+
}
67+
68+
self.__keyboard_proc = KeyboardProcess(**common, **keyboard)
69+
self.__mouse_current = self.__mouse_proc = MouseProcess(**common, **mouse)
70+
71+
self.__mouse_alt_proc: Optional[MouseProcess] = None
72+
self.__output_to_mouse: Dict[str, MouseProcess] = {}
73+
self.__mouse_to_output: Dict[MouseProcess, str] = {}
74+
if mouse_alt["device_path"]:
75+
self.__mouse_alt_proc = MouseProcess(absolute=(not mouse["absolute"]), **common, **mouse_alt)
76+
self.__output_to_mouse = {
77+
"usb": (self.__mouse_proc if mouse["absolute"] else self.__mouse_alt_proc),
78+
"usb_rel": (self.__mouse_alt_proc if mouse["absolute"] else self.__mouse_proc),
79+
}
80+
self.__mouse_to_output = tools.swapped_kvs(self.__output_to_mouse)
6181

6282
@classmethod
6383
def get_plugin_options(cls) -> Dict:
@@ -76,18 +96,27 @@ def get_plugin_options(cls) -> Dict:
7696
"absolute": Option(True, type=valid_bool),
7797
"horizontal_wheel": Option(True, type=valid_bool),
7898
},
99+
"mouse_alt": {
100+
"device": Option("", type=valid_abs_path, if_empty="", unpack_as="device_path"),
101+
"select_timeout": Option(0.1, type=valid_float_f01),
102+
"queue_timeout": Option(0.1, type=valid_float_f01),
103+
"write_retries": Option(150, type=valid_int_f1),
104+
# No absolute option here, initialized by (not mouse.absolute)
105+
"horizontal_wheel": Option(True, type=valid_bool),
106+
},
79107
"noop": Option(False, type=valid_bool),
80108
}
81109

82110
def sysprep(self) -> None:
83111
self.__udc.find()
84112
self.__keyboard_proc.start()
85113
self.__mouse_proc.start()
114+
if self.__mouse_alt_proc:
115+
self.__mouse_alt_proc.start()
86116

87117
async def get_state(self) -> Dict:
88118
keyboard_state = await self.__keyboard_proc.get_state()
89-
mouse_state = await self.__mouse_proc.get_state()
90-
outputs: Dict = {"available": [], "active": ""}
119+
mouse_state = await self.__mouse_current.get_state()
91120
return {
92121
"online": True,
93122
"busy": False,
@@ -99,9 +128,15 @@ async def get_state(self) -> Dict:
99128
"scroll": keyboard_state["scroll"],
100129
"num": keyboard_state["num"],
101130
},
102-
"outputs": outputs,
131+
"outputs": {"available": [], "active": ""},
132+
},
133+
"mouse": {
134+
"outputs": {
135+
"available": list(self.__output_to_mouse),
136+
"active": (self.__mouse_to_output[self.__mouse_current] if self.__mouse_alt_proc else ""),
137+
},
138+
**mouse_state,
103139
},
104-
"mouse": {**mouse_state, "outputs": outputs},
105140
}
106141

107142
async def poll_state(self) -> AsyncGenerator[Dict, None]:
@@ -115,31 +150,43 @@ async def poll_state(self) -> AsyncGenerator[Dict, None]:
115150

116151
async def reset(self) -> None:
117152
self.__keyboard_proc.send_reset_event()
118-
self.__mouse_proc.send_reset_event()
153+
self.__mouse_current.send_reset_event()
119154

120155
async def cleanup(self) -> None:
121156
try:
122157
self.__keyboard_proc.cleanup()
123158
finally:
124-
self.__mouse_proc.cleanup()
159+
try:
160+
self.__mouse_proc.cleanup()
161+
finally:
162+
if self.__mouse_alt_proc:
163+
self.__mouse_alt_proc.cleanup()
125164

126165
# =====
127166

128167
def send_key_events(self, keys: Iterable[Tuple[str, bool]]) -> None:
129168
self.__keyboard_proc.send_key_events(keys)
130169

131170
def send_mouse_button_event(self, button: str, state: bool) -> None:
132-
self.__mouse_proc.send_button_event(button, state)
171+
self.__mouse_current.send_button_event(button, state)
133172

134173
def send_mouse_move_event(self, to_x: int, to_y: int) -> None:
135-
self.__mouse_proc.send_move_event(to_x, to_y)
174+
self.__mouse_current.send_move_event(to_x, to_y)
136175

137176
def send_mouse_relative_event(self, delta_x: int, delta_y: int) -> None:
138-
self.__mouse_proc.send_relative_event(delta_x, delta_y)
177+
self.__mouse_current.send_relative_event(delta_x, delta_y)
139178

140179
def send_mouse_wheel_event(self, delta_x: int, delta_y: int) -> None:
141-
self.__mouse_proc.send_wheel_event(delta_x, delta_y)
180+
self.__mouse_current.send_wheel_event(delta_x, delta_y)
181+
182+
def set_params(self, keyboard_output: Optional[str]=None, mouse_output: Optional[str]=None) -> None:
183+
_ = keyboard_output
184+
if mouse_output != self.__mouse_to_output[self.__mouse_current]:
185+
if mouse_output in self.__output_to_mouse:
186+
self.__mouse_current.send_clear_event()
187+
self.__mouse_current = self.__output_to_mouse[mouse_output]
188+
self.__notifier.notify()
142189

143190
def clear_events(self) -> None:
144191
self.__keyboard_proc.send_clear_event()
145-
self.__mouse_proc.send_clear_event()
192+
self.__mouse_current.send_clear_event()

0 commit comments

Comments
 (0)