1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2021 Maxim Devaev <[email protected]> #
# #
# This program 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. #
# #
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import dataclasses
import struct
from ....keyboard.mappings import KEYMAP
from ....mouse import MouseRange
from .... import tools
# =====
class BaseEvent:
def make_request(self) -> bytes:
raise NotImplementedError
# =====
_KEYBOARD_NAMES_TO_CODES = {
"disabled": 0b00000000,
"usb": 0b00000001,
"ps2": 0b00000011,
}
_KEYBOARD_CODES_TO_NAMES = tools.swapped_kvs(_KEYBOARD_NAMES_TO_CODES)
def get_active_keyboard(outputs: int) -> str:
return _KEYBOARD_CODES_TO_NAMES.get(outputs & 0b00000111, "disabled")
@dataclasses.dataclass(frozen=True)
class SetKeyboardOutputEvent(BaseEvent):
keyboard: str
def __post_init__(self) -> None:
assert not self.keyboard or self.keyboard in _KEYBOARD_NAMES_TO_CODES
def make_request(self) -> bytes:
code = _KEYBOARD_NAMES_TO_CODES.get(self.keyboard, 0)
return _make_request(struct.pack(">BBxxx", 0x03, code))
# =====
_MOUSE_NAMES_TO_CODES = {
"disabled": 0b00000000,
"usb": 0b00001000,
"usb_rel": 0b00010000,
"ps2": 0b00011000,
"usb_win98": 0b00100000,
}
_MOUSE_CODES_TO_NAMES = tools.swapped_kvs(_MOUSE_NAMES_TO_CODES)
def get_active_mouse(outputs: int) -> str:
return _MOUSE_CODES_TO_NAMES.get(outputs & 0b00111000, "disabled")
@dataclasses.dataclass(frozen=True)
class SetMouseOutputEvent(BaseEvent):
mouse: str
def __post_init__(self) -> None:
assert not self.mouse or self.mouse in _MOUSE_NAMES_TO_CODES
def make_request(self) -> bytes:
return _make_request(struct.pack(">BBxxx", 0x04, _MOUSE_NAMES_TO_CODES.get(self.mouse, 0)))
# =====
@dataclasses.dataclass(frozen=True)
class SetConnectedEvent(BaseEvent):
connected: bool
def make_request(self) -> bytes:
return _make_request(struct.pack(">BBxxx", 0x05, int(self.connected)))
# =====
class ClearEvent(BaseEvent):
def make_request(self) -> bytes:
return _make_request(b"\x10\x00\x00\x00\x00")
@dataclasses.dataclass(frozen=True)
class KeyEvent(BaseEvent):
name: str
state: bool
def __post_init__(self) -> None:
assert self.name in KEYMAP
def make_request(self) -> bytes:
code = KEYMAP[self.name].mcu.code
return _make_request(struct.pack(">BBBxx", 0x11, code, int(self.state)))
@dataclasses.dataclass(frozen=True)
class MouseButtonEvent(BaseEvent):
name: str
state: bool
def __post_init__(self) -> None:
assert self.name in ["left", "right", "middle", "up", "down"]
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]
if self.state:
code |= state_pressed
if is_main:
main_code = code
extra_code = 0
else:
main_code = 0
extra_code = code
return _make_request(struct.pack(">BBBxx", 0x13, main_code, extra_code))
@dataclasses.dataclass(frozen=True)
class MouseMoveEvent(BaseEvent):
to_x: int
to_y: int
def __post_init__(self) -> None:
assert MouseRange.MIN <= self.to_x <= MouseRange.MAX
assert MouseRange.MIN <= self.to_y <= MouseRange.MAX
def make_request(self) -> bytes:
return _make_request(struct.pack(">Bhh", 0x12, self.to_x, self.to_y))
@dataclasses.dataclass(frozen=True)
class MouseRelativeEvent(BaseEvent):
delta_x: int
delta_y: int
def __post_init__(self) -> None:
assert -127 <= self.delta_x <= 127
assert -127 <= self.delta_y <= 127
def make_request(self) -> bytes:
return _make_request(struct.pack(">Bbbxx", 0x15, self.delta_x, self.delta_y))
@dataclasses.dataclass(frozen=True)
class MouseWheelEvent(BaseEvent):
delta_x: int
delta_y: int
def __post_init__(self) -> None:
assert -127 <= self.delta_x <= 127
assert -127 <= self.delta_y <= 127
def make_request(self) -> bytes:
# Горизонтальная прокрутка пока не поддерживается
return _make_request(struct.pack(">Bxbxx", 0x14, self.delta_y))
# =====
def check_response(response: bytes) -> bool:
assert len(response) in (4, 8), response
return (_make_crc16(response[:-2]) == struct.unpack(">H", response[-2:])[0])
def _make_request(command: bytes) -> bytes:
assert len(command) == 5, command
request = b"\x33" + command
request += struct.pack(">H", _make_crc16(request))
assert len(request) == 8, request
return request
def _make_crc16(data: bytes) -> int:
crc = 0xFFFF
for byte in data:
crc = crc ^ byte
for _ in range(8):
if crc & 0x0001 == 0:
crc = crc >> 1
else:
crc = crc >> 1
crc = crc ^ 0xA001
return crc
# =====
REQUEST_PING = _make_request(b"\x01\x00\x00\x00\x00")
REQUEST_REPEAT = _make_request(b"\x02\x00\x00\x00\x00")
RESPONSE_LEGACY_OK = b"\x33\x20" + struct.pack(">H", _make_crc16(b"\x33\x20"))
|