input-remapper/keymapper/mapping.py

201 lines
6.4 KiB
Python
Raw Normal View History

2020-11-09 22:16:30 +00:00
#!/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/>.
2020-11-09 22:34:30 +00:00
"""Contains and manages mappings."""
2020-11-09 22:16:30 +00:00
2020-11-18 09:33:59 +00:00
import os
import json
2020-11-28 14:43:24 +00:00
import copy
2020-11-09 22:16:30 +00:00
from keymapper.logger import logger
from keymapper.paths import get_config_path, touch
2020-11-09 22:16:30 +00:00
2020-11-26 23:34:45 +00:00
def keep_reverse_mapping_intact(func):
"""Decorator for Mapping.update_reverse_mapping."""
2020-11-23 17:42:36 +00:00
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
2020-11-26 23:34:45 +00:00
self.update_reverse_mapping()
2020-11-23 17:42:36 +00:00
return wrapper
2020-11-09 22:16:30 +00:00
class Mapping:
"""Contains and manages mappings.
The keycode is always unique, multiple keycodes may map to the same
character.
"""
def __init__(self):
self._mapping = {}
2020-11-23 17:42:36 +00:00
# Maintain this second mapping in order to optimize get_keycode.
# This mapping is not complete, since multiple keycodes can map
# to the same character in _mapping which is not possible in
# _reverse_mapping! It is based on _reverse_mapping and
# reconstructed on changes.
self._reverse_mapping = {}
2020-11-09 22:16:30 +00:00
self.changed = False
def __iter__(self):
"""Iterate over tuples of unique keycodes and their character."""
return iter(sorted(self._mapping.items()))
2020-11-09 22:16:30 +00:00
def __len__(self):
return len(self._mapping)
2020-11-26 23:34:45 +00:00
def update_reverse_mapping(self):
"""Generate a reverse mapping to optimize reverse lookups.
If _mapping contains `20: "a, A"` (the xkb syntax for modified keys),
reverse mapping will contain `"a": 20, "A": 20`.
"""
self._reverse_mapping = {}
for key, value in self._mapping.items():
for character in value.split(','):
self._reverse_mapping[character.strip()] = key
@keep_reverse_mapping_intact
2020-11-18 23:04:04 +00:00
def change(self, new_keycode, character, previous_keycode=None):
2020-11-09 22:16:30 +00:00
"""Replace the mapping of a keycode with a different one.
Return True on success.
Parameters
----------
new_keycode : int
The source keycode, what the mouse would report without any
modification.
2020-11-16 15:18:37 +00:00
character : string or string[]
A single character known to xkb, Examples: KP_1, Shift_L, a, B.
Can also be an array, which is used for reading the xkbmap output
completely.
2020-11-18 23:04:04 +00:00
previous_keycode : int or None
If None, will not remove any previous mapping. If you recently
used 10 for new_keycode and want to overwrite that with 11,
provide 5 here.
2020-11-09 22:16:30 +00:00
"""
try:
new_keycode = int(new_keycode)
2020-11-17 19:51:32 +00:00
if previous_keycode is not None:
previous_keycode = int(previous_keycode)
2020-11-09 22:16:30 +00:00
except ValueError:
2020-11-17 19:51:32 +00:00
logger.error('Can only use numbers as keycodes')
2020-11-09 22:16:30 +00:00
return False
if new_keycode and character:
self._mapping[new_keycode] = character
2020-11-09 22:16:30 +00:00
if new_keycode != previous_keycode:
# clear previous mapping of that code, because the line
# representing that one will now represent a different one.
self.clear(previous_keycode)
self.changed = True
return True
return False
2020-11-26 23:34:45 +00:00
@keep_reverse_mapping_intact
2020-11-09 22:16:30 +00:00
def clear(self, keycode):
"""Remove a keycode from the mapping.
Parameters
----------
keycode : int
"""
if self._mapping.get(keycode) is not None:
del self._mapping[keycode]
self.changed = True
2020-11-26 23:34:45 +00:00
@keep_reverse_mapping_intact
2020-11-09 22:16:30 +00:00
def empty(self):
"""Remove all mappings."""
self._mapping = {}
self.changed = True
2020-11-26 23:34:45 +00:00
@keep_reverse_mapping_intact
def load(self, device, preset):
"""Load a dumped JSON from home to overwrite the mappings."""
path = get_config_path(device, preset)
2020-11-22 20:04:09 +00:00
logger.info('Loading preset from "%s"', path)
2020-11-18 09:33:59 +00:00
if not os.path.exists(path):
2020-11-22 20:04:09 +00:00
logger.error('Tried to load non-existing preset "%s"', path)
2020-11-18 09:33:59 +00:00
return
2020-11-22 20:41:29 +00:00
with open(path, 'r') as file:
mapping = json.load(file)
if mapping.get('mapping') is None:
logger.error('Invalid preset config at "%s"', path)
return
for keycode, character in mapping['mapping'].items():
2020-11-18 12:17:49 +00:00
try:
keycode = int(keycode)
except ValueError:
logger.error('Found non-int keycode: %s', keycode)
continue
self._mapping[keycode] = character
2020-11-18 09:33:59 +00:00
self.changed = False
2020-11-28 14:43:24 +00:00
def clone(self):
"""Create a copy of the mapping."""
mapping = Mapping()
mapping._mapping = copy.deepcopy(self._mapping)
mapping.update_reverse_mapping()
mapping.changed = self.changed
2020-11-28 14:48:57 +00:00
return mapping
2020-11-28 14:43:24 +00:00
def save(self, device, preset):
"""Dump as JSON into home."""
2020-11-18 09:33:59 +00:00
path = get_config_path(device, preset)
logger.info('Saving preset to %s', path)
touch(path)
2020-11-18 09:33:59 +00:00
2020-11-22 20:41:29 +00:00
with open(path, 'w') as file:
# make sure to keep the option to add metadata if ever needed,
# so put the mapping into a special key
json.dump({
'mapping': self._mapping
}, file, indent=4)
2020-11-22 20:41:29 +00:00
file.write('\n')
2020-11-18 09:33:59 +00:00
self.changed = False
def get_keycode(self, character):
2020-11-23 17:42:36 +00:00
"""Get the keycode for that character.
If multiple keycodes map to that character, an arbitrary one of
those is returned.
"""
return self._reverse_mapping.get(character)
def get_character(self, keycode):
2020-11-09 22:16:30 +00:00
"""Read the character that is mapped to this keycode.
Parameters
----------
keycode : int
"""
return self._mapping.get(keycode)