From 96067f18caa000986223266038f526b6eca965c2 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 25 Jul 2017 13:37:41 +1000 Subject: [PATCH] ratbagd: change the key action types to enums Signed-off-by: Peter Hutterer Signed-off-by: Benjamin Tissoires --- DBusInterface.md | 14 ++++----- ratbagd/ratbagd-button.c | 63 ++++++++++++---------------------------- tools/ratbagctl.in | 10 +++++-- tools/ratbagctl.test.in | 3 +- tools/ratbagd.py | 11 +++++-- 5 files changed, 42 insertions(+), 59 deletions(-) diff --git a/DBusInterface.md b/DBusInterface.md index 82e9c201..39ffaf3a 100644 --- a/DBusInterface.md +++ b/DBusInterface.md @@ -236,15 +236,15 @@ String of the current special mapping (if mapped to special) Array of uints, first entry is the keycode, other entries, if any, are modifiers (if mapped to key) #### `ActionType` -- type: `s`, read-only, mutable - -String describing the action type of the button ("none", "button", "key", -"special", "macro", "unknown"). This decides which Mapping property has a -value +- type: `u`, read-only, mutable +An enum describing the action type of the button, see +ratbag\_button\_get\_action\_type for the list of enums. +This decides which *Mapping* property has a value. #### `ActionTypes` -- type: `as`, read-only, constant -Array of strings, possible values for ActionType +- type: `au`, read-only, constant +Array of enum ratbag\_button\_action\_type, possible values for ActionType +on the current device ### Methods: #### `SetButtonMapping(u) → ()` diff --git a/ratbagd/ratbagd-button.c b/ratbagd/ratbagd-button.c index 86a0cc94..1c7c190a 100644 --- a/ratbagd/ratbagd-button.c +++ b/ratbagd/ratbagd-button.c @@ -412,30 +412,11 @@ static int ratbagd_button_get_action_type(sd_bus *bus, sd_bus_error *error) { struct ratbagd_button *button = userdata; - const char *type; + enum ratbag_button_action_type type; - switch (ratbag_button_get_action_type(button->lib_button)) { - case RATBAG_BUTTON_ACTION_TYPE_NONE: - type = "none"; - break; - case RATBAG_BUTTON_ACTION_TYPE_BUTTON: - type = "button"; - break; - case RATBAG_BUTTON_ACTION_TYPE_KEY: - type = "key"; - break; - case RATBAG_BUTTON_ACTION_TYPE_SPECIAL: - type = "special"; - break; - case RATBAG_BUTTON_ACTION_TYPE_MACRO: - type = "macro"; - break; - case RATBAG_BUTTON_ACTION_TYPE_UNKNOWN: - type = "unknown"; - break; - } + type = ratbag_button_get_action_type(button->lib_button); - return sd_bus_message_append(reply, "s", type); + return sd_bus_message_append(reply, "u", type); } static int ratbagd_button_get_action_types(sd_bus *bus, @@ -447,34 +428,27 @@ static int ratbagd_button_get_action_types(sd_bus *bus, sd_bus_error *error) { struct ratbagd_button *button = userdata; - const char *types[5] = {NULL}; - const char **t; - int idx = 0; int r; + enum ratbag_button_action_type types[] = { + RATBAG_BUTTON_ACTION_TYPE_BUTTON, + RATBAG_BUTTON_ACTION_TYPE_SPECIAL, + RATBAG_BUTTON_ACTION_TYPE_KEY, + RATBAG_BUTTON_ACTION_TYPE_MACRO + }; + enum ratbag_button_action_type *t; - r = sd_bus_message_open_container(reply, 'a', "s"); + + r = sd_bus_message_open_container(reply, 'a', "u"); if (r < 0) return r; - if (ratbag_button_has_action_type(button->lib_button, - RATBAG_BUTTON_ACTION_TYPE_BUTTON)) - types[idx++] = "button"; - if (ratbag_button_has_action_type(button->lib_button, - RATBAG_BUTTON_ACTION_TYPE_KEY)) - types[idx++] = "key"; - if (ratbag_button_has_action_type(button->lib_button, - RATBAG_BUTTON_ACTION_TYPE_SPECIAL)) - types[idx++] = "special"; - if (ratbag_button_has_action_type(button->lib_button, - RATBAG_BUTTON_ACTION_TYPE_MACRO)) - types[idx++] = "macro"; + ARRAY_FOR_EACH(types, t) { + if (!ratbag_button_has_action_type(button->lib_button, *t)) + continue; - t = types; - while (*t) { - r = sd_bus_message_append(reply, "s", *t); + r = sd_bus_message_append(reply, "u", *t); if (r < 0) return r; - t++; } return sd_bus_message_close_container(reply); @@ -503,9 +477,8 @@ const sd_bus_vtable ratbagd_button_vtable[] = { SD_BUS_PROPERTY("ButtonMapping", "u", ratbagd_button_get_button, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("SpecialMapping", "s", ratbagd_button_get_special, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("KeyMapping", "au", ratbagd_button_get_key, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), - SD_BUS_PROPERTY("ActionType", "s", ratbagd_button_get_action_type, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), - SD_BUS_PROPERTY("ActionTypes", "as", ratbagd_button_get_action_types, 0, SD_BUS_VTABLE_PROPERTY_CONST), - + SD_BUS_PROPERTY("ActionType", "u", ratbagd_button_get_action_type, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("ActionTypes", "au", ratbagd_button_get_action_types, 0, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_METHOD("SetButtonMapping", "u", "u", ratbagd_button_set_button, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetSpecialMapping", "s", "u", ratbagd_button_set_special, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetKeyMapping", "au", "u", ratbagd_button_set_key, SD_BUS_VTABLE_UNPRIVILEGED), diff --git a/tools/ratbagctl.in b/tools/ratbagctl.in index acd59d46..ced2255a 100755 --- a/tools/ratbagctl.in +++ b/tools/ratbagctl.in @@ -137,12 +137,16 @@ def print_button(d, p, b, level): header = " " * level + "Button: {} type {} is mapped to ".format(b.index, b.type) - if b.action_type == "button": + if b.action_type == RatbagdButton.ACTION_TYPE_BUTTON: print("{}'button {}'".format(header, b.mapping)) - elif b.action_type == "key": + elif b.action_type == RatbagdButton.ACTION_TYPE_KEY: print("{}'{}'".format(header, b.key)) - elif b.action_type == "special": + elif b.action_type == RatbagdButton.ACTION_TYPE_SPECIAL: print("{}'{}'".format(header, b.special)) + elif b.action_type == RatbagdButton.ACTION_TYPE_MACRO: + print("{}macro".format(header)) + elif b.action_type == RatbagdButton.ACTION_TYPE_NONE: + print("{}none".format(header)) else: print("{}UNKNOWN".format(header)) diff --git a/tools/ratbagctl.test.in b/tools/ratbagctl.test.in index 51d688d4..6d558359 100755 --- a/tools/ratbagctl.test.in +++ b/tools/ratbagctl.test.in @@ -378,8 +378,7 @@ class TestRatbgagCtlButton(TestRatbagCtl): # FIXME: special maps to UNKNOWN???? self.assertEqual(r, "Button: 2 type unknown is mapped to UNKNOWN") r = self.launch_good_test("button 3 get test_device") - # FIXME: macro maps to UNKNOWN???? - self.assertEqual(r, "Button: 3 type unknown is mapped to UNKNOWN") + self.assertEqual(r, "Button: 3 type unknown is mapped to macro") self.launch_fail_test("button 1 get 10 test_device") self.launch_fail_test("button 10 get test_device") self.launch_fail_test("button 1 get test_device X") diff --git a/tools/ratbagd.py b/tools/ratbagd.py index 46930b2c..56036b28 100644 --- a/tools/ratbagd.py +++ b/tools/ratbagd.py @@ -429,6 +429,12 @@ class RatbagdResolution(_RatbagdDBus): class RatbagdButton(_RatbagdDBus): """Represents a ratbagd button.""" + ACTION_TYPE_NONE = 0 + ACTION_TYPE_BUTTON = 1 + ACTION_TYPE_SPECIAL = 2 + ACTION_TYPE_KEY = 3 + ACTION_TYPE_MACRO = 4 + def __init__(self, object_path): _RatbagdDBus.__init__(self, "Button", object_path) @@ -491,8 +497,9 @@ class RatbagdButton(_RatbagdDBus): @GObject.Property def action_type(self): - """A string describing the action type of the button. One of "none", - "button", "key", "special", "macro" or "unknown". This decides which + """An enum describing the action type of the button. One of + ACTION_TYPE_NONE, ACTION_TYPE_BUTTON, ACTION_TYPE_SPECIAL, + ACTION_TYPE_KEY, ACTION_TYPE_MACRO. This decides which *Mapping property has a value. """ return self._get_dbus_property("ActionType")