2020-12-02 14:18:23 +00:00
|
|
|
#!/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
|
|
|
|
|
2020-12-02 14:18:23 +00:00
|
|
|
from keymapper.logger import logger
|
2020-12-02 20:20:18 +00:00
|
|
|
from keymapper.dev.ev_abs_mapper import JOYSTICK
|
2020-12-02 14:18:23 +00:00
|
|
|
|
|
|
|
|
2020-12-02 20:03:59 +00:00
|
|
|
def should_map_event_as_btn(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-02 20:03:59 +00:00
|
|
|
type : int
|
|
|
|
one of evdev.events
|
|
|
|
code : int
|
|
|
|
linux keycode
|
2020-12-02 19:48:23 +00:00
|
|
|
"""
|
2020-12-02 20:03:59 +00:00
|
|
|
if type == evdev.events.EV_KEY:
|
2020-12-02 19:48:23 +00:00
|
|
|
return True
|
|
|
|
|
2020-12-02 20:20:18 +00:00
|
|
|
if 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
|
2020-12-02 20:36:54 +00:00
|
|
|
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 14:18:23 +00:00
|
|
|
|
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]
|
2020-12-02 14:18:23 +00:00
|
|
|
logger.spam(
|
|
|
|
'got code:%s value:%s, maps to macro %s',
|
2020-12-02 20:36:54 +00:00
|
|
|
input_keycode,
|
2020-12-02 14:18:23 +00:00
|
|
|
event.value,
|
2020-12-02 15:17:52 +00:00
|
|
|
macro.code
|
2020-12-02 14:18:23 +00:00
|
|
|
)
|
2020-12-02 15:17:52 +00:00
|
|
|
asyncio.ensure_future(macro.run())
|
2020-12-02 14:18:23 +00:00
|
|
|
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
|
2020-12-02 14:18:23 +00:00
|
|
|
logger.spam(
|
2020-12-02 20:03:59 +00:00
|
|
|
'got code:%s value:%s event:%s, maps to EV_KEY:%s',
|
2020-12-02 20:36:54 +00:00
|
|
|
input_keycode,
|
2020-12-02 14:18:23 +00:00
|
|
|
event.value,
|
2020-12-02 19:48:23 +00:00
|
|
|
evdev.ecodes.EV[event.type],
|
2020-12-02 20:36:54 +00:00
|
|
|
target_keycode
|
2020-12-02 14:18:23 +00:00
|
|
|
)
|
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',
|
2020-12-02 20:36:54 +00:00
|
|
|
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 14:18:23 +00:00
|
|
|
|
2020-12-02 20:03:59 +00:00
|
|
|
uinput.write(target_type, target_keycode, event.value)
|
2020-12-02 14:18:23 +00:00
|
|
|
uinput.syn()
|