2020-11-17 22:40:39 +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/>.
|
|
|
|
|
|
|
|
|
|
|
|
"""Device and evdev stuff that is independent from the display server."""
|
|
|
|
|
|
|
|
|
|
|
|
import multiprocessing
|
|
|
|
|
|
|
|
import evdev
|
|
|
|
|
|
|
|
from keymapper.logger import logger
|
|
|
|
|
|
|
|
|
|
|
|
_devices = None
|
|
|
|
|
|
|
|
|
2020-11-18 19:03:37 +00:00
|
|
|
class _GetDevicesProcess(multiprocessing.Process):
|
2020-11-17 22:40:39 +00:00
|
|
|
"""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."""
|
2020-11-18 21:06:54 +00:00
|
|
|
logger.debug('Discovering device paths')
|
2020-11-17 22:40:39 +00:00
|
|
|
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:
|
2020-11-18 23:22:22 +00:00
|
|
|
if device.phys.startswith('key-mapper'):
|
|
|
|
# injector device, not really periphery
|
|
|
|
continue
|
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
# only keyboard devices
|
|
|
|
# https://www.kernel.org/doc/html/latest/input/event-codes.html
|
2020-11-18 20:39:15 +00:00
|
|
|
capabilities = device.capabilities().keys()
|
|
|
|
if evdev.ecodes.EV_KEY not in capabilities:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if evdev.ecodes.EV_REL in capabilities:
|
2020-11-19 10:58:20 +00:00
|
|
|
# skip devices that control movement, because I would like
|
|
|
|
# to not affect the mouse movement performance
|
|
|
|
# TODO check for config, is set enable mapping buttons
|
|
|
|
# of that device as well
|
|
|
|
# TODO speaking of config, add checkbox to automatically load
|
|
|
|
# a preset. Store a mapping of device: preset somewhere to
|
|
|
|
# automatically load them. Autoloading presets should be a
|
|
|
|
# different executable, and controlling stuff in the gui
|
|
|
|
# should send a dbus message to the different bin to stop
|
|
|
|
# doing stuff
|
2020-11-18 21:06:54 +00:00
|
|
|
logger.debug(
|
|
|
|
'Skipping %s to avoid impairing mouse movement',
|
|
|
|
device.path
|
|
|
|
)
|
2020-11-17 22:40:39 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
usb = device.phys.split('/')[0]
|
|
|
|
if grouped.get(usb) is None:
|
|
|
|
grouped[usb] = []
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2020-11-18 23:22:22 +00:00
|
|
|
logger.debug('Found "%s", %s, %s', device.name, device.path, usb)
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-11-18 19:03:37 +00:00
|
|
|
def refresh_devices():
|
|
|
|
"""Get new devices, e.g. new ones created by key-mapper."""
|
|
|
|
global _devices
|
|
|
|
_devices = None
|
|
|
|
return get_devices()
|
|
|
|
|
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
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()
|
2020-11-18 19:03:37 +00:00
|
|
|
_GetDevicesProcess(pipe[1]).start()
|
2020-11-17 22:40:39 +00:00
|
|
|
# block until devices are available
|
|
|
|
_devices = pipe[0].recv()
|
|
|
|
logger.info('Found %s', ', '.join([f'"{name}"' for name in _devices]))
|
|
|
|
return _devices
|