default keys for unused keys

xkb
sezanzeb 4 years ago committed by sezanzeb
parent 6d80f3635f
commit 339154b5e1

@ -91,7 +91,7 @@ class SingleKeyMapping:
def iterate():
self.check_newest_keycode()
return self.keycode.is_focus()
return self.keycode.is_focus() and window.window.is_active()
GLib.timeout_add(1000 / 30, iterate)
@ -400,13 +400,9 @@ class Window:
self.selected_preset
)
print('lkjahsdfkjashdkj')
print(list(mapping))
create_setxkbmap_config(
self.selected_device,
self.selected_preset,
mapping
self.selected_preset
)

@ -3,5 +3,6 @@
default xkb_keycodes "basic" {{
minimum = {minimum};
maximum = {maximum};
include "evdev"
{xkb_keycodes}
}};

@ -22,7 +22,8 @@
"""Stuff that interacts with the X Server, be it commands or config files.
TODO create a base class to standardize the interface if a different
display server should be supported.
display server should be supported. Or does wayland use the same
config files?
Resources:
[1] https://wiki.archlinux.org/index.php/Keyboard_input
@ -33,6 +34,7 @@ Resources:
import os
import re
import shutil
import subprocess
from keymapper.paths import get_home_path, get_usr_path, KEYCODES_PATH, \
@ -117,6 +119,11 @@ class Mapping:
del self._mapping[keycode]
self.changed = True
def empty(self):
"""Remove all mappings."""
self._mapping = {}
self.changed = True
def get(self, keycode):
"""Read the character that is mapped to this keycode.
@ -161,11 +168,19 @@ def create_preset(device, name=None):
logger.info('Creating new file %s', path)
os.makedirs(os.path.dirname(path), exist_ok=True)
os.mknod(path)
# give those files to the user
user = os.getlogin()
for root, dirs, files in os.walk(CONFIG_PATH):
shutil.chown(root, user, user)
for file in files:
shutil.chown(os.path.join(root, file), user, user)
ensure_symlink()
return name
def create_setxkbmap_config(device, preset, mapping):
def create_setxkbmap_config(device, preset):
"""Generate a config file for setxkbmap.
The file is created in ~/.config/key-mapper/<device>/<preset> and,
@ -179,7 +194,6 @@ def create_setxkbmap_config(device, preset, mapping):
----------
device : string
preset : string
mapping : Mapping
"""
if len(mapping) == 0:
logger.debug('Got empty mappings')
@ -201,7 +215,7 @@ def create_setxkbmap_config(device, preset, mapping):
logger.info('Writing key mappings')
with open(home_preset_path, 'w') as f:
contents = generate_symbols_content(device, preset, mapping)
contents = generate_symbols_content(device, preset)
if contents is not None:
f.write(contents)
@ -252,8 +266,10 @@ def create_identity_mapping():
# and the minimum 8
maximum = 255
minimum = 8
for code in range(minimum, maximum + 1):
xkb_keycodes.append(f'<{code}> = {code};')
for keycode, _ in mapping:
# only those keys that are in the mapping, so that the others
# get mappings from the default keyboard.
xkb_keycodes.append(f'<{keycode}> = {keycode};')
template_path = get_data_path('xkb_keycodes_template')
with open(template_path, 'r') as template_file:
@ -273,7 +289,7 @@ def create_identity_mapping():
keycodes.write(result)
def generate_symbols_content(device, preset, mapping):
def generate_symbols_content(device, preset):
"""Create config contents to be placed in /usr/share/X11/xkb/symbols.
This file contains the mapping of the preset as expected by X.
@ -282,7 +298,6 @@ def generate_symbols_content(device, preset, mapping):
----------
device : string
preset : string
mapping : Mapping
"""
system_default = 'us' # TODO get the system default
@ -298,9 +313,6 @@ def generate_symbols_content(device, preset, mapping):
xkb_symbols = []
for keycode, character in mapping:
# since the side effect of a broken config is so severe, make
# sure to check the presence of the keycode once again even though
# their content is always the same (unless modified by a user).
if f'<{keycode}>' not in keycodes:
logger.error(f'Unknown keycode <{keycode}> for "{character}"')
# continue, otherwise X would crash when loading

@ -26,20 +26,15 @@ Is a module so that tests can modify them.
import os
import subprocess
# since this needs to run as sudo,
# get the home dir of the user who called sudo.
who = subprocess.check_output('who').decode().split()[0]
# the path in home, is symlinked with SYMBOLS_PATH
CONFIG_PATH = os.path.join('/home', who, '.config/key-mapper')
CONFIG_PATH = os.path.join('/home', os.getlogin(), '.config/key-mapper')
# should not contain spaces
SYMBOLS_PATH = os.path.join(
'/usr/share/X11/xkb/symbols/key-mapper',
who.replace(' ', '_')
os.getlogin().replace(' ', '_')
)
# those are the same for every preset and user

@ -23,7 +23,7 @@ import os
import unittest
import shutil
from keymapper.X import Mapping, generate_symbols_content, \
from keymapper.X import mapping, generate_symbols_content, \
create_identity_mapping, create_setxkbmap_config, get_home_path
from keymapper.paths import KEYCODES_PATH, SYMBOLS_PATH, CONFIG_PATH
@ -32,15 +32,15 @@ from test import tmp
class TestConfig(unittest.TestCase):
def setUp(self):
self.mapping = Mapping()
self.mapping.change(None, 10, 'a')
self.mapping.change(None, 11, 'KP_1')
self.mapping.change(None, 12, 3)
mapping.empty()
mapping.change(None, 10, 'a')
mapping.change(None, 11, 'KP_1')
mapping.change(None, 12, 3)
if os.path.exists(tmp):
shutil.rmtree(tmp)
def test_create_setxkbmap_config(self):
create_setxkbmap_config('device a', 'preset b', self.mapping)
create_setxkbmap_config('device a', 'preset b')
self.assertTrue(os.path.exists(os.path.join(
CONFIG_PATH,
@ -66,7 +66,7 @@ class TestConfig(unittest.TestCase):
self.assertRaises(
FileNotFoundError,
generate_symbols_content,
'device', 'preset', self.mapping
'device', 'preset'
)
# create the identity mapping, because it is required for
@ -75,10 +75,13 @@ class TestConfig(unittest.TestCase):
self.assertTrue(os.path.exists(KEYCODES_PATH))
with open(KEYCODES_PATH, 'r') as f:
keycodes = f.read()
self.assertIn('<8> = 8;', keycodes)
self.assertIn('<255> = 255;', keycodes)
self.assertNotIn('<9> = 9;', keycodes)
self.assertIn('<10> = 10;', keycodes)
self.assertIn('<11> = 11;', keycodes)
self.assertIn('<12> = 12;', keycodes)
self.assertNotIn('<13> = 13;', keycodes)
content = generate_symbols_content('device', 'preset', self.mapping)
content = generate_symbols_content('device', 'preset')
self.assertIn('key <10> { [ a ] };', content)
self.assertIn('key <11> { [ KP_1 ] };', content)
self.assertIn('key <12> { [ 3 ] };', content)

Loading…
Cancel
Save