From 260df588566471f3e3e14ebc586fb71b213ee3fa Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Tue, 29 Sep 2015 16:17:02 +0200 Subject: [PATCH] ratbagd: profile profile accessors Provide two new profile accessors that return the path and index of a profile. This also requires us to cache the dbus path in the profile itself. Signed-off-by: David Herrmann --- src/ratbagd-device.c | 5 ++++- src/ratbagd-profile.c | 26 ++++++++++++++++++++++++++ src/ratbagd.h | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/ratbagd-device.c b/src/ratbagd-device.c index 6961c269..99e16362 100644 --- a/src/ratbagd-device.c +++ b/src/ratbagd-device.c @@ -185,7 +185,10 @@ int ratbagd_device_new(struct ratbagd_device **out, if (!profile) continue; - r = ratbagd_profile_new(&device->profiles[i], profile, i); + r = ratbagd_profile_new(&device->profiles[i], + device, + profile, + i); if (r < 0) { errno = -r; fprintf(stderr, diff --git a/src/ratbagd-profile.c b/src/ratbagd-profile.c index ad7a9d24..69b5370c 100644 --- a/src/ratbagd-profile.c +++ b/src/ratbagd-profile.c @@ -37,6 +37,7 @@ struct ratbagd_profile { struct ratbag_profile *lib_profile; unsigned int index; + char *path; }; const sd_bus_vtable ratbagd_profile_vtable[] = { @@ -46,10 +47,13 @@ const sd_bus_vtable ratbagd_profile_vtable[] = { }; int ratbagd_profile_new(struct ratbagd_profile **out, + struct ratbagd_device *device, struct ratbag_profile *lib_profile, unsigned int index) { _cleanup_(ratbagd_profile_freep) struct ratbagd_profile *profile = NULL; + char index_buffer[DECIMAL_TOKEN_MAX(unsigned int) + 1]; + int r; assert(out); assert(lib_profile); @@ -61,6 +65,14 @@ int ratbagd_profile_new(struct ratbagd_profile **out, profile->lib_profile = lib_profile; profile->index = index; + sprintf(index_buffer, "%u", index); + r = sd_bus_path_encode_many(&profile->path, + "/org/freedesktop/ratbag1/profile/%/%", + ratbagd_device_get_name(device), + index_buffer); + if (r < 0) + return r; + *out = profile; profile = NULL; return 0; @@ -71,9 +83,23 @@ struct ratbagd_profile *ratbagd_profile_free(struct ratbagd_profile *profile) if (!profile) return NULL; + profile->path = mfree(profile->path); + /* we cannot assume the profile is gone, so set NULL explicitly */ ratbag_profile_unref(profile->lib_profile); profile->lib_profile = NULL; return mfree(profile); } + +const char *ratbagd_profile_get_path(struct ratbagd_profile *profile) +{ + assert(profile); + return profile->path; +} + +bool ratbagd_profile_is_active(struct ratbagd_profile *profile) +{ + assert(profile); + return ratbag_profile_is_active(profile->lib_profile) != 0; +} diff --git a/src/ratbagd.h b/src/ratbagd.h index 6492b2fc..4039cad6 100644 --- a/src/ratbagd.h +++ b/src/ratbagd.h @@ -47,9 +47,12 @@ struct ratbagd_profile; extern const sd_bus_vtable ratbagd_profile_vtable[]; int ratbagd_profile_new(struct ratbagd_profile **out, + struct ratbagd_device *device, struct ratbag_profile *lib_profile, unsigned int index); struct ratbagd_profile *ratbagd_profile_free(struct ratbagd_profile *profile); +const char *ratbagd_profile_get_path(struct ratbagd_profile *profile); +bool ratbagd_profile_is_active(struct ratbagd_profile *profile); DEFINE_TRIVIAL_CLEANUP_FUNC(struct ratbagd_profile *, ratbagd_profile_free);