2020-11-20 20:38:59 +00:00
|
|
|
#!/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>
|
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/>.
|
|
|
|
|
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
"""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
|
|
|
|
|
|
|
|
2021-01-04 19:50:05 +00:00
|
|
|
import os
|
2020-11-29 00:53:12 +00:00
|
|
|
import subprocess
|
2020-12-24 00:26:34 +00:00
|
|
|
import json
|
2020-11-29 00:53:12 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
from pydbus import SystemBus
|
2020-12-04 16:45:51 +00:00
|
|
|
from gi.repository import GLib
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
from keymapper.logger import logger
|
2021-01-04 19:50:05 +00:00
|
|
|
from keymapper.dev.injector import Injector
|
2020-11-20 20:38:59 +00:00
|
|
|
from keymapper.mapping import Mapping
|
2020-11-25 22:36:03 +00:00
|
|
|
from keymapper.config import config
|
2020-12-24 00:26:34 +00:00
|
|
|
from keymapper.state import system_mapping
|
|
|
|
from keymapper.getdevices import get_devices, refresh_devices
|
2020-11-22 20:41:29 +00:00
|
|
|
|
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
BUS_NAME = 'keymapper.Control'
|
|
|
|
|
|
|
|
|
2020-11-29 00:53:12 +00:00
|
|
|
def is_service_running():
|
|
|
|
"""Check if the daemon is running."""
|
|
|
|
try:
|
|
|
|
subprocess.check_output(['pgrep', '-f', 'key-mapper-service'])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def get_dbus_interface(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
|
|
|
|
"""
|
2020-12-04 16:45:51 +00:00
|
|
|
msg = (
|
|
|
|
'The daemon "key-mapper-service" is not running, mapping keys '
|
2020-12-31 20:46:57 +00:00
|
|
|
'only works as long as the window is open. '
|
|
|
|
'Try `sudo systemctl start key-mapper`'
|
2020-12-04 16:45:51 +00:00
|
|
|
)
|
|
|
|
|
2020-11-29 00:53:12 +00:00
|
|
|
if not is_service_running():
|
2020-12-24 00:26:34 +00:00
|
|
|
if not fallback:
|
|
|
|
logger.error('Service not running')
|
|
|
|
return None
|
|
|
|
|
2020-12-04 16:45:51 +00:00
|
|
|
logger.warning(msg)
|
2020-12-04 14:31:32 +00:00
|
|
|
return Daemon()
|
2020-11-29 00:53:12 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
bus = SystemBus()
|
2020-12-04 16:45:51 +00:00
|
|
|
try:
|
|
|
|
interface = bus.get(BUS_NAME)
|
|
|
|
except GLib.GError as error:
|
|
|
|
logger.debug(error)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
if not fallback:
|
|
|
|
logger.error('Failed to connect to the running service')
|
|
|
|
return None
|
|
|
|
|
2020-12-04 16:45:51 +00:00
|
|
|
logger.warning(msg)
|
|
|
|
return Daemon()
|
2020-11-23 22:42:23 +00:00
|
|
|
|
2020-11-22 20:41:29 +00:00
|
|
|
return interface
|
2020-11-22 20:04:09 +00:00
|
|
|
|
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
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
|
|
|
"""
|
2020-12-02 13:36:17 +00:00
|
|
|
|
|
|
|
dbus = f"""
|
|
|
|
<node>
|
|
|
|
<interface name='{BUS_NAME}'>
|
|
|
|
<method name='stop_injecting'>
|
|
|
|
<arg type='s' name='device' direction='in'/>
|
|
|
|
</method>
|
2020-12-02 17:07:46 +00:00
|
|
|
<method name='is_injecting'>
|
|
|
|
<arg type='s' name='device' direction='in'/>
|
|
|
|
<arg type='b' name='response' direction='out'/>
|
|
|
|
</method>
|
2020-12-02 13:36:17 +00:00
|
|
|
<method name='start_injecting'>
|
|
|
|
<arg type='s' name='device' direction='in'/>
|
2020-12-24 00:26:34 +00:00
|
|
|
<arg type='s' name='path' direction='in'/>
|
|
|
|
<arg type='s' name='xmodmap_path' direction='in'/>
|
2020-12-02 13:36:17 +00:00
|
|
|
<arg type='b' name='response' direction='out'/>
|
|
|
|
</method>
|
|
|
|
<method name='stop'>
|
|
|
|
</method>
|
|
|
|
<method name='hello'>
|
|
|
|
<arg type='s' name='out' direction='in'/>
|
|
|
|
<arg type='s' name='response' direction='out'/>
|
|
|
|
</method>
|
|
|
|
</interface>
|
|
|
|
</node>
|
|
|
|
"""
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Constructs the daemon."""
|
2020-12-02 13:36:17 +00:00
|
|
|
logger.debug('Creating daemon')
|
2020-11-20 20:38:59 +00:00
|
|
|
self.injectors = {}
|
|
|
|
|
|
|
|
def stop_injecting(self, device):
|
|
|
|
"""Stop injecting the mapping for a single device."""
|
|
|
|
if self.injectors.get(device) is None:
|
2020-11-22 20:04:09 +00:00
|
|
|
logger.error(
|
|
|
|
'Tried to stop injector, but none is running for device "%s"',
|
|
|
|
device
|
|
|
|
)
|
2020-11-20 20:38:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.injectors[device].stop_injecting()
|
2020-12-02 17:07:46 +00:00
|
|
|
del self.injectors[device]
|
|
|
|
|
|
|
|
def is_injecting(self, device):
|
|
|
|
"""Is this device being mapped?"""
|
|
|
|
return device in self.injectors
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2021-01-04 19:50:05 +00:00
|
|
|
def start_injecting(self, device, preset_path, config_dir=None):
|
2020-11-25 22:36:03 +00:00
|
|
|
"""Start injecting the preset for the device.
|
2020-11-22 20:04:09 +00:00
|
|
|
|
|
|
|
Returns True on success.
|
2020-11-24 23:23:34 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
device : string
|
|
|
|
The name of the device
|
2021-01-04 19:50:05 +00:00
|
|
|
preset_path : string
|
2020-12-24 00:26:34 +00:00
|
|
|
Path to the preset. The daemon, if started via systemctl, has no
|
|
|
|
knowledge of the user and their home path, so the complete
|
|
|
|
absolute path needs to be provided here.
|
2021-01-04 19:50:05 +00:00
|
|
|
config_dir : string
|
|
|
|
Contains xmodmap.json and config.json of the current users session
|
2020-11-22 20:04:09 +00:00
|
|
|
"""
|
2021-01-04 19:50:05 +00:00
|
|
|
# reload the config, since it may have been changed
|
|
|
|
if config_dir is not None:
|
|
|
|
config_path = os.path.join(config_dir, 'config.json')
|
|
|
|
config.load_config(config_path)
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if device not in get_devices():
|
|
|
|
logger.debug('Devices possibly outdated, refreshing')
|
|
|
|
refresh_devices()
|
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
if self.injectors.get(device) is not None:
|
|
|
|
self.injectors[device].stop_injecting()
|
|
|
|
|
|
|
|
mapping = Mapping()
|
2020-12-24 00:26:34 +00:00
|
|
|
try:
|
2021-01-04 19:50:05 +00:00
|
|
|
mapping.load(preset_path)
|
2020-12-24 00:26:34 +00:00
|
|
|
except FileNotFoundError as error:
|
|
|
|
logger.error(str(error))
|
2020-12-31 20:55:38 +00:00
|
|
|
return False
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-01-04 19:50:05 +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.
|
|
|
|
if config_dir is not None:
|
|
|
|
xmodmap_path = os.path.join(config_dir, 'xmodmap.json')
|
2020-12-24 00:26:34 +00:00
|
|
|
try:
|
|
|
|
with open(xmodmap_path, 'r') as file:
|
|
|
|
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:
|
2021-01-04 19:50:05 +00:00
|
|
|
injector = Injector(device, mapping)
|
2020-11-28 14:43:24 +00:00
|
|
|
injector.start_injecting()
|
|
|
|
self.injectors[device] = injector
|
2020-11-22 20:04:09 +00:00
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def stop(self):
|
2020-12-01 22:53:32 +00:00
|
|
|
"""Stop all injections and end the service.
|
|
|
|
|
|
|
|
Raises dbus.exceptions.DBusException in your main process.
|
|
|
|
"""
|
2020-12-24 00:26:34 +00:00
|
|
|
logger.info('Stopping all injections')
|
2020-11-20 20:38:59 +00:00
|
|
|
for injector in self.injectors.values():
|
|
|
|
injector.stop_injecting()
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
def hello(self, out):
|
|
|
|
"""Used for tests."""
|
2020-12-24 00:26:34 +00:00
|
|
|
logger.info('Received "%s" from client', out)
|
2020-12-02 13:36:17 +00:00
|
|
|
return out
|