input-remapper/keymapper/gui/row.py

410 lines
13 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
2021-02-22 18:48:20 +00:00
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.de>
2020-11-09 22:16:30 +00:00
#
# 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/>.
"""A single, configurable key mapping."""
import evdev
2020-12-27 20:36:14 +00:00
from gi.repository import Gtk, GLib, Gdk
2020-11-09 22:16:30 +00:00
2021-09-29 18:50:32 +00:00
from keymapper.system_mapping import system_mapping
from keymapper.gui.custom_mapping import custom_mapping
2020-11-09 22:16:30 +00:00
from keymapper.logger import logger
2020-12-31 20:47:56 +00:00
from keymapper.key import Key
2021-03-21 18:15:20 +00:00
from keymapper.gui.reader import reader
2020-11-09 22:16:30 +00:00
CTX_KEYCODE = 2
2020-12-06 14:50:05 +00:00
store = Gtk.ListStore(str)
2021-03-28 11:19:44 +00:00
def populate_store():
"""Fill the dropdown for key suggestions with values."""
for name in system_mapping.list_names():
store.append([name])
extra = [
2021-09-26 10:44:56 +00:00
"mouse(up, 1)",
"mouse(down, 1)",
"mouse(left, 1)",
"mouse(right, 1)",
"wheel(up, 1)",
"wheel(down, 1)",
"wheel(left, 1)",
"wheel(right, 1)",
2021-03-28 11:19:44 +00:00
]
for key in extra:
# add some more keys to the dropdown list
store.append([key])
populate_store()
2021-03-21 18:15:20 +00:00
2020-12-06 14:50:05 +00:00
2020-12-31 20:46:57 +00:00
def to_string(key):
"""A nice to show description of the pressed key."""
2020-12-31 20:47:56 +00:00
if isinstance(key, Key):
2021-09-26 10:44:56 +00:00
return " + ".join([to_string(sub_key) for sub_key in key])
2020-12-31 20:55:38 +00:00
if isinstance(key[0], tuple):
2021-09-26 10:44:56 +00:00
raise Exception("deprecated stuff")
2020-12-31 20:46:57 +00:00
ev_type, code, value = key
2020-12-31 20:47:56 +00:00
if ev_type not in evdev.ecodes.bytype:
2021-09-26 10:44:56 +00:00
logger.error("Unknown key type for %s", key)
return "unknown"
2020-12-02 20:03:59 +00:00
2020-12-31 20:47:56 +00:00
if code not in evdev.ecodes.bytype[ev_type]:
2021-09-26 10:44:56 +00:00
logger.error("Unknown key code for %s", key)
return "unknown"
2020-12-31 20:47:56 +00:00
2021-03-21 18:15:20 +00:00
key_name = None
# first try to find the name in xmodmap to not display wrong
# names due to the keyboard layout
if ev_type == evdev.ecodes.EV_KEY:
key_name = system_mapping.get_name(code)
if key_name is None:
# if no result, look in the linux key constants. On a german
# keyboard for example z and y are switched, which will therefore
# cause the wrong letter to be displayed.
key_name = evdev.ecodes.bytype[ev_type][code]
if isinstance(key_name, list):
key_name = key_name[0]
2020-12-31 20:47:56 +00:00
if ev_type != evdev.ecodes.EV_KEY:
direction = {
2021-01-01 21:20:33 +00:00
# D-Pad
2021-09-26 10:44:56 +00:00
(evdev.ecodes.ABS_HAT0X, -1): "Left",
(evdev.ecodes.ABS_HAT0X, 1): "Right",
(evdev.ecodes.ABS_HAT0Y, -1): "Up",
(evdev.ecodes.ABS_HAT0Y, 1): "Down",
(evdev.ecodes.ABS_HAT1X, -1): "Left",
(evdev.ecodes.ABS_HAT1X, 1): "Right",
(evdev.ecodes.ABS_HAT1Y, -1): "Up",
(evdev.ecodes.ABS_HAT1Y, 1): "Down",
(evdev.ecodes.ABS_HAT2X, -1): "Left",
(evdev.ecodes.ABS_HAT2X, 1): "Right",
(evdev.ecodes.ABS_HAT2Y, -1): "Up",
(evdev.ecodes.ABS_HAT2Y, 1): "Down",
2021-01-01 21:20:33 +00:00
# joystick
2021-09-26 10:44:56 +00:00
(evdev.ecodes.ABS_X, 1): "Right",
(evdev.ecodes.ABS_X, -1): "Left",
(evdev.ecodes.ABS_Y, 1): "Down",
(evdev.ecodes.ABS_Y, -1): "Up",
(evdev.ecodes.ABS_RX, 1): "Right",
(evdev.ecodes.ABS_RX, -1): "Left",
(evdev.ecodes.ABS_RY, 1): "Down",
(evdev.ecodes.ABS_RY, -1): "Up",
2021-01-05 18:33:47 +00:00
# wheel
2021-09-26 10:44:56 +00:00
(evdev.ecodes.REL_WHEEL, -1): "Down",
(evdev.ecodes.REL_WHEEL, 1): "Up",
(evdev.ecodes.REL_HWHEEL, -1): "Left",
(evdev.ecodes.REL_HWHEEL, 1): "Right",
2020-12-31 20:47:56 +00:00
}.get((code, value))
if direction is not None:
2021-09-26 10:44:56 +00:00
key_name += f" {direction}"
2020-12-31 20:47:56 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("ABS_Z", "Trigger Left")
key_name = key_name.replace("ABS_RZ", "Trigger Right")
2021-04-02 13:38:40 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("ABS_HAT0X", "DPad")
key_name = key_name.replace("ABS_HAT0Y", "DPad")
key_name = key_name.replace("ABS_HAT1X", "DPad 2")
key_name = key_name.replace("ABS_HAT1Y", "DPad 2")
key_name = key_name.replace("ABS_HAT2X", "DPad 3")
key_name = key_name.replace("ABS_HAT2Y", "DPad 3")
2021-04-02 13:38:40 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("ABS_X", "Joystick")
key_name = key_name.replace("ABS_Y", "Joystick")
key_name = key_name.replace("ABS_RX", "Joystick 2")
key_name = key_name.replace("ABS_RY", "Joystick 2")
2021-04-02 13:38:40 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("BTN_", "Button ")
key_name = key_name.replace("KEY_", "")
2021-04-02 13:38:40 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("REL_", "")
key_name = key_name.replace("HWHEEL", "Wheel")
key_name = key_name.replace("WHEEL", "Wheel")
2021-04-04 11:15:13 +00:00
2021-09-26 10:44:56 +00:00
key_name = key_name.replace("_", " ")
key_name = key_name.replace(" ", " ")
2021-04-02 13:38:40 +00:00
return key_name
2020-12-31 20:47:56 +00:00
2020-12-02 20:03:59 +00:00
2020-12-31 20:46:57 +00:00
IDLE = 0
HOLDING = 1
2020-11-14 23:27:45 +00:00
class Row(Gtk.ListBoxRow):
2020-11-09 22:16:30 +00:00
"""A single, configurable key mapping."""
2021-09-26 10:44:56 +00:00
__gtype_name__ = "ListBoxRow"
2020-11-14 23:27:45 +00:00
def __init__(self, delete_callback, window, key=None, symbol=None):
"""Construct a row widget.
Parameters
----------
2020-12-31 20:47:56 +00:00
key : Key
"""
2020-12-31 20:47:56 +00:00
if key is not None and not isinstance(key, Key):
2021-09-26 10:44:56 +00:00
raise TypeError("Expected key to be a Key object")
2020-12-31 20:47:56 +00:00
2020-11-14 23:27:45 +00:00
super().__init__()
self.device = window.group
2020-11-09 22:16:30 +00:00
self.window = window
self.delete_callback = delete_callback
self.symbol_input = None
2020-11-29 15:22:22 +00:00
self.keycode_input = None
self.key = key
self.put_together(symbol)
2020-11-09 22:16:30 +00:00
2021-01-07 16:15:12 +00:00
self._state = IDLE
2020-12-31 20:46:57 +00:00
2021-01-07 16:15:12 +00:00
def refresh_state(self):
"""Refresh the state.
The state is needed to switch focus when no keys are held anymore,
but only if the row has been in the HOLDING state before.
"""
old_state = self._state
if not self.keycode_input.is_focus():
self._state = IDLE
return
2021-03-21 18:15:20 +00:00
unreleased_keys = reader.get_unreleased_keys()
2021-01-07 16:15:12 +00:00
if unreleased_keys is None and old_state == HOLDING and self.key:
2020-12-31 20:46:57 +00:00
# A key was pressed and then released.
# Switch to the symbol. idle_add this so that the
# keycode event won't write into the symbol input as well.
2020-12-31 20:46:57 +00:00
window = self.window.window
GLib.idle_add(lambda: window.set_focus(self.symbol_input))
2020-12-31 20:46:57 +00:00
2021-01-07 16:15:12 +00:00
if unreleased_keys is not None:
self._state = HOLDING
return
self._state = IDLE
2021-01-01 21:20:33 +00:00
2020-12-31 20:47:56 +00:00
def get_key(self):
"""Get the Key object from the left column.
2020-12-26 17:46:15 +00:00
Or None if no code is mapped on this row.
"""
return self.key
2020-11-09 22:16:30 +00:00
def get_symbol(self):
"""Get the assigned symbol from the middle column."""
symbol = self.symbol_input.get_text()
return symbol if symbol else None
2020-11-09 22:16:30 +00:00
2020-12-31 20:47:56 +00:00
def set_new_key(self, new_key):
"""Check if a keycode has been pressed and if so, display it.
Parameters
----------
new_key : Key
"""
if new_key is not None and not isinstance(new_key, Key):
2021-09-26 10:44:56 +00:00
raise TypeError("Expected new_key to be a Key object")
2020-12-31 20:47:56 +00:00
# the newest_keycode is populated since the ui regularly polls it
# in order to display it in the status bar.
2020-12-31 20:47:56 +00:00
previous_key = self.get_key()
2020-11-09 22:16:30 +00:00
# no input
if new_key is None:
2020-11-09 22:16:30 +00:00
return
2020-12-31 20:46:57 +00:00
# it might end up being a key combination
2021-01-07 16:15:12 +00:00
self._state = HOLDING
2020-12-31 20:46:57 +00:00
2020-11-09 22:16:30 +00:00
# keycode didn't change, do nothing
if new_key == previous_key:
2020-11-09 22:16:30 +00:00
return
# keycode is already set by some other row
existing = custom_mapping.get_symbol(new_key)
if existing is not None:
2020-12-31 20:46:57 +00:00
msg = f'"{to_string(new_key)}" already mapped to "{existing}"'
2020-11-09 22:16:30 +00:00
logger.info(msg)
2020-12-31 20:46:57 +00:00
self.window.show_status(CTX_KEYCODE, msg)
2020-11-09 22:16:30 +00:00
return
2020-11-09 22:16:30 +00:00
# it's legal to display the keycode
2020-12-31 20:46:57 +00:00
# always ask for get_child to set the label, otherwise line breaking
# has to be configured again.
self.set_keycode_input_label(to_string(new_key))
self.key = new_key
2020-12-31 20:46:57 +00:00
symbol = self.get_symbol()
# the symbol is empty and therefore the mapping is not complete
if symbol is None:
2020-11-09 22:16:30 +00:00
return
# else, the keycode has changed, the symbol is set, all good
2021-09-26 10:44:56 +00:00
custom_mapping.change(new_key=new_key, symbol=symbol, previous_key=previous_key)
def on_symbol_input_change(self, _):
"""When the output symbol for that keycode is typed in."""
2020-12-31 20:47:56 +00:00
key = self.get_key()
symbol = self.get_symbol()
if symbol is None:
return
if key is not None:
2021-09-26 10:44:56 +00:00
custom_mapping.change(new_key=key, symbol=symbol, previous_key=None)
2020-11-09 22:16:30 +00:00
2021-01-01 13:45:07 +00:00
def match(self, _, key, tree_iter):
2020-12-06 14:50:05 +00:00
"""Search the avilable names."""
2020-12-26 15:46:01 +00:00
value = store.get_value(tree_iter, 0)
2020-12-06 14:50:05 +00:00
return key in value.lower()
2020-12-26 17:46:15 +00:00
def show_click_here(self):
"""Show 'click here' on the keycode input button."""
2020-12-31 20:47:56 +00:00
if self.get_key() is not None:
2020-12-26 17:46:15 +00:00
return
2021-09-26 10:44:56 +00:00
self.set_keycode_input_label("click here")
2020-12-26 17:46:15 +00:00
self.keycode_input.set_opacity(0.3)
def show_press_key(self):
"""Show 'press key' on the keycode input button."""
2020-12-31 20:47:56 +00:00
if self.get_key() is not None:
2020-12-26 17:46:15 +00:00
return
2021-09-26 10:44:56 +00:00
self.set_keycode_input_label("press key")
2020-12-26 17:46:15 +00:00
self.keycode_input.set_opacity(1)
2021-01-01 13:45:07 +00:00
def on_keycode_input_focus(self, *_):
2020-12-26 17:46:15 +00:00
"""Refresh useful usage information."""
2021-03-21 18:15:20 +00:00
reader.clear()
2020-12-26 17:46:15 +00:00
self.show_press_key()
self.window.can_modify_mapping()
2021-01-01 13:45:07 +00:00
def on_keycode_input_unfocus(self, *_):
2020-12-31 20:46:57 +00:00
"""Refresh useful usage information and set some state stuff."""
2020-12-26 17:46:15 +00:00
self.show_click_here()
self.keycode_input.set_active(False)
2021-01-07 16:15:12 +00:00
self._state = IDLE
2021-03-21 13:17:34 +00:00
self.window.save_preset()
2020-12-31 20:46:57 +00:00
def set_keycode_input_label(self, label):
"""Set the label of the keycode input."""
self.keycode_input.set_label(label)
# make the child label widget break lines, important for
# long combinations
2020-12-31 22:16:46 +00:00
label = self.keycode_input.get_child()
label.set_line_wrap(True)
label.set_line_wrap_mode(2)
label.set_max_width_chars(13)
label.set_justify(Gtk.Justification.CENTER)
self.keycode_input.set_opacity(1)
2020-12-26 17:46:15 +00:00
def on_symbol_input_unfocus(self, symbol_input, _):
"""Save the preset and correct the input casing."""
symbol = symbol_input.get_text()
correct_case = system_mapping.correct_case(symbol)
if symbol != correct_case:
symbol_input.set_text(correct_case)
self.window.save_preset()
def put_together(self, symbol):
2020-11-15 02:01:11 +00:00
"""Create all child GTK widgets and connect their signals."""
2020-11-09 22:16:30 +00:00
delete_button = Gtk.EventBox()
2021-09-26 10:44:56 +00:00
delete_button.add(
Gtk.Image.new_from_icon_name("window-close", Gtk.IconSize.BUTTON)
2020-11-09 22:16:30 +00:00
)
2021-09-26 10:44:56 +00:00
delete_button.connect("button-press-event", self.on_delete_button_clicked)
2020-11-25 20:55:04 +00:00
delete_button.set_size_request(50, -1)
2020-11-09 22:16:30 +00:00
keycode_input = Gtk.ToggleButton()
2020-12-26 17:46:15 +00:00
self.keycode_input = keycode_input
keycode_input.set_size_request(140, -1)
if self.key is not None:
2020-12-31 20:46:57 +00:00
self.set_keycode_input_label(to_string(self.key))
2020-12-26 17:46:15 +00:00
else:
self.show_click_here()
2020-11-09 22:16:30 +00:00
# make the togglebutton go back to its normal state when doing
# something else in the UI
2021-09-26 10:44:56 +00:00
keycode_input.connect("focus-in-event", self.on_keycode_input_focus)
keycode_input.connect("focus-out-event", self.on_keycode_input_unfocus)
2020-12-27 20:36:14 +00:00
# don't leave the input when using arrow keys or tab. wait for the
# window to consume the keycode from the reader
2021-09-26 10:44:56 +00:00
keycode_input.connect("key-press-event", lambda *args: Gdk.EVENT_STOP)
2020-11-09 22:16:30 +00:00
symbol_input = Gtk.Entry()
self.symbol_input = symbol_input
symbol_input.set_alignment(0.5)
symbol_input.set_width_chars(4)
symbol_input.set_has_frame(False)
2020-12-06 14:50:05 +00:00
completion = Gtk.EntryCompletion()
completion.set_model(store)
completion.set_text_column(0)
completion.set_match_func(self.match)
symbol_input.set_completion(completion)
2020-12-06 14:50:05 +00:00
if symbol is not None:
symbol_input.set_text(symbol)
2021-09-26 10:44:56 +00:00
symbol_input.connect("changed", self.on_symbol_input_change)
symbol_input.connect("focus-out-event", self.on_symbol_input_unfocus)
2020-11-09 22:16:30 +00:00
2020-11-12 22:09:22 +00:00
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
2020-11-25 20:55:04 +00:00
box.set_homogeneous(False)
box.set_spacing(0)
box.pack_start(keycode_input, expand=False, fill=True, padding=0)
box.pack_start(symbol_input, expand=True, fill=True, padding=0)
2020-11-25 20:55:04 +00:00
box.pack_start(delete_button, expand=False, fill=True, padding=0)
2020-11-12 22:09:22 +00:00
box.show_all()
2021-09-26 10:44:56 +00:00
box.get_style_context().add_class("row-box")
2020-11-09 22:16:30 +00:00
2020-11-14 23:27:45 +00:00
self.add(box)
self.show_all()
2020-11-09 22:16:30 +00:00
2021-01-01 13:45:07 +00:00
def on_delete_button_clicked(self, *_):
2020-11-09 22:16:30 +00:00
"""Destroy the row and remove it from the config."""
2020-12-31 20:47:56 +00:00
key = self.get_key()
if key is not None:
custom_mapping.clear(key)
2021-09-26 10:44:56 +00:00
self.symbol_input.set_text("")
self.set_keycode_input_label("")
2020-12-26 17:46:15 +00:00
self.key = None
2020-11-09 22:16:30 +00:00
self.delete_callback(self)