input-remapper/keymapper/dev/reader.py

357 lines
12 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2021-01-02 23:08:33 +00:00
# Copyright (C) 2021 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
2021-01-07 16:15:12 +00:00
import time
2020-11-30 13:34:27 +00:00
import select
import multiprocessing
2021-01-01 21:20:33 +00:00
import threading
2020-12-01 23:02:41 +00:00
import evdev
2021-01-07 16:15:12 +00:00
from evdev.ecodes import EV_KEY, EV_ABS, ABS_MISC, EV_REL
2020-12-01 23:02:41 +00:00
from keymapper.logger import logger
2020-12-31 20:47:56 +00:00
from keymapper.key import Key
2021-01-01 21:20:33 +00:00
from keymapper.state import custom_mapping
2021-01-07 16:15:12 +00:00
from keymapper.getdevices import get_devices
from keymapper.dev import utils
2020-11-22 17:02:55 +00:00
CLOSE = 1
PRIORITIES = {
EV_KEY: 100,
2020-12-27 20:36:14 +00:00
EV_ABS: 50,
}
FILTER_THRESHOLD = 0.01
2021-01-07 16:15:12 +00:00
DEBOUNCE_TICKS = 3
def prioritize(events):
"""Return the event that is most likely desired to be mapped.
KEY over ABS and everything over ABS_MISC.
"""
events = [
event for event in events
if event is not None
]
return sorted(events, key=lambda e: (
2021-01-05 18:33:47 +00:00
PRIORITIES.get(e.type, 0),
not (e.type == EV_ABS and e.code == ABS_MISC),
abs(e.value)
))[-1]
2021-01-07 16:15:12 +00:00
def will_report_up(ev_type):
"""Check if this event will ever report a key up (wheels)."""
return ev_type != EV_REL
def event_unix_time(event):
"""Get the unix timestamp of an event."""
if event is None:
return 0
return event.sec + event.usec / 1000000
2020-11-22 14:14:43 +00:00
class _KeycodeReader:
"""Keeps reading keycodes in the background for the UI to use.
Does not serve any purpose for the injection service.
When a button was pressed, the newest keycode can be obtained from this
2020-12-31 20:47:56 +00:00
object. GTK has get_key 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
self.fail_counter = 0
2021-01-07 16:15:12 +00:00
self.previous_event = None
self.previous_result = None
2020-12-31 20:46:57 +00:00
self._unreleased = {}
2021-01-07 16:15:12 +00:00
self._debounce_remove = {}
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:
logger.debug('Sending close msg to reader')
self._pipe[0].send(CLOSE)
2020-11-30 13:34:27 +00:00
self._pipe = None
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 = {}
2021-01-07 16:15:12 +00:00
self.previous_event = None
self.previous_result = None
2020-11-30 13:34:27 +00:00
def start_reading(self, device_name):
"""Tell the evdev lib to start looking for keycodes.
If read is called without prior start_reading, no keycodes
will be available.
"""
2021-01-07 16:15:12 +00:00
if self._pipe is not None:
self.stop_reading()
time.sleep(0.1)
self.virtual_devices = []
for name, group in get_devices().items():
2020-11-30 13:34:27 +00:00
if device_name not in name:
continue
# 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-30 13:34:27 +00:00
if evdev.ecodes.EV_KEY in device.capabilities():
self.virtual_devices.append(device)
logger.debug(
'Starting reading keycodes from "%s"',
'", "'.join([device.name for device in self.virtual_devices])
)
2020-11-30 13:34:27 +00:00
pipe = multiprocessing.Pipe()
2020-11-30 17:59:34 +00:00
self._pipe = pipe
2021-01-01 21:20:33 +00:00
self._process = threading.Thread(target=self._read_worker)
self._process.start()
2020-11-30 13:34:27 +00:00
2021-01-07 16:15:12 +00:00
def _pipe_event(self, event, device):
"""Write the event into the pipe to the main process."""
2020-11-30 13:34:27 +00:00
# value: 1 for down, 0 for up, 2 for hold.
2021-01-01 21:20:33 +00:00
if self._pipe is None or self._pipe[1].closed:
logger.debug('Pipe closed, reader stops.')
2020-12-01 23:02:41 +00:00
sys.exit(0)
2021-01-07 16:15:12 +00:00
if event.type == EV_KEY and event.value == 2:
# ignore hold-down events
return
click_events = [
evdev.ecodes.BTN_LEFT,
evdev.ecodes.BTN_TOOL_DOUBLETAP
]
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 utils.should_map_event_as_btn(device, event, custom_mapping):
return
self._pipe[1].send(event)
2020-11-30 13:34:27 +00:00
def _read_worker(self):
2021-01-01 21:20:33 +00:00
"""Thread that reads keycodes and buffers them into a pipe."""
# using a thread that blocks instead of read_one made it easier
2020-11-30 13:34:27 +00:00
# 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}
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:
2021-01-01 21:20:33 +00:00
readable = rlist[fd] # a device or a pipe
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():
2021-01-07 16:15:12 +00:00
self._pipe_event(event, readable)
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
2021-01-07 16:15:12 +00:00
def get_unreleased_keys(self):
"""Get a Key object of the current keyboard state."""
unreleased = list(self._unreleased.values())
if len(unreleased) == 0:
return None
return Key(*unreleased)
def _release(self, type_code):
"""Modify the state to recognize the releasing of the key."""
if type_code in self._unreleased:
del self._unreleased[type_code]
if type_code in self._debounce_remove:
del self._debounce_remove[type_code]
def _debounce_start(self, event_tuple):
"""Act like the key was released if no new event arrives in time."""
if not will_report_up(event_tuple[0]):
self._debounce_remove[event_tuple[:2]] = DEBOUNCE_TICKS
def _debounce_tick(self):
"""If the counter reaches 0, the key is not considered held down."""
for type_code in list(self._debounce_remove.keys()):
if type_code not in self._unreleased:
continue
# clear wheel events from unreleased after some time
if self._debounce_remove[type_code] == 0:
logger.key_spam(
self._unreleased[type_code],
'Considered as released'
)
self._release(type_code)
else:
self._debounce_remove[type_code] -= 1
2020-12-31 20:46:57 +00:00
2020-11-30 13:34:27 +00:00
def read(self):
2021-01-07 16:15:12 +00:00
"""Get the newest key/combination as Key object.
Only reports keys from down-events.
On key-down events the pipe returns changed combinations. Release
events won't cause that and the reader will return None as in
"nothing new to report". So In order to change a combination, one
of its keys has to be released and then a different one pressed.
Otherwise making combinations wouldn't be possible. Because at
some point the keys have to be released, and that shouldn't cause
the combination to get trimmed.
2020-12-12 13:12:45 +00:00
If the timing of two recent events is very close, prioritize
key events over abs events.
"""
2021-01-07 16:15:12 +00:00
# this is in some ways similar to the keycode_mapper and
# event_producer, but its much simpler because it doesn't
# have to trigger anything, manage any macros and only
# reports key-down events. This function is called periodically
# by the window.
2020-11-30 13:34:27 +00:00
if self._pipe is None:
self.fail_counter += 1
2021-01-07 16:15:12 +00:00
if self.fail_counter % 10 == 0: # spam less
logger.debug('No pipe available to read from')
return None
2020-11-30 13:34:27 +00:00
2021-01-07 16:15:12 +00:00
# remember the prevous down-event from the pipe in order to
# be able to prioritize events, and to be able to tell if the reader
# should return the updated combination
previous_event = self.previous_event
key_down_received = False
self._debounce_tick()
2020-11-30 17:59:34 +00:00
while self._pipe[0].poll():
2021-01-07 16:15:12 +00:00
# loop over all new and unhandled events
event = self._pipe[0].recv()
2021-01-05 18:33:47 +00:00
event_tuple = (event.type, event.code, event.value)
2021-01-07 16:15:12 +00:00
type_code = (event.type, event.code)
if event.value == 0:
2021-01-07 16:15:12 +00:00
logger.key_spam(event_tuple, 'release')
self._release(type_code)
2020-12-31 22:16:46 +00:00
continue
2020-12-31 20:46:57 +00:00
2021-01-07 16:15:12 +00:00
key_down_received = True
2021-01-07 16:15:12 +00:00
if self._unreleased.get(type_code) == event_tuple:
if event.type != EV_ABS: # spams a lot
logger.key_spam(event_tuple, 'duplicate key down')
self._debounce_start(event_tuple)
continue
2021-01-07 16:15:12 +00:00
delta = event_unix_time(event) - event_unix_time(previous_event)
if delta < FILTER_THRESHOLD:
2021-01-07 16:15:12 +00:00
if prioritize([previous_event, event]) == previous_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
2021-01-07 16:15:12 +00:00
logger.key_spam(event_tuple, 'ignoring new event')
continue
2021-01-07 16:15:12 +00:00
# the previous event of the previous iteration is ignored.
# clean stuff up to remove its side effects
prev_tuple = (
previous_event.type,
previous_event.code,
previous_event.value
)
if prev_tuple[:2] in self._unreleased:
logger.key_spam(prev_tuple, 'ignoring previous event')
self._release(prev_tuple[:2])
# 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. Since all release
# events have value 0, the value is not used in the key.
logger.key_spam(event_tuple, 'down')
self._unreleased[type_code] = event_tuple
self._debounce_start(event_tuple)
previous_event = event
if not key_down_received:
# This prevents writing a subset of the combination into
# result after keys were released. In order to control the gui,
# they have to be released.
return None
2021-01-07 16:15:12 +00:00
self.previous_event = previous_event
2020-11-22 14:14:43 +00:00
2020-12-31 20:47:56 +00:00
if len(self._unreleased) > 0:
2021-01-07 16:15:12 +00:00
result = Key(*self._unreleased.values())
if result == self.previous_result:
# don't return the same stuff twice
return None
self.previous_result = result
logger.key_spam(result.keys, 'read result')
return result
2020-12-31 20:55:38 +00:00
return None
2020-11-22 14:14:43 +00:00
keycode_reader = _KeycodeReader()