From 6bc44f4d5facaf284cad3c2e273b0e5efaf1b48d Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 26 Aug 2015 16:55:29 -0400 Subject: [PATCH] Factor out ratbag_button_action_match This will be used often in drivers, so let's have a helper for it. Signed-off-by: Benjamin Tissoires --- src/driver-etekcity.c | 19 +------------------ src/hidpp20.c | 19 +------------------ src/libratbag-private.h | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/driver-etekcity.c b/src/driver-etekcity.c index 2e04684b..494ad31a 100644 --- a/src/driver-etekcity.c +++ b/src/driver-etekcity.c @@ -197,25 +197,8 @@ etekcity_button_action_to_raw(const struct ratbag_button_action *action) struct etekcity_button_mapping *mapping; ARRAY_FOR_EACH(etekcity_button_mapping, mapping) { - if (mapping->action.type == action->type) { - switch (action->type) { - case RATBAG_BUTTON_ACTION_TYPE_BUTTON: - if (mapping->action.action.button != action->action.button) - continue; - break; - case RATBAG_BUTTON_ACTION_TYPE_KEY: - if (mapping->action.action.key.key != action->action.key.key) - continue; - case RATBAG_BUTTON_ACTION_TYPE_SPECIAL: - if (mapping->action.action.special != action->action.special) - continue; - case RATBAG_BUTTON_ACTION_TYPE_MACRO: - /* FIXME: currently, do nothing */ - default: /* do nothing, we are good */ - break; - } + if (ratbag_button_action_match(&mapping->action, action)) return mapping->raw; - } } return 0; diff --git a/src/hidpp20.c b/src/hidpp20.c index f9bb88a8..b8eb228c 100644 --- a/src/hidpp20.c +++ b/src/hidpp20.c @@ -577,25 +577,8 @@ hidpp20_1b04_get_logical_control_id(const struct ratbag_button_action *action) const struct hidpp20_1b04_action_mapping *mapping; ARRAY_FOR_EACH(hidpp20_1b04_logical_mapping, mapping) { - if (mapping->action.type == action->type) { - switch (action->type) { - case RATBAG_BUTTON_ACTION_TYPE_BUTTON: - if (mapping->action.action.button != action->action.button) - continue; - break; - case RATBAG_BUTTON_ACTION_TYPE_KEY: - if (mapping->action.action.key.key != action->action.key.key) - continue; - case RATBAG_BUTTON_ACTION_TYPE_SPECIAL: - if (mapping->action.action.special != action->action.special) - continue; - case RATBAG_BUTTON_ACTION_TYPE_MACRO: - /* FIXME: currently, do nothing */ - default: /* do nothing, we are good */ - break; - } + if (ratbag_button_action_match(&mapping->action, action)) return mapping->value; - } } return 0; diff --git a/src/libratbag-private.h b/src/libratbag-private.h index a9793f7f..8cecdbb3 100644 --- a/src/libratbag-private.h +++ b/src/libratbag-private.h @@ -270,6 +270,29 @@ ratbag_profile_get_drv_data(struct ratbag_profile *profile) return profile->drv_data; } +static inline int +ratbag_button_action_match(const struct ratbag_button_action *action, + const struct ratbag_button_action *match) +{ + if (action->type != match->type) + return 0; + + switch (action->type) { + case RATBAG_BUTTON_ACTION_TYPE_BUTTON: + return match->action.button == action->action.button; + case RATBAG_BUTTON_ACTION_TYPE_KEY: + return match->action.key.key == action->action.key.key; + case RATBAG_BUTTON_ACTION_TYPE_SPECIAL: + return match->action.special != action->action.special; + case RATBAG_BUTTON_ACTION_TYPE_MACRO: + /* FIXME: currently, do nothing */ + default: + break; + } + + return 0; +} + /** * Override the auto-picked hidraw device. */