2020-11-20 00:50:40 +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 00:50:40 +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/>.
|
|
|
|
|
|
|
|
|
|
|
|
"""Store which presets should be enabled for which device on login."""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
2020-12-24 00:26:34 +00:00
|
|
|
import sys
|
2020-11-20 00:50:40 +00:00
|
|
|
import json
|
2020-11-20 20:38:19 +00:00
|
|
|
import shutil
|
2021-02-12 19:29:26 +00:00
|
|
|
import copy
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
from keymapper.paths import CONFIG_PATH, USER, touch
|
2020-11-20 00:50:40 +00:00
|
|
|
from keymapper.logger import logger
|
|
|
|
|
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
MOUSE = 'mouse'
|
|
|
|
WHEEL = 'wheel'
|
2021-01-01 21:20:33 +00:00
|
|
|
BUTTONS = 'buttons'
|
2021-01-17 14:09:47 +00:00
|
|
|
NONE = 'none'
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2020-11-20 00:50:40 +00:00
|
|
|
INITIAL_CONFIG = {
|
2020-11-28 21:54:22 +00:00
|
|
|
'autoload': {},
|
|
|
|
'macros': {
|
|
|
|
# some time between keystrokes might be required for them to be
|
|
|
|
# detected properly in software.
|
|
|
|
'keystroke_sleep_ms': 10
|
2020-11-30 19:57:09 +00:00
|
|
|
},
|
|
|
|
'gamepad': {
|
2020-12-02 18:33:31 +00:00
|
|
|
'joystick': {
|
2020-12-12 14:22:34 +00:00
|
|
|
# very small movements of the joystick should result in very
|
|
|
|
# small mouse movements. With a non_linearity of 1 it is
|
|
|
|
# impossible/hard to even find a resting position that won't
|
|
|
|
# move the cursor.
|
2020-12-02 18:33:31 +00:00
|
|
|
'non_linearity': 4,
|
|
|
|
'pointer_speed': 80,
|
2020-12-19 15:04:07 +00:00
|
|
|
'left_purpose': MOUSE,
|
|
|
|
'right_purpose': WHEEL,
|
2020-12-27 18:06:17 +00:00
|
|
|
'x_scroll_speed': 2,
|
|
|
|
'y_scroll_speed': 0.5
|
2020-12-02 18:33:31 +00:00
|
|
|
},
|
2020-11-28 21:54:22 +00:00
|
|
|
}
|
2020-11-20 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
class ConfigBase:
|
|
|
|
"""Base class for config objects.
|
|
|
|
|
|
|
|
Loading and saving is optional and handled by classes that derive from
|
|
|
|
this base.
|
|
|
|
"""
|
|
|
|
def __init__(self, fallback=None):
|
|
|
|
"""Set up the needed members to turn your object into a config.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fallback : ConfigBase
|
|
|
|
a configuration that contains fallback default configs, if your
|
|
|
|
object doesn't configure a certain key.
|
|
|
|
"""
|
2020-11-20 20:38:19 +00:00
|
|
|
self._config = {}
|
2020-12-19 20:50:27 +00:00
|
|
|
self.fallback = fallback
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
def _resolve(self, path, func, config=None):
|
2020-12-19 20:50:27 +00:00
|
|
|
"""Call func for the given config value.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config : dict
|
|
|
|
The dictionary to search. Defaults to self._config.
|
|
|
|
"""
|
2021-01-03 11:39:09 +00:00
|
|
|
chunks = path.copy() if isinstance(path, list) else path.split('.')
|
2020-12-02 18:33:31 +00:00
|
|
|
|
|
|
|
if config is None:
|
|
|
|
child = self._config
|
|
|
|
else:
|
|
|
|
child = config
|
|
|
|
|
2020-11-30 19:57:09 +00:00
|
|
|
while True:
|
|
|
|
chunk = chunks.pop(0)
|
|
|
|
parent = child
|
|
|
|
child = child.get(chunk)
|
|
|
|
if len(chunks) == 0:
|
|
|
|
# child is the value _resolve is looking for
|
|
|
|
return func(parent, child, chunk)
|
2020-12-01 23:02:41 +00:00
|
|
|
|
|
|
|
# child is another object
|
|
|
|
if child is None:
|
|
|
|
parent[chunk] = {}
|
|
|
|
child = parent[chunk]
|
2020-11-30 19:57:09 +00:00
|
|
|
|
|
|
|
def remove(self, path):
|
|
|
|
"""Remove a config key.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-01-03 11:39:09 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
|
|
|
"""
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-11-30 19:57:09 +00:00
|
|
|
if child is not None:
|
|
|
|
del parent[chunk]
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
self._resolve(path, callback)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
|
|
|
def set(self, path, value):
|
|
|
|
"""Set a config key.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-01-03 11:39:09 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
2021-01-03 11:39:09 +00:00
|
|
|
or ['macros', 'keystroke_sleep_ms']
|
2020-11-30 19:57:09 +00:00
|
|
|
value : any
|
|
|
|
"""
|
2021-01-07 16:15:12 +00:00
|
|
|
logger.info(
|
2020-12-19 20:50:27 +00:00
|
|
|
'Changing "%s" to "%s" in %s',
|
|
|
|
path, value, self.__class__.__name__
|
|
|
|
)
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-11-30 19:57:09 +00:00
|
|
|
parent[chunk] = value
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
self._resolve(path, callback)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2020-12-02 18:33:31 +00:00
|
|
|
def get(self, path, log_unknown=True):
|
|
|
|
"""Get a config value. If not set, return the default
|
2020-11-30 19:57:09 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-01-07 16:15:12 +00:00
|
|
|
path : string or string[]
|
2020-11-30 19:57:09 +00:00
|
|
|
For example 'macros.keystroke_sleep_ms'
|
2020-12-02 18:33:31 +00:00
|
|
|
log_unknown : bool
|
2020-12-24 00:26:34 +00:00
|
|
|
If True, write an error if `path` does not exist in the config
|
2020-11-30 19:57:09 +00:00
|
|
|
"""
|
2020-12-04 16:38:04 +00:00
|
|
|
def callback(parent, child, chunk):
|
2020-12-02 18:33:31 +00:00
|
|
|
return child
|
|
|
|
|
2020-12-04 16:38:04 +00:00
|
|
|
resolved = self._resolve(path, callback)
|
2020-12-19 20:50:27 +00:00
|
|
|
if resolved is None and self.fallback is not None:
|
|
|
|
resolved = self.fallback._resolve(path, callback)
|
2020-12-02 18:33:31 +00:00
|
|
|
if resolved is None:
|
2020-12-04 16:38:04 +00:00
|
|
|
resolved = self._resolve(path, callback, INITIAL_CONFIG)
|
2020-12-02 18:33:31 +00:00
|
|
|
|
|
|
|
if resolved is None and log_unknown:
|
|
|
|
logger.error('Unknown config key "%s"', path)
|
|
|
|
|
2021-02-12 19:29:26 +00:00
|
|
|
# modifications are only allowed via set
|
|
|
|
return copy.deepcopy(resolved)
|
2020-11-30 19:57:09 +00:00
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
def clear_config(self):
|
|
|
|
"""Remove all configurations in memory."""
|
|
|
|
self._config = {}
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalConfig(ConfigBase):
|
|
|
|
"""Global default configuration.
|
|
|
|
|
|
|
|
It can also contain some extra stuff not relevant for presets, like the
|
|
|
|
autoload stuff. If presets have a config key set, it will ignore
|
|
|
|
the default global configuration for that one. If none of the configs
|
|
|
|
have the key set, a hardcoded default value will be used.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2020-12-24 00:26:34 +00:00
|
|
|
self.path = os.path.join(CONFIG_PATH, 'config.json')
|
|
|
|
|
2021-01-01 13:09:28 +00:00
|
|
|
# migrate from < 0.4.0, add the .json ending
|
2020-12-24 00:26:34 +00:00
|
|
|
deprecated_path = os.path.join(CONFIG_PATH, 'config')
|
|
|
|
if os.path.exists(deprecated_path) and not os.path.exists(self.path):
|
|
|
|
logger.info('Moving "%s" to "%s"', deprecated_path, self.path)
|
|
|
|
os.rename(os.path.join(CONFIG_PATH, 'config'), self.path)
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
super().__init__()
|
|
|
|
self.load_config()
|
|
|
|
|
2020-11-30 19:57:09 +00:00
|
|
|
def set_autoload_preset(self, device, preset):
|
|
|
|
"""Set a preset to be automatically applied on start.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
device : string
|
|
|
|
preset : string or None
|
2020-12-02 18:33:31 +00:00
|
|
|
if None, don't autoload something for this device.
|
2020-11-30 19:57:09 +00:00
|
|
|
"""
|
|
|
|
if preset is not None:
|
2021-01-03 11:39:09 +00:00
|
|
|
self.set(['autoload', device], preset)
|
2020-11-30 19:57:09 +00:00
|
|
|
else:
|
2020-12-19 21:58:36 +00:00
|
|
|
logger.info(
|
|
|
|
'Not loading injecting for "%s" automatically anmore',
|
|
|
|
device
|
|
|
|
)
|
2021-01-03 11:39:09 +00:00
|
|
|
self.remove(['autoload', device])
|
2020-11-28 21:54:22 +00:00
|
|
|
|
2020-11-20 20:38:19 +00:00
|
|
|
def iterate_autoload_presets(self):
|
2020-11-26 20:33:31 +00:00
|
|
|
"""Get tuples of (device, preset)."""
|
2020-11-25 18:20:44 +00:00
|
|
|
return self._config.get('autoload', {}).items()
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2020-11-26 20:33:31 +00:00
|
|
|
def is_autoloaded(self, device, preset):
|
|
|
|
"""Should this preset be loaded automatically?"""
|
2021-01-03 11:39:09 +00:00
|
|
|
return self.get(['autoload', device], log_unknown=False) == preset
|
2020-11-26 20:33:31 +00:00
|
|
|
|
2021-01-04 19:50:05 +00:00
|
|
|
def load_config(self, path=None):
|
|
|
|
"""Load the config from the file system.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
path : string or None
|
|
|
|
If set, will change the path to load from and save to.
|
|
|
|
"""
|
|
|
|
if path is not None:
|
|
|
|
if not os.path.exists(path):
|
|
|
|
logger.error('Config at "%s" not found', path)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.path = path
|
|
|
|
|
2020-11-25 18:20:44 +00:00
|
|
|
self.clear_config()
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
if not os.path.exists(self.path):
|
2020-11-25 18:20:44 +00:00
|
|
|
# treated like an empty config
|
2020-12-24 00:26:34 +00:00
|
|
|
logger.debug('Config "%s" doesn\'t exist yet', self.path)
|
2020-12-02 18:33:31 +00:00
|
|
|
self.clear_config()
|
2021-02-12 19:29:26 +00:00
|
|
|
self._config = copy.deepcopy(INITIAL_CONFIG)
|
2020-12-02 18:33:31 +00:00
|
|
|
self.save_config()
|
2020-11-20 20:38:19 +00:00
|
|
|
return
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
with open(self.path, 'r') as file:
|
|
|
|
try:
|
|
|
|
self._config.update(json.load(file))
|
|
|
|
logger.info('Loaded config from "%s"', self.path)
|
|
|
|
except json.decoder.JSONDecodeError as error:
|
|
|
|
logger.error(
|
|
|
|
'Failed to parse config "%s": %s',
|
|
|
|
self.path, str(error)
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
2020-11-20 20:38:19 +00:00
|
|
|
|
|
|
|
def save_config(self):
|
|
|
|
"""Save the config to the file system."""
|
2021-01-09 16:44:24 +00:00
|
|
|
if USER == 'root':
|
|
|
|
logger.debug('Skipping config file creation for the root user')
|
|
|
|
return
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
touch(self.path)
|
2020-11-21 14:00:52 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
with open(self.path, 'w') as file:
|
2020-11-22 20:41:29 +00:00
|
|
|
json.dump(self._config, file, indent=4)
|
2020-12-24 00:26:34 +00:00
|
|
|
logger.info('Saved config to %s', self.path)
|
|
|
|
shutil.chown(self.path, USER, USER)
|
2020-11-22 20:41:29 +00:00
|
|
|
file.write('\n')
|
2020-11-20 20:38:19 +00:00
|
|
|
|
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
config = GlobalConfig()
|