2020-11-22 12:59: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/>.
|
|
|
|
|
|
|
|
|
|
|
|
"""Keeps reading keycodes in the background for the UI to use."""
|
|
|
|
|
|
|
|
|
2020-12-01 23:02:41 +00:00
|
|
|
import sys
|
2020-11-30 13:34:27 +00:00
|
|
|
import select
|
|
|
|
import multiprocessing
|
2020-11-22 12:59:28 +00:00
|
|
|
|
2020-12-01 23:02:41 +00:00
|
|
|
import evdev
|
2020-12-12 14:22:34 +00:00
|
|
|
from evdev.events import EV_KEY, EV_ABS
|
2020-12-01 23:02:41 +00:00
|
|
|
|
2020-11-22 12:59:28 +00:00
|
|
|
from keymapper.logger import logger
|
2020-12-19 15:04:07 +00:00
|
|
|
from keymapper.util import sign
|
2020-11-22 16:30:06 +00:00
|
|
|
from keymapper.getdevices import get_devices, refresh_devices
|
2020-12-02 19:48:23 +00:00
|
|
|
from keymapper.dev.keycode_mapper import should_map_event_as_btn
|
2020-11-22 17:02:55 +00:00
|
|
|
|
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
CLOSE = 1
|
|
|
|
|
2020-12-12 14:22:34 +00:00
|
|
|
PRIORITIES = {
|
|
|
|
EV_KEY: 100,
|
2020-12-27 20:36:14 +00:00
|
|
|
EV_ABS: 50,
|
2020-12-12 14:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def prioritize(events):
|
2020-12-19 15:04:07 +00:00
|
|
|
"""Return the event that is most likely desired to be mapped.
|
|
|
|
|
|
|
|
High absolute values (down) over low values (up), KEY over ABS.
|
|
|
|
"""
|
|
|
|
return sorted(events, key=lambda e: (
|
|
|
|
PRIORITIES[e.type],
|
|
|
|
abs(e.value)
|
|
|
|
))[-1]
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
class _KeycodeReader:
|
2020-11-22 12:59:28 +00:00
|
|
|
"""Keeps reading keycodes in the background for the UI to use.
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
Does not serve any purpose for the injection service.
|
|
|
|
|
2020-11-22 12:59:28 +00:00
|
|
|
When a button was pressed, the newest keycode can be obtained from this
|
|
|
|
object. GTK has get_keycode for keyboard keys, but KeycodeReader also
|
|
|
|
has knowledge of buttons like the middle-mouse button.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.virtual_devices = []
|
2020-11-30 13:34:27 +00:00
|
|
|
self._pipe = None
|
|
|
|
self._process = None
|
2020-12-03 19:54:27 +00:00
|
|
|
self.fail_counter = 0
|
2020-12-12 14:22:34 +00:00
|
|
|
self.newest_event = None
|
2020-12-31 20:46:57 +00:00
|
|
|
# to keep track of combinations.
|
|
|
|
# "I have got this release event, what was this for?"
|
|
|
|
# A release event for a D-Pad axis might be any direction, hence
|
|
|
|
# this maps from release to input in order to remember it.
|
|
|
|
self._unreleased = {}
|
2020-11-30 13:34:27 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.stop_reading()
|
|
|
|
|
|
|
|
def stop_reading(self):
|
2020-12-04 16:38:04 +00:00
|
|
|
"""Stop reading keycodes."""
|
2020-11-30 13:34:27 +00:00
|
|
|
if self._pipe is not None:
|
2020-12-01 22:53:32 +00:00
|
|
|
logger.debug('Sending close msg to reader')
|
|
|
|
self._pipe[0].send(CLOSE)
|
2020-11-30 13:34:27 +00:00
|
|
|
self._pipe = None
|
2020-11-22 12:59:28 +00:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
"""Next time when reading don't return the previous keycode."""
|
2020-11-30 13:34:27 +00:00
|
|
|
# just call read to clear the pipe
|
|
|
|
self.read()
|
2020-12-31 20:46:57 +00:00
|
|
|
self._unreleased = {}
|
2020-11-22 12:59:28 +00:00
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
def start_reading(self, device_name):
|
2020-11-22 12:59:28 +00:00
|
|
|
"""Tell the evdev lib to start looking for keycodes.
|
|
|
|
|
|
|
|
If read is called without prior start_reading, no keycodes
|
|
|
|
will be available.
|
2020-11-22 15:54:40 +00:00
|
|
|
"""
|
2020-11-30 13:34:27 +00:00
|
|
|
self.stop_reading()
|
|
|
|
|
2020-11-22 16:30:06 +00:00
|
|
|
# make sure this sees up to date devices, including those created
|
|
|
|
# by key-mapper
|
|
|
|
refresh_devices()
|
|
|
|
|
|
|
|
self.virtual_devices = []
|
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
for name, group in get_devices().items():
|
2020-11-30 13:34:27 +00:00
|
|
|
if device_name not in name:
|
2020-11-22 16:30:06 +00:00
|
|
|
continue
|
2020-11-22 15:54:40 +00:00
|
|
|
|
|
|
|
# Watch over each one of the potentially multiple devices per
|
|
|
|
# hardware
|
2020-11-22 20:04:09 +00:00
|
|
|
for path in group['paths']:
|
|
|
|
try:
|
2020-11-30 13:34:27 +00:00
|
|
|
device = evdev.InputDevice(path)
|
2020-11-22 20:04:09 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
continue
|
2020-11-22 15:54:40 +00:00
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
if evdev.ecodes.EV_KEY in device.capabilities():
|
|
|
|
self.virtual_devices.append(device)
|
|
|
|
|
2020-11-22 15:54:40 +00:00
|
|
|
logger.debug(
|
|
|
|
'Starting reading keycodes from "%s"',
|
|
|
|
'", "'.join([device.name for device in self.virtual_devices])
|
|
|
|
)
|
2020-11-22 12:59:28 +00:00
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
pipe = multiprocessing.Pipe()
|
2020-11-30 17:59:34 +00:00
|
|
|
self._pipe = pipe
|
2020-12-01 22:53:32 +00:00
|
|
|
self._process = multiprocessing.Process(target=self._read_worker)
|
|
|
|
self._process.start()
|
2020-11-30 13:34:27 +00:00
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
def _consume_event(self, event):
|
2020-11-30 13:34:27 +00:00
|
|
|
"""Write the event code into the pipe if it is a key-down press."""
|
|
|
|
# value: 1 for down, 0 for up, 2 for hold.
|
2020-12-01 22:53:32 +00:00
|
|
|
if self._pipe[1].closed:
|
|
|
|
logger.debug('Pipe closed, reader stops.')
|
2020-12-01 23:02:41 +00:00
|
|
|
sys.exit(0)
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2020-12-27 12:09:28 +00:00
|
|
|
click_events = [
|
|
|
|
evdev.ecodes.BTN_LEFT,
|
|
|
|
evdev.ecodes.BTN_TOOL_DOUBLETAP
|
|
|
|
]
|
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
if event.type == EV_KEY and event.value == 2:
|
|
|
|
# ignore hold-down events
|
|
|
|
return
|
|
|
|
|
2020-12-27 12:09:28 +00:00
|
|
|
if event.type == EV_KEY and event.code in click_events:
|
|
|
|
# disable mapping the left mouse button because it would break
|
|
|
|
# the mouse. Also it is emitted right when focusing the row
|
|
|
|
# which breaks the current workflow.
|
|
|
|
return
|
|
|
|
|
|
|
|
if not should_map_event_as_btn(event.type, event.code):
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.spam(
|
|
|
|
'got (%s, %s, %s)',
|
|
|
|
event.type,
|
|
|
|
event.code,
|
|
|
|
event.value
|
|
|
|
)
|
|
|
|
self._pipe[1].send(event)
|
2020-11-30 13:34:27 +00:00
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
def _read_worker(self):
|
2020-11-30 13:34:27 +00:00
|
|
|
"""Process that reads keycodes and buffers them into a pipe."""
|
|
|
|
# using a process that blocks instead of read_one made it easier
|
|
|
|
# to debug via the logs, because the UI was not polling properly
|
|
|
|
# at some point which caused logs for events not to be written.
|
|
|
|
rlist = {device.fd: device for device in self.virtual_devices}
|
2020-12-01 22:53:32 +00:00
|
|
|
rlist[self._pipe[1]] = self._pipe[1]
|
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
while True:
|
|
|
|
ready = select.select(rlist, [], [])[0]
|
|
|
|
for fd in ready:
|
2020-12-01 22:53:32 +00:00
|
|
|
readable = rlist[fd]
|
|
|
|
if isinstance(readable, multiprocessing.connection.Connection):
|
|
|
|
msg = readable.recv()
|
|
|
|
if msg == CLOSE:
|
|
|
|
logger.debug('Reader stopped')
|
|
|
|
return
|
|
|
|
continue
|
|
|
|
|
2020-11-22 20:04:09 +00:00
|
|
|
try:
|
2020-11-30 13:34:27 +00:00
|
|
|
for event in rlist[fd].read():
|
2020-12-01 22:53:32 +00:00
|
|
|
self._consume_event(event)
|
2020-11-22 20:04:09 +00:00
|
|
|
except OSError:
|
|
|
|
logger.debug(
|
2020-11-30 13:34:27 +00:00
|
|
|
'Device "%s" disappeared from the reader',
|
|
|
|
rlist[fd].path
|
2020-11-22 20:04:09 +00:00
|
|
|
)
|
2020-11-30 13:34:27 +00:00
|
|
|
del rlist[fd]
|
2020-11-22 20:04:09 +00:00
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
def are_keys_pressed(self):
|
|
|
|
"""Check if any keys currently pressed down."""
|
|
|
|
return len(self._unreleased) > 0
|
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
def read(self):
|
2020-12-12 13:12:45 +00:00
|
|
|
"""Get the newest tuple of event type, keycode or None.
|
|
|
|
|
|
|
|
If the timing of two recent events is very close, prioritize
|
|
|
|
key events over abs events.
|
|
|
|
"""
|
2020-11-30 13:34:27 +00:00
|
|
|
if self._pipe is None:
|
2020-12-03 19:54:27 +00:00
|
|
|
self.fail_counter += 1
|
|
|
|
if self.fail_counter % 10 == 0:
|
|
|
|
# spam less
|
|
|
|
logger.debug('No pipe available to read from')
|
2020-12-19 15:04:07 +00:00
|
|
|
return None
|
2020-11-30 13:34:27 +00:00
|
|
|
|
2020-12-12 14:22:34 +00:00
|
|
|
newest_event = self.newest_event
|
|
|
|
newest_time = (
|
|
|
|
0 if newest_event is None
|
|
|
|
else newest_event.sec + newest_event.usec / 1000000
|
|
|
|
)
|
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
while self._pipe[0].poll():
|
2020-12-12 14:22:34 +00:00
|
|
|
event = self._pipe[0].recv()
|
2020-12-31 20:46:57 +00:00
|
|
|
without_value = (event.type, event.code)
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
if event.value == 0:
|
2020-12-31 20:46:57 +00:00
|
|
|
if without_value in self._unreleased:
|
|
|
|
del self._unreleased[without_value]
|
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
continue
|
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
self._unreleased[without_value] = (
|
|
|
|
event.type,
|
|
|
|
event.code,
|
|
|
|
sign(event.value)
|
|
|
|
)
|
|
|
|
|
2020-12-12 14:22:34 +00:00
|
|
|
time = event.sec + event.usec / 1000000
|
|
|
|
delta = time - newest_time
|
|
|
|
|
|
|
|
if delta < 0.01 and prioritize([newest_event, event]) != event:
|
|
|
|
# two events happened very close, probably some weird
|
|
|
|
# spam from the device. The wacom intuos 5 adds an
|
|
|
|
# ABS_MISC event to every button press, filter that out
|
|
|
|
logger.spam(
|
2020-12-19 15:04:07 +00:00
|
|
|
'Ignoring event (%s, %s, %s)',
|
|
|
|
event.type, event.code, event.value
|
2020-12-12 14:22:34 +00:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
newest_event = event
|
|
|
|
newest_time = time
|
|
|
|
|
|
|
|
if newest_event == self.newest_event:
|
|
|
|
# don't return the same event twice
|
2020-12-19 15:04:07 +00:00
|
|
|
return None
|
2020-12-12 14:22:34 +00:00
|
|
|
|
|
|
|
self.newest_event = newest_event
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
if len(self._unreleased) > 1:
|
|
|
|
# a combination
|
|
|
|
return tuple(self._unreleased.values())
|
|
|
|
elif len(self._unreleased) == 1:
|
|
|
|
# a single key
|
|
|
|
return list(self._unreleased.values())[0]
|
|
|
|
else:
|
|
|
|
# nothing
|
|
|
|
return None
|
2020-11-22 12:59:28 +00:00
|
|
|
|
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
keycode_reader = _KeycodeReader()
|