From 49d70577c05d6fb7797ad05a93f56fa2e55c720a Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 28 Aug 2015 10:30:22 +1000 Subject: [PATCH] Replace get_active_profile with profile_is_active And the same for set_active_profile -> set_active. This API is more reliable, the previous incarnation required the client to compare pointer values. Signed-off-by: Peter Hutterer --- src/driver-etekcity.c | 13 ++++++-- src/libratbag-private.h | 2 ++ src/libratbag.c | 21 +++++++++--- src/libratbag.h | 17 ++++------ src/libratbag.sym | 4 +-- tools/ratbag-command.c | 73 ++++++++++++++++++++++++++++++----------- 6 files changed, 91 insertions(+), 39 deletions(-) diff --git a/src/driver-etekcity.c b/src/driver-etekcity.c index 08db9222..eca68c3e 100644 --- a/src/driver-etekcity.c +++ b/src/driver-etekcity.c @@ -664,6 +664,7 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id) int rc; struct ratbag_profile *profile; struct etekcity_data *drv_data; + int active_idx; log_debug(device->ratbag, "data: %d\n", id.data); @@ -685,9 +686,8 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id) /* profiles are 0-indexed */ ratbag_device_init_profiles(device, ETEKCITY_PROFILE_MAX + 1, ETEKCITY_BUTTON_MAX + 1); - profile = ratbag_device_get_active_profile(device); - - if (!profile) { + active_idx = etekcity_current_profile(device); + if (active_idx < 0) { log_error(device->ratbag, "Can't talk to the mouse: '%s' (%d)\n", strerror(-rc), @@ -696,6 +696,13 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id) goto err; } + list_for_each(profile, &device->profiles, link) { + if (profile->index == (unsigned int)active_idx) { + profile->is_active = true; + break; + } + } + log_debug(device->ratbag, "'%s' is in profile %d\n", ratbag_device_get_name(device), diff --git a/src/libratbag-private.h b/src/libratbag-private.h index 68e6d7da..3aa0699d 100644 --- a/src/libratbag-private.h +++ b/src/libratbag-private.h @@ -204,6 +204,8 @@ struct ratbag_profile { struct ratbag_resolution modes[MAX_RESOLUTIONS]; unsigned int num_modes; } resolution; + + bool is_active; /**< profile is the currently active one */ }; #define BUTTON_ACTION_NONE \ diff --git a/src/libratbag.c b/src/libratbag.c index ead5fe4d..d5acb7a6 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -642,9 +642,13 @@ ratbag_device_get_profile_by_index(struct ratbag_device *device, unsigned int in return NULL; } -LIBRATBAG_EXPORT struct ratbag_profile * -ratbag_device_get_active_profile(struct ratbag_device *device) +LIBRATBAG_EXPORT int +ratbag_profile_is_active(struct ratbag_profile *profile) { + return profile->is_active; + + /* FIXME: should be read on startup so we can just do the above */ +#if 0 int current_profile; assert(device->driver->get_active_profile); @@ -655,6 +659,7 @@ ratbag_device_get_active_profile(struct ratbag_device *device) } return ratbag_device_get_profile_by_index(device, current_profile); +#endif } LIBRATBAG_EXPORT unsigned int @@ -678,9 +683,10 @@ ratbag_device_has_capability(const struct ratbag_device *device, } LIBRATBAG_EXPORT int -ratbag_device_set_active_profile(struct ratbag_profile *profile) +ratbag_profile_set_active(struct ratbag_profile *profile) { struct ratbag_device *device = profile->device; + struct ratbag_profile *p; int rc; assert(device->driver->write_profile); @@ -690,9 +696,16 @@ ratbag_device_set_active_profile(struct ratbag_profile *profile) if (ratbag_device_has_capability(device, RATBAG_CAP_SWITCHABLE_PROFILE)) { assert(device->driver->set_active_profile); - return device->driver->set_active_profile(device, profile->index); + rc = device->driver->set_active_profile(device, profile->index); } + if (rc) + return rc; + + list_for_each(p, &device->profiles, link) { + p->is_active = false; + } + profile->is_active = true; return rc; } diff --git a/src/libratbag.h b/src/libratbag.h index 7102e33e..20bf70bd 100644 --- a/src/libratbag.h +++ b/src/libratbag.h @@ -402,31 +402,28 @@ ratbag_device_get_profile_by_index(struct ratbag_device *device, unsigned int in /** * @ingroup profile * - * This function returns the currently active profile. Note that some + * Check if the given profile is the currently active one. Note that some * devices allow switching profiles with hardware buttons thus making the * use of this function racy. * - * The profile is refcounted with an initial value of at least 1. - * Use ratbag_profile_unref() to release the profile. + * @param profile A previously initialized ratbag profile * - * @param ratbag A previously initialized ratbag context - * - * @return The profile currently active on the device. + * @return non-zero if the profile is currently active, zero otherwise */ -struct ratbag_profile * -ratbag_device_get_active_profile(struct ratbag_device *device); +int +ratbag_profile_is_active(struct ratbag_profile *profile); /** * @ingroup profile * - * This function sets the currently active profile to the one provided. + * Make the given profile the currently active profile * * @param profile The profile to make the active profile. * * @return 0 on success or nonzero otherwise. */ int -ratbag_device_set_active_profile(struct ratbag_profile *profile); +ratbag_profile_set_active(struct ratbag_profile *profile); /** * @ingroup profile diff --git a/src/libratbag.sym b/src/libratbag.sym index 4af0f96b..4ee29c3c 100644 --- a/src/libratbag.sym +++ b/src/libratbag.sym @@ -16,7 +16,6 @@ global: ratbag_button_set_special; ratbag_button_set_user_data; ratbag_button_unref; - ratbag_device_get_active_profile; ratbag_device_get_name; ratbag_device_get_num_buttons; ratbag_device_get_num_profiles; @@ -25,7 +24,6 @@ global: ratbag_device_has_capability; ratbag_device_new_from_udev_device; ratbag_device_ref; - ratbag_device_set_active_profile; ratbag_device_set_user_data; ratbag_device_unref; ratbag_get_user_data; @@ -36,8 +34,10 @@ global: ratbag_profile_get_num_resolutions; ratbag_profile_get_resolution; ratbag_profile_get_user_data; + ratbag_profile_is_active; ratbag_profile_ref; ratbag_profile_set_user_data; + ratbag_profile_set_active; ratbag_profile_unref; ratbag_resolution_get_dpi; ratbag_resolution_get_report_rate; diff --git a/tools/ratbag-command.c b/tools/ratbag-command.c index d1785bdb..e84e5e73 100644 --- a/tools/ratbag-command.c +++ b/tools/ratbag-command.c @@ -273,7 +273,7 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv) { const char *path; struct ratbag_device *device; - struct ratbag_profile *profile, *active_profile; + struct ratbag_profile *profile; struct ratbag_button *button; char *action; int num_profiles, num_buttons; @@ -294,7 +294,6 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv) } printf("Device '%s' (%s)\n", ratbag_device_get_name(device), path); - active_profile = ratbag_device_get_active_profile(device); printf("Capabilities:"); if (ratbag_device_has_capability(device, @@ -322,7 +321,7 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv) profile = ratbag_device_get_profile_by_index(device, i); printf(" Profile %d%s\n", i, - profile == active_profile ? " (active)" : ""); + ratbag_profile_is_active(profile) ? " (active)" : ""); printf(" Resolutions:\n"); for (j = 0; j < ratbag_profile_get_num_resolutions(profile); j++) { struct ratbag_resolution *res; @@ -355,7 +354,6 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv) profile = ratbag_profile_unref(profile); } - active_profile = ratbag_profile_unref(active_profile); device = ratbag_device_unref(device); rc = 0; @@ -375,9 +373,10 @@ ratbag_cmd_switch_profile(struct ratbag *ratbag, uint32_t flags, int argc, char { const char *path; struct ratbag_device *device; - struct ratbag_profile *profile, *active_profile; + struct ratbag_profile *profile = NULL, *active_profile = NULL; int num_profiles, index; int rc = 1; + int i; if (argc != 2) { usage(); @@ -405,28 +404,37 @@ ratbag_cmd_switch_profile(struct ratbag *ratbag, uint32_t flags, int argc, char goto out; } - active_profile = ratbag_device_get_active_profile(device); profile = ratbag_device_get_profile_by_index(device, index); - if (!profile || !active_profile) { + if (ratbag_profile_is_active(profile)) { + printf("'%s' is already in profile '%d'\n", + ratbag_device_get_name(device), index); + goto out; + } + + for (i = 0; i < num_profiles; i++) { + active_profile = ratbag_device_get_profile_by_index(device, i); + if (ratbag_profile_is_active(active_profile)) + break; + ratbag_profile_unref(active_profile); + active_profile = NULL; + } + + if (!active_profile) { error("Huh hoh, something bad happened, unable to retrieve the profile '%d' \n", index); goto out; } - if (active_profile != profile) { - rc = ratbag_device_set_active_profile(profile); - if (!rc) - printf("Switched '%s' to profile '%d'\n", - ratbag_device_get_name(device), index); - } else { - printf("'%s' is already in profile '%d'\n", + rc = ratbag_profile_set_active(profile); + if (!rc) { + printf("Switched '%s' to profile '%d'\n", ratbag_device_get_name(device), index); } +out: profile = ratbag_profile_unref(profile); active_profile = ratbag_profile_unref(active_profile); -out: device = ratbag_device_unref(device); return rc; } @@ -448,6 +456,7 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char int rc = 1, commit = 0; unsigned int modifiers[10]; size_t modifiers_sz = 10; + int i; if (argc != 1) { usage(); @@ -468,7 +477,15 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char goto out; } - profile = ratbag_device_get_active_profile(device); + for (i = 0; i < ratbag_device_get_num_profiles(device); i++) { + profile = ratbag_device_get_profile_by_index(device, i); + if (ratbag_profile_is_active(profile)) + break; + + ratbag_profile_unref(profile); + profile = NULL; + } + if (!profile) { error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); goto out; @@ -495,7 +512,7 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char if (!commit) goto out; - rc = ratbag_device_set_active_profile(profile); + rc = ratbag_profile_set_active(profile); if (!rc) printf("Switched the current profile of '%s' to %sreport the volume keys\n", ratbag_device_get_name(device), @@ -556,6 +573,7 @@ ratbag_cmd_change_button(struct ratbag *ratbag, uint32_t flags, int argc, char * int rc = 1; unsigned int btnkey; enum ratbag_button_action_special special; + int i; if (argc != 4) { usage(); @@ -601,7 +619,14 @@ ratbag_cmd_change_button(struct ratbag *ratbag, uint32_t flags, int argc, char * goto out; } - profile = ratbag_device_get_active_profile(device); + for (i = 0; i < ratbag_device_get_num_profiles(device); i++) { + profile = ratbag_device_get_profile_by_index(device, i); + if (ratbag_profile_is_active(profile)) + break; + ratbag_profile_unref(profile); + profile = NULL; + } + if (!profile) { error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); goto out; @@ -636,7 +661,7 @@ ratbag_cmd_change_button(struct ratbag *ratbag, uint32_t flags, int argc, char * goto out; } - rc = ratbag_device_set_active_profile(profile); + rc = ratbag_profile_set_active(profile); if (rc) { error("Unable to apply the current profile: %s (%d)\n", strerror(-rc), @@ -739,7 +764,15 @@ ratbag_cmd_switch_dpi(struct ratbag *ratbag, uint32_t flags, int argc, char **ar goto out; } - profile = ratbag_device_get_active_profile(device); + for (i = 0; i < ratbag_device_get_num_profiles(device); i++) { + profile = ratbag_device_get_profile_by_index(device, i); + if (ratbag_profile_is_active(profile)) + break; + + ratbag_profile_unref(profile); + profile = NULL; + } + if (!profile) { error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); goto out;