add support for getting/setting the macro name

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2015-09-08 14:49:40 -04:00
parent c7b7996f09
commit cdf710d807
6 changed files with 44 additions and 8 deletions

View File

@@ -600,7 +600,7 @@ etekcity_read_button(struct ratbag_button *button)
button->index, button->profile->index,
rc < 0 ? strerror(-rc) : "not read enough", rc);
} else {
ratbag_button_set_macro(button);
ratbag_button_set_macro(button, macro->name);
log_raw(device->ratbag,
"macro on button %d of profile %d is named '%s', and contains %d events:\n",
button->index, button->profile->index,

View File

@@ -257,8 +257,8 @@ struct ratbag_macro_event {
#define MAX_MACRO_EVENTS 64
struct ratbag_macro {
const char *name;
const char *group;
char *name;
char *group;
struct ratbag_macro_event events[MAX_MACRO_EVENTS];
};

View File

@@ -1204,8 +1204,11 @@ static void
ratbag_button_destroy(struct ratbag_button *button)
{
list_remove(&button->link);
if (button->action.macro)
if (button->action.macro) {
if (button->action.macro->name)
free(button->action.macro->name);
free(button->action.macro);
}
free(button);
}
@@ -1288,16 +1291,20 @@ ratbag_resolution_get_user_data(const struct ratbag_resolution *ratbag_resolutio
}
LIBRATBAG_EXPORT int
ratbag_button_set_macro(struct ratbag_button *button)
ratbag_button_set_macro(struct ratbag_button *button, const char *name)
{
struct ratbag_macro *macro;
if (!button->action.macro)
button->action.macro = zalloc(sizeof(*macro));
else
else {
if (button->action.macro->name)
free(button->action.macro->name);
memset(button->action.macro, 0, sizeof(*macro));
}
button->action.type = RATBAG_BUTTON_ACTION_TYPE_MACRO;
button->action.macro->name = name ? strdup(name) : NULL;
return 0;
}
@@ -1380,3 +1387,12 @@ ratbag_button_get_macro_event_timeout(struct ratbag_button *button, unsigned int
return macro->events[index].event.timeout;
}
LIBRATBAG_EXPORT const char *
ratbag_button_get_macro_name(struct ratbag_button *button)
{
if (button->action.type != RATBAG_BUTTON_ACTION_TYPE_MACRO)
return NULL;
return button->action.macro->name;
}

View File

@@ -1269,6 +1269,22 @@ enum ratbag_macro_event_type {
RATBAG_MACRO_EVENT_WAIT,
};
/**
* @ingroup button
*
* If a button's action is @ref RATBAG_BUTTON_ACTION_TYPE_MACRO,
* this function returns the macro name.
*
* If the button's action type is not @ref RATBAG_BUTTON_ACTION_TYPE_MACRO,
* this function returns NULL.
*
* @param button A previously initialized ratbag button
*
* @return The name of the macro associated to the button.
*/
const char *
ratbag_button_get_macro_name(struct ratbag_button *button);
/**
* @ingroup button
*
@@ -1334,7 +1350,7 @@ ratbag_button_get_macro_event_timeout(struct ratbag_button *button, unsigned int
* allocates the required memory to store the macro.
*/
int
ratbag_button_set_macro(struct ratbag_button *button);
ratbag_button_set_macro(struct ratbag_button *button, const char *name);
/**
* @ingroup button

View File

@@ -11,6 +11,7 @@ global:
ratbag_button_get_macro_event_type;
ratbag_button_get_macro_event_key;
ratbag_button_get_macro_event_timeout;
ratbag_button_get_macro_name;
ratbag_button_get_special;
ratbag_button_get_user_data;
ratbag_button_has_action_type;

View File

@@ -158,10 +158,13 @@ char *
button_action_macro_to_str(struct ratbag_button *button)
{
char str[4096] = {0};
const char *name;
int offset;
unsigned int i;
offset = snprintf(str, sizeof(str), "macro:");
name = ratbag_button_get_macro_name(button);
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);