input-remapper/keymapper/dev/keycode_mapper.py

81 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# This file is part of key-mapper.
#
# key-mapper 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.
#
# key-mapper 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 key-mapper. If not, see <https://www.gnu.org/licenses/>.
"""Inject a keycode based on the mapping."""
import asyncio
from keymapper.logger import logger
2020-12-02 15:17:52 +00:00
from keymapper.state import KEYCODE_OFFSET
2020-12-02 15:17:52 +00:00
def handle_keycode(code_code_mapping, macros, event, uinput):
2020-12-02 16:40:21 +00:00
"""Write the mapped keycode.
Parameters
----------
code_code_mapping : dict
mapping of linux-keycode to linux-keycode. No need to substract
anything before writing to the device.
macros : dict
mapping of linux-keycode to _Macro objects
"""
2020-12-02 15:17:52 +00:00
input_keycode = event.code
2020-12-02 16:40:21 +00:00
# for logging purposes. It should log the same keycode as xev and gtk,
# which is also displayed in the UI.
xkb_keycode = input_keycode + KEYCODE_OFFSET
2020-12-02 15:17:52 +00:00
if input_keycode in macros:
2020-12-02 16:40:21 +00:00
if event.value != 1:
# only key-down events trigger macros
return
2020-12-02 15:17:52 +00:00
macro = macros[input_keycode]
logger.spam(
'got code:%s value:%s, maps to macro %s',
2020-12-02 16:40:21 +00:00
xkb_keycode,
event.value,
2020-12-02 15:17:52 +00:00
macro.code
)
2020-12-02 15:17:52 +00:00
asyncio.ensure_future(macro.run())
return
2020-12-02 15:17:52 +00:00
if input_keycode in code_code_mapping:
target_keycode = code_code_mapping[input_keycode]
logger.spam(
2020-12-02 15:17:52 +00:00
'got code:%s value:%s, maps to code:%s',
2020-12-02 16:40:21 +00:00
xkb_keycode,
event.value,
2020-12-02 16:40:21 +00:00
target_keycode + KEYCODE_OFFSET
)
2020-12-02 15:17:52 +00:00
else:
2020-12-02 16:40:21 +00:00
logger.spam(
'got unmapped code:%s value:%s',
xkb_keycode,
event.value,
)
2020-12-02 15:17:52 +00:00
target_keycode = input_keycode
2020-12-02 15:17:52 +00:00
uinput.write(event.type, target_keycode, event.value)
uinput.syn()