2020-11-18 19:03:37 +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-18 19:03:37 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-18 19:03:37 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-18 19:03:37 +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-18 19:03:37 +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/>.
|
2021-09-29 18:17:45 +00:00
|
|
|
"""Make the systems/environments mapping of keys and codes accessible."""
|
2020-11-18 19:03:37 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
import json
|
2022-07-23 08:53:41 +00:00
|
|
|
import re
|
2020-11-18 21:06:54 +00:00
|
|
|
import subprocess
|
2022-07-23 08:53:41 +00:00
|
|
|
from typing import Optional, List, Iterable
|
|
|
|
|
2020-11-30 15:22:17 +00:00
|
|
|
import evdev
|
2020-11-18 19:03:37 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.paths import get_config_path, touch
|
2022-07-23 08:53:41 +00:00
|
|
|
from inputremapper.logger import logger
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.utils import is_service
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
DISABLE_NAME = "disable"
|
|
|
|
|
|
|
|
DISABLE_CODE = -1
|
2020-11-18 21:06:54 +00:00
|
|
|
|
2020-12-02 20:36:54 +00:00
|
|
|
# xkb uses keycodes that are 8 higher than those from evdev
|
|
|
|
XKB_KEYCODE_OFFSET = 8
|
2020-11-30 15:22:17 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
XMODMAP_FILENAME = "xmodmap.json"
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-11-30 15:22:17 +00:00
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
class SystemMapping:
|
|
|
|
"""Stores information about all available keycodes."""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Construct the system_mapping."""
|
2021-11-21 20:45:02 +00:00
|
|
|
self._mapping = None
|
2022-01-10 19:37:22 +00:00
|
|
|
self._xmodmap = None
|
|
|
|
self._case_insensitive_mapping = None
|
2021-11-21 20:45:02 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def __getattribute__(self, wanted):
|
2021-11-21 20:45:02 +00:00
|
|
|
"""To lazy load system_mapping info only when needed.
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
For example, this helps to keep logs of input-remapper-control clear when it
|
|
|
|
doesnt need it the information.
|
2021-11-21 20:45:02 +00:00
|
|
|
"""
|
2022-01-10 19:37:22 +00:00
|
|
|
lazy_loaded_attributes = ["_mapping", "_xmodmap", "_case_insensitive_mapping"]
|
|
|
|
for lazy_loaded_attribute in lazy_loaded_attributes:
|
|
|
|
if wanted != lazy_loaded_attribute:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if object.__getattribute__(self, lazy_loaded_attribute) is None:
|
|
|
|
object.__setattr__(self, lazy_loaded_attribute, {})
|
|
|
|
object.__getattribute__(self, "populate")()
|
2021-11-21 20:45:02 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
return object.__getattribute__(self, wanted)
|
2020-12-04 13:38:41 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def list_names(self, codes: Optional[Iterable[int]] = None) -> List[str]:
|
2022-01-14 17:50:57 +00:00
|
|
|
"""Return a list of all possible names in the mapping, optionally filtered by codes.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
codes: list of event codes
|
|
|
|
"""
|
|
|
|
if not codes:
|
|
|
|
return self._mapping.keys()
|
|
|
|
|
|
|
|
return [name for name, code in self._mapping.items() if code in codes]
|
2020-12-05 18:38:38 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def correct_case(self, symbol):
|
|
|
|
"""Return the correct casing for a symbol."""
|
|
|
|
if symbol in self._mapping:
|
|
|
|
return symbol
|
2021-04-02 11:30:42 +00:00
|
|
|
# only if not e.g. both "a" and "A" are in the mapping
|
2021-04-23 09:51:21 +00:00
|
|
|
return self._case_insensitive_mapping.get(symbol.lower(), symbol)
|
2021-04-02 11:30:42 +00:00
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
def populate(self):
|
|
|
|
"""Get a mapping of all available names to their keycodes."""
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug("Gathering available keycodes")
|
2020-12-04 13:38:41 +00:00
|
|
|
self.clear()
|
|
|
|
|
2021-11-21 20:45:02 +00:00
|
|
|
if not is_service():
|
2022-01-16 12:34:20 +00:00
|
|
|
# xmodmap is only available from within the login session.
|
|
|
|
# The service that runs via systemd can't use this.
|
|
|
|
xmodmap_dict = {}
|
|
|
|
try:
|
|
|
|
xmodmap = subprocess.check_output(
|
2022-04-18 11:52:59 +00:00
|
|
|
["xmodmap", "-pke"],
|
|
|
|
stderr=subprocess.STDOUT,
|
2022-01-16 12:34:20 +00:00
|
|
|
).decode()
|
|
|
|
xmodmap = xmodmap
|
|
|
|
self._xmodmap = re.findall(r"(\d+) = (.+)\n", xmodmap + "\n")
|
|
|
|
xmodmap_dict = self._find_legit_mappings()
|
|
|
|
if len(xmodmap_dict) == 0:
|
|
|
|
logger.info("`xmodmap -pke` did not yield any symbol")
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.info(
|
|
|
|
"Optional `xmodmap` command not found. This is not critical."
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
logger.error('Call to `xmodmap -pke` failed with "%s"', e)
|
|
|
|
|
2021-11-21 20:45:02 +00:00
|
|
|
# Clients usually take care of that, don't let the service do funny things.
|
2022-01-01 12:00:49 +00:00
|
|
|
# Write this stuff into the input-remapper config directory, because
|
2021-11-21 20:45:02 +00:00
|
|
|
# the systemd service won't know the user sessions xmodmap.
|
2020-12-24 00:26:34 +00:00
|
|
|
path = get_config_path(XMODMAP_FILENAME)
|
|
|
|
touch(path)
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(path, "w") as file:
|
2021-01-09 16:44:24 +00:00
|
|
|
logger.debug('Writing "%s"', path)
|
2020-12-24 00:26:34 +00:00
|
|
|
json.dump(xmodmap_dict, file, indent=4)
|
|
|
|
|
2022-01-16 12:34:20 +00:00
|
|
|
for name, code in xmodmap_dict.items():
|
|
|
|
self._set(name, code)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
for name, ecode in evdev.ecodes.ecodes.items():
|
2021-09-26 10:44:56 +00:00
|
|
|
if name.startswith("KEY") or name.startswith("BTN"):
|
2020-12-05 18:38:38 +00:00
|
|
|
self._set(name, ecode)
|
2020-12-04 13:38:41 +00:00
|
|
|
|
2021-01-01 13:09:28 +00:00
|
|
|
self._set(DISABLE_NAME, DISABLE_CODE)
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def update(self, mapping):
|
|
|
|
"""Update this with new keys.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
mapping : dict
|
|
|
|
maps from name to code. Make sure your keys are lowercase.
|
|
|
|
"""
|
2021-11-21 20:45:02 +00:00
|
|
|
len_before = len(self._mapping)
|
2021-04-02 11:30:42 +00:00
|
|
|
for name, code in mapping.items():
|
|
|
|
self._set(name, code)
|
2022-01-18 20:20:42 +00:00
|
|
|
|
2021-11-21 20:45:02 +00:00
|
|
|
logger.debug(
|
|
|
|
"Updated keycodes with %d new ones", len(self._mapping) - len_before
|
|
|
|
)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-12-04 13:38:41 +00:00
|
|
|
def _set(self, name, code):
|
|
|
|
"""Map name to code."""
|
2021-04-02 11:30:42 +00:00
|
|
|
self._mapping[str(name)] = code
|
|
|
|
self._case_insensitive_mapping[str(name).lower()] = name
|
2020-12-04 13:38:41 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def get(self, name) -> int:
|
2020-12-04 13:38:41 +00:00
|
|
|
"""Return the code mapped to the key."""
|
2021-04-02 11:30:42 +00:00
|
|
|
# the correct casing should be shown when asking the system_mapping
|
2021-04-02 14:52:47 +00:00
|
|
|
# for stuff. indexing case insensitive to support old presets.
|
2021-04-02 11:30:42 +00:00
|
|
|
if name not in self._mapping:
|
|
|
|
# only if not e.g. both "a" and "A" are in the mapping
|
|
|
|
name = self._case_insensitive_mapping.get(str(name).lower())
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2021-04-02 11:30:42 +00:00
|
|
|
return self._mapping.get(name)
|
2020-12-04 13:38:41 +00:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
"""Remove all mapped keys. Only needed for tests."""
|
|
|
|
keys = list(self._mapping.keys())
|
|
|
|
for key in keys:
|
|
|
|
del self._mapping[key]
|
2020-11-18 19:03:37 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def get_name(self, code):
|
|
|
|
"""Get the first matching name for the code."""
|
2021-04-02 11:30:42 +00:00
|
|
|
for entry in self._xmodmap:
|
2021-03-21 18:15:20 +00:00
|
|
|
if int(entry[0]) - XKB_KEYCODE_OFFSET == code:
|
|
|
|
return entry[1].split()[0]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-04-02 11:30:42 +00:00
|
|
|
def _find_legit_mappings(self):
|
|
|
|
"""From the parsed xmodmap list find usable symbols and their codes."""
|
|
|
|
xmodmap_dict = {}
|
|
|
|
for keycode, names in self._xmodmap:
|
|
|
|
# there might be multiple, like:
|
|
|
|
# keycode 64 = Alt_L Meta_L Alt_L Meta_L
|
|
|
|
# keycode 204 = NoSymbol Alt_L NoSymbol Alt_L
|
|
|
|
# Alt_L should map to code 64. Writing code 204 only works
|
|
|
|
# if a modifier is applied at the same time. So take the first
|
|
|
|
# one.
|
|
|
|
name = names.split()[0]
|
|
|
|
xmodmap_dict[name] = int(keycode) - XKB_KEYCODE_OFFSET
|
|
|
|
|
|
|
|
return xmodmap_dict
|
|
|
|
|
2020-11-18 19:03:37 +00:00
|
|
|
|
2020-11-18 21:06:54 +00:00
|
|
|
# this mapping represents the xmodmap output, which stays constant
|
2020-12-04 13:38:41 +00:00
|
|
|
system_mapping = SystemMapping()
|