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-17 22:49:11 +00:00
|
|
|
import json
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
from keymapper.logger import logger
|
|
|
|
|
|
|
|
|
|
|
|
class Mapping:
|
|
|
|
"""Contains and manages mappings.
|
|
|
|
|
|
|
|
The keycode is always unique, multiple keycodes may map to the same
|
|
|
|
character.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self._mapping = {}
|
|
|
|
self.changed = False
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""Iterate over tuples of unique keycodes and their character."""
|
2020-11-12 22:59:49 +00:00
|
|
|
return iter(sorted(self._mapping.items()))
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._mapping)
|
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
def change(self, previous_keycode, new_keycode, character):
|
2020-11-09 22:16:30 +00:00
|
|
|
"""Replace the mapping of a keycode with a different one.
|
|
|
|
|
|
|
|
Return True on success.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
previous_keycode : int or None
|
2020-11-17 19:51:32 +00:00
|
|
|
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
|
|
|
new_keycode : int
|
2020-11-17 13:35:36 +00:00
|
|
|
The source keycode, what the mouse would report without any
|
|
|
|
modification.
|
2020-11-16 15:18:37 +00:00
|
|
|
character : string or string[]
|
2020-11-17 22:40:39 +00:00
|
|
|
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-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:
|
2020-11-17 22:40:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def empty(self):
|
|
|
|
"""Remove all mappings."""
|
|
|
|
self._mapping = {}
|
|
|
|
self.changed = True
|
|
|
|
|
2020-11-17 22:49:11 +00:00
|
|
|
def load(self, device, preset):
|
|
|
|
"""Load a dumped JSON from home to overwrite the mappings."""
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
def save(self, device, preset):
|
|
|
|
"""Dump as JSON into home."""
|
|
|
|
# TODO
|
|
|
|
|
2020-11-17 22:40:39 +00:00
|
|
|
def get_keycode(self, character):
|
|
|
|
"""Get the keycode for that character."""
|
|
|
|
# TODO prepare this with .lower() instead to make it faster
|
|
|
|
character = character.lower()
|
|
|
|
for keycode, mapping in self._mapping.items():
|
|
|
|
if isinstance(mapping, list):
|
|
|
|
if character in [c.lower() for c in mapping]:
|
|
|
|
return keycode
|
|
|
|
elif mapping.lower() == character:
|
|
|
|
return int(keycode)
|
|
|
|
|
|
|
|
return None
|
2020-11-17 13:35:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2020-11-17 22:40:39 +00:00
|
|
|
return self._mapping.get(keycode)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
|
2020-11-10 22:14:28 +00:00
|
|
|
# one mapping object for the whole application that holds all
|
|
|
|
# customizations
|
|
|
|
custom_mapping = Mapping()
|
2020-11-17 16:56:59 +00:00
|
|
|
|
|
|
|
# one mapping that represents the xmodmap output
|
|
|
|
system_mapping = Mapping()
|