button to load the default preset 2

first
sezanzeb 4 years ago
parent 07b12cf17c
commit 20dbb4c8a8

@ -483,7 +483,7 @@
</packing>
</child>
<child>
<object class="GtkButton" id="apply_defaults">
<object class="GtkButton" id="apply_system_layout">
<property name="label" translatable="yes">Apply Defaults</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@ -494,7 +494,7 @@
<property name="margin_bottom">10</property>
<property name="border_width">0</property>
<property name="image">gtk-redo-icon-2</property>
<signal name="clicked" handler="on_apply_defaults_clicked" swapped="no"/>
<signal name="clicked" handler="on_apply_system_layout_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>

@ -164,23 +164,48 @@ def apply_preset(device, preset):
setxkbmap(device, layout)
def get_system_layout():
"""Get the system wide configured default keyboard layout."""
localectl = subprocess.check_output(
['localectl', 'status']
).decode().split('\n')
# example:
# System Locale: LANG=en_GB.UTF-8
# VC Keymap: tmp
# X11 Layout: de
# X11 Model: pc105
return [
line for line in localectl
if 'X11 Layout' in line
][0].split(': ')[-1]
def setxkbmap(device, layout):
"""Apply a preset to the device.
Parameters
----------
device : string
layout : string
For example 'de', passed to setxkbmap unmodified
layout : string or None
For example 'de', passed to setxkbmap unmodified. If None, will
load the system default
"""
with open(os.path.join(X11_SYMBOLS, layout), 'r') as f:
if layout is not None:
path = os.path.join(X11_SYMBOLS, layout)
if not os.path.exists(path):
logger.error('Symbols %s don\'t exist', path)
return
with open(path, 'r') as f:
if f.read() == '':
logger.error('Tried to load empty config')
logger.error('Tried to load empty symbols %s', path)
return
logger.info('Applying layout "%s" on device %s', layout, device)
group = get_devices()[device]
keycodes = None if layout is None else 'key-mapper'
layout = layout or get_system_layout()
# apply it to every device that hangs on the same usb port, because I
# have no idea how to figure out which one of those 3 devices that are
# all named after my mouse to use.
@ -191,10 +216,13 @@ def setxkbmap(device, layout):
cmd = [
'setxkbmap',
'-layout', layout,
'-keycodes', 'key-mapper',
'-device', str(xinput_id)
]
if layout is not None:
cmd += ['-layout', layout]
if keycodes is not None:
cmd += ['-keycodes', keycodes]
logger.debug('Running `%s`', ' '.join(cmd))
subprocess.run(cmd)

@ -29,8 +29,7 @@ from gi.repository import Gtk, Gdk, GLib
from keymapper.data import get_data_path
from keymapper.X import create_setxkbmap_config, apply_preset, \
create_preset, custom_mapping, parse_symbols_file, setxkbmap, \
DEFAULT_SYMBOLS_NAME
create_preset, custom_mapping, parse_symbols_file, setxkbmap
from keymapper.presets import get_presets, find_newest_preset, \
delete_preset, rename_preset
from keymapper.logger import logger
@ -171,9 +170,9 @@ class Window:
key_list.forall(key_list.remove)
custom_mapping.empty()
def on_apply_defaults_clicked(self, button):
"""Load the mapping that was known to be used before key-mapper."""
setxkbmap(self.selected_device, DEFAULT_SYMBOLS_NAME)
def on_apply_system_layout_clicked(self, button):
"""Load the mapping."""
setxkbmap(self.selected_device, None)
def on_save_preset_clicked(self, button):
"""Save changes to a preset to the file system."""

@ -0,0 +1,39 @@
#!/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/>.
import re
import unittest
from keymapper.X import get_system_layout
class Test(unittest.TestCase):
def test_get_system_layout(self):
layout = get_system_layout()
self.assertGreater(len(layout), 0)
# should be all alphanumeric
match = re.findall(r'\w+', layout)
self.assertEqual(len(match), 1)
self.assertEqual(match[0], layout)
if __name__ == "__main__":
unittest.main()
Loading…
Cancel
Save