2020-11-02 20:57:28 +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/>.
|
|
|
|
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
"""Device and evdev stuff that is independent from the display server."""
|
2020-11-02 20:57:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
import subprocess
|
2020-11-17 22:40:39 +00:00
|
|
|
import time
|
2020-11-18 12:17:49 +00:00
|
|
|
import multiprocessing
|
2020-11-17 16:56:59 +00:00
|
|
|
import asyncio
|
2020-11-02 20:57:28 +00:00
|
|
|
|
2020-11-05 00:24:56 +00:00
|
|
|
import evdev
|
|
|
|
|
2020-11-02 20:57:28 +00:00
|
|
|
from keymapper.logger import logger
|
2020-11-18 21:06:54 +00:00
|
|
|
from keymapper.getdevices import get_devices
|
|
|
|
from keymapper.state import custom_mapping, system_mapping
|
2020-11-18 19:03:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
DEV_NAME = 'key-mapper'
|
2020-11-18 21:06:54 +00:00
|
|
|
DEVICE_CREATED = 1
|
2020-11-17 20:06:21 +00:00
|
|
|
|
|
|
|
|
2020-11-02 20:57:28 +00:00
|
|
|
def can_grab(path):
|
|
|
|
"""Can input events from the device be read?
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
path : string
|
|
|
|
Path in dev, for example '/dev/input/event7'
|
|
|
|
"""
|
|
|
|
p = subprocess.run(['fuser', '-v', path])
|
|
|
|
return p.returncode == 1
|
|
|
|
|
|
|
|
|
2020-11-18 12:17:49 +00:00
|
|
|
class KeycodeInjector:
|
|
|
|
"""Keeps injecting keycodes in the background based on the mapping."""
|
2020-11-17 13:35:36 +00:00
|
|
|
def __init__(self, device):
|
|
|
|
self.device = device
|
2020-11-05 23:26:49 +00:00
|
|
|
self.virtual_devices = []
|
2020-11-18 12:17:49 +00:00
|
|
|
self.processes = []
|
2020-11-17 16:56:59 +00:00
|
|
|
self.start_injecting()
|
2020-11-05 00:24:56 +00:00
|
|
|
|
2020-11-18 19:03:37 +00:00
|
|
|
def start_injecting(self):
|
|
|
|
"""Read keycodes and inject the mapped character forever."""
|
|
|
|
self.stop_injecting()
|
|
|
|
|
|
|
|
paths = get_devices()[self.device]['paths']
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
'Starting injecting the mapping for %s on %s',
|
|
|
|
self.device,
|
|
|
|
', '.join(paths)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Watch over each one of the potentially multiple devices per hardware
|
|
|
|
for path in paths:
|
2020-11-18 21:06:54 +00:00
|
|
|
pipe = multiprocessing.Pipe()
|
2020-11-18 19:03:37 +00:00
|
|
|
worker = multiprocessing.Process(
|
|
|
|
target=self._start_injecting_worker,
|
2020-11-18 21:06:54 +00:00
|
|
|
args=(path, custom_mapping, pipe[1])
|
2020-11-18 19:03:37 +00:00
|
|
|
)
|
|
|
|
worker.start()
|
2020-11-18 21:06:54 +00:00
|
|
|
# wait for the process to notify creation of the new injection
|
|
|
|
# device, to keep the logs in order.
|
|
|
|
pipe[0].recv()
|
2020-11-18 19:03:37 +00:00
|
|
|
self.processes.append(worker)
|
|
|
|
|
|
|
|
def stop_injecting(self):
|
|
|
|
"""Stop injecting keycodes."""
|
|
|
|
# TODO test
|
|
|
|
logger.info('Stopping injecting keycodes')
|
|
|
|
for i, process in enumerate(self.processes):
|
|
|
|
if process is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if process.is_alive():
|
|
|
|
process.terminate()
|
|
|
|
self.processes[i] = None
|
|
|
|
|
2020-11-18 21:06:54 +00:00
|
|
|
def _start_injecting_worker(self, path, mapping, pipe):
|
2020-11-17 22:40:39 +00:00
|
|
|
"""Inject keycodes for one of the virtual devices."""
|
2020-11-18 12:17:49 +00:00
|
|
|
# TODO test
|
2020-11-17 16:56:59 +00:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
2020-11-17 13:35:36 +00:00
|
|
|
device = evdev.InputDevice(path)
|
2020-11-18 21:06:54 +00:00
|
|
|
try:
|
2020-11-18 20:39:15 +00:00
|
|
|
# grab to avoid e.g. the disabled keycode of 10 to confuse X,
|
|
|
|
# especially when one of the buttons of your mouse also uses 10
|
|
|
|
device.grab()
|
|
|
|
except IOError:
|
2020-11-18 21:06:54 +00:00
|
|
|
logger.error('Cannot grab %s', path)
|
|
|
|
|
|
|
|
# copy the capabilities because the keymapper_device is going
|
|
|
|
# to act like the mouse
|
|
|
|
keymapper_device = evdev.UInput.from_device(device)
|
|
|
|
|
|
|
|
pipe.send(DEVICE_CREATED)
|
2020-11-17 16:56:59 +00:00
|
|
|
|
2020-11-18 12:17:49 +00:00
|
|
|
logger.debug(
|
|
|
|
'Started injecting into %s, fd %s',
|
|
|
|
device.path, keymapper_device.fd
|
|
|
|
)
|
|
|
|
|
2020-11-17 13:35:36 +00:00
|
|
|
for event in device.read_loop():
|
2020-11-17 16:56:59 +00:00
|
|
|
if event.type != evdev.ecodes.EV_KEY:
|
2020-11-18 21:06:54 +00:00
|
|
|
logger.spam(
|
|
|
|
'got type:%s code:%s value:%s, forward',
|
|
|
|
event.type, event.code, event.value
|
|
|
|
)
|
|
|
|
keymapper_device.write(event.type, event.code, event.value)
|
|
|
|
keymapper_device.syn()
|
2020-11-17 16:56:59 +00:00
|
|
|
continue
|
|
|
|
|
2020-11-18 20:39:15 +00:00
|
|
|
if event.value == 2:
|
2020-11-18 19:03:37 +00:00
|
|
|
# linux does them itself, no need to trigger them
|
|
|
|
continue
|
2020-11-18 12:17:49 +00:00
|
|
|
|
2020-11-17 16:56:59 +00:00
|
|
|
# this happens to report key codes that are 8 lower
|
2020-11-17 19:51:32 +00:00
|
|
|
# than the ones reported by xev and that X expects
|
2020-11-17 16:56:59 +00:00
|
|
|
input_keycode = event.code + 8
|
|
|
|
|
2020-11-18 12:17:49 +00:00
|
|
|
character = mapping.get_character(input_keycode)
|
2020-11-17 22:40:39 +00:00
|
|
|
|
|
|
|
if character is None:
|
|
|
|
# unknown keycode, forward it
|
2020-11-18 12:17:49 +00:00
|
|
|
continue
|
2020-11-17 22:40:39 +00:00
|
|
|
else:
|
2020-11-18 20:39:15 +00:00
|
|
|
target_keycode = system_mapping.get_keycode(character)
|
2020-11-17 22:40:39 +00:00
|
|
|
if target_keycode is None:
|
|
|
|
logger.error(
|
2020-11-18 19:03:37 +00:00
|
|
|
'Cannot find character %s in the internal mapping',
|
2020-11-17 22:40:39 +00:00
|
|
|
character
|
|
|
|
)
|
|
|
|
continue
|
2020-11-18 21:06:54 +00:00
|
|
|
|
|
|
|
logger.spam(
|
2020-11-18 19:03:37 +00:00
|
|
|
'got code:%s value:%s, maps to code:%s char:%s',
|
|
|
|
event.code + 8, event.value, target_keycode, character
|
|
|
|
)
|
2020-11-17 19:51:32 +00:00
|
|
|
|
|
|
|
# TODO test for the stuff put into write
|
2020-11-18 12:17:49 +00:00
|
|
|
keymapper_device.write(
|
|
|
|
evdev.ecodes.EV_KEY,
|
|
|
|
target_keycode - 8,
|
|
|
|
event.value
|
|
|
|
)
|
|
|
|
|
2020-11-17 19:51:32 +00:00
|
|
|
keymapper_device.syn()
|