mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
refactoring
This commit is contained in:
parent
5bf758e232
commit
0c82937d3c
8
Makefile
8
Makefile
@ -49,8 +49,12 @@ shell:
|
|||||||
make _run_cmd CMD=/bin/bash
|
make _run_cmd CMD=/bin/bash
|
||||||
|
|
||||||
|
|
||||||
regen:
|
regen: _testenv
|
||||||
python3 genmap.py
|
for file in kvmd/data/keymap.yaml hid/src/keymap.h; do \
|
||||||
|
docker run --user `id -u`:`id -g` --rm \
|
||||||
|
--volume `pwd`:/src \
|
||||||
|
-it $(TESTENV_IMAGE) bash -c "cd src && ./genmap.py keymap.in $$file.mako $$file"; \
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
|
|||||||
105
genmap.py
105
genmap.py
@ -21,95 +21,54 @@
|
|||||||
# ========================================================================== #
|
# ========================================================================== #
|
||||||
|
|
||||||
|
|
||||||
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
import yaml
|
import mako.template
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
class KeyMapping(NamedTuple):
|
class _KeyMapping(NamedTuple):
|
||||||
kvmd_code: int
|
kvmd_code: int
|
||||||
arduino_hid_key: str
|
arduino_hid_key: str
|
||||||
js_key: str
|
web_key: str
|
||||||
|
|
||||||
|
|
||||||
|
def _read_keymap_in(path: str) -> List[_KeyMapping]:
|
||||||
|
keymap: List[_KeyMapping] = []
|
||||||
|
with open(path) as keymap_file:
|
||||||
|
for line in keymap_file:
|
||||||
|
line = line.strip()
|
||||||
|
if len(line) > 0 and not line.startswith("#"):
|
||||||
|
parts = list(map(str.strip, line.split()))
|
||||||
|
if len(parts) >= 3:
|
||||||
|
keymap.append(_KeyMapping(
|
||||||
|
kvmd_code=int(parts[0]),
|
||||||
|
arduino_hid_key=parts[1],
|
||||||
|
web_key=parts[2],
|
||||||
|
))
|
||||||
|
return keymap
|
||||||
|
|
||||||
|
|
||||||
|
def _render_keymap(keymap: List[_KeyMapping], template_path: str, out_path: str) -> None:
|
||||||
|
with open(template_path) as template_file:
|
||||||
|
with open(out_path, "w") as out_file:
|
||||||
|
template = textwrap.dedent(template_file.read())
|
||||||
|
rendered = mako.template.Template(template).render(keymap=keymap)
|
||||||
|
out_file.write(rendered)
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
keymap: List[KeyMapping] = []
|
assert len(sys.argv) == 4, "%s <keymap.in> <template> <out>" % (sys.argv[0])
|
||||||
with open("keymap.in") as keymap_file:
|
|
||||||
for row in keymap_file:
|
|
||||||
if not row.startswith("#"):
|
|
||||||
parts = row.split()
|
|
||||||
keymap.append(KeyMapping(
|
|
||||||
kvmd_code=int(parts[0]),
|
|
||||||
arduino_hid_key=parts[1],
|
|
||||||
js_key=parts[2],
|
|
||||||
))
|
|
||||||
|
|
||||||
path = "kvmd/data/keymap.yaml"
|
keymap = _read_keymap_in(sys.argv[1])
|
||||||
with open(path, "w") as keymap_yaml_file:
|
_render_keymap(keymap, sys.argv[2], sys.argv[3])
|
||||||
keymap_yaml_file.write(textwrap.dedent("""
|
|
||||||
# ========================================================================== #
|
|
||||||
# #
|
|
||||||
# KVMD - The main Pi-KVM daemon. #
|
|
||||||
# #
|
|
||||||
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
|
||||||
# #
|
|
||||||
# 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/>. #
|
|
||||||
# #
|
|
||||||
# ========================================================================== #
|
|
||||||
""").strip() + "\n\n\n")
|
|
||||||
yaml.dump({
|
|
||||||
km.js_key: km.kvmd_code
|
|
||||||
for km in keymap
|
|
||||||
}, keymap_yaml_file, indent=4, default_flow_style=False)
|
|
||||||
print("Generated:", path)
|
|
||||||
|
|
||||||
path = "hid/src/keymap.h"
|
|
||||||
with open(path, "w") as hid_header_file:
|
|
||||||
hid_header_file.write(textwrap.dedent("""
|
|
||||||
/*****************************************************************************
|
|
||||||
# #
|
|
||||||
# KVMD - The main Pi-KVM daemon. #
|
|
||||||
# #
|
|
||||||
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
|
||||||
# #
|
|
||||||
# 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/>. #
|
|
||||||
# #
|
|
||||||
*****************************************************************************/
|
|
||||||
""").strip() + "\n\n\n")
|
|
||||||
hid_header_file.write("#pragma once\n\n#include <HID-Project.h>\n\n#include \"inline.h\"\n\n\n")
|
|
||||||
hid_header_file.write("INLINE KeyboardKeycode keymap(uint8_t code) {\n\tswitch(code) {\n")
|
|
||||||
for km in sorted(keymap, key=(lambda km: km.arduino_hid_key)):
|
|
||||||
hid_header_file.write("\t\tcase {km.kvmd_code}: return {km.arduino_hid_key};\n".format(km=km))
|
|
||||||
hid_header_file.write("\t\tdefault: return KEY_ERROR_UNDEFINED;\n\t}\n}\n")
|
|
||||||
print("Generated:", path)
|
|
||||||
|
|
||||||
|
|
||||||
|
# =====
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
37
hid/src/keymap.h.mako
Normal file
37
hid/src/keymap.h.mako
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
# #
|
||||||
|
# KVMD - The main Pi-KVM daemon. #
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||||
|
# #
|
||||||
|
# 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/>. #
|
||||||
|
# #
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <HID-Project.h>
|
||||||
|
|
||||||
|
#include "inline.h"
|
||||||
|
|
||||||
|
<%! import operator %>
|
||||||
|
INLINE KeyboardKeycode keymap(uint8_t code) {
|
||||||
|
switch(code) {
|
||||||
|
% for km in sorted(keymap, key=operator.attrgetter("arduino_hid_key")):
|
||||||
|
case ${km.kvmd_code}: return ${km.arduino_hid_key};
|
||||||
|
% endfor
|
||||||
|
default: return KEY_ERROR_UNDEFINED;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,11 +18,11 @@
|
|||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||||
# #
|
# #
|
||||||
# ========================================================================== #
|
# ========================================================================== #
|
||||||
#
|
|
||||||
#
|
|
||||||
# https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ImprovedKeylayouts.h
|
# https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ImprovedKeylayouts.h
|
||||||
# https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
|
# https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
|
||||||
#
|
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
# KVMD | Arduino HID | JS
|
# KVMD | Arduino HID | JS
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
|
|||||||
25
kvmd/data/keymap.yaml.mako
Normal file
25
kvmd/data/keymap.yaml.mako
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# ========================================================================== #
|
||||||
|
# #
|
||||||
|
# KVMD - The main Pi-KVM daemon. #
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||||
|
# #
|
||||||
|
# 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 operator %>
|
||||||
|
% for km in sorted(keymap, key=operator.attrgetter("web_key")):
|
||||||
|
${km.web_key}: ${km.kvmd_code}
|
||||||
|
% endfor
|
||||||
@ -32,8 +32,10 @@ RUN pacman -Syy \
|
|||||||
&& user-packer -S --noconfirm \
|
&& user-packer -S --noconfirm \
|
||||||
python \
|
python \
|
||||||
python-pip \
|
python-pip \
|
||||||
|
python-tox \
|
||||||
python-systemd \
|
python-systemd \
|
||||||
python-dbus \
|
python-dbus \
|
||||||
|
python-mako \
|
||||||
nginx-mainline \
|
nginx-mainline \
|
||||||
ustreamer \
|
ustreamer \
|
||||||
socat \
|
socat \
|
||||||
@ -42,9 +44,6 @@ RUN pacman -Syy \
|
|||||||
&& rm -rf /.npm \
|
&& rm -rf /.npm \
|
||||||
&& (pacman -Sc --noconfirm || true)
|
&& (pacman -Sc --noconfirm || true)
|
||||||
|
|
||||||
COPY testenv/requirements.txt requirements.txt
|
|
||||||
RUN pip install -r requirements.txt
|
|
||||||
|
|
||||||
RUN mkdir -p /etc/kvmd/nginx
|
RUN mkdir -p /etc/kvmd/nginx
|
||||||
|
|
||||||
CMD /bin/bash
|
CMD /bin/bash
|
||||||
|
|||||||
@ -4,3 +4,6 @@ _MassStorageDeviceInfo.real
|
|||||||
_MassStorageDeviceInfo.hw
|
_MassStorageDeviceInfo.hw
|
||||||
_MassStorageDeviceInfo.image
|
_MassStorageDeviceInfo.image
|
||||||
fake_rpi.RPi.GPIO
|
fake_rpi.RPi.GPIO
|
||||||
|
_KeyMapping.kvmd_code
|
||||||
|
_KeyMapping.arduino_hid_key
|
||||||
|
_KeyMapping.web_key
|
||||||
|
|||||||
@ -7,7 +7,4 @@ pyudev
|
|||||||
pyyaml
|
pyyaml
|
||||||
pyserial
|
pyserial
|
||||||
setproctitle
|
setproctitle
|
||||||
systemd-python
|
|
||||||
dbus-python
|
|
||||||
pygments
|
pygments
|
||||||
tox
|
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
[tox]
|
[tox]
|
||||||
envlist = flake8, pylint, mypy, vulture, pytest, eslint, htmlhint
|
envlist = flake8, pylint, mypy, vulture, pytest, eslint, htmlhint
|
||||||
skipsdist = True
|
skipsdist = true
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
basepython = python3.7
|
basepython = python3.7
|
||||||
|
sitepackages = true
|
||||||
changedir = /src
|
changedir = /src
|
||||||
|
|
||||||
[testenv:flake8]
|
[testenv:flake8]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user