1
0
forked from drath/libratbag

profiles: add name get/set capability

Some devices can have a custom name, so let's export that capability.

In ratbagd, libratbag/ratbag_profile_set_name() returns an error, we
put it under silence, the client has to check for the capabilities
before asking ratbagd to change the profile name.

Fixes: #266

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2017-08-02 14:21:43 +02:00
committed by Peter Hutterer
parent 97a927bb7d
commit c647d6df99
11 changed files with 228 additions and 3 deletions

View File

@@ -261,6 +261,7 @@ struct ratbag_led {
struct ratbag_profile {
int refcount;
void *userdata;
char *name;
struct list link;
unsigned index;

View File

@@ -118,6 +118,24 @@ snprintf_safe(char *buf, size_t n, const char *fmt, ...)
#define sprintf_safe(buf, fmt, ...) \
snprintf_safe(buf, ARRAY_LENGTH(buf), fmt, __VA_ARGS__)
__attribute__((format(printf, 1, 2)))
static inline void *
asprintf_safe(const char *fmt, ...)
{
va_list args;
int rc;
char *result;
va_start(args, fmt);
rc = vasprintf(&result, fmt, args);
va_end(args);
if (rc < 0)
abort();
return result;
}
static inline void
msleep(unsigned int ms)
{

View File

@@ -663,6 +663,7 @@ ratbag_create_profile(struct ratbag_device *device,
sizeof(*profile->resolution.modes));
profile->resolution.num_modes = num_resolutions;
profile->is_enabled = true;
profile->name = asprintf_safe("Profile %d", index + 1);
list_insert(&device->profiles, &profile->link);
list_init(&profile->buttons);
@@ -743,6 +744,9 @@ ratbag_profile_destroy(struct ratbag_profile *profile)
free(profile->resolution.modes);
if (profile->name)
free(profile->name);
list_remove(&profile->link);
free(profile);
}
@@ -1532,6 +1536,31 @@ ratbag_profile_get_led(struct ratbag_profile *profile,
return NULL;
}
LIBRATBAG_EXPORT const char *
ratbag_profile_get_name(struct ratbag_profile *profile)
{
return profile->name;
}
LIBRATBAG_EXPORT int
ratbag_profile_set_name(struct ratbag_profile *profile,
const char *name)
{
char *name_copy;
if (!ratbag_profile_has_capability(profile,
RATBAG_PROFILE_CAP_WRITABLE_NAME))
return RATBAG_ERROR_CAPABILITY;
name_copy = strdup_safe(name);
if (profile->name)
free(profile->name);
profile->name = name_copy;
return 0;
}
LIBRATBAG_EXPORT void
ratbag_set_user_data(struct ratbag *ratbag, void *userdata)
{

View File

@@ -651,6 +651,14 @@ ratbag_device_get_num_leds(struct ratbag_device *device);
*/
enum ratbag_profile_capability {
RATBAG_PROFILE_CAP_NONE = 0,
/**
* The device has the capability to write a name to a profile
* and store it in its flash.
*
* Any changes uploaded to the device will be cached in libratbag
* and will be committed to it after a commit only.
*/
RATBAG_PROFILE_CAP_WRITABLE_NAME = 100,
};
/**
* @ingroup profile
@@ -688,6 +696,31 @@ int
ratbag_profile_has_capability(const struct ratbag_profile *profile,
enum ratbag_profile_capability cap);
/**
* @ingroup profile
*
* Return the ratbag profile name.
*
* @param profile A previously initialized ratbag profile
* @return the profile name
*/
const char *
ratbag_profile_get_name(struct ratbag_profile *profile);
/**
* @ingroup profile
*
* Set the name of a ratbag profile.
*
* @param profile A previously initialized ratbag profile
* @param name the profile name
*
* @return 0 on success or an error code otherwise
*/
int
ratbag_profile_set_name(struct ratbag_profile *profile,
const char *name);
/**
* @ingroup profile
*

View File

@@ -59,6 +59,7 @@ global:
ratbag_led_unref;
ratbag_device_get_num_leds;
ratbag_profile_get_button;
ratbag_profile_get_name;
ratbag_profile_get_num_resolutions;
ratbag_profile_get_resolution;
ratbag_profile_get_user_data;
@@ -69,6 +70,7 @@ global:
ratbag_profile_set_user_data;
ratbag_profile_set_active;
ratbag_profile_set_enabled;
ratbag_profile_set_name;
ratbag_profile_unref;
ratbag_resolution_get_dpi;
ratbag_resolution_get_dpi_maximum;