|
|
|
#!/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/>.
|
|
|
|
|
|
|
|
|
|
|
|
"""User Interface."""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
gi.require_version('GLib', '2.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
from keymapper.data import get_data_path
|
|
|
|
from keymapper.presets import find_devices, get_presets, get_mappings, \
|
|
|
|
find_newest_preset, create_preset
|
|
|
|
from keymapper.logger import logger, update_verbosity, log_info
|
|
|
|
|
|
|
|
|
|
|
|
window = None
|
|
|
|
|
|
|
|
|
|
|
|
class SingleKeyMapping:
|
|
|
|
"""A single, configurable key mapping."""
|
|
|
|
def __init__(self, delete_callback):
|
|
|
|
"""Construct a row and add it to the list in the GUI."""
|
|
|
|
self.delete_callback = delete_callback
|
|
|
|
self.put_together()
|
|
|
|
|
|
|
|
def get_widgets(self):
|
|
|
|
"""Return the widget that wraps all the widgets of the row."""
|
|
|
|
return self.widgets
|
|
|
|
|
|
|
|
def put_together(self):
|
|
|
|
"""Create all GTK widgets."""
|
|
|
|
|
|
|
|
delete_button = Gtk.Button()
|
|
|
|
destroy_icon = Gtk.Image.new_from_icon_name(
|
|
|
|
'window-close', Gtk.IconSize.BUTTON
|
|
|
|
)
|
|
|
|
delete_button.set_image(destroy_icon)
|
|
|
|
delete_button.connect('clicked', self.on_delete_button_clicked)
|
|
|
|
|
|
|
|
key_code = Gtk.Entry()
|
|
|
|
key_code.set_width_chars(4)
|
|
|
|
|
|
|
|
original_key = Gtk.Entry()
|
|
|
|
original_key.set_width_chars(4)
|
|
|
|
|
|
|
|
self.widgets = (delete_button, key_code, original_key)
|
|
|
|
|
|
|
|
def on_delete_button_clicked(self, button):
|
|
|
|
"""Destroy the row and remove it from the config."""
|
|
|
|
for widget in self.widgets:
|
|
|
|
widget.destroy()
|
|
|
|
self.delete_callback(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Window:
|
|
|
|
"""User Interface."""
|
|
|
|
def __init__(self):
|
|
|
|
self.rows = 0
|
|
|
|
self.selected_device = None
|
|
|
|
|
|
|
|
gladefile = os.path.join(get_data_path(), 'key-mapper.glade')
|
|
|
|
builder = Gtk.Builder()
|
|
|
|
builder.add_from_file(gladefile)
|
|
|
|
builder.connect_signals(self)
|
|
|
|
self.builder = builder
|
|
|
|
|
|
|
|
window = builder.get_object('window')
|
|
|
|
window.show()
|
|
|
|
self.window = window
|
|
|
|
|
|
|
|
self.populate_devices()
|
|
|
|
|
|
|
|
# find an select the newest preset based on file modification dates
|
|
|
|
device, preset = find_newest_preset()
|
|
|
|
if device is not None:
|
|
|
|
self.on_select_device(device)
|
|
|
|
if preset is not None:
|
|
|
|
self.on_select_preset(preset)
|
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
"""Get a widget from the window"""
|
|
|
|
return self.builder.get_object(name)
|
|
|
|
|
|
|
|
def on_close(self, *_):
|
|
|
|
"""Safely close the application."""
|
|
|
|
Gtk.main_quit()
|
|
|
|
|
|
|
|
def populate_devices(self):
|
|
|
|
"""Make the devices selectable."""
|
|
|
|
devices = find_devices()
|
|
|
|
device_selection = self.get('device_selection')
|
|
|
|
for (id, device) in devices:
|
|
|
|
device_selection.append(device, device)
|
|
|
|
|
|
|
|
def populate_presets(self):
|
|
|
|
"""Show the available presets for the selected device."""
|
|
|
|
presets = get_presets(self.selected_device)
|
|
|
|
preset_selection = self.get('preset_selection')
|
|
|
|
for preset in presets:
|
|
|
|
preset_selection.append(preset, preset)
|
|
|
|
|
|
|
|
def on_select_device(self, device):
|
|
|
|
"""List all presets, create one if none exist yet."""
|
|
|
|
if isinstance(device, Gtk.ComboBoxText):
|
|
|
|
preset = device.get_active_text()
|
|
|
|
|
|
|
|
device = device.get_active_text()
|
|
|
|
presets = get_presets(device)
|
|
|
|
if len(presets) == 0:
|
|
|
|
create_preset(device)
|
|
|
|
self.selected_device = device
|
|
|
|
self.populate_presets()
|
|
|
|
|
|
|
|
def on_create_preset_clicked(self, button):
|
|
|
|
"""Create a new preset and select it."""
|
|
|
|
new_preset = create_preset(self.selected_device)
|
|
|
|
self.get('preset_selection').append(new_preset, new_preset)
|
|
|
|
self.get('preset_selection').set_active_id(new_preset)
|
|
|
|
|
|
|
|
def on_select_preset(self, preset):
|
|
|
|
"""Show the mappings of the preset"""
|
|
|
|
if isinstance(preset, Gtk.ComboBoxText):
|
|
|
|
preset = preset.get_active_text()
|
|
|
|
|
|
|
|
# prepare one empty input to add stuff, and to get the grid to
|
|
|
|
# the correct column width, otherwise it may jump if the user adds
|
|
|
|
# the first row.
|
|
|
|
key_list = self.get('key_list')
|
|
|
|
for i in range(self.rows):
|
|
|
|
# don't remove the header
|
|
|
|
key_list.remove_row(i + 1)
|
|
|
|
self.rows = 0
|
|
|
|
|
|
|
|
# TODO show all mapped keys from config
|
|
|
|
|
|
|
|
self.on_add_key_clicked()
|
|
|
|
|
|
|
|
def on_add_key_clicked(self, button=None):
|
|
|
|
"""Add a mapping to the list of mappings."""
|
|
|
|
single_key_mapping = SingleKeyMapping(self.on_row_removed)
|
|
|
|
key_list = self.get('key_list')
|
|
|
|
key_list.insert_row(1)
|
|
|
|
widgets = single_key_mapping.get_widgets()
|
|
|
|
key_list.attach(widgets[0], 0, 1, 1, 1)
|
|
|
|
key_list.attach(widgets[1], 1, 1, 1, 1)
|
|
|
|
key_list.attach(widgets[2], 2, 1, 1, 1)
|
|
|
|
key_list.show_all()
|
|
|
|
self.rows += 1
|
|
|
|
|
|
|
|
def on_row_removed(self, mapping):
|
|
|
|
"""Stuff to do when a row was removed
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
mapping : SingleKeyMapping
|
|
|
|
"""
|
|
|
|
# shrink the window down as much as possible, otherwise it
|
|
|
|
# will increase with each added mapping but won't go back when they
|
|
|
|
# are removed.
|
|
|
|
window = self.get('window')
|
|
|
|
window.resize(window.get_size()[0], 1)
|
|
|
|
# note, that the grid row still exist, it just shrank down to 0
|
|
|
|
# because there are no contents.
|
|
|
|
self.rows -= 1
|
|
|
|
if self.rows == 0:
|
|
|
|
# add back an empty row
|
|
|
|
self.on_add_key_clicked()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--debug', action='store_true', dest='debug',
|
|
|
|
help='Displays additional debug information',
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
|
|
|
|
options = parser.parse_args(sys.argv[1:])
|
|
|
|
update_verbosity(options.debug)
|
|
|
|
log_info()
|
|
|
|
|
|
|
|
window = Window()
|
|
|
|
Gtk.main()
|