2020-11-20 00:50:40 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-11-20 00:50:40 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-20 00:50:40 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-20 00:50:40 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2020-11-20 00:50:40 +00:00
|
|
|
# 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-11-20 00:50:40 +00:00
|
|
|
"""Store which presets should be enabled for which device on login."""
|
|
|
|
|
2021-02-12 19:29:26 +00:00
|
|
|
import copy
|
2022-07-23 08:53:41 +00:00
|
|
|
import json
|
|
|
|
import os
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.configs.base_config import ConfigBase, INITIAL_CONFIG
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.paths import CONFIG_PATH, USER, touch
|
|
|
|
from inputremapper.logger import logger
|
2020-11-20 00:50:40 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
MOUSE = "mouse"
|
|
|
|
WHEEL = "wheel"
|
|
|
|
BUTTONS = "buttons"
|
|
|
|
NONE = "none"
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-12-19 20:50:27 +00:00
|
|
|
def __init__(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.path = os.path.join(CONFIG_PATH, "config.json")
|
2020-12-19 20:50:27 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def get_dir(self) -> str:
|
|
|
|
"""the folder containing this config"""
|
|
|
|
return os.path.split(self.path)[0]
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def set_autoload_preset(self, group_key, preset):
|
2020-11-30 19:57:09 +00:00
|
|
|
"""Set a preset to be automatically applied on start.
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-04-23 09:51:21 +00:00
|
|
|
group_key : string
|
|
|
|
the unique identifier of the group. This is used instead of the
|
|
|
|
name to enable autoloading two different presets when two similar
|
|
|
|
devices are connected.
|
2020-11-30 19:57:09 +00:00
|
|
|
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-09-26 10:44:56 +00:00
|
|
|
self.set(["autoload", group_key], preset)
|
2020-11-30 19:57:09 +00:00
|
|
|
else:
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.info('Not injecting for "%s" automatically anmore', group_key)
|
|
|
|
self.remove(["autoload", group_key])
|
2020-11-28 21:54:22 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
self._save_config()
|
|
|
|
|
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)."""
|
2021-09-26 10:44:56 +00:00
|
|
|
return self._config.get("autoload", {}).items()
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def is_autoloaded(self, group_key, preset):
|
2020-11-26 20:33:31 +00:00
|
|
|
"""Should this preset be loaded automatically?"""
|
2022-01-31 12:42:42 +00:00
|
|
|
if group_key is None or preset is None:
|
|
|
|
raise ValueError("Expected group_key and preset to not be None")
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
return self.get(["autoload", group_key], 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)
|
2022-01-10 19:37:22 +00:00
|
|
|
self._save_config()
|
2020-11-20 20:38:19 +00:00
|
|
|
return
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(self.path, "r") as file:
|
2020-12-24 00:26:34 +00:00
|
|
|
try:
|
|
|
|
self._config.update(json.load(file))
|
|
|
|
logger.info('Loaded config from "%s"', self.path)
|
|
|
|
except json.decoder.JSONDecodeError as error:
|
|
|
|
logger.error(
|
2021-02-15 17:27:34 +00:00
|
|
|
'Failed to parse config "%s": %s. Using defaults',
|
2021-09-26 10:44:56 +00:00
|
|
|
self.path,
|
|
|
|
str(error),
|
2020-12-24 00:26:34 +00:00
|
|
|
)
|
2021-02-15 17:27:34 +00:00
|
|
|
# uses the default configuration when the config object
|
|
|
|
# is empty automatically
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def _save_config(self):
|
2020-11-20 20:38:19 +00:00
|
|
|
"""Save the config to the file system."""
|
2021-09-26 10:44:56 +00:00
|
|
|
if USER == "root":
|
|
|
|
logger.debug("Skipping config file creation for the root user")
|
2021-01-09 16:44:24 +00:00
|
|
|
return
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
touch(self.path)
|
2020-11-21 14:00:52 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(self.path, "w") as file:
|
2020-11-22 20:41:29 +00:00
|
|
|
json.dump(self._config, file, indent=4)
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.info("Saved config to %s", self.path)
|
|
|
|
file.write("\n")
|
2020-11-20 20:38:19 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config = GlobalConfig()
|