You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.0 KiB
Python
166 lines
5.0 KiB
Python
4 years ago
|
#!/usr/bin/python3
|
||
|
# -*- coding: utf-8 -*-
|
||
3 years ago
|
# input-remapper - GUI for device specific keyboard mappings
|
||
3 years ago
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
||
4 years ago
|
#
|
||
3 years ago
|
# This file is part of input-remapper.
|
||
4 years ago
|
#
|
||
3 years ago
|
# input-remapper is free software: you can redistribute it and/or modify
|
||
4 years ago
|
# 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.
|
||
|
#
|
||
3 years ago
|
# input-remapper is distributed in the hope that it will be useful,
|
||
4 years ago
|
# 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
|
||
3 years ago
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
||
4 years ago
|
|
||
4 years ago
|
import copy
|
||
4 years ago
|
|
||
3 years ago
|
from inputremapper.logger import logger, VERSION
|
||
4 years ago
|
|
||
3 years ago
|
NONE = "none"
|
||
4 years ago
|
|
||
4 years ago
|
INITIAL_CONFIG = {
|
||
3 years ago
|
"version": VERSION,
|
||
3 years ago
|
"autoload": {},
|
||
|
"macros": {
|
||
4 years ago
|
# some time between keystrokes might be required for them to be
|
||
|
# detected properly in software.
|
||
3 years ago
|
"keystroke_sleep_ms": 10
|
||
4 years ago
|
},
|
||
3 years ago
|
"gamepad": {
|
||
|
"joystick": {
|
||
4 years ago
|
# 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.
|
||
3 years ago
|
"non_linearity": 4,
|
||
|
"pointer_speed": 80,
|
||
|
"left_purpose": NONE,
|
||
|
"right_purpose": NONE,
|
||
|
"x_scroll_speed": 2,
|
||
|
"y_scroll_speed": 0.5,
|
||
4 years ago
|
},
|
||
3 years ago
|
},
|
||
4 years ago
|
}
|
||
|
|
||
|
|
||
4 years ago
|
class ConfigBase:
|
||
|
"""Base class for config objects.
|
||
|
|
||
|
Loading and saving is optional and handled by classes that derive from
|
||
|
this base.
|
||
|
"""
|
||
3 years ago
|
|
||
4 years ago
|
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.
|
||
|
"""
|
||
4 years ago
|
self._config = {}
|
||
4 years ago
|
self.fallback = fallback
|
||
4 years ago
|
|
||
4 years ago
|
def _resolve(self, path, func, config=None):
|
||
4 years ago
|
"""Call func for the given config value.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
3 years ago
|
path : string or string[]
|
||
|
For example 'macros.keystroke_sleep_ms'
|
||
|
or ['macros', 'keystroke_sleep_ms']
|
||
4 years ago
|
config : dict
|
||
|
The dictionary to search. Defaults to self._config.
|
||
|
"""
|
||
3 years ago
|
chunks = path.copy() if isinstance(path, list) else path.split(".")
|
||
4 years ago
|
|
||
|
if config is None:
|
||
|
child = self._config
|
||
|
else:
|
||
|
child = config
|
||
|
|
||
4 years ago
|
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)
|
||
4 years ago
|
|
||
|
# child is another object
|
||
|
if child is None:
|
||
|
parent[chunk] = {}
|
||
|
child = parent[chunk]
|
||
4 years ago
|
|
||
|
def remove(self, path):
|
||
|
"""Remove a config key.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
4 years ago
|
path : string or string[]
|
||
4 years ago
|
For example 'macros.keystroke_sleep_ms'
|
||
3 years ago
|
or ['macros', 'keystroke_sleep_ms']
|
||
4 years ago
|
"""
|
||
3 years ago
|
|
||
4 years ago
|
def callback(parent, child, chunk):
|
||
4 years ago
|
if child is not None:
|
||
|
del parent[chunk]
|
||
|
|
||
4 years ago
|
self._resolve(path, callback)
|
||
4 years ago
|
|
||
|
def set(self, path, value):
|
||
|
"""Set a config key.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
4 years ago
|
path : string or string[]
|
||
4 years ago
|
For example 'macros.keystroke_sleep_ms'
|
||
4 years ago
|
or ['macros', 'keystroke_sleep_ms']
|
||
4 years ago
|
value : any
|
||
|
"""
|
||
3 years ago
|
logger.info('Changing "%s" to "%s" in %s', path, value, self.__class__.__name__)
|
||
4 years ago
|
|
||
4 years ago
|
def callback(parent, child, chunk):
|
||
4 years ago
|
parent[chunk] = value
|
||
|
|
||
4 years ago
|
self._resolve(path, callback)
|
||
4 years ago
|
|
||
4 years ago
|
def get(self, path, log_unknown=True):
|
||
|
"""Get a config value. If not set, return the default
|
||
4 years ago
|
|
||
|
Parameters
|
||
|
----------
|
||
4 years ago
|
path : string or string[]
|
||
4 years ago
|
For example 'macros.keystroke_sleep_ms'
|
||
4 years ago
|
log_unknown : bool
|
||
4 years ago
|
If True, write an error if `path` does not exist in the config
|
||
4 years ago
|
"""
|
||
3 years ago
|
|
||
4 years ago
|
def callback(parent, child, chunk):
|
||
4 years ago
|
return child
|
||
|
|
||
4 years ago
|
resolved = self._resolve(path, callback)
|
||
4 years ago
|
if resolved is None and self.fallback is not None:
|
||
|
resolved = self.fallback._resolve(path, callback)
|
||
4 years ago
|
if resolved is None:
|
||
4 years ago
|
# don't create new empty stuff in INITIAL_CONFIG with _resolve
|
||
|
initial_copy = copy.deepcopy(INITIAL_CONFIG)
|
||
|
resolved = self._resolve(path, callback, initial_copy)
|
||
4 years ago
|
|
||
|
if resolved is None and log_unknown:
|
||
|
logger.error('Unknown config key "%s"', path)
|
||
|
|
||
4 years ago
|
# modifications are only allowed via set
|
||
|
return copy.deepcopy(resolved)
|
||
4 years ago
|
|
||
4 years ago
|
def clear_config(self):
|
||
|
"""Remove all configurations in memory."""
|
||
|
self._config = {}
|