input-remapper/keymapper/daemon.py

490 lines
16 KiB
Python
Raw Normal View History

2020-11-20 20:38:59 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2021-02-22 18:48:20 +00:00
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.de>
2020-11-20 20:38:59 +00:00
#
# 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/>.
"""Starts injecting keycodes based on the configuration.
https://github.com/LEW21/pydbus/tree/cc407c8b1d25b7e28a6d661a29f9e661b1c9b964/examples/clientserver # noqa pylint: disable=line-too-long
"""
2020-11-20 20:38:59 +00:00
import os
2021-03-21 18:15:20 +00:00
import sys
import json
2021-02-07 14:00:36 +00:00
import time
2021-03-21 18:15:20 +00:00
import atexit
2020-11-29 00:53:12 +00:00
from pydbus import SystemBus
2021-03-21 18:15:20 +00:00
import gi
2021-09-26 10:44:56 +00:00
gi.require_version("GLib", "2.0")
from gi.repository import GLib
2020-11-20 20:38:59 +00:00
2021-03-21 18:15:20 +00:00
from keymapper.logger import logger, is_debug
2021-02-13 19:34:33 +00:00
from keymapper.injection.injector import Injector, UNKNOWN
2020-11-20 20:38:59 +00:00
from keymapper.mapping import Mapping
from keymapper.config import config
2021-09-29 18:50:32 +00:00
from keymapper.system_mapping import system_mapping
from keymapper.groups import groups
from keymapper.paths import get_config_path, USER
from keymapper.injection.macros.macro import macro_variables
2020-11-22 20:41:29 +00:00
2021-09-26 10:44:56 +00:00
BUS_NAME = "keymapper.Control"
# timeout in seconds, see
# https://github.com/LEW21/pydbus/blob/cc407c8b1d25b7e28a6d661a29f9e661b1c9b964/pydbus/proxy.py
BUS_TIMEOUT = 10
2021-02-07 14:00:36 +00:00
class AutoloadHistory:
"""Contains the autoloading history and constraints."""
2021-09-26 10:44:56 +00:00
2021-02-07 14:00:36 +00:00
def __init__(self):
"""Construct this with an empty history."""
# mapping of device -> (timestamp, preset)
self._autoload_history = {}
def remember(self, group_key, preset):
2021-02-07 14:00:36 +00:00
"""Remember when this preset was autoloaded for the device."""
self._autoload_history[group_key] = (time.time(), preset)
2021-02-07 14:00:36 +00:00
def forget(self, group_key):
2021-02-07 14:00:36 +00:00
"""The injection was stopped or started by hand."""
if group_key in self._autoload_history:
del self._autoload_history[group_key]
2021-02-07 14:00:36 +00:00
def may_autoload(self, group_key, preset):
2021-02-07 14:00:36 +00:00
"""Check if this autoload would be redundant.
This is needed because udev triggers multiple times per hardware
device, and because it should be possible to stop the injection
by unplugging the device if the preset goes wrong or if key-mapper
has some bug that prevents the computer from being controlled.
For that unplug and reconnect the device twice within a 15 seconds
timeframe which will then not ask for autoloading again. Wait 3
seconds between replugging.
"""
if group_key not in self._autoload_history:
2021-02-07 14:00:36 +00:00
return True
if self._autoload_history[group_key][1] != preset:
2021-02-07 14:00:36 +00:00
return True
# bluetooth devices go to standby mode after some time. After a
# certain time of being disconnected it should be legit to autoload
# again. It takes 2.5 seconds for me when quickly replugging my usb
# mouse until the daemon is asked to autoload again. Redundant calls
# by udev to autoload for the device seem to happen within 0.2
# seconds in my case.
now = time.time()
threshold = 15 # seconds
if self._autoload_history[group_key][0] < now - threshold:
2021-02-07 14:00:36 +00:00
return True
return False
def remove_timeout(func):
"""Remove timeout to ensure the call works if the daemon is not a proxy."""
# the timeout kwarg is a feature of pydbus. This is needed to make tests work
# that create a Daemon by calling its constructor instead of using pydbus.
def wrapped(*args, **kwargs):
if "timeout" in kwargs:
del kwargs["timeout"]
return func(*args, **kwargs)
return wrapped
class Daemon:
2020-11-22 20:04:09 +00:00
"""Starts injecting keycodes based on the configuration.
Can be talked to either over dbus or by instantiating it.
2020-11-25 20:55:04 +00:00
The Daemon may not have any knowledge about the logged in user, so it
can't read any config files. It has to be told what to do and will
continue to do so afterwards, but it can't decide to start injecting
on its own.
2020-11-22 20:04:09 +00:00
"""
2021-02-07 14:00:36 +00:00
# https://dbus.freedesktop.org/doc/dbus-specification.html#type-system
dbus = f"""
<node>
<interface name='{BUS_NAME}'>
<method name='stop_injecting'>
<arg type='s' name='group_key' direction='in'/>
</method>
2021-01-07 16:15:12 +00:00
<method name='get_state'>
<arg type='s' name='group_key' direction='in'/>
2021-01-07 16:15:12 +00:00
<arg type='i' name='response' direction='out'/>
2020-12-02 17:07:46 +00:00
</method>
<method name='start_injecting'>
<arg type='s' name='group_key' direction='in'/>
2021-02-07 14:00:36 +00:00
<arg type='s' name='preset' direction='in'/>
<arg type='b' name='response' direction='out'/>
</method>
2021-02-07 14:00:36 +00:00
<method name='stop_all'>
</method>
<method name='set_config_dir'>
<arg type='s' name='config_dir' direction='in'/>
</method>
<method name='autoload'>
</method>
<method name='autoload_single'>
<arg type='s' name='group_key' direction='in'/>
</method>
<method name='hello'>
<arg type='s' name='out' direction='in'/>
<arg type='s' name='response' direction='out'/>
</method>
</interface>
</node>
"""
def __init__(self):
"""Constructs the daemon."""
2021-09-26 10:44:56 +00:00
logger.debug("Creating daemon")
2020-11-20 20:38:59 +00:00
self.injectors = {}
2021-02-07 14:00:36 +00:00
self.config_dir = None
if USER != "root":
self.set_config_dir(get_config_path())
# check privileges
if os.getuid() != 0:
logger.warning("The service usually needs elevated privileges")
2021-02-07 14:00:36 +00:00
self.autoload_history = AutoloadHistory()
2021-02-12 19:29:26 +00:00
self.refreshed_devices_at = 0
2021-03-21 18:15:20 +00:00
atexit.register(self.stop_all)
# initialize stuff that is needed alongside the daemon process
macro_variables.start()
2021-03-21 18:15:20 +00:00
@classmethod
def connect(cls, fallback=True):
"""Get an interface to start and stop injecting keystrokes.
Parameters
----------
fallback : bool
If true, returns an instance of the daemon instead if it cannot
connect
"""
try:
bus = SystemBus()
interface = bus.get(BUS_NAME, timeout=BUS_TIMEOUT)
2021-09-26 10:44:56 +00:00
logger.info("Connected to the service")
2021-03-21 18:15:20 +00:00
except GLib.GError as error:
if not fallback:
2021-09-26 10:44:56 +00:00
logger.error("Service not running? %s", error)
2021-03-21 18:15:20 +00:00
return None
2021-09-26 10:44:56 +00:00
logger.info("Starting the service")
2021-03-21 18:15:20 +00:00
# Blocks until pkexec is done asking for the password.
# Runs via key-mapper-control so that auth_admin_keep works
# for all pkexec calls of the gui
2021-09-26 10:44:56 +00:00
debug = " -d" if is_debug() else ""
cmd = f"pkexec key-mapper-control --command start-daemon {debug}"
2021-03-21 18:15:20 +00:00
# using pkexec will also cause the service to continue running in
# the background after the gui has been closed, which will keep
# the injections ongoing
2021-09-26 10:44:56 +00:00
logger.debug("Running `%s`", cmd)
2021-03-21 18:15:20 +00:00
os.system(cmd)
time.sleep(0.2)
# try a few times if the service was just started
for attempt in range(3):
try:
interface = bus.get(BUS_NAME, timeout=BUS_TIMEOUT)
2021-03-21 18:15:20 +00:00
break
except GLib.GError as error:
logger.debug(
'Attempt %d to connect to the service failed: "%s"',
2021-09-26 10:44:56 +00:00
attempt + 1,
error,
2021-03-21 18:15:20 +00:00
)
time.sleep(0.2)
else:
2021-09-26 10:44:56 +00:00
logger.error("Failed to connect to the service")
2021-03-21 18:15:20 +00:00
sys.exit(1)
if USER != "root":
config_path = get_config_path()
logger.debug('Telling service about "%s"', config_path)
2021-11-21 21:13:10 +00:00
interface.set_config_dir(get_config_path(), timeout=2)
2021-03-21 18:15:20 +00:00
return interface
def publish(self):
"""Make the dbus interface available."""
bus = SystemBus()
try:
bus.publish(BUS_NAME, self)
except RuntimeError as error:
2021-09-26 10:44:56 +00:00
logger.error("Is the service already running? (%s)", str(error))
2021-03-21 18:15:20 +00:00
sys.exit(1)
def run(self):
"""Start the daemons loop. Blocks until the daemon stops."""
loop = GLib.MainLoop()
2021-09-26 10:44:56 +00:00
logger.debug("Running daemon")
2021-03-21 18:15:20 +00:00
loop.run()
def refresh(self, group_key=None):
"""Refresh groups if the specified group is unknown.
Parameters
----------
group_key : str
unique identifier used by the groups object
"""
2021-02-12 19:29:26 +00:00
now = time.time()
if now - 10 > self.refreshed_devices_at:
2021-09-26 10:44:56 +00:00
logger.debug("Refreshing because last info is too old")
# it may take a little bit of time until devices are visible after
# changes
time.sleep(0.1)
groups.refresh()
2021-02-12 19:29:26 +00:00
self.refreshed_devices_at = now
return
if not groups.find(key=group_key):
logger.debug('Refreshing because "%s" is unknown', group_key)
time.sleep(0.1)
groups.refresh()
self.refreshed_devices_at = now
2020-11-20 20:38:59 +00:00
def stop_injecting(self, group_key):
2020-11-20 20:38:59 +00:00
"""Stop injecting the mapping for a single device."""
if self.injectors.get(group_key) is None:
2021-01-07 16:15:12 +00:00
logger.debug(
2021-09-29 18:17:45 +00:00
'Tried to stop injector, but none is running for group "%s"', group_key
2020-11-22 20:04:09 +00:00
)
2020-11-20 20:38:59 +00:00
return
self.injectors[group_key].stop_injecting()
self.autoload_history.forget(group_key)
2020-12-02 17:07:46 +00:00
def get_state(self, group_key):
2021-01-07 16:15:12 +00:00
"""Get the injectors state."""
injector = self.injectors.get(group_key)
2021-01-07 16:15:12 +00:00
return injector.get_state() if injector else UNKNOWN
2020-11-20 20:38:59 +00:00
2021-11-21 21:13:10 +00:00
@remove_timeout
2021-02-07 14:00:36 +00:00
def set_config_dir(self, config_dir):
"""All future operations will use this config dir.
Existing injections (possibly of the previous user) will be kept
alive, call stop_all to stop them.
Parameters
----------
config_dir : string
This path contains config.json, xmodmap.json and the
presets directory
"""
2021-09-26 10:44:56 +00:00
config_path = os.path.join(config_dir, "config.json")
2021-02-07 14:00:36 +00:00
if not os.path.exists(config_path):
logger.error('"%s" does not exist', config_path)
return
self.config_dir = config_dir
config.load_config(config_path)
def _autoload(self, group_key):
2021-02-07 14:00:36 +00:00
"""Check if autoloading is a good idea, and if so do it.
Parameters
----------
group_key : str
unique identifier used by the groups object
2021-02-07 14:00:36 +00:00
"""
self.refresh(group_key)
2021-02-12 19:29:26 +00:00
group = groups.find(key=group_key)
if group is None:
# even after groups.refresh, the device is unknown, so it's
# either not relevant for key-mapper, or not connected yet
2021-02-12 19:29:26 +00:00
return
2021-09-26 10:44:56 +00:00
preset = config.get(["autoload", group.key], log_unknown=False)
2021-02-07 14:00:36 +00:00
if preset is None:
# no autoloading is configured for this device
return
if not isinstance(preset, str):
# might be broken due to a previous bug
2021-09-26 10:44:56 +00:00
config.remove(["autoload", group.key])
2021-02-07 14:00:36 +00:00
config.save_config()
return
logger.info('Autoloading for "%s"', group.key)
2021-02-07 14:00:36 +00:00
if not self.autoload_history.may_autoload(group.key, preset):
2021-02-07 14:00:36 +00:00
logger.info(
'Not autoloading the same preset "%s" again for group "%s"',
2021-09-26 10:44:56 +00:00
preset,
group.key,
2021-02-07 14:00:36 +00:00
)
return
self.start_injecting(group.key, preset)
self.autoload_history.remember(group.key, preset)
2021-02-07 14:00:36 +00:00
@remove_timeout
def autoload_single(self, group_key):
2021-02-07 14:00:36 +00:00
"""Inject the configured autoload preset for the device.
If the preset is already being injected, it won't autoload it again.
Parameters
----------
group_key : str
unique identifier used by the groups object
2021-02-07 14:00:36 +00:00
"""
# avoid some confusing logs and filter obviously invalid requests
2021-09-26 10:44:56 +00:00
if group_key.startswith("key-mapper"):
return
logger.info('Request to autoload for "%s"', group_key)
2021-02-12 19:29:26 +00:00
2021-02-07 14:00:36 +00:00
if self.config_dir is None:
2021-11-21 20:57:51 +00:00
logger.error(
'Request to autoload "%s" before a user told the service about their '
"session using set_config_dir",
2021-09-26 10:44:56 +00:00
group_key,
2021-02-07 14:00:36 +00:00
)
return
self._autoload(group_key)
2021-02-07 14:00:36 +00:00
@remove_timeout
2021-02-07 14:00:36 +00:00
def autoload(self):
"""Load all autoloaded presets for the current config_dir.
If the preset is already being injected, it won't autoload it again.
"""
if self.config_dir is None:
logger.error(
2021-11-21 20:57:51 +00:00
"Request to autoload all before a user told the service about their "
"session using set_config_dir",
2021-02-07 14:00:36 +00:00
)
return
autoload_presets = list(config.iterate_autoload_presets())
2021-09-26 10:44:56 +00:00
logger.info("Autoloading for all devices")
2021-02-07 14:00:36 +00:00
if len(autoload_presets) == 0:
2021-09-26 10:44:56 +00:00
logger.error("No presets configured to autoload")
2021-02-07 14:00:36 +00:00
return
2021-04-29 20:22:05 +00:00
for group_key, _ in autoload_presets:
self._autoload(group_key)
2021-02-07 14:00:36 +00:00
def start_injecting(self, group_key, preset):
"""Start injecting the preset for the device.
2020-11-22 20:04:09 +00:00
2021-02-07 14:00:36 +00:00
Returns True on success. If an injection is already ongoing for
the specified device it will stop it automatically first.
Parameters
----------
group_key : string
The unique key of the group
2021-02-07 14:00:36 +00:00
preset : string
The name of the preset
2020-11-22 20:04:09 +00:00
"""
self.refresh(group_key)
2021-02-07 14:00:36 +00:00
if self.config_dir is None:
logger.error(
2021-11-21 20:57:51 +00:00
"Request to start an injectoin before a user told the service about "
"their session using set_config_dir",
2021-02-07 14:00:36 +00:00
)
2021-03-21 18:15:20 +00:00
return False
group = groups.find(key=group_key)
if group is None:
logger.error('Could not find group "%s"', group_key)
2021-03-21 18:15:20 +00:00
return False
2021-02-07 14:00:36 +00:00
preset_path = os.path.join(
2021-09-26 10:44:56 +00:00
self.config_dir, "presets", group.name, f"{preset}.json"
2021-02-07 14:00:36 +00:00
)
2020-11-20 20:38:59 +00:00
mapping = Mapping()
try:
mapping.load(preset_path)
except FileNotFoundError as error:
logger.error(str(error))
2020-12-31 20:55:38 +00:00
return False
if self.injectors.get(group_key) is not None:
self.stop_injecting(group_key)
2021-02-07 14:00:36 +00:00
# Path to a dump of the xkb mappings, to provide more human
# readable keys in the correct keyboard layout to the service.
# The service cannot use `xmodmap -pke` because it's running via
# systemd.
2021-09-26 10:44:56 +00:00
xmodmap_path = os.path.join(self.config_dir, "xmodmap.json")
2021-02-07 14:00:36 +00:00
try:
2021-09-26 10:44:56 +00:00
with open(xmodmap_path, "r") as file:
2021-02-07 14:00:36 +00:00
# do this for each injection to make sure it is up to
# date when the system layout changes.
xmodmap = json.load(file)
logger.debug('Using keycodes from "%s"', xmodmap_path)
system_mapping.update(xmodmap)
# the service now has process wide knowledge of xmodmap
# keys of the users session
except FileNotFoundError:
logger.error('Could not find "%s"', xmodmap_path)
2020-11-22 20:04:09 +00:00
try:
injector = Injector(group, mapping)
injector.start()
self.injectors[group.key] = injector
2020-11-22 20:04:09 +00:00
except OSError:
2021-01-07 16:15:12 +00:00
# I think this will never happen, probably leftover from
# some earlier version
2020-11-22 20:04:09 +00:00
return False
return True
2020-11-20 20:38:59 +00:00
2021-02-07 14:00:36 +00:00
def stop_all(self):
2021-01-09 16:44:24 +00:00
"""Stop all injections."""
2021-09-26 10:44:56 +00:00
logger.info("Stopping all injections")
for group_key in list(self.injectors.keys()):
self.stop_injecting(group_key)
def hello(self, out):
"""Used for tests."""
logger.info('Received "%s" from client', out)
return out