From: Takashi Sakamoto Date: Wed, 27 Nov 2019 06:33:13 +0000 (+0900) Subject: hwdep: add global method to get sysname for hwdep device of sound card X-Git-Tag: v0.1.0~202 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=1a97e76098a38963f529dc403a63335af2a83698;p=alsa-gobject.git hwdep: add global method to get sysname for hwdep device of sound card Signed-off-by: Takashi Sakamoto --- diff --git a/src/hwdep/alsahwdep.map b/src/hwdep/alsahwdep.map index 779c754..466d65a 100644 --- a/src/hwdep/alsahwdep.map +++ b/src/hwdep/alsahwdep.map @@ -3,6 +3,7 @@ ALSA_GOBJECT_0_0_0 { "alsahwdep_iface_type_get_type"; "alsahwdep_get_device_id_list"; + "alsahwdep_get_hwdep_sysname"; local: *; }; diff --git a/src/hwdep/query.c b/src/hwdep/query.c index 93357e4..1b0a836 100644 --- a/src/hwdep/query.c +++ b/src/hwdep/query.c @@ -17,6 +17,7 @@ G_DEFINE_QUARK("alsahwdep-error", alsahwdep_error) // 'C' is required apart from emulation of Open Sound System. #define PREFIX_SYSNAME_TEMPLATE "hwC%u" +#define HWDEP_SYSNAME_TEMPLATE "hwC%uD%u" static void prepare_udev_enum(struct udev_enumerate **enumerator, GError **error) { @@ -199,3 +200,48 @@ end: g_free(prefix); release_udev_enum(enumerator); } + +/** + * alsahwdep_get_hwdep_sysname: + * @card_id: The numeridcal ID of sound card. + * @device_id: The numerical ID of hwdep device for the sound card. + * @sysname: (out): The string for sysname of hwdep device. + * @error: A #GError. + * + * Allocate sysname for hwdep device and return it when it exists. + */ +void alsahwdep_get_hwdep_sysname(guint card_id, guint device_id, + char **sysname, GError **error) +{ + unsigned int length; + char *name; + struct udev *ctx; + struct udev_device *dev; + + length = strlen(HWDEP_SYSNAME_TEMPLATE) + calculate_digits(card_id) + + calculate_digits(device_id) + 1; + name = g_try_malloc0(length); + if (name == NULL) { + generate_error(error, ENOMEM); + return; + } + snprintf(name, length, HWDEP_SYSNAME_TEMPLATE, card_id, device_id); + + ctx = udev_new(); + if (ctx == NULL) { + generate_error(error, errno); + g_free(name); + return; + } + + dev = udev_device_new_from_subsystem_sysname(ctx, "sound", name); + if (dev == NULL) { + generate_error(error, ENODEV); + g_free(name); + } else { + *sysname = name; + udev_device_unref(dev); + } + + udev_unref(ctx); +} diff --git a/src/hwdep/query.h b/src/hwdep/query.h index 4cee162..4eed987 100644 --- a/src/hwdep/query.h +++ b/src/hwdep/query.h @@ -10,6 +10,9 @@ G_BEGIN_DECLS void alsahwdep_get_device_id_list(guint card_id, guint **entries, gsize *entry_count, GError **error); +void alsahwdep_get_hwdep_sysname(guint card_id, guint device_id, + char **sysname, GError **error); + G_END_DECLS #endif