]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
timer: instance_params: add properties
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/instance-params.c
src/timer/instance-params.h
tests/alsatimer-instance-params

index 4293a54eb4504ae9cf50b93b82684638be5cebb7..ea0dab5c6480bc98a85f2ee1a07c852894c21e5f 100644 (file)
@@ -38,6 +38,8 @@ ALSA_GOBJECT_0_0_0 {
 
     "alsatimer_instance_params_get_type";
     "alsatimer_instance_params_new";
+    "alsatimer_instance_params_set_event_filter";
+    "alsatimer_instance_params_get_event_filter";
   local:
     *;
 };
index ee1339fe98f6a03a913cf7b94875ddcd3c13bb48..cf728c0a406a19ef186c3460befc54ea2d6ebc1b 100644 (file)
 // SPDX-License-Identifier: LGPL-3.0-or-later
 #include "instance-params.h"
+#include "privates.h"
 
-G_DEFINE_TYPE(ALSATimerInstanceParams, alsatimer_instance_params, G_TYPE_OBJECT)
+#include <sound/asound.h>
+
+#include <errno.h>
+
+struct _ALSATimerInstanceParamsPrivate{
+    struct snd_timer_params params;
+};
+G_DEFINE_TYPE_WITH_PRIVATE(ALSATimerInstanceParams, alsatimer_instance_params, G_TYPE_OBJECT)
+
+enum timer_instance_params_prop_type {
+    TIMER_INSTANCE_PARAMS_PROP_FLAGS = 1,
+    TIMER_INSTANCE_PARAMS_PROP_INTERVAL,
+    TIMER_INSTANCE_PARAMS_PROP_QUEUE_SIZE,
+    TIMER_INSTANCE_PARAMS_PROP_COUNT,
+};
+static GParamSpec *timer_instance_params_props[TIMER_INSTANCE_PARAMS_PROP_COUNT] = { NULL, };
+
+static void timer_instance_params_set_property(GObject *obj, guint id,
+                                        const GValue *val, GParamSpec *spec)
+{
+    ALSATimerInstanceParams *self = ALSATIMER_INSTANCE_PARAMS(obj);
+    ALSATimerInstanceParamsPrivate *priv =
+                            alsatimer_instance_params_get_instance_private(self);
+
+    switch (id) {
+    case TIMER_INSTANCE_PARAMS_PROP_FLAGS:
+        priv->params.flags = (unsigned int)g_value_get_flags(val);
+        break;
+    case TIMER_INSTANCE_PARAMS_PROP_INTERVAL:
+        priv->params.ticks = g_value_get_uint(val);
+        break;
+    case TIMER_INSTANCE_PARAMS_PROP_QUEUE_SIZE:
+        priv->params.queue_size = g_value_get_uint(val);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
+        break;
+    }
+}
+
+static void timer_instance_params_get_property(GObject *obj, guint id,
+                                             GValue *val, GParamSpec *spec)
+{
+    ALSATimerInstanceParams *self = ALSATIMER_INSTANCE_PARAMS(obj);
+    ALSATimerInstanceParamsPrivate *priv =
+                            alsatimer_instance_params_get_instance_private(self);
+
+    switch (id) {
+    case TIMER_INSTANCE_PARAMS_PROP_FLAGS:
+        g_value_set_flags(val, (ALSATimerInstanceParamFlag)priv->params.flags);
+        break;
+    case TIMER_INSTANCE_PARAMS_PROP_INTERVAL:
+        g_value_set_uint(val, priv->params.ticks);
+        break;
+    case TIMER_INSTANCE_PARAMS_PROP_QUEUE_SIZE:
+        g_value_set_uint(val, priv->params.queue_size);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
+        break;
+    }
+}
 
 static void alsatimer_instance_params_class_init(ALSATimerInstanceParamsClass *klass)
 {
-    return;
+    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+
+    gobject_class->set_property = timer_instance_params_set_property;
+    gobject_class->get_property = timer_instance_params_get_property;
+
+    timer_instance_params_props[TIMER_INSTANCE_PARAMS_PROP_FLAGS] =
+        g_param_spec_flags("flags", "flags",
+                           "The flags for user instance, as a set of "
+                          "ALSATimerInstanceParamFlag",
+                           ALSATIMER_TYPE_INSTANCE_PARAM_FLAG,
+                           0,
+                           G_PARAM_READWRITE);
+
+    timer_instance_params_props[TIMER_INSTANCE_PARAMS_PROP_INTERVAL] =
+        g_param_spec_uint("interval", "interval",
+                          "The interval to generate event in tick count.",
+                          0, G_MAXUINT,
+                          0,
+                          G_PARAM_READWRITE);
 
+    timer_instance_params_props[TIMER_INSTANCE_PARAMS_PROP_QUEUE_SIZE] =
+        g_param_spec_uint("queue-size", "queue-size",
+                          "The size of queue.",
+                          0, G_MAXUINT,
+                          0,
+                          G_PARAM_READWRITE);
+
+    g_object_class_install_properties(gobject_class,
+                                      TIMER_INSTANCE_PARAMS_PROP_COUNT,
+                                      timer_instance_params_props);
 }
 
 static void alsatimer_instance_params_init(ALSATimerInstanceParams *self)
@@ -23,3 +113,108 @@ ALSATimerInstanceParams *alsatimer_instance_params_new()
 {
     return g_object_new(ALSATIMER_TYPE_INSTANCE_PARAMS, NULL);
 }
+
+/**
+ * alsatimer_instance_params_set_event_filter:
+ * @self: A #ALSATimerInstanceParams.
+ * @entries: (array length=entry_count): The array with elements for entries of
+ *           #ALSATimerEventType.
+ * @entry_count: The number of elements in the above array.
+ * @error: A #GError at failure.
+ *
+ * Set the list of ALSATimerEventType to filter events.
+ */
+void alsatimer_instance_params_set_event_filter(ALSATimerInstanceParams *self,
+                                            const ALSATimerEventType *entries,
+                                            gsize entry_count, GError **error)
+{
+    ALSATimerInstanceParamsPrivate *priv;
+    unsigned int filter;
+    int i;
+
+    g_return_if_fail(ALSATIMER_IS_INSTANCE_PARAMS(self));
+    g_return_if_fail(entries != NULL);
+    priv = alsatimer_instance_params_get_instance_private(self);
+
+    priv->params.filter = 0;
+
+    // Clear the event filter.
+    if (entries == NULL || entry_count == 0)
+        return;
+
+    filter = 0;
+    for (i = 0; i < entry_count; ++i) {
+        int val = (int)entries[i];
+        if (val < SNDRV_TIMER_EVENT_RESOLUTION ||
+            (val > SNDRV_TIMER_EVENT_RESUME &&
+             val < SNDRV_TIMER_EVENT_MSTART) ||
+            val > SNDRV_TIMER_EVENT_MRESUME) {
+            generate_error(error, EINVAL);
+            return;
+        }
+        filter |= (1u << val);
+    }
+
+    priv->params.filter = filter;
+}
+
+/**
+ * alsatimer_instance_params_get_event_filter:
+ * @self: A #ALSATimerInstanceParams.
+ * @entries: (array length=entry_count)(out): The array with elements for
+ *           entries of #ALSATimerEventType.
+ * @entry_count: The number of elements in the above array.
+ * @error: A #GError at failure.
+ *
+ * Get the list of ALSATimerEventType to filter events.
+ */
+void alsatimer_instance_params_get_event_filter(ALSATimerInstanceParams *self,
+                                            ALSATimerEventType **entries,
+                                            gsize *entry_count, GError **error)
+{
+    ALSATimerInstanceParamsPrivate *priv;
+    ALSATimerEventType *list;
+    unsigned int filter;
+    unsigned int count;
+    unsigned int index;
+    int i;
+
+    g_return_if_fail(ALSATIMER_IS_INSTANCE_PARAMS(self));
+    g_return_if_fail(entries != NULL);
+    g_return_if_fail(entry_count != NULL);
+    priv = alsatimer_instance_params_get_instance_private(self);
+
+    count = 0;
+    filter = priv->params.filter;
+    for (i = 0; i < sizeof(filter) * 8; ++i) {
+        if ((1u << i) & filter)
+            ++count;
+    }
+
+    if (count == 0)
+        return;
+
+    list = g_try_malloc0_n(count, sizeof(*list));
+    if (list == NULL) {
+        generate_error(error, ENOMEM);
+        return;
+    }
+
+    index = 0;
+    for (i = 0; i < sizeof(filter) * 8; ++i) {
+        if ((1u << i) & filter) {
+            list[index] = (ALSATimerEventType)i;
+            if (++index >= count)
+                break;
+        }
+    }
+
+    if (index != count) {
+        generate_error(error, ENXIO);
+        g_free(list);
+        return;
+    }
+
+    *entries = list;
+    *entry_count = count;
+}
index f7be8f4bddb850dc90e6a8021181b187531528f5..5c3da3999680f093068d28ef1995aacfc4412efb 100644 (file)
@@ -5,6 +5,8 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include <timer/alsatimer-enums.h>
+
 G_BEGIN_DECLS
 
 #define ALSATIMER_TYPE_INSTANCE_PARAMS  (alsatimer_instance_params_get_type())
@@ -31,9 +33,12 @@ G_BEGIN_DECLS
 
 typedef struct _ALSATimerInstanceParams         ALSATimerInstanceParams;
 typedef struct _ALSATimerInstanceParamsClass    ALSATimerInstanceParamsClass;
+typedef struct _ALSATimerInstanceParamsPrivate  ALSATimerInstanceParamsPrivate;
 
 struct _ALSATimerInstanceParams {
     GObject parent_instance;
+
+    ALSATimerInstanceParamsPrivate *priv;
 };
 
 struct _ALSATimerInstanceParamsClass {
@@ -44,6 +49,14 @@ GType alsatimer_instance_params_get_type() G_GNUC_CONST;
 
 ALSATimerInstanceParams *alsatimer_instance_params_new();
 
+void alsatimer_instance_params_set_event_filter(ALSATimerInstanceParams *self,
+                                            const ALSATimerEventType *entries,
+                                            gsize entry_count, GError **error);
+
+void alsatimer_instance_params_get_event_filter(ALSATimerInstanceParams *self,
+                                            ALSATimerEventType **entries,
+                                            gsize *entry_count, GError **error);
+
 G_END_DECLS
 
 #endif
index dbc576c2a3551c99f5a006295ba55e0f5bda35f2..358ea70399dacdcc11c42dbeac5602a2527b7509 100644 (file)
@@ -10,9 +10,15 @@ gi.require_version('ALSATimer', '0.0')
 from gi.repository import ALSATimer
 
 target = ALSATimer.InstanceParams()
-props = ()
+props = (
+    'flags',
+    'interval',
+    'queue-size',
+)
 methods = (
     'new',
+    'set_event_filter',
+    'get_event_filter',
 )
 signals = ()