diff --git a/bin/key-mapper-gtk b/bin/key-mapper-gtk index 99badd19..3238a418 100755 --- a/bin/key-mapper-gtk +++ b/bin/key-mapper-gtk @@ -85,7 +85,7 @@ class Window: self.selected_preset = None self.mappings = [] - gladefile = os.path.join(get_data_path(), 'key-mapper.glade') + gladefile = get_data_path('key-mapper.glade') builder = Gtk.Builder() builder.add_from_file(gladefile) builder.connect_signals(self) @@ -233,7 +233,7 @@ class Window: ) # TODO use user defined mapping - self.mappings = [(10, 'f')] + self.mappings = [(10, 'c')] create_setxkbmap_config( self.selected_device, diff --git a/keymapper/X.py b/keymapper/X.py index cc578e6d..ddd96455 100644 --- a/keymapper/X.py +++ b/keymapper/X.py @@ -35,7 +35,7 @@ import os import re import subprocess -from keymapper.paths import CONFIG_PATH, SYMBOLS_PATH, KEYCODES_PATH +from keymapper.paths import get_home_path, get_usr_path, KEYCODES_PATH from keymapper.logger import logger from keymapper.data import get_data_path from keymapper.presets import get_presets @@ -78,21 +78,21 @@ def create_setxkbmap_config(device, preset, mappings): """ create_identity_mapping() - config_path = os.path.join(CONFIG_PATH, device, preset) + home_path = get_home_path(device, preset) # setxkbmap cannot handle spaces - usr_path = os.path.join(SYMBOLS_PATH, device, preset).replace(' ', '_') + usr_path = get_usr_path(device, preset) - if not os.path.exists(config_path): - logger.info('Creating config file "%s"', config_path) - os.makedirs(os.path.dirname(config_path), exist_ok=True) - os.mknod(config_path) + if not os.path.exists(home_path): + logger.info('Creating config file "%s"', home_path) + os.makedirs(os.path.dirname(home_path), exist_ok=True) + os.mknod(home_path) if not os.path.exists(usr_path): logger.info('Creating symlink in "%s"', usr_path) os.makedirs(os.path.dirname(usr_path), exist_ok=True) - os.symlink(config_path, usr_path) + os.symlink(home_path, usr_path) logger.info('Writing key mappings') - with open(config_path, 'w') as f: + with open(home_path, 'w') as f: f.write(generate_symbols_file_content(device, preset, mappings)) @@ -143,7 +143,7 @@ def create_identity_mapping(): for code in range(minimum, maximum + 1): xkb_keycodes.append(f'<{code}> = {code};') - template_path = os.path.join(get_data_path(), 'xkb_keycodes_template') + template_path = get_data_path('xkb_keycodes_template') with open(template_path, 'r') as template_file: template = template_file.read() @@ -183,7 +183,7 @@ def generate_symbols_file_content(device, preset, mappings): logger.error(f'Unknown keycode <{code}> for "{character}"') xkb_symbols.append(f'key <{code}> {{ [ {character} ] }};') - template_path = os.path.join(get_data_path(), 'xkb_symbols_template') + template_path = get_data_path('xkb_symbols_template') with open(template_path, 'r') as template_file: template = template_file.read() diff --git a/keymapper/data.py b/keymapper/data.py index 46bd1e89..18cd7a82 100644 --- a/keymapper/data.py +++ b/keymapper/data.py @@ -27,7 +27,7 @@ import site import pkg_resources -def get_data_path(): +def get_data_path(filename=''): """Depending on the installation prefix, return the data dir.""" source_path = pkg_resources.require('keymapper')[0].location @@ -44,4 +44,4 @@ def get_data_path(): # installed with -e, running from the cloned git source data_path = os.path.join(source_path, 'data') - return data_path + return os.path.join(data_path, filename) diff --git a/keymapper/paths.py b/keymapper/paths.py index cf5ff9f5..5599a1c4 100644 --- a/keymapper/paths.py +++ b/keymapper/paths.py @@ -29,11 +29,25 @@ import os import subprocess +# should not contain spaces SYMBOLS_PATH = '/usr/share/X11/xkb/symbols/key-mapper' - KEYCODES_PATH = '/usr/share/X11/xkb/keycodes/key-mapper' # 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] CONFIG_PATH = os.path.join('/home', who, '.config/key-mapper') + + +def get_home_path(device, preset=''): + """Get the path to the config file in /usr.""" + return os.path.join(CONFIG_PATH, device, preset) + + +def get_usr_path(device, preset=''): + """Get the path to the config file in /usr.""" + return os.path.join( + SYMBOLS_PATH, + device.replace(' ', '_'), + preset.replace(' ', '_') + )