summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-07-11 05:29:30 +0000
committerDevaev Maxim <[email protected]>2018-07-11 05:29:30 +0000
commitc6b6e54875158d22bee9cdd82f1342e1fc83724e (patch)
tree34d6b3890f7ff98a6269b64c6a30de73fdd6a5a0 /kvmd
parent157828997a9e756e58210804c4f3025dce57f853 (diff)
using HID-Project library
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/kvmd/data/keymap.yaml84
-rw-r--r--kvmd/kvmd/hid.py61
-rwxr-xr-xkvmd/setup.py4
3 files changed, 106 insertions, 43 deletions
diff --git a/kvmd/kvmd/data/keymap.yaml b/kvmd/kvmd/data/keymap.yaml
new file mode 100644
index 00000000..1904c639
--- /dev/null
+++ b/kvmd/kvmd/data/keymap.yaml
@@ -0,0 +1,84 @@
+AltLeft: 79
+AltRight: 83
+ArrowDown: 75
+ArrowLeft: 74
+ArrowRight: 73
+ArrowUp: 76
+Backquote: 49
+Backslash: 46
+Backspace: 39
+BracketLeft: 44
+BracketRight: 45
+CapsLock: 53
+Comma: 50
+ControlLeft: 77
+ControlRight: 81
+Delete: 70
+Digit0: 36
+Digit1: 27
+Digit2: 28
+Digit3: 29
+Digit4: 30
+Digit5: 31
+Digit6: 32
+Digit7: 33
+Digit8: 34
+Digit9: 35
+End: 71
+Enter: 37
+Equal: 43
+Escape: 38
+F1: 54
+F10: 63
+F11: 64
+F12: 65
+F2: 55
+F3: 56
+F4: 57
+F5: 58
+F6: 59
+F7: 60
+F8: 61
+F9: 62
+Home: 68
+Insert: 67
+KeyA: 1
+KeyB: 2
+KeyC: 3
+KeyD: 4
+KeyE: 5
+KeyF: 6
+KeyG: 7
+KeyH: 8
+KeyI: 9
+KeyJ: 10
+KeyK: 11
+KeyL: 12
+KeyM: 13
+KeyN: 14
+KeyO: 15
+KeyP: 16
+KeyQ: 17
+KeyR: 18
+KeyS: 19
+KeyT: 20
+KeyU: 21
+KeyV: 22
+KeyW: 23
+KeyX: 24
+KeyY: 25
+KeyZ: 26
+MetaLeft: 80
+MetaRight: 84
+Minus: 42
+PageDown: 72
+PageUp: 69
+Period: 51
+PrintScreen: 66
+Quote: 48
+Semicolon: 47
+ShiftLeft: 78
+ShiftRight: 82
+Slash: 52
+Space: 41
+Tab: 40
diff --git a/kvmd/kvmd/hid.py b/kvmd/kvmd/hid.py
index 9ad5ba62..d600d438 100644
--- a/kvmd/kvmd/hid.py
+++ b/kvmd/kvmd/hid.py
@@ -1,13 +1,14 @@
-import re
import asyncio
import multiprocessing
import multiprocessing.queues
import queue
+import pkgutil
+from typing import Dict
from typing import Set
from typing import NamedTuple
-from typing import Union
+import yaml
import serial
from .logging import get_logger
@@ -16,50 +17,23 @@ from . import gpio
# =====
+def _get_keymap() -> Dict[str, int]:
+ return yaml.load(pkgutil.get_data(__name__, "data/keymap.yaml").decode()) # type: ignore
+
+
+_KEYMAP = _get_keymap()
+
+
+def _keymap(key: str) -> bytes:
+ code = _KEYMAP.get(key)
+ return (bytes([code]) if code else b"") # type: ignore
+
+
class _KeyEvent(NamedTuple):
key: str
state: bool
-def _key_to_bytes(key: str) -> bytes:
- # https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
- # Also locate Keyboard.h
-
- match = re.match(r"(Digit|Key)([0-9A-Z])", key)
- code: Union[str, int, None]
- if match:
- code = match.group(2)
- else:
- code = { # type: ignore
- "Escape": 0xB1, "Backspace": 0xB2,
- "Tab": 0xB3, "Enter": 0xB0,
- "Insert": 0xD1, "Delete": 0xD4,
- "Home": 0xD2, "End": 0xD5,
- "PageUp": 0xD3, "PageDown": 0xD6,
- "ArrowLeft": 0xD8, "ArrowRight": 0xD7,
- "ArrowUp": 0xDA, "ArrowDown": 0xD9,
-
- "CapsLock": 0xC1,
- "ShiftLeft": 0x81, "ShiftRight": 0x85,
- "ControlLeft": 0x80, "ControlRight": 0x84,
- "AltLeft": 0x82, "AltRight": 0x86,
- "MetaLeft": 0x83, "MetaRight": 0x87,
-
- "Backquote": "`", "Minus": "-", "Equal": "=", "Space": " ",
- "BracketLeft": "[", "BracketRight": "]", "Semicolon": ";", "Quote": "'",
- "Comma": ",", "Period": ".", "Slash": "/", "Backslash": "\\",
-
- "F1": 0xC2, "F2": 0xC3, "F3": 0xC4, "F4": 0xC5,
- "F5": 0xC6, "F6": 0xC7, "F7": 0xC8, "F8": 0xC9,
- "F9": 0xCA, "F10": 0xCB, "F11": 0xCC, "F12": 0xCD,
- }.get(key)
- if isinstance(code, str):
- return bytes(code, encoding="ascii") # type: ignore
- elif isinstance(code, int):
- return bytes([code])
- return b""
-
-
class Hid(multiprocessing.Process):
def __init__(
self,
@@ -138,14 +112,15 @@ class Hid(multiprocessing.Process):
raise
def __send_key_event(self, tty: serial.Serial, event: _KeyEvent) -> None:
- key_bytes = _key_to_bytes(event.key)
+ key_bytes = _keymap(event.key)
if key_bytes:
assert len(key_bytes) == 1, (event, key_bytes)
tty.write(
b"\01"
+ (b"\01" if event.state else b"\00")
+ key_bytes
+ + b"\00"
)
def __send_clear_hid(self, tty: serial.Serial) -> None:
- tty.write(b"\00")
+ tty.write(b"\00\00\00\00")
diff --git a/kvmd/setup.py b/kvmd/setup.py
index d5319f24..a2d842b9 100755
--- a/kvmd/setup.py
+++ b/kvmd/setup.py
@@ -26,6 +26,10 @@ def main() -> None:
"kvmd.extras.wscli",
],
+ package_data={
+ "kvmd": ["data/*.yaml"],
+ },
+
entry_points={
"console_scripts": [
"kvmd = kvmd:main",