overthrowing the functionality completely
parent
7e8c14bb09
commit
364170c71b
@ -0,0 +1,145 @@
|
|||||||
|
#!/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/>.
|
||||||
|
|
||||||
|
|
||||||
|
"""Parsing and running CLI tools."""
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from keymapper.paths import X11_SYMBOLS
|
||||||
|
from keymapper.logger import logger, is_debug
|
||||||
|
from keymapper.getdevices import get_devices
|
||||||
|
from keymapper.mapping import system_mapping
|
||||||
|
|
||||||
|
|
||||||
|
def get_system_layout_locale():
|
||||||
|
"""Get the system wide configured default keyboard layout locale."""
|
||||||
|
localectl = subprocess.check_output(
|
||||||
|
['localectl', 'status']
|
||||||
|
).decode().split('\n')
|
||||||
|
# example:
|
||||||
|
# System Locale: LANG=en_GB.UTF-8
|
||||||
|
# VC Keymap: tmp
|
||||||
|
# X11 Layout: de
|
||||||
|
# X11 Model: pc105
|
||||||
|
return [
|
||||||
|
line for line in localectl
|
||||||
|
if 'X11 Layout' in line
|
||||||
|
][0].split(': ')[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def setxkbmap(device, layout):
|
||||||
|
"""Apply a preset to the device.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
device : string
|
||||||
|
layout : string or None
|
||||||
|
For example 'de', passed to setxkbmap unmodified. If None, will
|
||||||
|
load the system default
|
||||||
|
"""
|
||||||
|
if layout is not None:
|
||||||
|
path = os.path.join(X11_SYMBOLS, layout)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
logger.error('Symbols %s don\'t exist', path)
|
||||||
|
return
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
if f.read() == '':
|
||||||
|
logger.error('Tried to load empty symbols %s', path)
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info('Applying layout "%s" on device %s', layout, device)
|
||||||
|
group = get_devices()[device]
|
||||||
|
|
||||||
|
if layout is None:
|
||||||
|
cmd = ['setxkbmap', '-layout', get_system_layout_locale()]
|
||||||
|
else:
|
||||||
|
cmd = ['setxkbmap', '-layout', layout, '-keycodes', 'key-mapper']
|
||||||
|
|
||||||
|
# apply it to every device that hangs on the same usb port, because I
|
||||||
|
# have no idea how to figure out which one of those 3 devices that are
|
||||||
|
# all named after my mouse to use.
|
||||||
|
for xinput_name, xinput_id in get_xinput_id_mapping():
|
||||||
|
if xinput_name not in group['devices']:
|
||||||
|
# only all virtual devices of the same hardware device
|
||||||
|
continue
|
||||||
|
|
||||||
|
device_cmd = cmd + ['-device', str(xinput_id)]
|
||||||
|
logger.debug('Running `%s`', ' '.join(device_cmd))
|
||||||
|
subprocess.run(device_cmd, capture_output=(not is_debug()))
|
||||||
|
|
||||||
|
|
||||||
|
def apply_empty_symbols(device):
|
||||||
|
"""Make the device not write any character anymore."""
|
||||||
|
logger.debug('Applying the empty symbols to %s', device)
|
||||||
|
group = get_devices()[device]
|
||||||
|
|
||||||
|
cmd = ['setxkbmap', '-layout', 'key-mapper/empty']
|
||||||
|
|
||||||
|
# apply it to every device that hangs on the same usb port, because I
|
||||||
|
# have no idea how to figure out which one of those 3 devices that are
|
||||||
|
# all named after my mouse to use.
|
||||||
|
for xinput_name, xinput_id in get_xinput_id_mapping():
|
||||||
|
if xinput_name not in group['devices']:
|
||||||
|
# only all virtual devices of the same hardware device
|
||||||
|
continue
|
||||||
|
|
||||||
|
device_cmd = cmd + ['-device', str(xinput_id)]
|
||||||
|
logger.debug('Running `%s`', ' '.join(device_cmd))
|
||||||
|
subprocess.run(device_cmd, capture_output=(not is_debug()))
|
||||||
|
|
||||||
|
|
||||||
|
def get_xinput_id_mapping():
|
||||||
|
"""Run xinput and get a list of name, id tuplies.
|
||||||
|
|
||||||
|
The ids are needed for setxkbmap. There might be duplicate names with
|
||||||
|
different ids.
|
||||||
|
"""
|
||||||
|
names = subprocess.check_output(
|
||||||
|
['xinput', 'list', '--name-only']
|
||||||
|
).decode().split('\n')
|
||||||
|
ids = subprocess.check_output(
|
||||||
|
['xinput', 'list', '--id-only']
|
||||||
|
).decode().split('\n')
|
||||||
|
|
||||||
|
names = [name for name in names if name != '']
|
||||||
|
ids = [int(id) for id in ids if id != '']
|
||||||
|
return zip(names, ids)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_xmodmap():
|
||||||
|
"""Read the output of xmodmap as a Mapping object."""
|
||||||
|
xmodmap = subprocess.check_output(['xmodmap', '-pke']).decode() + '\n'
|
||||||
|
mappings = re.findall(r'(\d+) = (.+)\n', xmodmap)
|
||||||
|
# TODO is this tested?
|
||||||
|
for keycode, characters in mappings:
|
||||||
|
system_mapping.change(
|
||||||
|
previous_keycode=None,
|
||||||
|
new_keycode=int(keycode),
|
||||||
|
character=characters.split()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO verify that this is the system default and not changed when I
|
||||||
|
# setxkbmap my mouse
|
||||||
|
parse_xmodmap()
|
@ -0,0 +1,106 @@
|
|||||||
|
#!/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/>.
|
||||||
|
|
||||||
|
|
||||||
|
"""Device and evdev stuff that is independent from the display server."""
|
||||||
|
|
||||||
|
|
||||||
|
import multiprocessing
|
||||||
|
|
||||||
|
import evdev
|
||||||
|
|
||||||
|
from keymapper.logger import logger
|
||||||
|
|
||||||
|
|
||||||
|
_devices = None
|
||||||
|
|
||||||
|
|
||||||
|
class GetDevicesProcess(multiprocessing.Process):
|
||||||
|
"""Process to get the devices that can be worked with.
|
||||||
|
|
||||||
|
Since InputDevice destructors take quite some time, do this
|
||||||
|
asynchronously so that they can take as much time as they want without
|
||||||
|
slowing down the initialization. To avoid evdevs asyncio stuff spamming
|
||||||
|
errors, do this with multiprocessing and not multithreading.
|
||||||
|
TODO to threading, make eventloop
|
||||||
|
"""
|
||||||
|
def __init__(self, pipe):
|
||||||
|
"""Construct the process.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
pipe : multiprocessing.Pipe
|
||||||
|
used to communicate the result
|
||||||
|
"""
|
||||||
|
self.pipe = pipe
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""Do what get_devices describes."""
|
||||||
|
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
|
||||||
|
|
||||||
|
# group them together by usb device because there could be stuff like
|
||||||
|
# "Logitech USB Keyboard" and "Logitech USB Keyboard Consumer Control"
|
||||||
|
grouped = {}
|
||||||
|
for device in devices:
|
||||||
|
# only keyboard devices
|
||||||
|
# https://www.kernel.org/doc/html/latest/input/event-codes.html
|
||||||
|
if evdev.ecodes.EV_KEY not in device.capabilities().keys():
|
||||||
|
continue
|
||||||
|
|
||||||
|
usb = device.phys.split('/')[0]
|
||||||
|
if grouped.get(usb) is None:
|
||||||
|
grouped[usb] = []
|
||||||
|
grouped[usb].append((device.name, device.path))
|
||||||
|
|
||||||
|
# now write down all the paths of that group
|
||||||
|
result = {}
|
||||||
|
for group in grouped.values():
|
||||||
|
names = [entry[0] for entry in group]
|
||||||
|
devs = [entry[1] for entry in group]
|
||||||
|
shortest_name = sorted(names, key=len)[0]
|
||||||
|
result[shortest_name] = {
|
||||||
|
'paths': devs,
|
||||||
|
'devices': names
|
||||||
|
}
|
||||||
|
|
||||||
|
self.pipe.send(result)
|
||||||
|
|
||||||
|
|
||||||
|
def get_devices():
|
||||||
|
"""Group devices and get relevant infos per group.
|
||||||
|
|
||||||
|
Returns a list containing mappings of
|
||||||
|
{group_name: {paths: [paths], devices: [names]} for input devices.
|
||||||
|
|
||||||
|
For example, group_name could be "Logitech USB Keyboard", devices might
|
||||||
|
contain "Logitech USB Keyboard System Control" and "Logitech USB Keyboard".
|
||||||
|
paths is a list of files in /dev/input that belong to the devices.
|
||||||
|
|
||||||
|
They are grouped by usb port.
|
||||||
|
"""
|
||||||
|
global _devices
|
||||||
|
if _devices is None:
|
||||||
|
pipe = multiprocessing.Pipe()
|
||||||
|
GetDevicesProcess(pipe[1]).start()
|
||||||
|
# block until devices are available
|
||||||
|
_devices = pipe[0].recv()
|
||||||
|
logger.info('Found %s', ', '.join([f'"{name}"' for name in _devices]))
|
||||||
|
return _devices
|
Loading…
Reference in New Issue