config for autoloading presets

pull/14/head
sezanzeb 4 years ago
parent 75f11cbe05
commit 437cb533aa

@ -24,6 +24,8 @@
import os import os
import json import json
import shutil
import copy
from keymapper.paths import CONFIG from keymapper.paths import CONFIG
from keymapper.logger import logger from keymapper.logger import logger
@ -33,57 +35,63 @@ CONFIG_PATH = os.path.join(CONFIG, 'config')
# an empty config with basic expected substructures # an empty config with basic expected substructures
INITIAL_CONFIG = { INITIAL_CONFIG = {
'autoload': [], 'autoload': {},
'map_EV_REL_devices': True 'map_EV_REL_devices': True
} }
_config = INITIAL_CONFIG.copy()
class _Config:
def set_autoload_preset(device, preset): def __init__(self):
"""Set a preset to be automatically applied on start.""" self._config = {}
_config['autoload'].append({ self.load_config()
'device': device,
'preset': preset def set_autoload_preset(self, device, preset):
}) """Set a preset to be automatically applied on start."""
self._config['autoload'][device] = preset
def iterate_autoload_presets(): def iterate_autoload_presets(self):
"""Yield tuples of (device, preset).""" """get tuples of (device, preset)."""
for entry in _config['autoload']: return self._config['autoload'].items()
yield entry['device'], entry['preset']
def set_modify_movement_devices(self, active):
"""Set if devices that control movements should also be mapped.
def set_modify_movement_devices(active):
"""Set if devices that control movements should also be mapped. This causes many movements event to be passed through python code,
and if this ever seems to affect the responsiveness of mouse movements,
This causes many movements event to be passed through python code, it can be disabled. This is just an optional precaution. Disabling this
and if this ever seems to affect the responsiveness of mouse movements, may make mapping some keys of the device impossible.
it can be disabled. This may make mapping some keys of the device """
impossible. self._config['map_EV_REL_devices'] = active
"""
global _config def may_modify_movement_devices(self):
_config['map_EV_REL_devices'] = active """Get if devices that control movements may be modified as well."""
return self._config['map_EV_REL_devices']
def load_config(): def load_config(self):
"""Load the config from the file system.""" """Load the config from the file system."""
global _config if not os.path.exists(CONFIG_PATH):
# has not yet been saved
if not os.path.exists(CONFIG_PATH): logger.info('Creating initial config')
# has not yet been saved self._config = copy.deepcopy(INITIAL_CONFIG)
logger.debug('Config file not found') self.save_config()
_config = INITIAL_CONFIG.copy() return
return
with open(CONFIG_PATH, 'r') as f:
with open(CONFIG_PATH, 'r') as f: self._config = copy.deepcopy(INITIAL_CONFIG)
_config = INITIAL_CONFIG.copy() self._config.update(json.load(f))
_config.update(json.load(f)) logger.info('Loaded config from %s', CONFIG_PATH)
logger.info('Loaded config from %s', CONFIG_PATH)
def clear_config(self):
"""Needed for tests."""
def save_config(): self._config = copy.deepcopy(INITIAL_CONFIG)
"""Save the config to the file system."""
with open(CONFIG_PATH, 'w') as f: def save_config(self):
json.dump(_config, f) """Save the config to the file system."""
logger.info('Saved config to %s', CONFIG_PATH) with open(CONFIG_PATH, 'w') as f:
json.dump(self._config, f, indent=4)
logger.info('Saved config to %s', CONFIG_PATH)
shutil.chown(CONFIG_PATH, os.getlogin())
f.write('\n')
config = _Config()

@ -27,6 +27,7 @@ import multiprocessing
import evdev import evdev
from keymapper.logger import logger from keymapper.logger import logger
from keymapper.config import config
_devices = None _devices = None
@ -71,17 +72,14 @@ class _GetDevicesProcess(multiprocessing.Process):
if evdev.ecodes.EV_KEY not in capabilities: if evdev.ecodes.EV_KEY not in capabilities:
continue continue
if evdev.ecodes.EV_REL in capabilities: if (
# skip devices that control movement, because I would like not config.may_modify_movement_devices()
# to not affect the mouse movement performance and evdev.ecodes.EV_REL in capabilities
# TODO check for config, is set enable mapping buttons ):
# of that device as well # skip devices that control movement to avoid affecting
# TODO speaking of config, add checkbox to automatically load # their performance due to the amount of their events.
# a preset. Store a mapping of device: preset somewhere to # TODO add checkbox to automatically load
# automatically load them. Autoloading presets should be a # a preset on login
# different executable, and controlling stuff in the gui
# should send a dbus message to the different bin to stop
# doing stuff
logger.debug( logger.debug(
'Skipping %s to avoid impairing mouse movement', 'Skipping %s to avoid impairing mouse movement',
device.path device.path

@ -0,0 +1,67 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
#
# 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/>.
import unittest
from keymapper.config import config
class TestConfig(unittest.TestCase):
def tearDown(self):
config.clear_config()
self.assertEqual(len(config.iterate_autoload_presets()), 0)
config.save_config()
def test_autoload(self):
config.set_autoload_preset('d1', 'a')
self.assertEqual(len(config.iterate_autoload_presets()), 1)
config.set_autoload_preset('d2', 'b')
self.assertEqual(len(config.iterate_autoload_presets()), 2)
config.set_autoload_preset('d2', 'c')
self.assertEqual(len(config.iterate_autoload_presets()), 2)
self.assertListEqual(
list(config.iterate_autoload_presets()),
[('d1', 'a'), ('d2', 'c')]
)
def test_save_load(self):
config.set_autoload_preset('d1', 'a')
config.set_autoload_preset('d2', 'b')
config.set_modify_movement_devices(True)
config.save_config()
# ignored after load
config.set_modify_movement_devices(False)
config.set_autoload_preset('d3', 'c')
config.load_config()
self.assertListEqual(
list(config.iterate_autoload_presets()),
[('d1', 'a'), ('d2', 'b')]
)
self.assertTrue(config.may_modify_movement_devices())
if __name__ == "__main__":
unittest.main()
Loading…
Cancel
Save