]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
timer: user_instance: add an API to open ALSA Timer character device
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Sun, 9 Feb 2020 03:20:53 +0000 (12:20 +0900)
committer坂本 貴史 <o-takashi@sakamocchi.jp>
Tue, 11 Feb 2020 04:28:18 +0000 (13:28 +0900)
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
src/timer/alsatimer.map
src/timer/user-instance.c
src/timer/user-instance.h
tests/alsatimer-user-instance

index b3d6eaee0786a61037f2497ce31fd0b01fc63185..63be1ebb91c98f312882f7b831b66a970e9d26f8 100644 (file)
@@ -29,6 +29,7 @@ ALSA_GOBJECT_0_0_0 {
 
     "alsatimer_user_instance_get_type";
     "alsatimer_user_instance_new";
+    "alsatimer_user_instance_open";
   local:
     *;
 };
index d8e873d363bf6189ab872b2a3ac4eea74fca3b2f..f9e0835e0e44f7f7fe7f20661c4ba83b88d38da6 100644 (file)
@@ -1,16 +1,70 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "user-instance.h"
+#include "query.h"
+#include "privates.h"
 
-G_DEFINE_TYPE(ALSATimerUserInstance, alsatimer_user_instance, G_TYPE_OBJECT)
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+struct _ALSATimerUserInstancePrivate {
+    int fd;
+};
+G_DEFINE_TYPE_WITH_PRIVATE(ALSATimerUserInstance, alsatimer_user_instance, G_TYPE_OBJECT)
+
+static void timer_user_instance_finalize(GObject *obj)
+{
+    ALSATimerUserInstance *self = ALSATIMER_USER_INSTANCE(obj);
+    ALSATimerUserInstancePrivate *priv =
+                            alsatimer_user_instance_get_instance_private(self);
+
+    if (priv->fd >= 0)
+        close(priv->fd);
+
+    G_OBJECT_CLASS(alsatimer_user_instance_parent_class)->finalize(obj);
+}
 
 static void alsatimer_user_instance_class_init(ALSATimerUserInstanceClass *klass)
 {
-    return;
+    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+
+    gobject_class->finalize = timer_user_instance_finalize;
 }
 
 static void alsatimer_user_instance_init(ALSATimerUserInstance *self)
 {
-    return;
+    ALSATimerUserInstancePrivate *priv =
+                            alsatimer_user_instance_get_instance_private(self);
+
+    priv->fd = -1;
+}
+
+/**
+ * alsatimer_user_instance_open:
+ * @self: A #ALSATimerUserInstance.
+ * @error: A #GError.
+ *
+ * Open ALSA Timer character device to allocate queue.
+ */
+void alsatimer_user_instance_open(ALSATimerUserInstance *self, GError **error)
+{
+    ALSATimerUserInstancePrivate *priv;
+    char *devnode;
+
+    g_return_if_fail(ALSATIMER_IS_USER_INSTANCE(self));
+    priv = alsatimer_user_instance_get_instance_private(self);
+
+    alsatimer_get_devnode(&devnode, error);
+    if (*error != NULL)
+        return;
+
+    priv->fd = open(devnode, O_RDONLY);
+    g_free(devnode);
+    if (priv->fd < 0) {
+        generate_error(error, errno);
+    }
 }
 
 ALSATimerUserInstance *alsatimer_user_instance_new()
index 141d79801c81744818402e0855701223e56a4153..768b8bf3bbd51f4ddbe5f07702eae83be2e491b9 100644 (file)
@@ -31,9 +31,12 @@ G_BEGIN_DECLS
 
 typedef struct _ALSATimerUserInstance           ALSATimerUserInstance;
 typedef struct _ALSATimerUserInstanceClass      ALSATimerUserInstanceClass;
+typedef struct _ALSATimerUserInstancePrivate    ALSATimerUserInstancePrivate;
 
 struct _ALSATimerUserInstance {
     GObject parent_instance;
+
+    ALSATimerUserInstancePrivate *priv;
 };
 
 struct _ALSATimerUserInstanceClass {
@@ -44,6 +47,8 @@ GType alsatimer_user_instance_get_type() G_GNUC_CONST;
 
 ALSATimerUserInstance *alsatimer_user_instance_new();
 
+void alsatimer_user_instance_open(ALSATimerUserInstance *self, GError **error);
+
 G_END_DECLS
 
 #endif
index 60cbcf010aa12f7d4459c48f4840c20407a538e6..33ad6b359b62d36320e37381f69c2bcc6574cd5b 100644 (file)
@@ -13,6 +13,7 @@ target = ALSATimer.UserInstance()
 props = ()
 methods = (
     'new',
+    'open',
 )
 signals = ()