|
|
|
@ -159,113 +159,7 @@ def generate_symbols_file_content(device, preset, mappings):
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_libinput_list():
|
|
|
|
|
# TODO return something meaningful. {name: {paths:, related:}}
|
|
|
|
|
"""Get a mapping of {name: [paths]} for `libinput list-devices` devices.
|
|
|
|
|
|
|
|
|
|
This is grouped by group, so the "Logitech USB Keyboard" and
|
|
|
|
|
"Logitech USB Keyboard Consumer Control" are one key (the shorter one),
|
|
|
|
|
and the paths array for that is therefore 2 entries large.
|
|
|
|
|
"""
|
|
|
|
|
stdout = subprocess.check_output(['libinput', 'list-devices'])
|
|
|
|
|
devices = [
|
|
|
|
|
device for device in stdout.decode().split('\n\n')
|
|
|
|
|
if device != ''
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
grouped = {}
|
|
|
|
|
for device in devices:
|
|
|
|
|
info = {}
|
|
|
|
|
for line in device.split('\n'):
|
|
|
|
|
# example:
|
|
|
|
|
# "Kernel: /dev/input/event0"
|
|
|
|
|
match = re.match(r'(\w+):\s+(.+)', line)
|
|
|
|
|
if match is None:
|
|
|
|
|
continue
|
|
|
|
|
info[match[1]] = match[2]
|
|
|
|
|
|
|
|
|
|
name = info['Device']
|
|
|
|
|
group = info['Group'] # int
|
|
|
|
|
dev = info['Kernel'] # /dev/input/event#
|
|
|
|
|
|
|
|
|
|
if grouped.get(group) is None:
|
|
|
|
|
grouped[group] = []
|
|
|
|
|
grouped[group].append((name, dev))
|
|
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
for i in grouped:
|
|
|
|
|
group = grouped[i]
|
|
|
|
|
names = [entry[0] for entry in group]
|
|
|
|
|
devs = [entry[1] for entry in group]
|
|
|
|
|
shortest_name = sorted(names, key=len)[0]
|
|
|
|
|
result[shortest_name] = devs
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_evtest():
|
|
|
|
|
"""Get a mapping of {name: [paths]} for each evtest device.
|
|
|
|
|
|
|
|
|
|
This is grouped by name, so "Logitech USB Keyboard" and
|
|
|
|
|
"Logitech USB Keyboard Consumer Control" are two keys in result. Some
|
|
|
|
|
devices have the same name for each of those entries.
|
|
|
|
|
|
|
|
|
|
Use parse_libinput_list instead, which properly groups all of them.
|
|
|
|
|
"""
|
|
|
|
|
# It asks for a device afterwads, so just insert garbage into it
|
|
|
|
|
p = subprocess.Popen(
|
|
|
|
|
'echo a | sudo evtest',
|
|
|
|
|
shell=True,
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE
|
|
|
|
|
)
|
|
|
|
|
# the list we are looking for is in stderr
|
|
|
|
|
_, evtest = p.communicate()
|
|
|
|
|
evtest = [
|
|
|
|
|
line
|
|
|
|
|
for line in evtest.decode().split('\n')
|
|
|
|
|
if line.startswith('/dev')
|
|
|
|
|
]
|
|
|
|
|
logger.debug('evtest devices: \n%s', '\n'.join(evtest))
|
|
|
|
|
|
|
|
|
|
# evtest also returns a bunch of other devices, like some audio devices,
|
|
|
|
|
# so check this list against `xinput list` to get keyboards and mice
|
|
|
|
|
xinput = get_xinput_list()
|
|
|
|
|
logger.debug('xinput devices: \n%s', '\n'.join(xinput))
|
|
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
for line in evtest:
|
|
|
|
|
match = re.search(r'(/dev/input/event\d+):\s+(.+)', line)
|
|
|
|
|
if match is None:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# the path refers to a file in /dev/input/event#. Note, that this is
|
|
|
|
|
# different from the id that `xinput list` can return.
|
|
|
|
|
path = match[1]
|
|
|
|
|
name = match[2]
|
|
|
|
|
if name not in xinput:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not result.get(name):
|
|
|
|
|
result[name] = []
|
|
|
|
|
result[name].append(path)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_xinput_list():
|
|
|
|
|
"""Run xinput and get the resulting device names as list."""
|
|
|
|
|
xinput = subprocess.check_output(['xinput', 'list', f'--name-only'])
|
|
|
|
|
return [line for line in xinput.decode().split('\n') if line != '']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_devices = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_devices():
|
|
|
|
|
"""Return a mapping of {name: [paths]} for each input device."""
|
|
|
|
|
global _devices
|
|
|
|
|
# this is expensive, do it only once
|
|
|
|
|
if _devices is None:
|
|
|
|
|
_devices = parse_libinput_list()
|
|
|
|
|
logger.info('Found %s', ', '.join([f'"{name}"' for name in _devices]))
|
|
|
|
|
return _devices
|
|
|
|
|