2020-11-09 22:16:30 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-11-09 22:16:30 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-09 22:16:30 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-09 22:16:30 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2020-11-09 22:16:30 +00:00
|
|
|
# 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""User Interface."""
|
|
|
|
|
|
|
|
|
2020-12-19 23:34:37 +00:00
|
|
|
import math
|
2021-03-21 18:15:20 +00:00
|
|
|
import os
|
2022-01-10 19:37:22 +00:00
|
|
|
import re
|
2021-03-27 12:21:35 +00:00
|
|
|
import sys
|
2022-02-08 10:00:10 +00:00
|
|
|
from inputremapper.gui.gettext import _
|
2020-12-19 23:34:37 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
from evdev.ecodes import EV_KEY
|
2022-01-10 19:37:22 +00:00
|
|
|
from gi.repository import Gtk, GtkSource, Gdk, GLib, GObject
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.input_event import InputEvent
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.data import get_data_path
|
2022-04-17 10:19:23 +00:00
|
|
|
from inputremapper.exceptions import MacroParsingError
|
|
|
|
from inputremapper.configs.paths import get_config_path, get_preset_path
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.system_mapping import system_mapping
|
|
|
|
from inputremapper.gui.active_preset import active_preset
|
2022-01-10 19:37:22 +00:00
|
|
|
from inputremapper.gui.utils import HandlerDisabled
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.preset import (
|
2021-09-26 10:44:56 +00:00
|
|
|
find_newest_preset,
|
|
|
|
get_presets,
|
|
|
|
delete_preset,
|
|
|
|
rename_preset,
|
|
|
|
get_available_preset_name,
|
|
|
|
)
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.logger import logger, COMMIT_HASH, VERSION, EVDEV_VERSION, is_debug
|
|
|
|
from inputremapper.groups import (
|
2021-09-26 10:44:56 +00:00
|
|
|
groups,
|
|
|
|
GAMEPAD,
|
|
|
|
KEYBOARD,
|
|
|
|
UNKNOWN,
|
|
|
|
GRAPHICS_TABLET,
|
|
|
|
TOUCHPAD,
|
|
|
|
MOUSE,
|
|
|
|
)
|
2022-01-10 19:37:22 +00:00
|
|
|
from inputremapper.gui.editor.editor import Editor
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.event_combination import EventCombination
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.gui.reader import reader
|
|
|
|
from inputremapper.gui.helper import is_helper_running
|
2022-03-18 19:03:38 +00:00
|
|
|
from inputremapper.injection.injector import RUNNING, FAILED, NO_GRAB, UPGRADE_EVDEV
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.daemon import Daemon
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.global_config import global_config
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.injection.macros.parse import is_this_a_macro, parse
|
2022-01-14 17:50:57 +00:00
|
|
|
from inputremapper.injection.global_uinputs import global_uinputs
|
2022-01-10 19:37:22 +00:00
|
|
|
from inputremapper.gui.utils import (
|
|
|
|
CTX_ERROR,
|
|
|
|
CTX_MAPPING,
|
|
|
|
CTX_APPLY,
|
|
|
|
CTX_WARNING,
|
|
|
|
gtk_iteration,
|
2022-05-11 21:17:42 +00:00
|
|
|
debounce,
|
2022-01-10 19:37:22 +00:00
|
|
|
)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
# TODO add to .deb and AUR dependencies
|
|
|
|
# https://cjenkins.wordpress.com/2012/05/08/use-gtksourceview-widget-in-glade/
|
|
|
|
GObject.type_register(GtkSource.View)
|
|
|
|
# GtkSource.View() also works:
|
|
|
|
# https://stackoverflow.com/questions/60126579/gtk-builder-error-quark-invalid-object-type-webkitwebview
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
|
2021-01-09 16:44:24 +00:00
|
|
|
CONTINUE = True
|
|
|
|
GO_BACK = False
|
|
|
|
|
2021-03-28 11:19:44 +00:00
|
|
|
ICON_NAMES = {
|
2021-09-26 10:44:56 +00:00
|
|
|
GAMEPAD: "input-gaming",
|
|
|
|
MOUSE: "input-mouse",
|
|
|
|
KEYBOARD: "input-keyboard",
|
|
|
|
GRAPHICS_TABLET: "input-tablet",
|
|
|
|
TOUCHPAD: "input-touchpad",
|
2021-03-28 11:19:44 +00:00
|
|
|
UNKNOWN: None,
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
# sort types that most devices would fall in easily to the right.
|
2021-09-29 18:17:45 +00:00
|
|
|
ICON_PRIORITIES = [GRAPHICS_TABLET, TOUCHPAD, GAMEPAD, MOUSE, KEYBOARD, UNKNOWN]
|
2021-04-02 10:16:34 +00:00
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def if_group_selected(func):
|
2020-12-27 14:38:08 +00:00
|
|
|
"""Decorate a function to only execute if a device is selected."""
|
|
|
|
# this should only happen if no device was found at all
|
2022-01-10 19:37:22 +00:00
|
|
|
def wrapped(self, *args, **kwargs):
|
|
|
|
if self.group is None:
|
2020-12-27 14:38:08 +00:00
|
|
|
return True # work with timeout_add
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
return func(self, *args, **kwargs)
|
2020-12-27 14:38:08 +00:00
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def if_preset_selected(func):
|
2020-12-27 14:38:08 +00:00
|
|
|
"""Decorate a function to only execute if a preset is selected."""
|
|
|
|
# this should only happen if no device was found at all
|
2022-01-10 19:37:22 +00:00
|
|
|
def wrapped(self, *args, **kwargs):
|
|
|
|
if self.preset_name is None or self.group is None:
|
2020-12-27 14:38:08 +00:00
|
|
|
return True # work with timeout_add
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
return func(self, *args, **kwargs)
|
2020-12-27 14:38:08 +00:00
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_close_about(about, event):
|
2022-01-10 19:37:22 +00:00
|
|
|
"""Hide the about dialog without destroying it."""
|
|
|
|
about.hide()
|
|
|
|
return True
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-12-19 21:24:23 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def ensure_everything_saved(func):
|
2022-01-31 19:58:37 +00:00
|
|
|
"""Make sure the editor has written its changes to active_preset and save."""
|
2020-12-19 21:24:23 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def wrapped(self, *args, **kwargs):
|
|
|
|
if self.preset_name:
|
|
|
|
self.editor.gather_changes_and_save()
|
2020-12-19 21:24:23 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
return func(self, *args, **kwargs)
|
2020-12-19 21:24:23 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
return wrapped
|
2021-03-21 13:17:34 +00:00
|
|
|
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
class UserInterface:
|
2022-01-31 19:58:37 +00:00
|
|
|
"""The input-remapper gtk window."""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
def __init__(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
self.dbus = None
|
2020-11-22 20:04:09 +00:00
|
|
|
|
2021-03-22 08:59:16 +00:00
|
|
|
self.start_processes()
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.group = None
|
|
|
|
self.preset_name = None
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
global_uinputs.prepare_all()
|
2020-11-09 22:16:30 +00:00
|
|
|
css_provider = Gtk.CssProvider()
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(get_data_path("style.css"), "r") as file:
|
|
|
|
css_provider.load_from_data(bytes(file.read(), encoding="UTF-8"))
|
2020-11-12 22:59:49 +00:00
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
Gtk.StyleContext.add_provider_for_screen(
|
|
|
|
Gdk.Screen.get_default(),
|
|
|
|
css_provider,
|
2021-09-26 10:44:56 +00:00
|
|
|
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
|
2020-11-09 22:16:30 +00:00
|
|
|
)
|
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
gladefile = get_data_path("input-remapper.glade")
|
2020-11-09 22:16:30 +00:00
|
|
|
builder = Gtk.Builder()
|
|
|
|
builder.add_from_file(gladefile)
|
|
|
|
builder.connect_signals(self)
|
|
|
|
self.builder = builder
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
self.editor = Editor(self)
|
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
# set up the device selection
|
|
|
|
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html#the-view
|
2022-04-17 10:19:23 +00:00
|
|
|
combobox: Gtk.ComboBox = self.get("device_selection")
|
2021-04-23 09:51:21 +00:00
|
|
|
self.device_store = Gtk.ListStore(str, str, str)
|
2021-03-27 12:21:35 +00:00
|
|
|
combobox.set_model(self.device_store)
|
|
|
|
renderer_icon = Gtk.CellRendererPixbuf()
|
|
|
|
renderer_text = Gtk.CellRendererText()
|
|
|
|
renderer_text.set_padding(5, 0)
|
|
|
|
combobox.pack_start(renderer_icon, False)
|
|
|
|
combobox.pack_start(renderer_text, False)
|
2021-09-26 10:44:56 +00:00
|
|
|
combobox.add_attribute(renderer_icon, "icon-name", 1)
|
|
|
|
combobox.add_attribute(renderer_text, "text", 2)
|
2021-04-23 09:51:21 +00:00
|
|
|
combobox.set_id_column(0)
|
2021-03-27 12:21:35 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.confirm_delete = builder.get_object("confirm-delete")
|
|
|
|
self.about = builder.get_object("about-dialog")
|
|
|
|
self.about.connect("delete-event", on_close_about)
|
2021-03-22 21:50:08 +00:00
|
|
|
# set_position needs to be done once initially, otherwise the
|
|
|
|
# dialog is not centered when it is opened for the first time
|
|
|
|
self.about.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
|
2021-03-21 13:17:34 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("version-label").set_text(
|
2022-01-10 19:37:22 +00:00
|
|
|
f"input-remapper {VERSION} {COMMIT_HASH[:7]}"
|
|
|
|
f"\npython-evdev {EVDEV_VERSION}"
|
2021-09-26 10:44:56 +00:00
|
|
|
if EVDEV_VERSION
|
|
|
|
else ""
|
2021-03-21 13:17:34 +00:00
|
|
|
)
|
2021-01-09 16:44:24 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
window = self.get("window")
|
2020-11-09 22:16:30 +00:00
|
|
|
window.show()
|
2020-11-16 16:04:04 +00:00
|
|
|
# hide everything until stuff is populated
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("vertical-wrapper").set_opacity(0)
|
2020-11-09 22:16:30 +00:00
|
|
|
self.window = window
|
|
|
|
|
2022-05-11 21:17:42 +00:00
|
|
|
source_view = self.get("code_editor")
|
|
|
|
source_view.get_buffer().connect("changed", self.check_on_typing)
|
|
|
|
|
2020-11-16 13:08:29 +00:00
|
|
|
# if any of the next steps take a bit to complete, have the window
|
2020-12-04 15:23:45 +00:00
|
|
|
# already visible (without content) to make it look more responsive.
|
2020-11-15 19:45:41 +00:00
|
|
|
gtk_iteration()
|
2020-11-09 22:16:30 +00:00
|
|
|
self.populate_devices()
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.timeouts = []
|
|
|
|
self.setup_timeouts()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2020-11-16 16:04:04 +00:00
|
|
|
# now show the proper finished content of the window
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("vertical-wrapper").set_opacity(1)
|
2020-11-16 16:04:04 +00:00
|
|
|
|
2021-03-17 20:08:15 +00:00
|
|
|
self.ctrl = False
|
2021-04-02 14:33:20 +00:00
|
|
|
self.unreleased_warn = False
|
|
|
|
self.button_left_warn = False
|
2020-12-26 15:42:57 +00:00
|
|
|
|
2021-03-22 08:59:16 +00:00
|
|
|
if not is_helper_running():
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_ERROR, _("The helper did not start"))
|
2021-03-22 08:59:16 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def setup_timeouts(self):
|
|
|
|
"""Setup all GLib timeouts."""
|
|
|
|
self.timeouts = [
|
2021-09-26 10:44:56 +00:00
|
|
|
GLib.timeout_add(1000 / 30, self.consume_newest_keycode),
|
2021-04-23 09:51:21 +00:00
|
|
|
]
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def start_processes(self):
|
|
|
|
"""Start helper and daemon via pkexec to run in the background."""
|
|
|
|
# this function is overwritten in tests
|
|
|
|
self.dbus = Daemon.connect()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
debug = " -d" if is_debug() else ""
|
2022-01-01 12:00:49 +00:00
|
|
|
cmd = f"pkexec input-remapper-control --command helper {debug}"
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug("Running `%s`", cmd)
|
2021-03-27 12:21:35 +00:00
|
|
|
exit_code = os.system(cmd)
|
|
|
|
|
|
|
|
if exit_code != 0:
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.error("Failed to pkexec the helper, code %d", exit_code)
|
2022-02-03 15:46:34 +00:00
|
|
|
sys.exit(11)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
def show_confirm_delete(self):
|
2021-01-09 16:44:24 +00:00
|
|
|
"""Blocks until the user decided about an action."""
|
2022-02-08 10:00:10 +00:00
|
|
|
text = _("Are you sure to delete preset %s?") % self.preset_name
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("confirm-delete-label").set_text(text)
|
2021-03-21 19:47:36 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
self.confirm_delete.show()
|
|
|
|
response = self.confirm_delete.run()
|
|
|
|
self.confirm_delete.hide()
|
|
|
|
return response
|
2021-01-09 16:44:24 +00:00
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_key_press(self, window, event):
|
2020-12-26 15:42:57 +00:00
|
|
|
"""To execute shortcuts.
|
|
|
|
|
|
|
|
This has nothing to do with the keycode reader.
|
|
|
|
"""
|
2022-01-10 19:37:22 +00:00
|
|
|
if self.editor.is_waiting_for_input():
|
|
|
|
# don't perform shortcuts while keys are being recorded
|
2021-04-02 13:08:36 +00:00
|
|
|
return
|
|
|
|
|
2020-12-26 15:42:57 +00:00
|
|
|
gdk_keycode = event.get_keyval()[1]
|
2021-01-05 18:33:47 +00:00
|
|
|
|
|
|
|
if gdk_keycode in [Gdk.KEY_Control_L, Gdk.KEY_Control_R]:
|
2020-12-26 15:42:57 +00:00
|
|
|
self.ctrl = True
|
|
|
|
|
2021-04-02 13:08:36 +00:00
|
|
|
if self.ctrl:
|
|
|
|
# shortcuts
|
|
|
|
if gdk_keycode == Gdk.KEY_q:
|
|
|
|
self.on_close()
|
|
|
|
|
|
|
|
if gdk_keycode == Gdk.KEY_r:
|
2021-04-23 09:51:21 +00:00
|
|
|
reader.refresh_groups()
|
2021-04-02 13:08:36 +00:00
|
|
|
|
|
|
|
if gdk_keycode == Gdk.KEY_Delete:
|
2022-02-12 10:51:42 +00:00
|
|
|
self.on_stop_injecting_clicked()
|
2020-12-26 15:42:57 +00:00
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_key_release(self, window, event):
|
2020-12-26 15:42:57 +00:00
|
|
|
"""To execute shortcuts.
|
|
|
|
|
|
|
|
This has nothing to do with the keycode reader.
|
|
|
|
"""
|
|
|
|
gdk_keycode = event.get_keyval()[1]
|
2021-01-05 18:33:47 +00:00
|
|
|
|
|
|
|
if gdk_keycode in [Gdk.KEY_Control_L, Gdk.KEY_Control_R]:
|
2020-12-26 15:42:57 +00:00
|
|
|
self.ctrl = False
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
def get(self, name):
|
2022-05-11 21:17:42 +00:00
|
|
|
"""Get a widget from the window"""
|
2020-11-09 22:16:30 +00:00
|
|
|
return self.builder.get_object(name)
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2022-02-08 10:00:10 +00:00
|
|
|
def on_close(self, *args):
|
2020-11-09 22:16:30 +00:00
|
|
|
"""Safely close the application."""
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug("Closing window")
|
2020-12-26 15:42:57 +00:00
|
|
|
self.window.hide()
|
2020-11-30 13:34:27 +00:00
|
|
|
for timeout in self.timeouts:
|
|
|
|
GLib.source_remove(timeout)
|
2020-11-30 19:57:09 +00:00
|
|
|
self.timeouts = []
|
2021-03-21 18:15:20 +00:00
|
|
|
reader.terminate()
|
2020-12-04 14:31:32 +00:00
|
|
|
Gtk.main_quit()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2020-11-09 22:16:30 +00:00
|
|
|
def select_newest_preset(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
"""Find and select the newest preset (and its device)."""
|
2022-01-10 19:37:22 +00:00
|
|
|
group_name, preset = find_newest_preset()
|
|
|
|
if group_name is not None:
|
|
|
|
self.get("device_selection").set_active_id(group_name)
|
2020-11-09 22:16:30 +00:00
|
|
|
if preset is not None:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("preset_selection").set_active_id(preset)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2020-11-09 22:16:30 +00:00
|
|
|
def populate_devices(self):
|
|
|
|
"""Make the devices selectable."""
|
2021-09-26 10:44:56 +00:00
|
|
|
device_selection = self.get("device_selection")
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
with HandlerDisabled(device_selection, self.on_select_device):
|
2021-03-27 12:21:35 +00:00
|
|
|
self.device_store.clear()
|
2022-01-01 12:00:49 +00:00
|
|
|
for group in groups.filter(include_inputremapper=False):
|
2021-04-23 09:51:21 +00:00
|
|
|
types = group.types
|
2021-04-02 18:58:05 +00:00
|
|
|
if len(types) > 0:
|
|
|
|
device_type = sorted(types, key=ICON_PRIORITIES.index)[0]
|
|
|
|
icon_name = ICON_NAMES[device_type]
|
|
|
|
else:
|
|
|
|
icon_name = None
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.device_store.append([group.key, icon_name, group.key])
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
self.select_newest_preset()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_group_selected
|
|
|
|
@ensure_everything_saved
|
2020-11-09 22:16:30 +00:00
|
|
|
def populate_presets(self):
|
2020-11-30 13:34:27 +00:00
|
|
|
"""Show the available presets for the selected device.
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
This will destroy unsaved changes in the active_preset.
|
2020-11-30 13:34:27 +00:00
|
|
|
"""
|
2021-04-23 09:51:21 +00:00
|
|
|
presets = get_presets(self.group.name)
|
2020-11-25 22:36:03 +00:00
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
if len(presets) == 0:
|
2021-04-23 09:51:21 +00:00
|
|
|
new_preset = get_available_preset_name(self.group.name)
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.clear()
|
2021-04-23 09:51:21 +00:00
|
|
|
path = self.group.get_preset_path(new_preset)
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.path = path
|
|
|
|
active_preset.save()
|
2020-11-18 09:33:59 +00:00
|
|
|
presets = [new_preset]
|
2020-11-09 22:16:30 +00:00
|
|
|
else:
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.debug('"%s" presets: "%s"', self.group.name, '", "'.join(presets))
|
2020-11-25 22:36:03 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
preset_selection = self.get("preset_selection")
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
with HandlerDisabled(preset_selection, self.on_select_preset):
|
|
|
|
# otherwise the handler is called with None for each preset
|
|
|
|
preset_selection.remove_all()
|
2022-04-17 10:19:23 +00:00
|
|
|
for preset in presets:
|
|
|
|
preset_selection.append(preset, preset)
|
2022-01-10 19:37:22 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
# and select the newest one (on the top). triggers on_select_preset
|
2020-11-09 22:16:30 +00:00
|
|
|
preset_selection.set_active(0)
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
@if_group_selected
|
2022-02-08 10:00:10 +00:00
|
|
|
def can_modify_preset(self, *args) -> bool:
|
2022-05-11 21:17:42 +00:00
|
|
|
"""if changing the preset is possible."""
|
2022-01-16 10:03:07 +00:00
|
|
|
return self.dbus.get_state(self.group.key) != RUNNING
|
2020-12-04 17:01:56 +00:00
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
def consume_newest_keycode(self):
|
2020-12-04 17:01:56 +00:00
|
|
|
"""To capture events from keyboards, mice and gamepads."""
|
2020-11-30 13:34:27 +00:00
|
|
|
# the "event" event of Gtk.Window wouldn't trigger on gamepad
|
2020-12-19 15:04:07 +00:00
|
|
|
# events, so it became a GLib timeout to periodically check kernel
|
|
|
|
# events.
|
2020-12-31 22:16:46 +00:00
|
|
|
|
|
|
|
# letting go of one of the keys of a combination won't just make
|
|
|
|
# it return the leftover key, it will continue to return None because
|
|
|
|
# they have already been read.
|
2022-02-27 14:43:01 +00:00
|
|
|
combination = reader.read()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
if reader.are_new_groups_available():
|
2021-03-21 18:15:20 +00:00
|
|
|
self.populate_devices()
|
2020-11-22 13:47:34 +00:00
|
|
|
|
2022-02-27 14:43:01 +00:00
|
|
|
# giving editor its own interval and making it call reader.read itself causes
|
|
|
|
# incredibly frustrating and miraculous problems. Do not do it. Observations:
|
|
|
|
# - test_autocomplete_key fails if the gui has been launched and closed by a
|
|
|
|
# previous test already
|
|
|
|
# Maybe it has something to do with the order of editor.consume_newest_keycode
|
|
|
|
# and user_interface.populate_devices.
|
|
|
|
self.editor.consume_newest_keycode(combination)
|
2020-11-15 20:03:56 +00:00
|
|
|
|
2020-11-30 13:34:27 +00:00
|
|
|
return True
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_group_selected
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_stop_injecting_clicked(self, *args):
|
2022-01-31 19:58:37 +00:00
|
|
|
"""Stop injecting the preset."""
|
2021-04-23 09:51:21 +00:00
|
|
|
self.dbus.stop_injecting(self.group.key)
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_APPLY, _("Applied the system default"))
|
2021-01-07 16:15:12 +00:00
|
|
|
GLib.timeout_add(100, self.show_device_mapping_status)
|
2020-11-14 19:35:57 +00:00
|
|
|
|
2020-12-05 12:08:07 +00:00
|
|
|
def show_status(self, context_id, message, tooltip=None):
|
2021-03-21 13:17:34 +00:00
|
|
|
"""Show a status message and set its tooltip.
|
2020-12-05 12:08:07 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
If message is None, it will remove the newest message of the
|
|
|
|
given context_id.
|
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
status_bar = self.get("status_bar")
|
2020-12-27 14:38:08 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
if message is None:
|
|
|
|
status_bar.remove_all(context_id)
|
2020-12-27 14:38:08 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
if context_id in (CTX_ERROR, CTX_MAPPING):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("error_status_icon").hide()
|
2020-12-27 12:27:49 +00:00
|
|
|
|
2021-03-21 13:17:34 +00:00
|
|
|
if context_id == CTX_WARNING:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("warning_status_icon").hide()
|
2020-12-31 20:46:57 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
status_bar.set_tooltip_text("")
|
2021-03-21 13:17:34 +00:00
|
|
|
else:
|
|
|
|
if tooltip is None:
|
|
|
|
tooltip = message
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("error_status_icon").hide()
|
|
|
|
self.get("warning_status_icon").hide()
|
2021-03-21 13:17:34 +00:00
|
|
|
|
|
|
|
if context_id in (CTX_ERROR, CTX_MAPPING):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("error_status_icon").show()
|
2021-03-21 13:17:34 +00:00
|
|
|
|
|
|
|
if context_id == CTX_WARNING:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("warning_status_icon").show()
|
2021-03-21 13:17:34 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
max_length = 45
|
|
|
|
if len(message) > max_length:
|
|
|
|
message = message[: max_length - 3] + "..."
|
2021-03-21 13:17:34 +00:00
|
|
|
|
|
|
|
status_bar.push(context_id, message)
|
|
|
|
status_bar.set_tooltip_text(tooltip)
|
2020-12-05 12:08:07 +00:00
|
|
|
|
2022-05-11 21:17:42 +00:00
|
|
|
@debounce(500)
|
|
|
|
def check_on_typing(self, *_):
|
|
|
|
"""To save latest input from code editor and call syntax check."""
|
|
|
|
self.editor.gather_changes_and_save()
|
|
|
|
self.check_macro_syntax()
|
|
|
|
|
|
|
|
def check_macro_syntax(self):
|
|
|
|
"""Check if the programmed macros are allright."""
|
|
|
|
# this is totally redundant as the mapping itself has already checked for
|
|
|
|
# validity but will be reworked anyway.
|
|
|
|
self.show_status(CTX_MAPPING, None)
|
|
|
|
for mapping in active_preset:
|
|
|
|
if not is_this_a_macro(mapping.output_symbol):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
parse(mapping.output_symbol)
|
|
|
|
except MacroParsingError as error:
|
|
|
|
position = mapping.event_combination.beautify()
|
|
|
|
msg = _("Syntax error at %s, hover for info") % position
|
|
|
|
self.show_status(CTX_MAPPING, msg, error)
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_rename_button_clicked(self, button):
|
2021-03-21 13:17:34 +00:00
|
|
|
"""Rename the preset based on the contents of the name input."""
|
2021-09-26 10:44:56 +00:00
|
|
|
new_name = self.get("preset_name_input").get_text()
|
2021-03-17 20:08:15 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
if new_name in ["", self.preset_name]:
|
2021-03-21 13:17:34 +00:00
|
|
|
return
|
2020-12-05 12:08:07 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
new_name = rename_preset(self.group.name, self.preset_name, new_name)
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.path = get_preset_path(self.group.name, new_name)
|
2021-03-21 13:17:34 +00:00
|
|
|
|
|
|
|
# if the old preset was being autoloaded, change the
|
|
|
|
# name there as well
|
2022-01-31 19:58:37 +00:00
|
|
|
is_autoloaded = global_config.is_autoloaded(self.group.key, self.preset_name)
|
2021-03-21 13:17:34 +00:00
|
|
|
if is_autoloaded:
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set_autoload_preset(self.group.key, new_name)
|
2021-03-21 13:17:34 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("preset_name_input").set_text("")
|
2021-03-21 13:17:34 +00:00
|
|
|
self.populate_presets()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_preset_selected
|
2022-02-08 10:00:10 +00:00
|
|
|
def on_delete_preset_clicked(self, *args):
|
2020-11-09 22:16:30 +00:00
|
|
|
"""Delete a preset from the file system."""
|
2021-03-21 13:17:34 +00:00
|
|
|
accept = Gtk.ResponseType.ACCEPT
|
2022-01-31 19:58:37 +00:00
|
|
|
if len(active_preset) > 0 and self.show_confirm_delete() != accept:
|
2021-03-21 13:17:34 +00:00
|
|
|
return
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
# avoid having the text of the symbol input leak into the active_preset again
|
2022-01-10 19:37:22 +00:00
|
|
|
# via a gazillion hooks, causing the preset to be saved again after deleting.
|
|
|
|
self.editor.clear()
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
delete_preset(self.group.name, self.preset_name)
|
2022-01-10 19:37:22 +00:00
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
self.populate_presets()
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_preset_selected
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_apply_preset_clicked(self, button):
|
2020-11-09 22:16:30 +00:00
|
|
|
"""Apply a preset without saving changes."""
|
2021-03-21 18:15:20 +00:00
|
|
|
self.save_preset()
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
if len(active_preset) == 0:
|
2022-02-08 10:00:10 +00:00
|
|
|
logger.error(_("Cannot apply empty preset file"))
|
2021-01-07 16:15:12 +00:00
|
|
|
# also helpful for first time use
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_ERROR, _("You need to add keys and save first"))
|
2021-01-07 16:15:12 +00:00
|
|
|
return
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
preset = self.preset_name
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.info('Applying preset "%s" for "%s"', preset, self.group.key)
|
2020-12-04 14:06:47 +00:00
|
|
|
|
2021-04-02 14:33:20 +00:00
|
|
|
if not self.button_left_warn:
|
2022-01-31 19:58:37 +00:00
|
|
|
if active_preset.dangerously_mapped_btn_left():
|
2021-04-02 14:33:20 +00:00
|
|
|
self.show_status(
|
|
|
|
CTX_ERROR,
|
2021-09-26 10:44:56 +00:00
|
|
|
"This would disable your click button",
|
|
|
|
"Map a button to BTN_LEFT to avoid this.\n"
|
|
|
|
"To overwrite this warning, press apply again.",
|
2021-04-02 14:33:20 +00:00
|
|
|
)
|
|
|
|
self.button_left_warn = True
|
|
|
|
return
|
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
if not self.unreleased_warn:
|
2021-03-21 18:15:20 +00:00
|
|
|
unreleased = reader.get_unreleased_keys()
|
2022-01-31 19:58:37 +00:00
|
|
|
if unreleased is not None and unreleased != EventCombination(
|
|
|
|
InputEvent.btn_left()
|
|
|
|
):
|
2021-01-07 16:15:12 +00:00
|
|
|
# it's super annoying if that happens and may break the user
|
2022-01-31 19:58:37 +00:00
|
|
|
# input in such a way to prevent disabling the preset
|
2021-01-07 16:15:12 +00:00
|
|
|
logger.error(
|
2021-09-29 18:17:45 +00:00
|
|
|
"Tried to apply a preset while keys were held down: %s", unreleased
|
2021-01-07 16:15:12 +00:00
|
|
|
)
|
|
|
|
self.show_status(
|
|
|
|
CTX_ERROR,
|
2021-09-26 10:44:56 +00:00
|
|
|
"Please release your pressed keys first",
|
|
|
|
"X11 will think they are held down forever otherwise.\n"
|
|
|
|
"To overwrite this warning, press apply again.",
|
2021-01-07 16:15:12 +00:00
|
|
|
)
|
|
|
|
self.unreleased_warn = True
|
|
|
|
return
|
|
|
|
|
|
|
|
self.unreleased_warn = False
|
2021-04-02 14:33:20 +00:00
|
|
|
self.button_left_warn = False
|
2021-02-07 14:00:36 +00:00
|
|
|
self.dbus.set_config_dir(get_config_path())
|
2021-04-23 09:51:21 +00:00
|
|
|
self.dbus.start_injecting(self.group.key, preset)
|
2020-11-22 20:04:09 +00:00
|
|
|
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_APPLY, _("Starting injection..."))
|
2020-11-19 00:40:47 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
GLib.timeout_add(100, self.show_injection_result)
|
2020-11-22 16:30:06 +00:00
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_autoload_switch(self, switch, active):
|
2020-11-25 22:55:31 +00:00
|
|
|
"""Load the preset automatically next time the user logs in."""
|
2021-04-23 09:51:21 +00:00
|
|
|
key = self.group.key
|
|
|
|
preset = self.preset_name
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set_autoload_preset(key, preset if active else None)
|
2021-02-07 14:00:36 +00:00
|
|
|
# tell the service to refresh its config
|
|
|
|
self.dbus.set_config_dir(get_config_path())
|
2020-11-25 22:55:31 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2020-11-09 22:16:30 +00:00
|
|
|
def on_select_device(self, dropdown):
|
|
|
|
"""List all presets, create one if none exist yet."""
|
2021-04-23 09:51:21 +00:00
|
|
|
if self.group and dropdown.get_active_id() == self.group.key:
|
2020-11-14 23:27:45 +00:00
|
|
|
return
|
2020-11-12 20:36:40 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
group_key = dropdown.get_active_id()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
if group_key is None:
|
2021-03-21 18:15:20 +00:00
|
|
|
return
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
logger.debug('Selecting device "%s"', group_key)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.group = groups.find(key=group_key)
|
|
|
|
self.preset_name = None
|
2020-11-09 22:16:30 +00:00
|
|
|
|
|
|
|
self.populate_presets()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
reader.start_reading(groups.find(key=group_key))
|
2020-12-02 17:07:46 +00:00
|
|
|
|
|
|
|
self.show_device_mapping_status()
|
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
def show_injection_result(self):
|
|
|
|
"""Show if the injection was successfully started."""
|
2021-04-23 09:51:21 +00:00
|
|
|
state = self.dbus.get_state(self.group.key)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
if state == RUNNING:
|
2022-02-08 10:00:10 +00:00
|
|
|
msg = _("Applied preset %s") % self.preset_name
|
2021-04-02 14:33:20 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
if active_preset.get_mapping(EventCombination(InputEvent.btn_left())):
|
2022-02-08 10:00:10 +00:00
|
|
|
msg += _(", CTRL + DEL to stop")
|
2021-04-02 14:33:20 +00:00
|
|
|
|
|
|
|
self.show_status(CTX_APPLY, msg)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
self.show_device_mapping_status()
|
|
|
|
return False
|
|
|
|
|
|
|
|
if state == FAILED:
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(
|
|
|
|
CTX_ERROR, _("Failed to apply preset %s") % self.preset_name
|
|
|
|
)
|
2021-01-07 16:15:12 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if state == NO_GRAB:
|
|
|
|
self.show_status(
|
|
|
|
CTX_ERROR,
|
2021-09-26 10:44:56 +00:00
|
|
|
"The device was not grabbed",
|
|
|
|
"Either another application is already grabbing it or "
|
|
|
|
"your preset doesn't contain anything that is sent by the "
|
|
|
|
"device.",
|
2021-01-07 16:15:12 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
2022-03-18 19:03:38 +00:00
|
|
|
if state == UPGRADE_EVDEV:
|
|
|
|
self.show_status(
|
|
|
|
CTX_ERROR,
|
|
|
|
"Upgrade python-evdev",
|
|
|
|
"Your python-evdev version is too old.",
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
# keep the timeout running until a relevant state is found
|
2021-01-07 16:15:12 +00:00
|
|
|
return True
|
|
|
|
|
2020-12-02 17:07:46 +00:00
|
|
|
def show_device_mapping_status(self):
|
2022-01-01 12:00:49 +00:00
|
|
|
"""Figure out if this device is currently under inputremappers control."""
|
2022-02-12 10:51:42 +00:00
|
|
|
self.editor.update_toggle_opacity()
|
2021-04-23 09:51:21 +00:00
|
|
|
group_key = self.group.key
|
|
|
|
state = self.dbus.get_state(group_key)
|
2021-03-21 18:15:20 +00:00
|
|
|
if state == RUNNING:
|
2021-04-23 09:51:21 +00:00
|
|
|
logger.info('Group "%s" is currently mapped', group_key)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("apply_system_layout").set_opacity(1)
|
2020-12-02 17:07:46 +00:00
|
|
|
else:
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("apply_system_layout").set_opacity(0.4)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_preset_selected
|
2022-02-08 10:00:10 +00:00
|
|
|
def on_copy_preset_clicked(self, *args):
|
2021-03-21 13:17:34 +00:00
|
|
|
"""Copy the current preset and select it."""
|
2022-01-10 19:37:22 +00:00
|
|
|
self.create_preset(copy=True)
|
2021-03-21 13:17:34 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@if_group_selected
|
2022-02-08 10:00:10 +00:00
|
|
|
def on_create_preset_clicked(self, *args):
|
2022-01-10 19:37:22 +00:00
|
|
|
"""Create a new empty preset and select it."""
|
2021-03-21 13:17:34 +00:00
|
|
|
self.create_preset()
|
2020-11-12 20:36:40 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2021-03-21 13:17:34 +00:00
|
|
|
def create_preset(self, copy=False):
|
|
|
|
"""Create a new preset and select it."""
|
2021-04-23 09:51:21 +00:00
|
|
|
name = self.group.name
|
|
|
|
preset = self.preset_name
|
2021-03-17 20:08:15 +00:00
|
|
|
|
2020-11-16 23:51:57 +00:00
|
|
|
try:
|
2021-03-18 23:24:33 +00:00
|
|
|
if copy:
|
2021-04-23 09:51:21 +00:00
|
|
|
new_preset = get_available_preset_name(name, preset, copy)
|
2021-03-18 23:24:33 +00:00
|
|
|
else:
|
2021-04-23 09:51:21 +00:00
|
|
|
new_preset = get_available_preset_name(name)
|
2022-01-10 19:37:22 +00:00
|
|
|
self.editor.clear()
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.clear()
|
2021-03-17 20:08:15 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
path = self.group.get_preset_path(new_preset)
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.path = path
|
|
|
|
active_preset.save()
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("preset_selection").append(new_preset, new_preset)
|
2022-01-10 19:37:22 +00:00
|
|
|
# triggers on_select_preset
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("preset_selection").set_active_id(new_preset)
|
2022-01-10 19:37:22 +00:00
|
|
|
if self.get("preset_selection").get_active_id() != new_preset:
|
|
|
|
# for whatever reason I have to use set_active_id twice for this
|
|
|
|
# to work in tests all of the sudden
|
|
|
|
self.get("preset_selection").set_active_id(new_preset)
|
2020-11-22 20:41:29 +00:00
|
|
|
except PermissionError as error:
|
2020-12-26 18:36:47 +00:00
|
|
|
error = str(error)
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_ERROR, _("Permission denied!"), error)
|
2020-12-26 18:36:47 +00:00
|
|
|
logger.error(error)
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
@ensure_everything_saved
|
2020-11-09 22:16:30 +00:00
|
|
|
def on_select_preset(self, dropdown):
|
|
|
|
"""Show the mappings of the preset."""
|
2021-03-21 18:15:20 +00:00
|
|
|
# beware in tests that this function won't be called at all if the
|
|
|
|
# active_id stays the same
|
2021-04-23 09:51:21 +00:00
|
|
|
if dropdown.get_active_id() == self.preset_name:
|
2020-11-14 23:27:45 +00:00
|
|
|
return
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
preset = dropdown.get_active_text()
|
2021-03-21 18:15:20 +00:00
|
|
|
if preset is None:
|
|
|
|
return
|
|
|
|
|
2020-11-09 22:16:30 +00:00
|
|
|
logger.debug('Selecting preset "%s"', preset)
|
2022-01-31 12:42:42 +00:00
|
|
|
self.editor.clear_mapping_list()
|
2021-04-23 09:51:21 +00:00
|
|
|
self.preset_name = preset
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.clear()
|
|
|
|
active_preset.path = self.group.get_preset_path(preset)
|
|
|
|
active_preset.load()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
self.editor.load_custom_mapping()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
autoload_switch = self.get("preset_autoload_switch")
|
2020-12-19 21:24:23 +00:00
|
|
|
|
|
|
|
with HandlerDisabled(autoload_switch, self.on_autoload_switch):
|
2022-01-31 19:58:37 +00:00
|
|
|
is_autoloaded = global_config.is_autoloaded(
|
|
|
|
self.group.key, self.preset_name
|
|
|
|
)
|
2022-01-10 19:37:22 +00:00
|
|
|
autoload_switch.set_active(is_autoloaded)
|
2020-11-26 20:33:31 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.get("preset_name_input").set_text("")
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-02-08 10:00:10 +00:00
|
|
|
def save_preset(self, *args):
|
2022-01-31 19:58:37 +00:00
|
|
|
"""Write changes in the active_preset to disk."""
|
|
|
|
if not active_preset.has_unsaved_changes():
|
2022-01-10 19:37:22 +00:00
|
|
|
# optimization, and also avoids tons of redundant logs
|
2022-01-31 19:58:37 +00:00
|
|
|
logger.debug("Not saving because preset did not change")
|
2021-03-21 13:17:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2022-01-10 19:37:22 +00:00
|
|
|
assert self.preset_name is not None
|
2022-04-17 10:19:23 +00:00
|
|
|
active_preset.save()
|
2020-11-09 22:16:30 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
# after saving the preset, its modification date will be the
|
2021-03-21 13:17:34 +00:00
|
|
|
# newest, so populate_presets will automatically select the
|
|
|
|
# right one again.
|
|
|
|
self.populate_presets()
|
|
|
|
except PermissionError as error:
|
|
|
|
error = str(error)
|
2022-02-08 10:00:10 +00:00
|
|
|
self.show_status(CTX_ERROR, _("Permission denied!"), error)
|
2021-03-21 13:17:34 +00:00
|
|
|
logger.error(error)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.show_status(CTX_MAPPING, None)
|
2021-03-21 13:17:34 +00:00
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_about_clicked(self, button):
|
2021-03-21 13:17:34 +00:00
|
|
|
"""Show the about/help dialog."""
|
|
|
|
self.about.show()
|
2021-03-21 19:47:36 +00:00
|
|
|
|
2022-02-12 10:51:42 +00:00
|
|
|
def on_about_key_press(self, window, event):
|
2021-03-21 19:47:36 +00:00
|
|
|
"""Hide the about/help dialog."""
|
|
|
|
gdk_keycode = event.get_keyval()[1]
|
|
|
|
if gdk_keycode == Gdk.KEY_Escape:
|
|
|
|
self.about.hide()
|