input-remapper/keymapper/dev/keycode_mapper.py

108 lines
2.8 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
2020-12-02 19:48:23 +00:00
import evdev
from keymapper.logger import logger
2020-12-02 20:20:18 +00:00
from keymapper.dev.ev_abs_mapper import JOYSTICK
def should_map_event_as_btn(ev_type, code):
2020-12-02 19:48:23 +00:00
"""Does this event describe a button.
Especially important for gamepad events, some of the buttons
require special rules.
Parameters
----------
2020-12-04 16:38:04 +00:00
ev_type : int
2020-12-02 20:03:59 +00:00
one of evdev.events
code : int
linux keycode
2020-12-02 19:48:23 +00:00
"""
2020-12-04 16:38:04 +00:00
if ev_type == evdev.events.EV_KEY:
2020-12-02 19:48:23 +00:00
return True
2020-12-04 16:38:04 +00:00
if ev_type == evdev.events.EV_ABS and code not in JOYSTICK:
2020-12-02 19:48:23 +00:00
return True
return False
2020-12-04 13:38:41 +00:00
def handle_keycode(_code_to_code, macros, event, uinput):
2020-12-02 19:48:23 +00:00
"""Write the mapped keycode or forward unmapped ones.
2020-12-02 16:40:21 +00:00
Parameters
----------
2020-12-04 13:38:41 +00:00
_code_to_code : dict
mapping of linux-keycode to linux-keycode.
2020-12-02 16:40:21 +00:00
macros : dict
mapping of linux-keycode to _Macro objects
"""
2020-12-02 19:48:23 +00:00
if event.value == 2:
# button-hold event
return
2020-12-02 15:17:52 +00:00
input_keycode = event.code
2020-12-02 20:03:59 +00:00
input_type = event.type
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',
input_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-04 13:38:41 +00:00
if input_keycode in _code_to_code:
target_keycode = _code_to_code[input_keycode]
2020-12-02 20:03:59 +00:00
target_type = evdev.events.EV_KEY
logger.spam(
2020-12-02 20:03:59 +00:00
'got code:%s value:%s event:%s, maps to EV_KEY:%s',
input_keycode,
event.value,
2020-12-02 19:48:23 +00:00
evdev.ecodes.EV[event.type],
target_keycode
)
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',
input_keycode,
2020-12-02 16:40:21 +00:00
event.value,
)
2020-12-02 15:17:52 +00:00
target_keycode = input_keycode
2020-12-02 20:03:59 +00:00
target_type = input_type
2020-12-02 20:03:59 +00:00
uinput.write(target_type, target_keycode, event.value)
uinput.syn()