input-remapper/keymapper/mapping.py

200 lines
6.8 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
from keymapper.logger import logger
2020-11-17 16:56:59 +00:00
# if MIN_KEYCODE < 255 and MAX_KEYCODE > 255: X crashes
2020-11-17 19:51:32 +00:00
# the maximum specified in /usr/share/X11/xkb/keycodes is usually 255
# and the minimum 8
2020-11-17 16:56:59 +00:00
MAX_KEYCODE = 255
MIN_KEYCODE = 8
2020-11-17 19:51:32 +00:00
# modes for change:
GENERATE = -1
DONTMAP = None
2020-11-17 16:56:59 +00:00
def get_input_keycode(keycode):
"""Same as get_output_keycode, but vice versa."""
return keycode - MIN_KEYCODE
2020-11-17 19:51:32 +00:00
def get_target_keycode():
# see HELP.md
for keycode in range(MAX_KEYCODE, MIN_KEYCODE - 1, -1):
# starting from the MAX_KEYCODE, find the first keycode that is
# unused in both custom_mapping and system_mapping.
if not (custom_mapping.has(keycode) or system_mapping.has(keycode)):
return keycode
# no unused keycode found, take the highest keycode that is unused
# in the current custom_mapping.
for keycode in range(MAX_KEYCODE, MIN_KEYCODE - 1, -1):
# starting from the MAX_KEYCODE, find the first keycode that is
# unused in both custom_mapping and system_mapping.
if not (custom_mapping.has(keycode)):
return keycode
logger.error('All %s keycodes are mapped!', MAX_KEYCODE - MIN_KEYCODE)
return None
2020-11-17 16:56:59 +00:00
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):
2020-11-17 19:51:32 +00:00
# TODO this is a stupid data structure if there are two keys
# that should be unique individually. system_keycode and
# target_keycode. two _mapping objects maybe?
2020-11-09 22:16:30 +00:00
self._mapping = {}
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-17 16:56:59 +00:00
def find_keycode(self, character, case=False):
2020-11-17 19:51:32 +00:00
"""For a given character, find the used keycodes in the mapping."""
2020-11-17 16:56:59 +00:00
# TODO test
if not case:
character = character.lower()
for keycode, (mapped_keycode, mapped_character) in self._mapping:
# keycode is what the system would use for that key,
# mapped_keycode is what we use instead by writing into /dev,
# and mapped_character is what we expect to appear.
# mapped_character might be multiple things, like "a, A"
if not case:
mapped_character = mapped_character.lower()
if character in [c.strip() for c in mapped_character.split(',')]:
return keycode, mapped_keycode
2020-11-17 19:51:32 +00:00
def change(self, previous_keycode, new_keycode, character, target_keycode):
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
The source keycode, what the mouse would report without any
modification.
2020-11-16 15:18:37 +00:00
character : string or string[]
If an array of strings, will put something like { [ a, A ] };
into the symbols file.
2020-11-17 19:51:32 +00:00
target_keycode : int or None
Which keycode should be used for that key instead. If -1,
will figure out a new one. This is for stuff that happens
under the hood and the user won't see this unless they open
config files. If None, will only map new_keycode to character
without any in-between step.
2020-11-09 22:16:30 +00:00
"""
try:
new_keycode = int(new_keycode)
2020-11-17 19:51:32 +00:00
if target_keycode is not None:
target_keycode = int(target_keycode)
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
2020-11-17 19:51:32 +00:00
# TODO test
if target_keycode == GENERATE:
target_keycode = get_target_keycode()
2020-11-09 22:16:30 +00:00
if new_keycode and character:
self._mapping[new_keycode] = (target_keycode, str(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
def get_keycode(self, keycode):
"""Read the output keycode that is mapped to this input keycode."""
return self._mapping.get(keycode, (None, None))[0]
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, (None, None))[1]
2020-11-09 22:16:30 +00:00
2020-11-17 19:51:32 +00:00
def has(self, keycode):
"""Check if this keycode is going to be a line in the symbols file."""
# TODO test
if self._mapping.get(keycode) is not None:
# the keycode that is disabled, because it is mapped to
# something else
return True
for _, (target_keycode, _) in self._mapping.items():
if target_keycode == keycode:
# the keycode that is actually being mapped
return True
return False
2020-11-09 22:16:30 +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()