]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
hwdep: add global method to get information of hwdep device
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Wed, 27 Nov 2019 07:08:52 +0000 (16:08 +0900)
committer坂本 貴史 <o-takashi@sakamocchi.jp>
Wed, 8 Apr 2020 01:40:25 +0000 (10:40 +0900)
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
src/hwdep/alsahwdep.map
src/hwdep/device-info.c
src/hwdep/privates.h
src/hwdep/query.c
src/hwdep/query.h

index 58b4aac812fb7b8b13117f0d19797536b23a9b95..979c10941b585d409beddec6c96c2d6ef03c77b8 100644 (file)
@@ -5,6 +5,7 @@ ALSA_GOBJECT_0_0_0 {
     "alsahwdep_get_device_id_list";
     "alsahwdep_get_hwdep_sysname";
     "alsahwdep_get_hwdep_devnode";
+    "alsahwdep_get_device_info";
 
     "alsahwdep_device_info_get_type";
   local:
index 409c0e578491f78662962b6573ceae390921eff6..44ff77e8a235bce9eaa5ab0594ba1cde35c181f9 100644 (file)
@@ -1,7 +1,5 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
-#include "device-info.h"
-
-#include <sound/asound.h>
+#include "privates.h"
 
 struct _ALSAHwdepDeviceInfoPrivate {
     struct snd_hwdep_info info;
@@ -27,19 +25,19 @@ static void hwdep_device_info_get_property(GObject *obj, guint id, GValue *val,
 
     switch (id) {
     case HWDEP_DEVICE_INFO_PROP_DEVICE_ID:
-        priv->info.device = g_value_get_uint(val);
+        g_value_set_uint(val, priv->info.device);
         break;
     case HWDEP_DEVICE_INFO_PROP_CARD_ID:
-        priv->info.card = g_value_get_int(val);
+        g_value_set_int(val, priv->info.card);
         break;
     case HWDEP_DEVICE_INFO_PROP_ID:
-        strncpy((char *)priv->info.id, g_value_get_string(val), sizeof(priv->info.id));
+        g_value_set_static_string(val, (char *)priv->info.id);
         break;
     case HWDEP_DEVICE_INFO_PROP_NAME:
-        strncpy((char *)priv->info.name, g_value_get_string(val), sizeof(priv->info.name));
+        g_value_set_static_string(val, (char *)priv->info.name);
         break;
     case HWDEP_DEVICE_INFO_PROP_IFACE:
-        priv->info.iface = (int)g_value_get_enum(val);
+        g_value_set_enum(val, (ALSAHwdepIfaceType)priv->info.iface);
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
@@ -96,3 +94,12 @@ static void alsahwdep_device_info_init(ALSAHwdepDeviceInfo *self)
 {
     return;
 }
+
+void hwdep_device_info_refer_private(ALSAHwdepDeviceInfo *self,
+                                     struct snd_hwdep_info **info)
+{
+    ALSAHwdepDeviceInfoPrivate *priv =
+                            alsahwdep_device_info_get_instance_private(self);
+
+    *info = &priv->info;
+}
index 621edc347399241aa0956b4b7f405898ef78ff93..5e881bb3b9f940410c76924f8d6c6d3c2247acfc 100644 (file)
@@ -2,6 +2,10 @@
 #ifndef __ALSA_GOBJECT_ALSAHWDEP_PRIVATES__H__
 #define __ALSA_GOBJECT_ALSAHWDEP_PRIVATES__H__
 
+#include "device-info.h"
+
+#include <sound/asound.h>
+
 G_BEGIN_DECLS
 
 GQuark alsahwdep_error_quark(void);
@@ -10,6 +14,9 @@ GQuark alsahwdep_error_quark(void);
     g_set_error(err, alsahwdep_error_quark(), errno,            \
                 __FILE__ ":%d: %s", __LINE__, strerror(errno))
 
+void hwdep_device_info_refer_private(ALSAHwdepDeviceInfo *self,
+                                     struct snd_hwdep_info **info);
+
 G_END_DECLS
 
 #endif
index eaf9c13821fd53fd954e7f6ef8f76d981e6a1df6..5f92f0fa1ad7d6a281ad5ae770a1c85e9a1948ab 100644 (file)
@@ -18,6 +18,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"
+#define CTL_SYSNAME_TEMPLATE        "controlC%u"
 
 static void prepare_udev_enum(struct udev_enumerate **enumerator, GError **error)
 {
@@ -300,3 +301,80 @@ void alsahwdep_get_hwdep_devnode(guint card_id, guint device_id,
     udev_device_unref(dev);
     udev_unref(ctx);
 }
+
+static void hwdep_perform_ctl_ioctl(guint card_id, long request, void *data,
+                                      GError **error)
+{
+    unsigned int length;
+    char *sysname;
+    struct udev *ctx;
+    struct udev_device *dev;
+    const char *devnode;
+    int fd;
+
+    length = strlen(CTL_SYSNAME_TEMPLATE) + calculate_digits(card_id) + 1;
+    sysname = g_try_malloc0(length);
+    if (sysname == NULL) {
+        generate_error(error, ENOMEM);
+        return;
+    }
+    snprintf(sysname, length, CTL_SYSNAME_TEMPLATE, card_id);
+
+    ctx = udev_new();
+    if (ctx == NULL) {
+        generate_error(error, errno);
+        goto err_sysname;
+    }
+
+    dev = udev_device_new_from_subsystem_sysname(ctx, "sound", sysname);
+    if (dev == NULL) {
+        generate_error(error, errno);
+        goto err_ctx;
+    }
+
+    devnode = udev_device_get_devnode(dev);
+    if (devnode == NULL) {
+        generate_error(error, ENODEV);
+        goto err_device;
+    }
+
+    fd = open(devnode, O_RDONLY | O_NONBLOCK);
+    if (fd < 0) {
+        generate_error(error, errno);
+        goto err_device;
+    }
+
+    if (ioctl(fd, request, data) < 0)
+        generate_error(error, errno);
+err_device:
+    udev_device_unref(dev);
+err_ctx:
+    udev_unref(ctx);
+err_sysname:
+    g_free(sysname);
+}
+
+/**
+ * alsahwdep_get_device_info:
+ * @card_id: The numberical value for sound card to query.
+ * @device_id: The numerical value of hwdep device to query.
+ * @device_info: (out): The information of the device.
+ * @error: A #GError.
+ */
+void alsahwdep_get_device_info(guint card_id, guint device_id,
+                               ALSAHwdepDeviceInfo **device_info,
+                               GError **error)
+{
+    struct snd_hwdep_info *info;
+
+    g_return_if_fail(device_info != NULL);
+
+    *device_info = g_object_new(ALSAHWDEP_TYPE_DEVICE_INFO, NULL);
+    hwdep_device_info_refer_private(*device_info, &info);
+
+    info->device = device_id;
+    info->card = card_id;
+    hwdep_perform_ctl_ioctl(card_id, SNDRV_CTL_IOCTL_HWDEP_INFO, info, error);
+    if (*error != NULL)
+        g_object_unref(*device_info);
+}
index ed005ef8ddf87cb11f7708f43eef2b0429059795..58369d45fb1fe5fc0a7b11d4b891e9ecb8ef6456 100644 (file)
@@ -5,6 +5,8 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include <hwdep/device-info.h>
+
 G_BEGIN_DECLS
 
 void alsahwdep_get_device_id_list(guint card_id, guint **entries,
@@ -16,6 +18,10 @@ void alsahwdep_get_hwdep_sysname(guint card_id, guint device_id,
 void alsahwdep_get_hwdep_devnode(guint card_id, guint device_id,
                                  char **devnode, GError **error);
 
+void alsahwdep_get_device_info(guint card_id, guint device_id,
+                               ALSAHwdepDeviceInfo **device_info,
+                               GError **error);
+
 G_END_DECLS
 
 #endif