From 335411422c5ca298ef3e2eee6b725284073a4353 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Sun, 22 Dec 2019 11:05:29 +0900 Subject: [PATCH] ctl: elem_id: add an API to check whether two instances indicates the same element It's convenient for users to check whether two instances of elem_id indicates the same element. This commit adds an API to satisfy the convenience. Signed-off-by: Takashi Sakamoto --- src/ctl/alsactl.map | 1 + src/ctl/elem-id.c | 22 ++++++++++++++++++++++ src/ctl/elem-id.h | 3 +++ tests/alsactl-elem-id | 1 + 4 files changed, 27 insertions(+) diff --git a/src/ctl/alsactl.map b/src/ctl/alsactl.map index b88de55..37268cf 100644 --- a/src/ctl/alsactl.map +++ b/src/ctl/alsactl.map @@ -40,6 +40,7 @@ ALSA_GOBJECT_0_0_0 { "alsactl_elem_id_get_subdevice_id"; "alsactl_elem_id_get_name"; "alsactl_elem_id_get_index"; + "alsactl_elem_id_equal"; "alsactl_elem_info_get_type"; diff --git a/src/ctl/elem-id.c b/src/ctl/elem-id.c index b598e91..79c7017 100644 --- a/src/ctl/elem-id.c +++ b/src/ctl/elem-id.c @@ -129,3 +129,25 @@ void alsactl_elem_id_get_index(const ALSACtlElemId *self, guint *index) { *index = self->index; } + +/** + * alsactl_elem_id_equal: + * @self: A #ALSACtlElemId. + * @target: A #ALSACtlElemId to compare. + * + * Returns: whether the given object indicates the same element. + */ +gboolean alsactl_elem_id_equal(const ALSACtlElemId *self, + const ALSACtlElemId *target) { + const struct snd_ctl_elem_id *lhs, *rhs; + + lhs = (const struct snd_ctl_elem_id *)self; + rhs = (const struct snd_ctl_elem_id *)target; + + return lhs->numid == rhs->numid || + (lhs->iface == rhs->iface && + lhs->device == rhs->device && + lhs->subdevice == rhs->subdevice && + !strcmp((const char *)lhs->name, (const char *)rhs->name) && + lhs->index == rhs->index); +} diff --git a/src/ctl/elem-id.h b/src/ctl/elem-id.h index 7482348..b20f581 100644 --- a/src/ctl/elem-id.h +++ b/src/ctl/elem-id.h @@ -31,6 +31,9 @@ void alsactl_elem_id_get_subdevice_id(const ALSACtlElemId *self, void alsactl_elem_id_get_name(const ALSACtlElemId *self, const gchar **name); void alsactl_elem_id_get_index(const ALSACtlElemId *self, guint *index); +gboolean alsactl_elem_id_equal(const ALSACtlElemId *self, + const ALSACtlElemId *target); + G_END_DECLS #endif diff --git a/tests/alsactl-elem-id b/tests/alsactl-elem-id index 454ff9d..08f6b1e 100644 --- a/tests/alsactl-elem-id +++ b/tests/alsactl-elem-id @@ -18,6 +18,7 @@ methods = ( 'get_subdevice', 'get_name', 'get_index', + 'equals', ) signals = () -- 2.47.3