option to ignore some keys

This commit is contained in:
Devaev Maxim 2021-05-06 19:38:49 +03:00
parent e35aa1199b
commit 3aa92a87d4
4 changed files with 17 additions and 9 deletions

View File

@ -370,12 +370,12 @@ def _get_config_scheme() -> Dict:
"type": Option("", type=valid_stripped_string_not_empty), "type": Option("", type=valid_stripped_string_not_empty),
"keymap": Option("/usr/share/kvmd/keymaps/en-us", type=valid_abs_file), "keymap": Option("/usr/share/kvmd/keymaps/en-us", type=valid_abs_file),
"ignore_keys": Option([], type=functools.partial(valid_string_list, subval=valid_hid_key)),
"mouse_x_range": { "mouse_x_range": {
"min": Option(MouseRange.MIN, type=valid_hid_mouse_move), "min": Option(MouseRange.MIN, type=valid_hid_mouse_move),
"max": Option(MouseRange.MAX, type=valid_hid_mouse_move), "max": Option(MouseRange.MAX, type=valid_hid_mouse_move),
}, },
"mouse_y_range": { "mouse_y_range": {
"min": Option(MouseRange.MIN, type=valid_hid_mouse_move), "min": Option(MouseRange.MIN, type=valid_hid_mouse_move),
"max": Option(MouseRange.MAX, type=valid_hid_mouse_move), "max": Option(MouseRange.MAX, type=valid_hid_mouse_move),

View File

@ -59,7 +59,7 @@ def main(argv: Optional[List[str]]=None) -> None:
if config.kvmd.msd.type == "otg": if config.kvmd.msd.type == "otg":
msd_kwargs["gadget"] = config.otg.gadget # XXX: Small crutch to pass gadget name to the plugin msd_kwargs["gadget"] = config.otg.gadget # XXX: Small crutch to pass gadget name to the plugin
hid_kwargs = config.kvmd.hid._unpack(ignore=["type", "keymap", "mouse_x_range", "mouse_y_range"]) hid_kwargs = config.kvmd.hid._unpack(ignore=["type", "keymap", "ignore_keys", "mouse_x_range", "mouse_y_range"])
if config.kvmd.hid.type == "otg": if config.kvmd.hid.type == "otg":
hid_kwargs["udc"] = config.otg.udc # XXX: Small crutch to pass UDC to the plugin hid_kwargs["udc"] = config.otg.udc # XXX: Small crutch to pass UDC to the plugin
@ -104,6 +104,7 @@ def main(argv: Optional[List[str]]=None) -> None:
sync_chunk_size=config.server.sync_chunk_size, sync_chunk_size=config.server.sync_chunk_size,
keymap_path=config.hid.keymap, keymap_path=config.hid.keymap,
ignore_keys=config.hid.ignore_keys,
mouse_x_range=(config.hid.mouse_x_range.min, config.hid.mouse_x_range.max), mouse_x_range=(config.hid.mouse_x_range.min, config.hid.mouse_x_range.max),
mouse_y_range=(config.hid.mouse_y_range.min, config.hid.mouse_y_range.max), mouse_y_range=(config.hid.mouse_y_range.min, config.hid.mouse_y_range.max),

View File

@ -25,6 +25,7 @@ import stat
import functools import functools
from typing import Tuple from typing import Tuple
from typing import List
from typing import Dict from typing import Dict
from typing import Set from typing import Set
from typing import Callable from typing import Callable
@ -63,6 +64,7 @@ class HidApi:
hid: BaseHid, hid: BaseHid,
keymap_path: str, keymap_path: str,
ignore_keys: List[str],
mouse_x_range: Tuple[int, int], mouse_x_range: Tuple[int, int],
mouse_y_range: Tuple[int, int], mouse_y_range: Tuple[int, int],
@ -74,6 +76,8 @@ class HidApi:
self.__default_keymap_name = os.path.basename(keymap_path) self.__default_keymap_name = os.path.basename(keymap_path)
self.__ensure_symmap(self.__default_keymap_name) self.__ensure_symmap(self.__default_keymap_name)
self.__ignore_keys = ignore_keys
self.__mouse_x_range = mouse_x_range self.__mouse_x_range = mouse_x_range
self.__mouse_y_range = mouse_y_range self.__mouse_y_range = mouse_y_range
@ -160,7 +164,8 @@ class HidApi:
state = valid_bool(event["state"]) state = valid_bool(event["state"])
except Exception: except Exception:
return return
self.__hid.send_key_events([(key, state)]) if key not in self.__ignore_keys:
self.__hid.send_key_events([(key, state)])
@exposed_ws("mouse_button") @exposed_ws("mouse_button")
async def __ws_mouse_button_handler(self, _: WebSocketResponse, event: Dict) -> None: async def __ws_mouse_button_handler(self, _: WebSocketResponse, event: Dict) -> None:
@ -217,11 +222,12 @@ class HidApi:
@exposed_http("POST", "/hid/events/send_key") @exposed_http("POST", "/hid/events/send_key")
async def __events_send_key_handler(self, request: Request) -> Response: async def __events_send_key_handler(self, request: Request) -> Response:
key = valid_hid_key(request.query.get("key")) key = valid_hid_key(request.query.get("key"))
if "state" in request.query: if key not in self.__ignore_keys:
state = valid_bool(request.query["state"]) if "state" in request.query:
self.__hid.send_key_events([(key, state)]) state = valid_bool(request.query["state"])
else: self.__hid.send_key_events([(key, state)])
self.__hid.send_key_events([(key, True), (key, False)]) else:
self.__hid.send_key_events([(key, True), (key, False)])
return make_json_response() return make_json_response()
@exposed_http("POST", "/hid/events/send_mouse_button") @exposed_http("POST", "/hid/events/send_mouse_button")

View File

@ -161,6 +161,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
sync_chunk_size: int, sync_chunk_size: int,
keymap_path: str, keymap_path: str,
ignore_keys: List[str],
mouse_x_range: Tuple[int, int], mouse_x_range: Tuple[int, int],
mouse_y_range: Tuple[int, int], mouse_y_range: Tuple[int, int],
@ -195,7 +196,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
], ],
] ]
self.__hid_api = HidApi(hid, keymap_path, mouse_x_range, mouse_y_range) # Ugly hack to get keymaps state self.__hid_api = HidApi(hid, keymap_path, ignore_keys, mouse_x_range, mouse_y_range) # Ugly hack to get keymaps state
self.__apis: List[object] = [ self.__apis: List[object] = [
self, self,
AuthApi(auth_manager), AuthApi(auth_manager),