1
0
forked from drath/libratbag

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 <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2015-08-28 10:30:22 +10:00
parent 7410939f88
commit 49d70577c0
6 changed files with 91 additions and 39 deletions

View File

@@ -664,6 +664,7 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id)
int rc; int rc;
struct ratbag_profile *profile; struct ratbag_profile *profile;
struct etekcity_data *drv_data; struct etekcity_data *drv_data;
int active_idx;
log_debug(device->ratbag, "data: %d\n", id.data); 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 */ /* profiles are 0-indexed */
ratbag_device_init_profiles(device, ETEKCITY_PROFILE_MAX + 1, ETEKCITY_BUTTON_MAX + 1); ratbag_device_init_profiles(device, ETEKCITY_PROFILE_MAX + 1, ETEKCITY_BUTTON_MAX + 1);
profile = ratbag_device_get_active_profile(device); active_idx = etekcity_current_profile(device);
if (active_idx < 0) {
if (!profile) {
log_error(device->ratbag, log_error(device->ratbag,
"Can't talk to the mouse: '%s' (%d)\n", "Can't talk to the mouse: '%s' (%d)\n",
strerror(-rc), strerror(-rc),
@@ -696,6 +696,13 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id)
goto err; 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, log_debug(device->ratbag,
"'%s' is in profile %d\n", "'%s' is in profile %d\n",
ratbag_device_get_name(device), ratbag_device_get_name(device),

View File

@@ -204,6 +204,8 @@ struct ratbag_profile {
struct ratbag_resolution modes[MAX_RESOLUTIONS]; struct ratbag_resolution modes[MAX_RESOLUTIONS];
unsigned int num_modes; unsigned int num_modes;
} resolution; } resolution;
bool is_active; /**< profile is the currently active one */
}; };
#define BUTTON_ACTION_NONE \ #define BUTTON_ACTION_NONE \

View File

@@ -642,9 +642,13 @@ ratbag_device_get_profile_by_index(struct ratbag_device *device, unsigned int in
return NULL; return NULL;
} }
LIBRATBAG_EXPORT struct ratbag_profile * LIBRATBAG_EXPORT int
ratbag_device_get_active_profile(struct ratbag_device *device) 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; int current_profile;
assert(device->driver->get_active_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); return ratbag_device_get_profile_by_index(device, current_profile);
#endif
} }
LIBRATBAG_EXPORT unsigned int LIBRATBAG_EXPORT unsigned int
@@ -678,9 +683,10 @@ ratbag_device_has_capability(const struct ratbag_device *device,
} }
LIBRATBAG_EXPORT int 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_device *device = profile->device;
struct ratbag_profile *p;
int rc; int rc;
assert(device->driver->write_profile); 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)) { if (ratbag_device_has_capability(device, RATBAG_CAP_SWITCHABLE_PROFILE)) {
assert(device->driver->set_active_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; return rc;
} }

View File

@@ -402,31 +402,28 @@ ratbag_device_get_profile_by_index(struct ratbag_device *device, unsigned int in
/** /**
* @ingroup profile * @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 * devices allow switching profiles with hardware buttons thus making the
* use of this function racy. * use of this function racy.
* *
* The profile is refcounted with an initial value of at least 1. * @param profile A previously initialized ratbag profile
* Use ratbag_profile_unref() to release the profile.
* *
* @param ratbag A previously initialized ratbag context * @return non-zero if the profile is currently active, zero otherwise
*
* @return The profile currently active on the device.
*/ */
struct ratbag_profile * int
ratbag_device_get_active_profile(struct ratbag_device *device); ratbag_profile_is_active(struct ratbag_profile *profile);
/** /**
* @ingroup 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. * @param profile The profile to make the active profile.
* *
* @return 0 on success or nonzero otherwise. * @return 0 on success or nonzero otherwise.
*/ */
int int
ratbag_device_set_active_profile(struct ratbag_profile *profile); ratbag_profile_set_active(struct ratbag_profile *profile);
/** /**
* @ingroup profile * @ingroup profile

View File

@@ -16,7 +16,6 @@ global:
ratbag_button_set_special; ratbag_button_set_special;
ratbag_button_set_user_data; ratbag_button_set_user_data;
ratbag_button_unref; ratbag_button_unref;
ratbag_device_get_active_profile;
ratbag_device_get_name; ratbag_device_get_name;
ratbag_device_get_num_buttons; ratbag_device_get_num_buttons;
ratbag_device_get_num_profiles; ratbag_device_get_num_profiles;
@@ -25,7 +24,6 @@ global:
ratbag_device_has_capability; ratbag_device_has_capability;
ratbag_device_new_from_udev_device; ratbag_device_new_from_udev_device;
ratbag_device_ref; ratbag_device_ref;
ratbag_device_set_active_profile;
ratbag_device_set_user_data; ratbag_device_set_user_data;
ratbag_device_unref; ratbag_device_unref;
ratbag_get_user_data; ratbag_get_user_data;
@@ -36,8 +34,10 @@ global:
ratbag_profile_get_num_resolutions; ratbag_profile_get_num_resolutions;
ratbag_profile_get_resolution; ratbag_profile_get_resolution;
ratbag_profile_get_user_data; ratbag_profile_get_user_data;
ratbag_profile_is_active;
ratbag_profile_ref; ratbag_profile_ref;
ratbag_profile_set_user_data; ratbag_profile_set_user_data;
ratbag_profile_set_active;
ratbag_profile_unref; ratbag_profile_unref;
ratbag_resolution_get_dpi; ratbag_resolution_get_dpi;
ratbag_resolution_get_report_rate; ratbag_resolution_get_report_rate;

View File

@@ -273,7 +273,7 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv)
{ {
const char *path; const char *path;
struct ratbag_device *device; struct ratbag_device *device;
struct ratbag_profile *profile, *active_profile; struct ratbag_profile *profile;
struct ratbag_button *button; struct ratbag_button *button;
char *action; char *action;
int num_profiles, num_buttons; 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); printf("Device '%s' (%s)\n", ratbag_device_get_name(device), path);
active_profile = ratbag_device_get_active_profile(device);
printf("Capabilities:"); printf("Capabilities:");
if (ratbag_device_has_capability(device, 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); profile = ratbag_device_get_profile_by_index(device, i);
printf(" Profile %d%s\n", i, printf(" Profile %d%s\n", i,
profile == active_profile ? " (active)" : ""); ratbag_profile_is_active(profile) ? " (active)" : "");
printf(" Resolutions:\n"); printf(" Resolutions:\n");
for (j = 0; j < ratbag_profile_get_num_resolutions(profile); j++) { for (j = 0; j < ratbag_profile_get_num_resolutions(profile); j++) {
struct ratbag_resolution *res; 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); profile = ratbag_profile_unref(profile);
} }
active_profile = ratbag_profile_unref(active_profile);
device = ratbag_device_unref(device); device = ratbag_device_unref(device);
rc = 0; rc = 0;
@@ -375,9 +373,10 @@ ratbag_cmd_switch_profile(struct ratbag *ratbag, uint32_t flags, int argc, char
{ {
const char *path; const char *path;
struct ratbag_device *device; struct ratbag_device *device;
struct ratbag_profile *profile, *active_profile; struct ratbag_profile *profile = NULL, *active_profile = NULL;
int num_profiles, index; int num_profiles, index;
int rc = 1; int rc = 1;
int i;
if (argc != 2) { if (argc != 2) {
usage(); usage();
@@ -405,28 +404,37 @@ ratbag_cmd_switch_profile(struct ratbag *ratbag, uint32_t flags, int argc, char
goto out; goto out;
} }
active_profile = ratbag_device_get_active_profile(device);
profile = ratbag_device_get_profile_by_index(device, index); 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", error("Huh hoh, something bad happened, unable to retrieve the profile '%d' \n",
index); index);
goto out; goto out;
} }
if (active_profile != profile) { rc = ratbag_profile_set_active(profile);
rc = ratbag_device_set_active_profile(profile); if (!rc) {
if (!rc) printf("Switched '%s' to profile '%d'\n",
printf("Switched '%s' to profile '%d'\n",
ratbag_device_get_name(device), index);
} else {
printf("'%s' is already in profile '%d'\n",
ratbag_device_get_name(device), index); ratbag_device_get_name(device), index);
} }
out:
profile = ratbag_profile_unref(profile); profile = ratbag_profile_unref(profile);
active_profile = ratbag_profile_unref(active_profile); active_profile = ratbag_profile_unref(active_profile);
out:
device = ratbag_device_unref(device); device = ratbag_device_unref(device);
return rc; return rc;
} }
@@ -448,6 +456,7 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char
int rc = 1, commit = 0; int rc = 1, commit = 0;
unsigned int modifiers[10]; unsigned int modifiers[10];
size_t modifiers_sz = 10; size_t modifiers_sz = 10;
int i;
if (argc != 1) { if (argc != 1) {
usage(); usage();
@@ -468,7 +477,15 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char
goto out; 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) { if (!profile) {
error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); error("Huh hoh, something bad happened, unable to retrieve the active profile\n");
goto out; goto out;
@@ -495,7 +512,7 @@ ratbag_cmd_switch_etekcity(struct ratbag *ratbag, uint32_t flags, int argc, char
if (!commit) if (!commit)
goto out; goto out;
rc = ratbag_device_set_active_profile(profile); rc = ratbag_profile_set_active(profile);
if (!rc) if (!rc)
printf("Switched the current profile of '%s' to %sreport the volume keys\n", printf("Switched the current profile of '%s' to %sreport the volume keys\n",
ratbag_device_get_name(device), 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; int rc = 1;
unsigned int btnkey; unsigned int btnkey;
enum ratbag_button_action_special special; enum ratbag_button_action_special special;
int i;
if (argc != 4) { if (argc != 4) {
usage(); usage();
@@ -601,7 +619,14 @@ ratbag_cmd_change_button(struct ratbag *ratbag, uint32_t flags, int argc, char *
goto out; 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) { if (!profile) {
error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); error("Huh hoh, something bad happened, unable to retrieve the active profile\n");
goto out; goto out;
@@ -636,7 +661,7 @@ ratbag_cmd_change_button(struct ratbag *ratbag, uint32_t flags, int argc, char *
goto out; goto out;
} }
rc = ratbag_device_set_active_profile(profile); rc = ratbag_profile_set_active(profile);
if (rc) { if (rc) {
error("Unable to apply the current profile: %s (%d)\n", error("Unable to apply the current profile: %s (%d)\n",
strerror(-rc), strerror(-rc),
@@ -739,7 +764,15 @@ ratbag_cmd_switch_dpi(struct ratbag *ratbag, uint32_t flags, int argc, char **ar
goto out; 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) { if (!profile) {
error("Huh hoh, something bad happened, unable to retrieve the active profile\n"); error("Huh hoh, something bad happened, unable to retrieve the active profile\n");
goto out; goto out;