From 9ef572bc95ada897e6bf58e328adb7cdeb6bd2f4 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Wed, 27 Nov 2019 15:36:46 +0900 Subject: [PATCH] hwdep: add global method to get devnode string for hwdep device Signed-off-by: Takashi Sakamoto --- src/hwdep/alsahwdep.map | 1 + src/hwdep/query.c | 55 +++++++++++++++++++++++++++++++++++++++++ src/hwdep/query.h | 3 +++ 3 files changed, 59 insertions(+) diff --git a/src/hwdep/alsahwdep.map b/src/hwdep/alsahwdep.map index 466d65a..5b5eb5b 100644 --- a/src/hwdep/alsahwdep.map +++ b/src/hwdep/alsahwdep.map @@ -4,6 +4,7 @@ ALSA_GOBJECT_0_0_0 { "alsahwdep_get_device_id_list"; "alsahwdep_get_hwdep_sysname"; + "alsahwdep_get_hwdep_devnode"; local: *; }; diff --git a/src/hwdep/query.c b/src/hwdep/query.c index 1b0a836..eaf9c13 100644 --- a/src/hwdep/query.c +++ b/src/hwdep/query.c @@ -245,3 +245,58 @@ void alsahwdep_get_hwdep_sysname(guint card_id, guint device_id, udev_unref(ctx); } + +/** + * alsahwdep_get_hwdep_devnode: + * @card_id: The numeridcal ID of sound card. + * @device_id: The numerical ID of hwdep device for the sound card. + * @devnode: (out): The string for devnode of hwdep device. + * @error: A #GError. + * + * Allocate devnode string for hwdep device and return it when exists. + */ +void alsahwdep_get_hwdep_devnode(guint card_id, guint device_id, + char **devnode, GError **error) +{ + unsigned int length; + char *name; + struct udev *ctx; + struct udev_device *dev; + const char *node; + + 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); + g_free(name); + if (dev == NULL) { + generate_error(error, ENODEV); + udev_unref(ctx); + return; + } + + node = udev_device_get_devnode(dev); + if (node == NULL) { + generate_error(error, ENOENT); + } else { + *devnode = strdup(node); + if (*devnode == NULL) + generate_error(error, ENOMEM); + } + + udev_device_unref(dev); + udev_unref(ctx); +} diff --git a/src/hwdep/query.h b/src/hwdep/query.h index 4eed987..ed005ef 100644 --- a/src/hwdep/query.h +++ b/src/hwdep/query.h @@ -13,6 +13,9 @@ void alsahwdep_get_device_id_list(guint card_id, guint **entries, void alsahwdep_get_hwdep_sysname(guint card_id, guint device_id, char **sysname, GError **error); +void alsahwdep_get_hwdep_devnode(guint card_id, guint device_id, + char **devnode, GError **error); + G_END_DECLS #endif -- 2.47.3