From: Takashi Sakamoto Date: Mon, 8 Jun 2020 14:10:10 +0000 (+0900) Subject: seq: add private structure and constructor X-Git-Tag: v0.1.0~134 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=781b93209457c432ae0808f00f0f30d78a25e777;p=alsa-gobject.git seq: add private structure and constructor Signed-off-by: Takashi Sakamoto --- diff --git a/src/seq/alsaseq.map b/src/seq/alsaseq.map index 09985d3..d03af9b 100644 --- a/src/seq/alsaseq.map +++ b/src/seq/alsaseq.map @@ -171,6 +171,7 @@ ALSA_GOBJECT_0_0_0 { "alsaseq_remove_filter_new_with_real_time"; "alsaseq_event_cntr_get_type"; + "alsaseq_event_cntr_new"; local: *; }; diff --git a/src/seq/event-cntr.c b/src/seq/event-cntr.c index 7d96ff5..cc935c2 100644 --- a/src/seq/event-cntr.c +++ b/src/seq/event-cntr.c @@ -1,13 +1,76 @@ -#include "event-cntr.h" +// SPDX-License-Identifier: LGPL-3.0-or-later +#include "privates.h" -G_DEFINE_TYPE(ALSASeqEventCntr, alsaseq_event_cntr, G_TYPE_OBJECT) +#include + +typedef struct _ALSASeqEventCntrPrivate { + guint8 *buf; + gsize length; + + gboolean allocated; +} ALSASeqEventCntrPrivate; +G_DEFINE_TYPE_WITH_PRIVATE(ALSASeqEventCntr, alsaseq_event_cntr, G_TYPE_OBJECT) + +static void seq_event_cntr_finaize(GObject *obj) +{ + ALSASeqEventCntr *self = ALSASEQ_EVENT_CNTR(obj); + ALSASeqEventCntrPrivate *priv = + alsaseq_event_cntr_get_instance_private(self); + + if (priv->allocated) + g_free(priv->buf); + + G_OBJECT_CLASS(alsaseq_event_cntr_parent_class)->finalize(obj); +} static void alsaseq_event_cntr_class_init(ALSASeqEventCntrClass *klass) { - return; + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = seq_event_cntr_finaize; } static void alsaseq_event_cntr_init(ALSASeqEventCntr *self) { - return; + ALSASeqEventCntrPrivate *priv = + alsaseq_event_cntr_get_instance_private(self); + + priv->buf = NULL; + priv->length = 0; + priv->allocated = FALSE; +} + +/** + * alsaseq_event_cntr_new: + * @count: The number of events going to be allocated. + * @error: A #GError. + * + * Allocates and return an instance of #ALSASeqEventCntr to store the count + * of events. + * + * Returns: A #ALSASeqEventCntr. + */ +ALSASeqEventCntr *alsaseq_event_cntr_new(guint count, GError **error) +{ + ALSASeqEventCntr *self = g_object_new(ALSASEQ_TYPE_EVENT_CNTR, NULL); + ALSASeqEventCntrPrivate *priv = + alsaseq_event_cntr_get_instance_private(self); + struct snd_seq_event *ev; + int i; + + priv->length = sizeof(struct snd_seq_event) * count; + priv->buf = g_try_malloc0(priv->length); + if (priv->buf == NULL) { + generate_error(error, ENOMEM); + return NULL; + } + priv->allocated = TRUE; + + ev = (struct snd_seq_event *)priv->buf; + for (i = 0; i < count; ++i) { + ev[i].type = SNDRV_SEQ_EVENT_NONE; + ev[i].queue = SNDRV_SEQ_QUEUE_DIRECT; + } + + return self; } diff --git a/src/seq/event-cntr.h b/src/seq/event-cntr.h index 8a6ca85..c737606 100644 --- a/src/seq/event-cntr.h +++ b/src/seq/event-cntr.h @@ -31,9 +31,12 @@ G_BEGIN_DECLS typedef struct _ALSASeqEventCntr ALSASeqEventCntr; typedef struct _ALSASeqEventCntrClass ALSASeqEventCntrClass; +typedef struct _ALSASeqEventCntrPrivate ALSASeqEventCntrPrivate; struct _ALSASeqEventCntr { GObject parent_instance; + + ALSASeqEventCntrPrivate *priv; }; struct _ALSASeqEventCntrClass { @@ -42,6 +45,8 @@ struct _ALSASeqEventCntrClass { GType alsaseq_event_cntr_get_type() G_GNUC_CONST; +ALSASeqEventCntr *alsaseq_event_cntr_new(guint count, GError **error); + G_END_DECLS #endif diff --git a/src/seq/privates.h b/src/seq/privates.h index 6556e21..ff07c7b 100644 --- a/src/seq/privates.h +++ b/src/seq/privates.h @@ -17,6 +17,7 @@ #include "queue-status.h" #include "queue-tempo.h" #include "queue-timer.h" +#include "event-cntr.h" #include diff --git a/tests/alsaseq-event-cntr b/tests/alsaseq-event-cntr index d3e1986..9623190 100644 --- a/tests/alsaseq-event-cntr +++ b/tests/alsaseq-event-cntr @@ -11,7 +11,9 @@ from gi.repository import ALSASeq target = ALSASeq.EventCntr() props = () -methods = () +methods = ( + 'new', +) signals = () if not test(target, props, methods, signals):