Skip to content

Commit

Permalink
rename SystemMapping to KeyboardLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Oct 3, 2024
1 parent e0a8127 commit 332837b
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 242 deletions.
4 changes: 2 additions & 2 deletions bin/input-remapper-control
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class InputRemapperControl:
print(group.key)

def list_key_names(self):
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout

print("\n".join(system_mapping.list_names()))
print("\n".join(keyboard_layout.list_names()))

def communicate(
self,
Expand Down
4 changes: 2 additions & 2 deletions bin/input-remapper-gtk
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if __name__ == "__main__":
# log-level is set.
# import input-remapper stuff after setting the log verbosity
from inputremapper.gui.messages.message_broker import MessageBroker, MessageType
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.gui.data_manager import DataManager
from inputremapper.gui.user_interface import UserInterface
from inputremapper.gui.controller import Controller
Expand Down Expand Up @@ -105,7 +105,7 @@ if __name__ == "__main__":
reader_client,
daemon,
global_uinputs,
system_mapping,
keyboard_layout,
)
controller = Controller(message_broker, data_manager)
user_interface = UserInterface(message_broker, controller)
Expand Down
4 changes: 2 additions & 2 deletions inputremapper/configs/input_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
except ImportError:
from pydantic import BaseModel, root_validator, validator

from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.gui.messages.message_types import MessageType
from inputremapper.logging.logger import logger
from inputremapper.utils import get_evdev_constant_name
Expand Down Expand Up @@ -142,7 +142,7 @@ def _get_name(self) -> Optional[str]:
# first try to find the name in xmodmap to not display wrong
# names due to the keyboard layout
if self.type == ecodes.EV_KEY:
key_name = system_mapping.get_name(self.code)
key_name = keyboard_layout.get_name(self.code)

if key_name is None:
# if no result, look in the linux combination constants. On a german
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
LAZY_LOAD = None


class SystemMapping:
class KeyboardLayout:
"""Stores information about all available keycodes."""

_mapping: Optional[dict] = LAZY_LOAD
_xmodmap: Optional[List[Tuple[str, str]]] = LAZY_LOAD
_case_insensitive_mapping: Optional[dict] = LAZY_LOAD

def __getattribute__(self, wanted: str):
"""To lazy load system_mapping info only when needed.
"""To lazy load keyboard_layout info only when needed.
For example, this helps to keep logs of input-remapper-control clear when it
doesn't need it the information.
Expand Down Expand Up @@ -160,7 +160,7 @@ def _set(self, name: str, code: int):

def get(self, name: str) -> int:
"""Return the code mapped to the key."""
# the correct casing should be shown when asking the system_mapping
# the correct casing should be shown when asking the keyboard_layout
# for stuff. indexing case insensitive to support old presets.
if name not in self._mapping:
# only if not e.g. both "a" and "A" are in the mapping
Expand Down Expand Up @@ -216,4 +216,4 @@ def _find_legit_mappings(self) -> dict:

# TODO DI
# this mapping represents the xmodmap output, which stays constant
system_mapping = SystemMapping()
keyboard_layout = KeyboardLayout()
10 changes: 5 additions & 5 deletions inputremapper/configs/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
)

from inputremapper.configs.input_config import InputCombination
from inputremapper.configs.system_mapping import system_mapping, DISABLE_NAME
from inputremapper.configs.keyboard_layout import keyboard_layout, DISABLE_NAME
from inputremapper.configs.validation_errors import (
OutputSymbolUnknownError,
SymbolNotAvailableInTargetError,
Expand Down Expand Up @@ -288,13 +288,13 @@ def remove_combination_changed_callback(self):

def get_output_type_code(self) -> Optional[Tuple[int, int]]:
"""Returns the output_type and output_code if set,
otherwise looks the output_symbol up in the system_mapping
otherwise looks the output_symbol up in the keyboard_layout
return None for unknown symbols and macros
"""
if self.output_code and self.output_type:
return self.output_type, self.output_code
if self.output_symbol and not is_this_a_macro(self.output_symbol):
return EV_KEY, system_mapping.get(self.output_symbol)
return EV_KEY, keyboard_layout.get(self.output_symbol)
return None

def get_output_name_constant(self) -> str:
Expand Down Expand Up @@ -387,7 +387,7 @@ def validate_symbol(cls, values):
parse(symbol, mapping=mapping_mock, verbose=False)
return values

code = system_mapping.get(symbol)
code = keyboard_layout.get(symbol)
if code is None:
raise OutputSymbolUnknownError(symbol)

Expand Down Expand Up @@ -452,7 +452,7 @@ def validate_output_integrity(cls, values):
if type_ is not None or code is not None:
raise MacroButTypeOrCodeSetError()

if code is not None and code != system_mapping.get(symbol) or type_ != EV_KEY:
if code is not None and code != keyboard_layout.get(symbol) or type_ != EV_KEY:
raise SymbolAndCodeMismatchError(symbol, code)
return values

Expand Down
4 changes: 2 additions & 2 deletions inputremapper/configs/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from inputremapper.configs.mapping import Mapping, UIMapping
from inputremapper.configs.paths import PathUtils
from inputremapper.configs.preset import Preset
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.injection.global_uinputs import GlobalUInputs
from inputremapper.injection.macros.parse import is_this_a_macro
from inputremapper.logging.logger import logger, VERSION
Expand Down Expand Up @@ -230,7 +230,7 @@ def _find_target(self, symbol):
# capabilities = parse(symbol).get_capabilities()
return None

capabilities[EV_KEY] = {system_mapping.get(symbol)}
capabilities[EV_KEY] = {keyboard_layout.get(symbol)}

if len(capabilities[EV_REL]) > 0:
return "mouse"
Expand Down
8 changes: 4 additions & 4 deletions inputremapper/configs/validation_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from evdev.ecodes import EV_KEY

from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.injection.global_uinputs import GlobalUInputs


Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, analog_events):

class SymbolNotAvailableInTargetError(ValueError):
def __init__(self, symbol, target):
code = system_mapping.get(symbol)
code = keyboard_layout.get(symbol)

fitting_targets = GlobalUInputs.find_fitting_default_uinputs(EV_KEY, code)
fitting_targets_string = '", "'.join(fitting_targets)
Expand Down Expand Up @@ -92,8 +92,8 @@ class SymbolAndCodeMismatchError(ValueError):
def __init__(self, symbol, code):
super().__init__(
"output_symbol and output_code mismatch: "
f"output macro is {symbol} -> {system_mapping.get(symbol)} "
f"but output_code is {code} -> {system_mapping.get_name(code)} "
f"output macro is {symbol} -> {keyboard_layout.get(symbol)} "
f"but output_code is {code} -> {keyboard_layout.get_name(code)} "
)


Expand Down
10 changes: 5 additions & 5 deletions inputremapper/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from inputremapper.injection.injector import Injector, InjectorState
from inputremapper.configs.preset import Preset
from inputremapper.configs.global_config import GlobalConfig
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.groups import groups
from inputremapper.configs.paths import PathUtils
from inputremapper.user import UserUtils
Expand Down Expand Up @@ -487,13 +487,13 @@ def start_injecting(self, group_key: str, preset_name: str) -> bool:
xmodmap = json.load(file)
logger.debug('Using keycodes from "%s"', xmodmap_path)

# this creates the system_mapping._xmodmap, which we need to do now
# this creates the keyboard_layout._xmodmap, which we need to do now
# otherwise it might be created later which will override the changes
# we do here.
# Do we really need to lazyload in the system_mapping?
# Do we really need to lazyload in the keyboard_layout?
# this kind of bug is stupid to track down
system_mapping.get_name(0)
system_mapping.update(xmodmap)
keyboard_layout.get_name(0)
keyboard_layout.update(xmodmap)
# the service now has process wide knowledge of xmodmap
# keys of the users session
except FileNotFoundError:
Expand Down
4 changes: 2 additions & 2 deletions inputremapper/gui/autocompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from gi.repository import Gdk, Gtk, GLib, GObject

from inputremapper.configs.mapping import MappingData
from inputremapper.configs.system_mapping import system_mapping, DISABLE_NAME
from inputremapper.configs.keyboard_layout import keyboard_layout, DISABLE_NAME
from inputremapper.gui.components.editor import CodeEditor
from inputremapper.gui.controller import Controller
from inputremapper.gui.messages.message_broker import MessageBroker, MessageType
Expand Down Expand Up @@ -111,7 +111,7 @@ def propose_symbols(text_iter: Gtk.TextIter, codes: List[int]) -> List[Tuple[str

incomplete_name = incomplete_name.lower()

names = list(system_mapping.list_names(codes=codes)) + [DISABLE_NAME]
names = list(keyboard_layout.list_names(codes=codes)) + [DISABLE_NAME]

return [
(name, name)
Expand Down
6 changes: 3 additions & 3 deletions inputremapper/gui/components/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
from inputremapper.gui.utils import HandlerDisabled, Colors
from inputremapper.injection.mapping_handlers.axis_transform import Transformation
from inputremapper.input_event import InputEvent
from inputremapper.configs.system_mapping import system_mapping, XKB_KEYCODE_OFFSET
from inputremapper.configs.keyboard_layout import keyboard_layout, XKB_KEYCODE_OFFSET
from inputremapper.utils import get_evdev_constant_name

Capabilities = Dict[int, List]
Expand Down Expand Up @@ -384,9 +384,9 @@ def _display(self, event):

if is_press and len(self._combination) > 0:
names = [
system_mapping.get_name(code)
keyboard_layout.get_name(code)
for code in self._combination
if code is not None and system_mapping.get_name(code) is not None
if code is not None and keyboard_layout.get_name(code) is not None
]
self._gui.set_text(" + ".join(names))

Expand Down
8 changes: 4 additions & 4 deletions inputremapper/gui/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from inputremapper.configs.mapping import UIMapping, MappingData
from inputremapper.configs.paths import PathUtils
from inputremapper.configs.preset import Preset
from inputremapper.configs.system_mapping import SystemMapping
from inputremapper.configs.keyboard_layout import KeyboardLayout
from inputremapper.daemon import DaemonProxy
from inputremapper.exceptions import DataManagementError
from inputremapper.groups import _Group
Expand Down Expand Up @@ -73,13 +73,13 @@ def __init__(
reader_client: ReaderClient,
daemon: DaemonProxy,
uinputs: GlobalUInputs,
system_mapping: SystemMapping,
keyboard_layout: KeyboardLayout,
):
self.message_broker = message_broker
self._reader_client = reader_client
self._daemon = daemon
self._uinputs = uinputs
self._system_mapping = system_mapping
self._keyboard_layout = keyboard_layout
uinputs.prepare_all()

self._config = config
Expand Down Expand Up @@ -454,7 +454,7 @@ def update_mapping(self, **kwargs):
raise DataManagementError("Cannot modify Mapping: Mapping is not set")

if symbol := kwargs.get("output_symbol"):
kwargs["output_symbol"] = self._system_mapping.correct_case(symbol)
kwargs["output_symbol"] = self._keyboard_layout.correct_case(symbol)

combination = self.active_mapping.input_combination
for key, value in kwargs.items():
Expand Down
5 changes: 2 additions & 3 deletions inputremapper/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ def run_all_now(self):
logger.error(exception)


debounce_manager = DebounceManager()


def debounce(timeout):
"""Debounce a method call to improve performance.
Expand Down Expand Up @@ -176,6 +173,8 @@ def wrapped(self, *args, **kwargs):

return decorator

debounce_manager = DebounceManager()


class HandlerDisabled:
"""Safely modify a widget without causing handlers to be called.
Expand Down
4 changes: 2 additions & 2 deletions inputremapper/injection/macros/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
REL_HWHEEL,
)

from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import (
SymbolNotAvailableInTargetError,
MacroParsingError,
Expand Down Expand Up @@ -728,7 +728,7 @@ def _type_check_symbol(self, keyname: Union[str, Variable]) -> Union[Variable, i
return keyname

symbol = str(keyname)
code = system_mapping.get(symbol)
code = keyboard_layout.get(symbol)

if code is None:
raise MacroParsingError(msg=f'Unknown key "{symbol}"')
Expand Down
2 changes: 1 addition & 1 deletion inputremapper/injection/mapping_handlers/mapping_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from inputremapper.configs.input_config import InputCombination, InputConfig
from inputremapper.configs.mapping import Mapping
from inputremapper.configs.preset import Preset
from inputremapper.configs.system_mapping import DISABLE_CODE, DISABLE_NAME
from inputremapper.configs.keyboard_layout import DISABLE_CODE, DISABLE_NAME
from inputremapper.exceptions import MappingParsingError
from inputremapper.injection.global_uinputs import GlobalUInputs
from inputremapper.injection.macros.parse import is_this_a_macro
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from tests.lib.logger import logger

from inputremapper.gui.controller import Controller
from inputremapper.configs.system_mapping import XKB_KEYCODE_OFFSET
from inputremapper.configs.keyboard_layout import XKB_KEYCODE_OFFSET
from inputremapper.gui.utils import CTX_ERROR, CTX_WARNING, gtk_iteration
from inputremapper.gui.messages.message_broker import (
MessageBroker,
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
gi.require_version("GLib", "2.0")
from gi.repository import Gtk, GLib, Gdk, GtkSource

from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.mapping import Mapping
from inputremapper.configs.paths import PathUtils
from inputremapper.configs.global_config import GlobalConfig
Expand Down Expand Up @@ -2156,9 +2156,9 @@ def test_autocomplete_key(self):

complete_key_name = "Test_Foo_Bar"

system_mapping.clear()
system_mapping._set(complete_key_name, 1)
system_mapping._set("KEY_A", 30) # we need this for the UIMapping to work
keyboard_layout.clear()
keyboard_layout._set(complete_key_name, 1)
keyboard_layout._set("KEY_A", 30) # we need this for the UIMapping to work

# it can autocomplete a combination inbetween other things
incomplete = "qux_1\n + + qux_2"
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def quick_cleanup(log=True):
# Reminder: before patches are applied in test.py, no inputremapper module
# may be imported. So tests.lib imports them just-in-time in functions instead.
from inputremapper.injection.macros.macro import macro_variables
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.gui.utils import debounce_manager
from inputremapper.injection.global_uinputs import GlobalUInputs

Expand Down Expand Up @@ -128,7 +128,7 @@ def quick_cleanup(log=True):
if os.path.exists(tmp):
shutil.rmtree(tmp)

system_mapping.populate()
keyboard_layout.populate()

clear_write_history()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import gi
from evdev.ecodes import EV_ABS, ABS_X, ABS_Y, ABS_RX

from inputremapper.configs.system_mapping import system_mapping
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.injection.injector import InjectorState

gi.require_version("Gtk", "3.0")
Expand Down Expand Up @@ -76,7 +76,7 @@ def setUp(self) -> None:
ReaderClient(self.message_broker, _Groups()),
FakeDaemonProxy(),
uinputs,
system_mapping,
keyboard_layout,
)
self.user_interface = MagicMock()
self.controller = Controller(self.message_broker, self.data_manager)
Expand Down
Loading

0 comments on commit 332837b

Please sign in to comment.