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
|
2020-11-22 20:54:09 +00:00
|
|
|
import threading
|
2020-11-22 15:54:40 +00:00
|
|
|
import time
|
2020-11-22 20:54:09 +00:00
|
|
|
import asyncio
|
2020-11-17 22:40:39 +00:00
|
|
|
|
|
|
|
import evdev
|
2020-12-24 00:26:34 +00:00
|
|
|
from evdev.ecodes import EV_KEY, EV_ABS
|
2020-11-17 22:40:39 +00:00
|
|
|
|
|
|
|
from keymapper.logger import logger
|
|
|
|
|
|
|
|
|
|
|
|
_devices = None
|
|
|
|
|
|
|
|
|
2020-11-29 19:18:00 +00:00
|
|
|
if not hasattr(evdev.InputDevice, 'path'):
|
|
|
|
# for evdev < 1.0.0 patch the path property
|
|
|
|
@property
|
|
|
|
def path(device):
|
|
|
|
return device.fn
|
|
|
|
|
|
|
|
evdev.InputDevice.path = path
|
|
|
|
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def map_abs_to_rel(capabilities):
|
|
|
|
"""Check if joystick movements can and should be mapped."""
|
|
|
|
# mapping buttons only works without ABS events in the capabilities,
|
|
|
|
# possibly due to some intentional constraints in the os. So always
|
|
|
|
# just map those events to REL if possible and remove ABS from
|
|
|
|
# the capabilities, because ABS events prevent regular button
|
|
|
|
# mappings from working,
|
|
|
|
abs_capabilities = capabilities.get(EV_ABS)
|
|
|
|
if abs_capabilities is not None:
|
|
|
|
if evdev.ecodes.ABS_MT_TRACKING_ID in abs_capabilities:
|
|
|
|
# check for some random mousepad capability
|
|
|
|
return False
|
|
|
|
|
|
|
|
if evdev.ecodes.ABS_X in abs_capabilities:
|
|
|
|
# can be a joystick or a mousepad (already handled), so it's
|
|
|
|
# a joystick
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-11-22 20:54:09 +00:00
|
|
|
class _GetDevices(threading.Thread):
|
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
|
2020-11-22 20:54:09 +00:00
|
|
|
slowing down the initialization.
|
2020-11-17 22:40:39 +00:00
|
|
|
"""
|
|
|
|
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-22 20:54:09 +00:00
|
|
|
# evdev needs asyncio to work
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
2020-11-22 20:56:25 +00:00
|
|
|
|
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-22 20:54:09 +00:00
|
|
|
if device.name == 'Power Button':
|
|
|
|
continue
|
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
# only keyboard devices
|
|
|
|
# https://www.kernel.org/doc/html/latest/input/event-codes.html
|
2020-12-19 23:34:37 +00:00
|
|
|
capabilities = device.capabilities(absinfo=False)
|
2020-11-30 21:42:53 +00:00
|
|
|
if EV_KEY not in capabilities and EV_ABS not in capabilities:
|
2020-12-02 18:33:31 +00:00
|
|
|
# or gamepads, because they can be mapped like a keyboard
|
2020-11-18 20:39:15 +00:00
|
|
|
continue
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
is_gamepad = map_abs_to_rel(capabilities)
|
2020-12-19 23:34:37 +00:00
|
|
|
|
2020-12-27 10:26:07 +00:00
|
|
|
name = device.name
|
|
|
|
path = device.path
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2020-12-27 10:26:07 +00:00
|
|
|
info = str(device.info)
|
|
|
|
if grouped.get(info) is None:
|
|
|
|
grouped[info] = []
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2020-12-27 10:26:07 +00:00
|
|
|
logger.spam('Found "%s", "%s", "%s"', info, path, name)
|
|
|
|
|
|
|
|
grouped[info].append((name, path, is_gamepad))
|
2020-11-17 22:40:39 +00:00
|
|
|
|
|
|
|
# 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]
|
2020-12-19 23:34:37 +00:00
|
|
|
is_gamepad = True in [entry[2] for entry in group]
|
2020-11-17 22:40:39 +00:00
|
|
|
shortest_name = sorted(names, key=len)[0]
|
|
|
|
result[shortest_name] = {
|
|
|
|
'paths': devs,
|
2020-12-19 23:34:37 +00:00
|
|
|
'devices': names,
|
|
|
|
'gamepad': is_gamepad
|
2020-11-17 22:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.pipe.send(result)
|
|
|
|
|
|
|
|
|
2020-11-18 19:03:37 +00:00
|
|
|
def refresh_devices():
|
2020-11-22 15:54:40 +00:00
|
|
|
"""Get new devices, e.g. new ones created by key-mapper.
|
|
|
|
|
|
|
|
This should be called whenever devices in /dev are added or removed.
|
|
|
|
"""
|
|
|
|
# it may take a little bit of time until devices are visible after
|
|
|
|
# changes
|
|
|
|
time.sleep(0.1)
|
2020-11-18 19:03:37 +00:00
|
|
|
global _devices
|
|
|
|
_devices = None
|
|
|
|
return get_devices()
|
|
|
|
|
|
|
|
|
2020-11-22 15:54:40 +00:00
|
|
|
def get_devices(include_keymapper=False):
|
2020-11-17 22:40:39 +00:00
|
|
|
"""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.
|
2020-11-22 16:30:06 +00:00
|
|
|
|
|
|
|
Since this needs to do some stuff with /dev and spawn processes the
|
|
|
|
result is cached. Use refresh_devices if you need up to date
|
|
|
|
devices.
|
2020-11-17 22:40:39 +00:00
|
|
|
"""
|
|
|
|
global _devices
|
|
|
|
if _devices is None:
|
|
|
|
pipe = multiprocessing.Pipe()
|
2020-11-22 20:54:09 +00:00
|
|
|
_GetDevices(pipe[1]).start()
|
2020-11-17 22:40:39 +00:00
|
|
|
# block until devices are available
|
|
|
|
_devices = pipe[0].recv()
|
2020-11-21 14:07:08 +00:00
|
|
|
if len(_devices) == 0:
|
2020-11-25 22:36:03 +00:00
|
|
|
logger.error(
|
|
|
|
'Did not find any device. If you added yourself to the '
|
|
|
|
'needed groups (see `ls -l /dev/input`) already, make sure '
|
|
|
|
'you also logged out and back in.'
|
|
|
|
)
|
2020-11-21 14:07:08 +00:00
|
|
|
else:
|
|
|
|
names = [f'"{name}"' for name in _devices]
|
|
|
|
logger.info('Found %s', ', '.join(names))
|
2020-11-22 15:54:40 +00:00
|
|
|
|
|
|
|
# filter the result
|
|
|
|
result = {}
|
|
|
|
for device in _devices.keys():
|
2020-11-22 16:30:06 +00:00
|
|
|
if not include_keymapper and device.startswith('key-mapper'):
|
2020-11-22 15:54:40 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
result[device] = _devices[device]
|
|
|
|
|
|
|
|
return result
|