From 49b9ad5647f548f37d1654d673536d9c026d5221 Mon Sep 17 00:00:00 2001 From: Kyoken Date: Tue, 15 Nov 2022 16:27:15 +0300 Subject: [PATCH] use key name instead of code for binding --- tools/ratbagc.py.in | 4 ++-- tools/ratbagctl.in.in | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/tools/ratbagc.py.in b/tools/ratbagc.py.in index 251dd532..aad42a11 100644 --- a/tools/ratbagc.py.in +++ b/tools/ratbagc.py.in @@ -634,7 +634,7 @@ class RatbagdButton(metaclass=MetaRatbag): @key.setter def key(self, key): - libratbag.ratbag_button_set_key(self._button, int(key), None, 0) + libratbag.ratbag_button_set_key(self._button, key, None, 0) @property def action_type(self): @@ -648,7 +648,7 @@ class RatbagdButton(metaclass=MetaRatbag): @property def action_types(self): """An array of possible values for ActionType.""" - return [t for t in (RatbagdButton.ActionType.BUTTON, RatbagdButton.ActionType.SPECIAL, RatbagdButton.ActionType.MACRO) + return [t for t in (RatbagdButton.ActionType.BUTTON, RatbagdButton.ActionType.SPECIAL, RatbagdButton.ActionType.KEY, RatbagdButton.ActionType.MACRO) if libratbag.ratbag_button_has_action_type(self._button, t)] def disable(self): diff --git a/tools/ratbagctl.in.in b/tools/ratbagctl.in.in index ac66f8e7..1dae2604 100755 --- a/tools/ratbagctl.in.in +++ b/tools/ratbagctl.in.in @@ -42,6 +42,14 @@ button_specials_strmap = { } +def convert_str_to_evcode(s: str) -> int: + if not s.startswith("KEY_") and not s.startswith("BTN_"): + msg = "Don't know how to convert {}".format(s) + raise argparse.ArgumentTypeError(msg) + + return evdev.ecodes.ecodes[s] + + def list_devices(r, args): if not r.devices: print("No devices available.") @@ -164,7 +172,8 @@ def print_button(d, p, b, level): elif b.action_type == RatbagdButton.ActionType.SPECIAL: print("{}'{}'".format(header, button_specials_strmap[b.special])) elif b.action_type == RatbagdButton.ActionType.KEY: - print("{}key '{}'".format(header, str(b.key))) + key_name = evdev.ecodes.KEY[b.key][RatbagdMacro._PREFIX_LEN:] + print("{}key '{}'".format(header, key_name)) elif b.action_type == RatbagdButton.ActionType.MACRO: print("{}macro '{}'".format(header, str(b.macro))) elif b.action_type == RatbagdButton.ActionType.NONE: @@ -329,12 +338,15 @@ def func_button_action_set_special(r, args): def func_button_action_set_key(r, args): b, p, d = find_button(r, args) + if not b.ActionType.KEY in b.action_types: + raise RatbagErrorCapability("assigning a key is not supported on this device") + try: key = args.target_key except AttributeError: pass else: - b.key = key + b.key = convert_str_to_evcode(key) commit(d, args) @@ -366,11 +378,7 @@ def func_button_action_set_macro(r, args): t = int(s[1:]) macro.append(RatbagdButton.Macro.WAIT, t) else: - if not s.startswith("KEY_") and not s.startswith("BTN_"): - msg = "Don't know how to convert {}".format(s) - raise argparse.ArgumentTypeError(msg) - - code = evdev.ecodes.ecodes[s] + code = convert_str_to_evcode(key) if is_press: macro.append(RatbagdButton.Macro.KEY_PRESS, code) if is_release: