input-remapper/keymapper/gtk/window.py

574 lines
20 KiB
Python
Raw Normal View History

2020-11-09 22:16:30 +00:00
#!/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."""
2020-12-19 23:34:37 +00:00
import math
import evdev
from evdev.ecodes import EV_KEY
2020-11-09 22:16:30 +00:00
from gi.repository import Gtk, Gdk, GLib
from keymapper.data import get_data_path
from keymapper.paths import get_config_path, get_preset_path
from keymapper.state import custom_mapping, XMODMAP_FILENAME
2020-11-09 22:16:30 +00:00
from keymapper.presets import get_presets, find_newest_preset, \
2020-11-18 09:33:59 +00:00
delete_preset, rename_preset, get_available_preset_name
2020-11-09 22:16:30 +00:00
from keymapper.logger import logger
from keymapper.getdevices import get_devices
from keymapper.gtk.row import Row, to_string
2020-11-14 23:27:45 +00:00
from keymapper.gtk.unsaved import unsaved_changes_dialog, GO_BACK
from keymapper.dev.reader import keycode_reader
2020-11-22 20:04:09 +00:00
from keymapper.daemon import get_dbus_interface
2020-11-25 22:55:31 +00:00
from keymapper.config import config
2020-12-05 12:08:07 +00:00
from keymapper.dev.macros import is_this_a_macro, parse
2020-12-06 14:11:13 +00:00
from keymapper.dev import permissions
2020-11-09 22:16:30 +00:00
def gtk_iteration():
"""Iterate while events are pending."""
while Gtk.events_pending():
Gtk.main_iteration()
CTX_SAVE = 0
CTX_APPLY = 1
CTX_ERROR = 3
def get_selected_row_bg():
"""Get the background color that a row is going to have when selected."""
# ListBoxRows can be selected, but either they are always selectable
# via mouse clicks and via code, or not at all. I just want to controll
# it over code. So I have to add a class and change the background color
# to act like it's selected. For this I need the right color, but
# @selected_bg_color doesn't work for every theme. So get it from
# some widget (which is deprecated according to the docs, but it works...)
row = Gtk.ListBoxRow()
row.show_all()
context = row.get_style_context()
color = context.get_background_color(Gtk.StateFlags.SELECTED)
# but this way it can be made only slightly highlighted, which is nice
color.alpha /= 4
row.destroy()
return color.to_string()
2020-12-19 21:24:23 +00:00
class HandlerDisabled:
"""Safely modify a widget without causing handlers to be called.
Use in a with statement.
"""
def __init__(self, widget, handler):
self.widget = widget
self.handler = handler
def __enter__(self):
self.widget.handler_block_by_func(self.handler)
def __exit__(self, *_):
self.widget.handler_unblock_by_func(self.handler)
2020-11-09 22:16:30 +00:00
class Window:
"""User Interface."""
def __init__(self):
2020-11-22 20:04:09 +00:00
self.dbus = get_dbus_interface()
2020-11-09 22:16:30 +00:00
self.selected_device = None
self.selected_preset = None
css_provider = Gtk.CssProvider()
2020-11-22 20:41:29 +00:00
with open(get_data_path('style.css'), 'r') as file:
data = (
2020-11-22 20:41:29 +00:00
file.read() +
'\n.changed{background-color:' +
get_selected_row_bg() +
';}\n'
)
css_provider.load_from_data(bytes(data, encoding='UTF-8'))
2020-11-09 22:16:30 +00:00
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
gladefile = get_data_path('key-mapper.glade')
builder = Gtk.Builder()
builder.add_from_file(gladefile)
builder.connect_signals(self)
self.builder = builder
window = self.get('window')
window.show()
2020-11-16 16:04:04 +00:00
# hide everything until stuff is populated
2020-11-25 23:07:28 +00:00
self.get('vertical-wrapper').set_opacity(0)
2020-11-09 22:16:30 +00:00
self.window = window
# if any of the next steps take a bit to complete, have the window
# already visible (without content) to make it look more responsive.
2020-11-15 19:45:41 +00:00
gtk_iteration()
2020-12-06 14:11:13 +00:00
permission_errors = permissions.can_read_devices()
2020-12-05 17:49:52 +00:00
if len(permission_errors) > 0:
permission_errors = [(
'Usually, key-mapper-gtk should be started with pkexec '
'or sudo.'
)] + permission_errors
2020-12-05 17:49:52 +00:00
self.show_status(
CTX_ERROR,
'Permission error, hover for info',
2020-12-05 17:49:52 +00:00
'\n\n'.join(permission_errors)
)
2020-11-09 22:16:30 +00:00
self.populate_devices()
self.select_newest_preset()
2020-11-30 13:34:27 +00:00
self.timeouts = [
GLib.timeout_add(100, self.check_add_row),
GLib.timeout_add(1000 / 30, self.consume_newest_keycode)
]
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
2020-11-25 23:07:28 +00:00
self.get('vertical-wrapper').set_opacity(1)
2020-11-16 16:04:04 +00:00
2020-12-26 15:42:57 +00:00
self.ctrl = 0
def key_press(self, window, event):
"""To execute shortcuts.
This has nothing to do with the keycode reader.
"""
gdk_keycode = event.get_keyval()[1]
if gdk_keycode == Gdk.KEY_Control_L:
self.ctrl = True
if gdk_keycode == Gdk.KEY_q and self.ctrl:
self.on_close()
def key_release(self, window, event):
"""To execute shortcuts.
This has nothing to do with the keycode reader.
"""
gdk_keycode = event.get_keyval()[1]
if gdk_keycode == Gdk.KEY_Control_L:
self.ctrl = False
2020-12-19 23:34:37 +00:00
def initialize_gamepad_config(self):
"""Set slider and dropdown values when a gamepad is selected."""
devices = get_devices()
if devices[self.selected_device]['gamepad']:
2020-12-19 23:47:01 +00:00
self.get('gamepad_separator').show()
2020-12-19 23:34:37 +00:00
self.get('gamepad_config').show()
else:
2020-12-19 23:47:01 +00:00
self.get('gamepad_separator').hide()
2020-12-19 23:34:37 +00:00
self.get('gamepad_config').hide()
return
left_purpose = custom_mapping.get('gamepad.joystick.left_purpose')
self.get('left_joystick_purpose').set_active_id(left_purpose)
right_purpose = custom_mapping.get('gamepad.joystick.right_purpose')
self.get('right_joystick_purpose').set_active_id(right_purpose)
pointer_speed = custom_mapping.get('gamepad.joystick.pointer_speed')
range_value = math.log(pointer_speed, 2)
self.get('joystick_mouse_speed').set_value(range_value)
2020-11-09 22:16:30 +00:00
def get(self, name):
"""Get a widget from the window"""
return self.builder.get_object(name)
def on_close(self, *_):
"""Safely close the application."""
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 = []
2020-11-30 13:34:27 +00:00
keycode_reader.stop_reading()
2020-12-04 14:31:32 +00:00
Gtk.main_quit()
2020-11-09 22:16:30 +00:00
def check_add_row(self):
"""Ensure that one empty row is available at all times."""
2020-11-15 00:35:35 +00:00
num_rows = len(self.get('key_list').get_children())
2020-11-09 22:16:30 +00:00
# verify that all mappings are displayed. One of them
# is possible the empty row
num_maps = len(custom_mapping)
if num_rows < num_maps or num_rows > num_maps + 1:
2020-11-15 00:35:35 +00:00
raise AssertionError(
f'custom_mapping contains {len(custom_mapping)} rows, '
f'but {num_rows} are displayed'
2020-11-15 00:35:35 +00:00
)
2020-11-09 22:16:30 +00:00
2020-11-15 00:35:35 +00:00
if num_rows == len(custom_mapping):
2020-11-09 22:16:30 +00:00
self.add_empty()
return True
def select_newest_preset(self):
"""Find and select the newest preset."""
device, preset = find_newest_preset()
if device is not None:
self.get('device_selection').set_active_id(device)
if preset is not None:
self.get('device_selection').set_active_id(preset)
def populate_devices(self):
"""Make the devices selectable."""
devices = get_devices()
device_selection = self.get('device_selection')
for device in devices:
device_selection.append(device, device)
def populate_presets(self):
2020-11-30 13:34:27 +00:00
"""Show the available presets for the selected device.
This will destroy unsaved changes in the custom_mapping.
"""
self.get('preset_name_input').set_text('')
2020-11-09 22:16:30 +00:00
device = self.selected_device
presets = get_presets(device)
2020-11-09 22:16:30 +00:00
if len(presets) == 0:
2020-11-18 09:33:59 +00:00
new_preset = get_available_preset_name(self.selected_device)
2020-11-30 13:34:27 +00:00
custom_mapping.empty()
path = get_preset_path(self.selected_device, new_preset)
custom_mapping.save(path)
2020-11-18 09:33:59 +00:00
presets = [new_preset]
2020-11-09 22:16:30 +00:00
else:
logger.debug('"%s" presets: "%s"', device, '", "'.join(presets))
2020-11-09 22:16:30 +00:00
preset_selection = self.get('preset_selection')
preset_selection.handler_block_by_func(self.on_select_preset)
# otherwise the handler is called with None for each removed preset
preset_selection.remove_all()
preset_selection.handler_unblock_by_func(self.on_select_preset)
for preset in presets:
preset_selection.append(preset, preset)
# and select the newest one (on the top)
preset_selection.set_active(0)
def clear_mapping_table(self):
"""Remove all rows from the mappings table."""
key_list = self.get('key_list')
key_list.forall(key_list.remove)
custom_mapping.empty()
2020-11-09 22:16:30 +00:00
2020-11-14 23:40:19 +00:00
def unhighlight_all_rows(self):
"""Remove all rows from the mappings table."""
key_list = self.get('key_list')
key_list.forall(lambda row: row.unhighlight())
def can_modify_mapping(self, *args):
"""Show a message if changing the mapping is not possible."""
2020-12-26 18:36:47 +00:00
if not self.dbus.is_injecting(self.selected_device):
return
# because the device is in grab mode by the daemon and
# therefore the original keycode inaccessible
logger.info('Cannot change keycodes while injecting')
self.show_status(CTX_ERROR, 'Use "Restore Defaults" before editing')
def get_focused_row(self):
"""Get the Row and its child that is currently in focus."""
focused = self.window.get_focus()
if focused is None:
return None, None
box = focused.get_parent()
if box is None:
return None, None
row = box.get_parent()
if not isinstance(row, Row):
return None, None
return row, focused
2020-11-30 13:34:27 +00:00
def consume_newest_keycode(self):
"""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
# events, so it became a GLib timeout to periodically check kernel
# events.
key = keycode_reader.read()
if key is None:
2020-11-30 13:34:27 +00:00
return True
self.get('keycode').set_text(to_string(*key))
# inform the currently selected row about the new keycode
row, focused = self.get_focused_row()
if isinstance(focused, Gtk.ToggleButton):
row.set_new_keycode(key)
2020-11-30 13:34:27 +00:00
return True
2020-11-22 20:41:29 +00:00
def on_apply_system_layout_clicked(self, _):
2020-11-14 21:07:43 +00:00
"""Load the mapping."""
2020-11-22 20:04:09 +00:00
self.dbus.stop_injecting(self.selected_device)
2020-12-05 17:49:52 +00:00
self.show_status(CTX_APPLY, 'Applied the system default')
2020-12-02 17:07:46 +00:00
GLib.timeout_add(10, 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):
"""Show a status message and set its tooltip."""
if tooltip is None:
tooltip = message
if context_id == CTX_ERROR:
self.get('error_status_icon').show()
else:
self.get('error_status_icon').hide()
2020-12-05 12:08:07 +00:00
status_bar = self.get('status_bar')
status_bar.push(context_id, message)
status_bar.set_tooltip_text(tooltip)
def check_macro_syntax(self):
"""Check if the programmed macros are allright."""
for key, output in custom_mapping:
2020-12-05 12:08:07 +00:00
if not is_this_a_macro(output):
continue
error = parse(output, custom_mapping, return_errors=True)
2020-12-05 12:08:07 +00:00
if error is None:
continue
position = to_string(*key)
2020-12-05 12:08:07 +00:00
msg = f'Syntax error at {position}, hover for info'
self.show_status(CTX_ERROR, msg, error)
2020-11-09 22:16:30 +00:00
def on_save_preset_clicked(self, button):
"""Save changes to a preset to the file system."""
new_name = self.get('preset_name_input').get_text()
try:
2020-12-05 12:08:07 +00:00
self.save_preset()
2020-11-22 20:41:29 +00:00
if new_name not in ['', self.selected_preset]:
2020-11-09 22:16:30 +00:00
rename_preset(
self.selected_device,
self.selected_preset,
new_name
)
# after saving the config, its modification date will be the
# newest, so populate_presets will automatically select the
# right one again.
self.populate_presets()
2020-12-05 12:08:07 +00:00
self.show_status(CTX_SAVE, f'Saved "{self.selected_preset}"')
self.check_macro_syntax()
2020-11-22 20:41:29 +00:00
except PermissionError as error:
2020-12-26 18:36:47 +00:00
error = str(error)
self.show_status(CTX_ERROR, 'Error: Permission denied!', error)
logger.error(error)
2020-11-09 22:16:30 +00:00
2020-11-22 20:41:29 +00:00
def on_delete_preset_clicked(self, _):
2020-11-09 22:16:30 +00:00
"""Delete a preset from the file system."""
delete_preset(self.selected_device, self.selected_preset)
self.populate_presets()
2020-11-22 20:41:29 +00:00
def on_apply_preset_clicked(self, _):
2020-11-09 22:16:30 +00:00
"""Apply a preset without saving changes."""
preset = self.selected_preset
device = self.selected_device
logger.debug('Applying preset "%s" for "%s"', preset, device)
if custom_mapping.changed:
2020-12-26 17:46:15 +00:00
self.show_status(
CTX_APPLY,
f'Applied outdated preset "{preset}"',
'Click "Save" first for changes to take effect'
)
else:
2020-12-05 12:08:07 +00:00
self.show_status(CTX_APPLY, f'Applied preset "{preset}"')
2020-12-26 18:36:47 +00:00
path = get_preset_path(device, preset)
xmodmap = get_config_path(XMODMAP_FILENAME)
2020-12-26 18:36:47 +00:00
success = self.dbus.start_injecting(device, path, xmodmap)
2020-11-22 20:04:09 +00:00
if not success:
2020-12-05 12:08:07 +00:00
self.show_status(CTX_ERROR, 'Error: Could not grab devices!')
# restart reading because after injecting the device landscape
# changes a bit
keycode_reader.start_reading(device)
2020-12-02 17:07:46 +00:00
GLib.timeout_add(10, self.show_device_mapping_status)
2020-12-19 21:24:23 +00:00
def on_autoload_switch(self, _, active):
2020-11-25 22:55:31 +00:00
"""Load the preset automatically next time the user logs in."""
device = self.selected_device
preset = self.selected_preset
2020-11-30 19:57:09 +00:00
config.set_autoload_preset(device, preset if active else None)
2020-11-25 22:55:31 +00:00
config.save_config()
2020-11-09 22:16:30 +00:00
def on_select_device(self, dropdown):
"""List all presets, create one if none exist yet."""
2020-11-14 23:27:45 +00:00
if dropdown.get_active_id() == self.selected_device:
return
if custom_mapping.changed and unsaved_changes_dialog() == GO_BACK:
dropdown.set_active_id(self.selected_device)
return
# selecting a device will also automatically select a different
# preset. Prevent another unsaved-changes dialog to pop up
custom_mapping.changed = False
2020-11-09 22:16:30 +00:00
device = dropdown.get_active_text()
logger.debug('Selecting device "%s"', device)
self.selected_device = device
self.selected_preset = None
self.populate_presets()
2020-12-02 17:07:46 +00:00
GLib.idle_add(lambda: keycode_reader.start_reading(device))
self.show_device_mapping_status()
def show_device_mapping_status(self):
"""Figure out if this device is currently under keymappers control."""
if self.dbus.is_injecting(self.selected_device):
logger.info('This device is currently mapped.')
self.get('apply_system_layout').set_opacity(1)
else:
self.get('apply_system_layout').set_opacity(0.4)
2020-11-09 22:16:30 +00:00
2020-11-22 20:41:29 +00:00
def on_create_preset_clicked(self, _):
2020-11-09 22:16:30 +00:00
"""Create a new preset and select it."""
2020-11-14 19:35:57 +00:00
if custom_mapping.changed:
2020-11-14 23:27:45 +00:00
if unsaved_changes_dialog() == GO_BACK:
2020-11-14 19:35:57 +00:00
return
2020-11-16 23:51:57 +00:00
try:
2020-11-18 09:33:59 +00:00
new_preset = get_available_preset_name(self.selected_device)
custom_mapping.empty()
path = get_preset_path(self.selected_device, new_preset)
custom_mapping.save(path)
2020-11-16 23:51:57 +00:00
self.get('preset_selection').append(new_preset, new_preset)
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)
self.show_status(CTX_ERROR, 'Error: Permission denied!', error)
logger.error(error)
2020-11-09 22:16:30 +00:00
def on_select_preset(self, dropdown):
"""Show the mappings of the preset."""
2020-11-14 23:27:45 +00:00
if dropdown.get_active_id() == self.selected_preset:
return
if custom_mapping.changed and unsaved_changes_dialog() == GO_BACK:
dropdown.set_active_id(self.selected_preset)
return
2020-11-09 22:16:30 +00:00
self.clear_mapping_table()
preset = dropdown.get_active_text()
logger.debug('Selecting preset "%s"', preset)
self.selected_preset = preset
custom_mapping.load(get_preset_path(self.selected_device, self.selected_preset))
2020-11-09 22:16:30 +00:00
key_list = self.get('key_list')
for key, output in custom_mapping:
2020-11-09 22:16:30 +00:00
single_key_mapping = Row(
window=self,
delete_callback=self.on_row_removed,
key=key,
2020-11-18 09:33:59 +00:00
character=output
2020-11-09 22:16:30 +00:00
)
2020-11-14 23:27:45 +00:00
key_list.insert(single_key_mapping, -1)
2020-11-09 22:16:30 +00:00
2020-11-26 20:33:31 +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):
autoload_switch.set_active(config.is_autoloaded(
self.selected_device,
self.selected_preset
))
2020-11-26 20:33:31 +00:00
self.get('preset_name_input').set_text('')
2020-11-09 22:16:30 +00:00
self.add_empty()
2020-12-19 23:34:37 +00:00
self.initialize_gamepad_config()
def on_left_joystick_purpose_changed(self, dropdown):
"""Set the purpose of the left joystick."""
purpose = dropdown.get_active_id()
custom_mapping.set('gamepad.joystick.left_purpose', purpose)
def on_right_joystick_purpose_changed(self, dropdown):
"""Set the purpose of the right joystick."""
purpose = dropdown.get_active_id()
custom_mapping.set('gamepad.joystick.right_purpose', purpose)
def on_joystick_mouse_speed_change_value(self, gtk_range):
2020-12-19 23:34:37 +00:00
"""Set how fast the joystick moves the mouse."""
speed = 2 ** gtk_range.get_value()
2020-12-19 23:34:37 +00:00
custom_mapping.set('gamepad.joystick.pointer_speed', speed)
2020-11-09 22:16:30 +00:00
def add_empty(self):
2020-11-22 20:41:29 +00:00
"""Add one empty row for a single mapped key."""
2020-11-09 22:16:30 +00:00
empty = Row(
window=self,
delete_callback=self.on_row_removed
)
key_list = self.get('key_list')
2020-11-14 23:27:45 +00:00
key_list.insert(empty, -1)
2020-11-09 22:16:30 +00:00
def on_row_removed(self, single_key_mapping):
"""Stuff to do when a row was removed
Parameters
----------
single_key_mapping : Row
"""
key_list = self.get('key_list')
# https://stackoverflow.com/a/30329591/4417769
2020-11-14 23:27:45 +00:00
key_list.remove(single_key_mapping)
2020-11-09 22:16:30 +00:00
2020-12-05 12:08:07 +00:00
def save_preset(self):
"""Write changes to presets to disk."""
2020-11-09 22:16:30 +00:00
if self.selected_device is None or self.selected_preset is None:
return
logger.info(
'Updating configs for "%s", "%s"',
self.selected_device,
self.selected_preset
)
path = get_preset_path(self.selected_device, self.selected_preset)
custom_mapping.save(path)
2020-11-14 19:35:57 +00:00
custom_mapping.changed = False
2020-11-14 23:40:19 +00:00
self.unhighlight_all_rows()