1
0
forked from drath/libratbag

Revamp the button macro code

The previous API was inconsistent and different to the rest of libratbag's
API. Replace with one where a macro is a separate struct, can be initialized
and then written to the device with a set_macro() call. The device copies the
contents, the macro is otherwise unmodified and can be re-used by the client
if need be.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2016-03-11 15:18:08 +10:00
parent 578142964d
commit 4054616190
10 changed files with 303 additions and 122 deletions

View File

@@ -556,6 +556,7 @@ ratbag_cmd_change_button(const struct ratbag_cmd *cmd,
struct ratbag_device *device;
struct ratbag_button *button = NULL;
struct ratbag_profile *profile = NULL;
struct ratbag_button_macro *m = NULL;
int button_index;
enum ratbag_button_action_type action_type;
int rc = ERR_DEVICE;
@@ -630,17 +631,18 @@ ratbag_cmd_change_button(const struct ratbag_cmd *cmd,
rc = ratbag_button_set_special(button, special);
break;
case RATBAG_BUTTON_ACTION_TYPE_MACRO:
rc = ratbag_button_set_macro(button, macro.name);
m = ratbag_button_macro_new(macro.name);
for (i = 0; i < ARRAY_LENGTH(macro.events); i++) {
if (macro.events[i].type == RATBAG_MACRO_EVENT_NONE)
break;
ratbag_button_set_macro_event(button,
ratbag_button_macro_set_event(m,
i,
macro.events[i].type,
macro.events[i].data);
}
rc = ratbag_button_write_macro(button);
rc = ratbag_button_set_macro(button, m);
ratbag_button_macro_unref(m);
break;
default:
error("well, that shouldn't have happened\n");
@@ -1265,6 +1267,7 @@ ratbag_cmd_button_set_macro(const struct ratbag_cmd *cmd,
{
struct ratbag_device *device;
struct ratbag_button *button;
struct ratbag_button_macro *m;
struct macro macro = {0};
int rc;
int i;
@@ -1289,17 +1292,18 @@ ratbag_cmd_button_set_macro(const struct ratbag_cmd *cmd,
return ERR_UNSUPPORTED;
button = options->button;
rc = ratbag_button_set_macro(button, macro.name);
m = ratbag_button_macro_new(macro.name);
for (i = 0; i < ARRAY_LENGTH(macro.events); i++) {
if (macro.events[i].type == RATBAG_MACRO_EVENT_NONE)
break;
ratbag_button_set_macro_event(button,
ratbag_button_macro_set_event(m,
i,
macro.events[i].type,
macro.events[i].data);
}
rc = ratbag_button_write_macro(button);
rc = ratbag_button_set_macro(button, m);
ratbag_button_macro_unref(m);
if (rc != 0)
return ERR_DEVICE;

View File

@@ -174,18 +174,20 @@ static const char *strip_ev_key(int key)
char *
button_action_macro_to_str(struct ratbag_button *button)
{
struct ratbag_button_macro *macro;
char str[4096] = {0};
const char *name;
int offset;
unsigned int i;
name = ratbag_button_get_macro_name(button);
macro = ratbag_button_get_macro(button);
name = ratbag_button_macro_get_name(macro);
offset = snprintf(str, sizeof(str), "macro \"%s\":",
name ? name : "UNKNOWN");
for (i = 0; i < MAX_MACRO_EVENTS; i++) {
enum ratbag_macro_event_type type = ratbag_button_get_macro_event_type(button, i);
int key = ratbag_button_get_macro_event_key(button, i);
int timeout = ratbag_button_get_macro_event_timeout(button, i);
enum ratbag_macro_event_type type = ratbag_button_macro_get_event_type(macro, i);
int key = ratbag_button_macro_get_event_key(macro, i);
int timeout = ratbag_button_macro_get_event_timeout(macro, i);
if (type == RATBAG_MACRO_EVENT_NONE)
break;
@@ -205,6 +207,8 @@ button_action_macro_to_str(struct ratbag_button *button)
}
}
ratbag_button_macro_unref(macro);
return strdup_safe(str);
}