2020-11-18 19:03:37 +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-25 22:36:03 +00:00
|
|
|
"""Create some singleton objects that are needed for the app to work."""
|
2020-11-18 19:03:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
import stat
|
2020-11-18 21:06:54 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
2020-11-30 15:22:17 +00:00
|
|
|
import evdev
|
2020-11-18 19:03:37 +00:00
|
|
|
|
|
|
|
from keymapper.mapping import Mapping
|
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
|
|
|
|
|
|
|
|
|
|
|
def populate_system_mapping():
|
|
|
|
"""Get a mapping of all available names to their keycodes."""
|
|
|
|
mapping = {}
|
|
|
|
|
2020-11-18 21:06:54 +00:00
|
|
|
xmodmap = subprocess.check_output(['xmodmap', '-pke']).decode() + '\n'
|
|
|
|
mappings = re.findall(r'(\d+) = (.+)\n', xmodmap)
|
2020-11-30 15:22:17 +00:00
|
|
|
for keycode, names in mappings:
|
|
|
|
for name in names.split():
|
2020-12-02 20:36:54 +00:00
|
|
|
mapping[name] = int(keycode) - XKB_KEYCODE_OFFSET
|
2020-11-30 15:22:17 +00:00
|
|
|
|
|
|
|
for name, ecode in evdev.ecodes.ecodes.items():
|
2020-12-02 20:36:54 +00:00
|
|
|
mapping[name] = ecode
|
2020-11-30 15:22:17 +00:00
|
|
|
|
|
|
|
return mapping
|
|
|
|
|
|
|
|
|
|
|
|
def clear_system_mapping():
|
|
|
|
"""Remove all mapped keys. Only needed for tests."""
|
2020-11-30 17:59:34 +00:00
|
|
|
keys = list(system_mapping.keys())
|
|
|
|
for key in keys:
|
2020-11-30 15:22:17 +00:00
|
|
|
del system_mapping[key]
|
2020-11-18 19:03:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
# one mapping object for the whole application that holds all
|
|
|
|
# customizations, as shown in the UI
|
|
|
|
custom_mapping = Mapping()
|
|
|
|
|
2020-11-18 21:06:54 +00:00
|
|
|
# this mapping represents the xmodmap output, which stays constant
|
2020-11-30 15:22:17 +00:00
|
|
|
system_mapping = populate_system_mapping()
|
2020-11-18 19:03:37 +00:00
|
|
|
|
|
|
|
# permissions for files created in /usr
|
|
|
|
_PERMISSIONS = stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IROTH
|