1
0
forked from drath/libratbag

libratbag: refactor ratbag_action_macro_num_keys()

Also use a helper for checking modifiers, this will be handy for
drivers.
This commit is contained in:
Yaroslav Chvanov
2023-09-25 18:49:27 +03:00
parent 068a34c1dc
commit d155bd27ec
2 changed files with 32 additions and 22 deletions

View File

@@ -25,6 +25,8 @@
#include "config.h"
#include <linux/input-event-codes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -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;
}
}

View File

@@ -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;
}