2020-12-24 00:26:34 +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) 2020 sezanzeb <proxima@sezanzeb.de>
|
2020-12-24 00:26:34 +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/>.
|
|
|
|
|
|
|
|
|
|
|
|
"""Control the dbus service from the command line."""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import grp
|
|
|
|
import sys
|
2021-03-21 18:15:20 +00:00
|
|
|
import argparse
|
|
|
|
import subprocess
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
from keymapper.logger import logger, update_verbosity, log_info
|
2020-12-24 00:26:34 +00:00
|
|
|
from keymapper.config import config
|
2021-03-21 18:15:20 +00:00
|
|
|
from keymapper.daemon import Daemon
|
2021-01-09 16:44:24 +00:00
|
|
|
from keymapper.state import system_mapping
|
2020-12-24 00:26:34 +00:00
|
|
|
from keymapper.getdevices import get_devices
|
2021-02-07 14:00:36 +00:00
|
|
|
from keymapper.paths import USER
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
AUTOLOAD = 'autoload'
|
|
|
|
START = 'start'
|
|
|
|
STOP = 'stop'
|
2021-01-09 16:44:24 +00:00
|
|
|
STOP_ALL = 'stop-all'
|
2020-12-24 00:26:34 +00:00
|
|
|
HELLO = 'hello'
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
# internal stuff that the gui uses
|
|
|
|
START_DAEMON = 'start-daemon'
|
|
|
|
HELPER = 'helper'
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
def run(cmd):
|
|
|
|
"""Run and log a command."""
|
|
|
|
logger.info('Running `%s`...', cmd)
|
|
|
|
code = os.system(cmd)
|
|
|
|
if code != 0:
|
|
|
|
logger.error('Failed. exit code %d', code)
|
|
|
|
|
|
|
|
|
|
|
|
def group_exists(name):
|
|
|
|
"""Check if a group with that name exists."""
|
|
|
|
try:
|
|
|
|
grp.getgrnam(name)
|
|
|
|
return True
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
COMMANDS = [AUTOLOAD, START, STOP, HELLO, STOP_ALL]
|
|
|
|
|
|
|
|
INTERNALS = [START_DAEMON, HELPER]
|
|
|
|
|
|
|
|
|
2021-01-09 16:44:24 +00:00
|
|
|
def main(options, daemon):
|
2021-03-21 18:15:20 +00:00
|
|
|
"""Do the stuff that the executable is supposed to do.
|
|
|
|
|
|
|
|
Is a function so that I can import it and test it
|
|
|
|
"""
|
2021-01-09 16:44:24 +00:00
|
|
|
if options.config_dir is not None:
|
|
|
|
path = os.path.abspath(os.path.expanduser(os.path.join(
|
|
|
|
options.config_dir,
|
|
|
|
'config.json'
|
|
|
|
)))
|
|
|
|
if not os.path.exists(path):
|
|
|
|
logger.error('"%s" does not exist', path)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
logger.info('Using config from "%s" instead', path)
|
|
|
|
config.load_config(path)
|
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
# this is either the user-provided path or the default path in home
|
2021-01-09 16:44:24 +00:00
|
|
|
config_dir = os.path.dirname(config.path)
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if options.list_devices:
|
|
|
|
get_devices()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if options.key_names:
|
|
|
|
print('\n'.join(system_mapping.list_names()))
|
|
|
|
sys.exit(0)
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
if options.command not in COMMANDS:
|
2020-12-24 00:26:34 +00:00
|
|
|
logger.error('Unknown command "%s"', options.command)
|
|
|
|
|
2021-01-11 00:01:48 +00:00
|
|
|
if daemon is None:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
if USER != 'root':
|
|
|
|
# might be triggered by udev, so skip the root user.
|
|
|
|
# this will also refresh the config of the daemon if the user changed
|
|
|
|
# it in the meantime.
|
|
|
|
daemon.set_config_dir(config_dir)
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if options.command == AUTOLOAD:
|
2021-02-07 14:00:36 +00:00
|
|
|
# if device was specified, autoload for that one. if None autoload
|
|
|
|
# for all devices.
|
|
|
|
if options.device is None:
|
|
|
|
daemon.autoload()
|
|
|
|
else:
|
|
|
|
daemon.autoload_single(options.device)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
if options.command == START:
|
|
|
|
if options.device is None:
|
|
|
|
logger.error('--device missing')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if options.preset is None:
|
|
|
|
logger.error('--preset missing')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
logger.info(
|
|
|
|
'Starting injection: "%s", "%s"',
|
|
|
|
options.device, options.preset
|
|
|
|
)
|
2021-02-07 14:00:36 +00:00
|
|
|
daemon.start_injecting(options.device, options.preset)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
if options.command == STOP:
|
|
|
|
if options.device is None:
|
|
|
|
logger.error('--device missing')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
daemon.stop_injecting(options.device)
|
|
|
|
|
2021-01-09 16:44:24 +00:00
|
|
|
if options.command == STOP_ALL:
|
2021-02-07 14:00:36 +00:00
|
|
|
daemon.stop_all()
|
2021-01-09 16:44:24 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if options.command == HELLO:
|
|
|
|
response = daemon.hello('hello')
|
|
|
|
logger.info('Daemon answered with "%s"', response)
|
|
|
|
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def internals(options):
|
|
|
|
"""Methods that are needed to get the gui to work and that require root.
|
|
|
|
|
|
|
|
key-mapper-control should be started with sudo or pkexec for this.
|
|
|
|
"""
|
|
|
|
if options.command == HELPER:
|
|
|
|
cmd = ['key-mapper-helper']
|
|
|
|
elif options.command == START_DAEMON:
|
|
|
|
cmd = ['key-mapper-service', '--hide-info']
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if options.debug:
|
|
|
|
cmd.append('-d')
|
|
|
|
|
|
|
|
# Popen makes os.system of the main process continue with stuff while
|
|
|
|
# cmd runs in the background.
|
|
|
|
subprocess.Popen(cmd)
|
|
|
|
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if __name__ == '__main__':
|
2021-03-21 18:15:20 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2020-12-24 00:26:34 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--command', action='store', dest='command',
|
2021-02-07 14:00:36 +00:00
|
|
|
help='start, stop, autoload, hello or stop-all',
|
2020-12-24 00:26:34 +00:00
|
|
|
default=None, metavar='NAME'
|
|
|
|
)
|
2021-01-09 16:44:24 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--config-dir', action='store', dest='config_dir',
|
|
|
|
help=(
|
2021-02-07 14:00:36 +00:00
|
|
|
'path to the config directory containing config.json, '
|
|
|
|
'xmodmap.json and the presets folder. '
|
2021-01-09 16:44:24 +00:00
|
|
|
'defaults to ~/.config/key-mapper/'
|
|
|
|
),
|
|
|
|
default=None, metavar='PATH',
|
|
|
|
)
|
2020-12-24 00:26:34 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--preset', action='store', dest='preset',
|
2021-02-07 14:00:36 +00:00
|
|
|
help='The filename of the preset without the .json extension.',
|
|
|
|
default=None, metavar='NAME',
|
2020-12-24 00:26:34 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--device', action='store', dest='device',
|
2021-02-07 14:00:36 +00:00
|
|
|
help=(
|
|
|
|
'The device name which is used for subfolders in the '
|
|
|
|
'presets directory'
|
|
|
|
),
|
|
|
|
default=None, metavar='NAME'
|
2020-12-24 00:26:34 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--list-devices', action='store_true', dest='list_devices',
|
|
|
|
help='List available device names and exit',
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-n', '--key-names', action='store_true', dest='key_names',
|
|
|
|
help='Print all available names for the mapping',
|
|
|
|
default=False
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--debug', action='store_true', dest='debug',
|
|
|
|
help='Displays additional debug information',
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--version', action='store_true', dest='version',
|
|
|
|
help='Print the version and exit', default=False
|
|
|
|
)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
options = parser.parse_args(sys.argv[1:])
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
if options.version:
|
|
|
|
log_info()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
update_verbosity(options.debug)
|
|
|
|
|
|
|
|
logger.debug('Call for "%s"', sys.argv)
|
|
|
|
|
|
|
|
config.load_config()
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
if options.command in INTERNALS:
|
|
|
|
options.debug = True
|
|
|
|
internals(options)
|
|
|
|
else:
|
|
|
|
daemon = Daemon.connect(fallback=False)
|
|
|
|
main(options, daemon)
|