From 6ec078d43bee37f32de31fa63b2376e2b67ac4ee Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 2 Sep 2015 17:01:35 +1000 Subject: [PATCH] Add hooks for getting/setting the default profile Signed-off-by: Peter Hutterer --- src/driver-etekcity.c | 7 +++++++ src/driver-hidpp10.c | 7 +++++++ src/driver-hidpp20.c | 7 +++++++ src/libratbag-private.h | 9 +++++++++ src/libratbag.c | 32 ++++++++++++++++++++++++++++++++ src/libratbag.h | 25 +++++++++++++++++++++++++ src/libratbag.sym | 2 ++ tools/ratbag-command.c | 5 +++-- 8 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/driver-etekcity.c b/src/driver-etekcity.c index 017cd782..b3aa0ceb 100644 --- a/src/driver-etekcity.c +++ b/src/driver-etekcity.c @@ -295,6 +295,12 @@ etekcity_set_current_profile(struct ratbag_device *device, unsigned int index) return ret == sizeof(buf) ? 0 : ret; } +static int +etekcity_set_default_profile(struct ratbag_device *device, unsigned int index) +{ + return -ENOTSUP; +} + static int etekcity_set_config_profile(struct ratbag_device *device, uint8_t profile, uint8_t type) { @@ -743,6 +749,7 @@ struct ratbag_driver etekcity_driver = { .read_profile = etekcity_read_profile, .write_profile = etekcity_write_profile, .set_active_profile = etekcity_set_current_profile, + .set_default_profile = etekcity_set_default_profile, .has_capability = etekcity_has_capability, .read_button = etekcity_read_button, .write_button = etekcity_write_button, diff --git a/src/driver-hidpp10.c b/src/driver-hidpp10.c index 3d6a615f..8e4a026d 100644 --- a/src/driver-hidpp10.c +++ b/src/driver-hidpp10.c @@ -115,6 +115,12 @@ hidpp10drv_set_current_profile(struct ratbag_device *device, unsigned int index) return -ENOTSUP; } +static int +hidpp10drv_set_default_profile(struct ratbag_device *device, unsigned int index) +{ + return -ENOTSUP; +} + static void hidpp10drv_read_profile(struct ratbag_profile *profile, unsigned int index) { @@ -280,6 +286,7 @@ struct ratbag_driver hidpp10_driver = { .read_profile = hidpp10drv_read_profile, .write_profile = hidpp10drv_write_profile, .set_active_profile = hidpp10drv_set_current_profile, + .set_default_profile = hidpp10drv_set_default_profile, .has_capability = hidpp10drv_has_capability, .read_button = hidpp10drv_read_button, .write_button = hidpp10drv_write_button, diff --git a/src/driver-hidpp20.c b/src/driver-hidpp20.c index 4e9ad5e3..06896f6e 100644 --- a/src/driver-hidpp20.c +++ b/src/driver-hidpp20.c @@ -163,6 +163,12 @@ hidpp20drv_set_current_profile(struct ratbag_device *device, unsigned int index) return -ENOTSUP; } +static int +hidpp20drv_set_default_profile(struct ratbag_device *device, unsigned int index) +{ + return -ENOTSUP; +} + static int hidpp20drv_read_resolution_dpi(struct ratbag_profile *profile) { @@ -510,6 +516,7 @@ struct ratbag_driver hidpp20_driver = { .read_profile = hidpp20drv_read_profile, .write_profile = hidpp20drv_write_profile, .set_active_profile = hidpp20drv_set_current_profile, + .set_default_profile = hidpp20drv_set_default_profile, .has_capability = hidpp20drv_has_capability, .read_button = hidpp20drv_read_button, .write_button = hidpp20drv_write_button, diff --git a/src/libratbag-private.h b/src/libratbag-private.h index 82765bec..ed16752a 100644 --- a/src/libratbag-private.h +++ b/src/libratbag-private.h @@ -134,6 +134,14 @@ struct ratbag_driver { */ int (*set_active_profile)(struct ratbag_device *device, unsigned int index); + /** + * Called to mark a previously writen profile as the default. + * + * There should be no need to write the profile here, a + * .write_profile() call is issued before calling this. + */ + int (*set_default_profile)(struct ratbag_device *device, unsigned int index); + /** * This should return a boolean whether or not the device * supports the given capability. @@ -215,6 +223,7 @@ struct ratbag_profile { } resolution; bool is_active; /**< profile is the currently active one */ + bool is_default; /**< profile is the default one */ }; #define BUTTON_ACTION_NONE \ diff --git a/src/libratbag.c b/src/libratbag.c index dc81b2e8..43c34191 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -663,6 +663,12 @@ ratbag_profile_is_active(struct ratbag_profile *profile) #endif } +LIBRATBAG_EXPORT int +ratbag_profile_is_default(struct ratbag_profile *profile) +{ + return profile->is_default; +} + LIBRATBAG_EXPORT unsigned int ratbag_device_get_num_profiles(struct ratbag_device *device) { @@ -710,6 +716,32 @@ ratbag_profile_set_active(struct ratbag_profile *profile) return rc; } +LIBRATBAG_EXPORT int +ratbag_profile_set_default(struct ratbag_profile *profile) +{ + struct ratbag_device *device = profile->device; + struct ratbag_profile *p; + int rc; + + assert(device->driver->write_profile); + rc = device->driver->write_profile(profile); + if (rc) + return rc; + + if (ratbag_device_has_capability(device, RATBAG_DEVICE_CAP_SWITCHABLE_PROFILE)) { + assert(device->driver->set_active_profile); + rc = device->driver->set_default_profile(device, profile->index); + } + + if (rc) + return rc; + + list_for_each(p, &device->profiles, link) { + p->is_default = false; + } + profile->is_default = true; + return rc; +} LIBRATBAG_EXPORT int ratbag_profile_get_num_resolutions(struct ratbag_profile *profile) diff --git a/src/libratbag.h b/src/libratbag.h index 3fb23885..fe6403c8 100644 --- a/src/libratbag.h +++ b/src/libratbag.h @@ -564,6 +564,31 @@ ratbag_profile_is_active(struct ratbag_profile *profile); int ratbag_profile_set_active(struct ratbag_profile *profile); +/** + * @ingroup profile + * + * Check if the given profile is the default one. The default profile is the + * one selected by the device when it is plugged in. + * + * @param profile A previously initialized ratbag profile + * + * @return non-zero if the profile is the default, zero otherwise + */ +int +ratbag_profile_is_default(struct ratbag_profile *profile); + +/** + * @ingroup profile + * + * Make the given profile the default profile + * + * @param profile The profile to make the default profile. + * + * @return 0 on success or nonzero otherwise. + */ +int +ratbag_profile_set_default(struct ratbag_profile *profile); + /** * @ingroup profile * diff --git a/src/libratbag.sym b/src/libratbag.sym index 1d689a78..28af0c79 100644 --- a/src/libratbag.sym +++ b/src/libratbag.sym @@ -36,9 +36,11 @@ global: ratbag_profile_get_resolution; ratbag_profile_get_user_data; ratbag_profile_is_active; + ratbag_profile_is_default; ratbag_profile_ref; ratbag_profile_set_user_data; ratbag_profile_set_active; + ratbag_profile_set_default; ratbag_profile_unref; ratbag_resolution_get_dpi; ratbag_resolution_get_dpi_x; diff --git a/tools/ratbag-command.c b/tools/ratbag-command.c index aa1e3215..d6f5762a 100644 --- a/tools/ratbag-command.c +++ b/tools/ratbag-command.c @@ -323,8 +323,9 @@ ratbag_cmd_info(struct ratbag *ratbag, uint32_t flags, int argc, char **argv) if (!profile) continue; - printf(" Profile %d%s\n", i, - ratbag_profile_is_active(profile) ? " (active)" : ""); + printf(" Profile %d%s%s\n", i, + ratbag_profile_is_active(profile) ? " (active)" : "", + ratbag_profile_is_default(profile) ? " (default)" : ""); printf(" Resolutions:\n"); for (j = 0; j < ratbag_profile_get_num_resolutions(profile); j++) { struct ratbag_resolution *res;