diff --git a/src/libratbag-util.h b/src/libratbag-util.h index a22d82bd..5661c91f 100644 --- a/src/libratbag-util.h +++ b/src/libratbag-util.h @@ -25,6 +25,8 @@ #include "config.h" +#include + #include #include #include @@ -364,3 +366,21 @@ get_unaligned_be_u32(const uint8_t *buf) { return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; } + +static inline bool +ratbag_key_is_modifier(const unsigned int key) +{ + switch (key) { + case KEY_LEFTALT: + case KEY_LEFTCTRL: + case KEY_LEFTMETA: + case KEY_LEFTSHIFT: + case KEY_RIGHTALT: + case KEY_RIGHTCTRL: + case KEY_RIGHTMETA: + case KEY_RIGHTSHIFT: + return true; + default: + return false; + } +} diff --git a/src/libratbag.c b/src/libratbag.c index 32b3a6d8..07df67db 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -1952,31 +1952,21 @@ int ratbag_action_macro_num_keys(const struct ratbag_button_action *action) { const struct ratbag_macro *macro = action->macro; - int i, count; - count = 0; - for (i = 0; i < MAX_MACRO_EVENTS; i++) { - struct ratbag_macro_event event; - - event = macro->events[i]; - if (event.type == RATBAG_MACRO_EVENT_KEY_PRESSED) - { - switch(event.event.key) { - case KEY_LEFTCTRL: - case KEY_LEFTSHIFT: - case KEY_LEFTALT: - case KEY_LEFTMETA: - case KEY_RIGHTCTRL: - case KEY_RIGHTSHIFT: - case KEY_RIGHTALT: - case KEY_RIGHTMETA: - break; - default: - count++; - } + int count = 0; + for (int i = 0; i < MAX_MACRO_EVENTS; i++) { + struct ratbag_macro_event event = macro->events[i]; + if (event.type == RATBAG_MACRO_EVENT_NONE || + event.type == RATBAG_MACRO_EVENT_INVALID) { + break; + } + if (ratbag_key_is_modifier(event.event.key)) { + continue; + } + if (event.type == RATBAG_MACRO_EVENT_KEY_PRESSED) { + count += 1; } } - return count; }