From: Takashi Sakamoto Date: Wed, 1 Apr 2020 09:13:28 +0000 (+0900) Subject: seq: event_variable: add accessor methods and constructor X-Git-Tag: v0.1.0~278 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=7a7c0a999967df27fffe271cb7fee3da51fe433e;p=alsa-gobject.git seq: event_variable: add accessor methods and constructor Signed-off-by: Takashi Sakamoto --- diff --git a/src/seq/alsaseq.map b/src/seq/alsaseq.map index a3ecbe0..5589432 100644 --- a/src/seq/alsaseq.map +++ b/src/seq/alsaseq.map @@ -65,6 +65,9 @@ ALSA_GOBJECT_0_0_0 { "alsaseq_event_fixed_new"; "alsaseq_event_variable_get_type"; + "alsaseq_event_variable_new"; + "alsaseq_event_variable_get_data"; + "alsaseq_event_variable_set_data"; local: *; }; diff --git a/src/seq/event-variable.c b/src/seq/event-variable.c index 7cb192f..537388c 100644 --- a/src/seq/event-variable.c +++ b/src/seq/event-variable.c @@ -1,14 +1,125 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "event-variable.h" +#include "privates.h" -G_DEFINE_TYPE(ALSASeqEventVariable, alsaseq_event_variable, ALSASEQ_TYPE_EVENT) +#include + +struct _ALSASeqEventVariablePrivate { + void *data; +}; +G_DEFINE_TYPE_WITH_PRIVATE(ALSASeqEventVariable, alsaseq_event_variable, ALSASEQ_TYPE_EVENT) + +static void seq_event_variable_finalize(GObject *obj) +{ + ALSASeqEventVariable *self = ALSASEQ_EVENT_VARIABLE(obj); + ALSASeqEventVariablePrivate *priv = + alsaseq_event_variable_get_instance_private(self); + + if (priv->data != NULL) + g_free(priv->data); + + G_OBJECT_CLASS(alsaseq_event_variable_parent_class)->finalize(obj); +} static void alsaseq_event_variable_class_init(ALSASeqEventVariableClass *klass) { - return; + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = seq_event_variable_finalize; } static void alsaseq_event_variable_init(ALSASeqEventVariable *self) { - return; + ALSASeqEventVariablePrivate *priv = + alsaseq_event_variable_get_instance_private(self); + + priv->data = NULL; +} + +/** + * alsaseq_event_variable_new: + * @event_type: A #ALSASeqEventType. + * @error: A #GError. + * + * Allocate and return an instance of #ALSASeqEventVariable class. + * + * Returns: (transfer full): A #ALSASeqEventVariable. + */ +ALSASeqEventVariable *alsaseq_event_variable_new(ALSASeqEventType event_type, + GError **error) +{ + switch (event_type) { + case SNDRV_SEQ_EVENT_SYSEX: + case SNDRV_SEQ_EVENT_BOUNCE: + case SNDRV_SEQ_EVENT_USR_VAR0: + case SNDRV_SEQ_EVENT_USR_VAR1: + case SNDRV_SEQ_EVENT_USR_VAR2: + case SNDRV_SEQ_EVENT_USR_VAR3: + case SNDRV_SEQ_EVENT_USR_VAR4: + break; + default: + generate_error(error, EINVAL); + break; + } + + return g_object_new(ALSASEQ_TYPE_EVENT_VARIABLE, + "type", event_type, + "length-mode", ALSASEQ_EVENT_LENGTH_MODE_VARIABLE, + NULL); +} + +/** + * alsaseq_event_variable_get_data: + * @self: A #ALSASeqEventVariable. + * @data: (array length=size)(out)(transfer none): The pointer to external data. + * @size: The size of data. + * + * Refer to the external data for the event. + */ +void alsaseq_event_variable_get_data(ALSASeqEventVariable *self, + const guint8 **data, gsize *size) +{ + ALSASeqEvent *parent; + struct snd_seq_event *ev; + + g_return_if_fail(ALSASEQ_IS_EVENT_VARIABLE(self)); + parent = ALSASEQ_EVENT(self); + seq_event_refer_private(parent, &ev); + + *data = ev->data.ext.ptr; + *size = ev->data.ext.len; +} + +/** + * alsaseq_event_variable_set_data: + * @self: A #ALSASeqEventVariable. + * @data: (array length=size)(transfer none): The pointer to external data. + * @size: The size of data. + * + * Allocate and copy the external data for the event. + */ +void alsaseq_event_variable_set_data(ALSASeqEventVariable *self, + const guint8 *data, gsize size) +{ + ALSASeqEventVariablePrivate *priv; + ALSASeqEvent *parent; + struct snd_seq_event *ev; + + g_return_if_fail(ALSASEQ_IS_EVENT_VARIABLE(self)); + priv = alsaseq_event_variable_get_instance_private(self); + parent = ALSASEQ_EVENT(self); + seq_event_refer_private(parent, &ev); + + if (priv->data != NULL) + g_free(priv->data); + priv->data = NULL; + ev->data.ext.ptr = NULL; + ev->data.ext.len = 0; + + priv->data = g_memdup(data, size); + if (priv->data == NULL) + return; + + ev->data.ext.ptr = priv->data; + ev->data.ext.len = size; } diff --git a/src/seq/event-variable.h b/src/seq/event-variable.h index 9ca9303..169c26c 100644 --- a/src/seq/event-variable.h +++ b/src/seq/event-variable.h @@ -33,9 +33,12 @@ G_BEGIN_DECLS typedef struct _ALSASeqEventVariable ALSASeqEventVariable; typedef struct _ALSASeqEventVariableClass ALSASeqEventVariableClass; +typedef struct _ALSASeqEventVariablePrivate ALSASeqEventVariablePrivate; struct _ALSASeqEventVariable { ALSASeqEvent parent_instance; + + ALSASeqEventVariablePrivate *priv; }; struct _ALSASeqEventVariableClass { @@ -44,6 +47,13 @@ struct _ALSASeqEventVariableClass { GType alsaseq_event_variable_get_type() G_GNUC_CONST; +ALSASeqEventVariable *alsaseq_event_variable_new(ALSASeqEventType event_type, + GError **error); + +void alsaseq_event_variable_get_data(ALSASeqEventVariable *self, + const guint8 **data, gsize *size); +void alsaseq_event_variable_set_data(ALSASeqEventVariable *self, + const guint8 *data, gsize size); G_END_DECLS #endif diff --git a/src/seq/event.c b/src/seq/event.c index 8d6a08e..dd49584 100644 --- a/src/seq/event.c +++ b/src/seq/event.c @@ -232,3 +232,9 @@ static void alsaseq_event_init(ALSASeqEvent *self) { return; } + +void seq_event_refer_private(ALSASeqEvent *self, struct snd_seq_event **ev) +{ + ALSASeqEventPrivate *priv = alsaseq_event_get_instance_private(self); + *ev = &priv->ev; +} diff --git a/src/seq/privates.h b/src/seq/privates.h index 6604029..2ec43a4 100644 --- a/src/seq/privates.h +++ b/src/seq/privates.h @@ -35,6 +35,8 @@ void seq_port_info_refer_private(ALSASeqPortInfo *self, void seq_client_pool_refer_private(ALSASeqClientPool *self, struct snd_seq_client_pool **pool); +void seq_event_refer_private(ALSASeqEvent *self, struct snd_seq_event **ev); + G_END_DECLS #endif diff --git a/tests/alsaseq-event-variable b/tests/alsaseq-event-variable index f28a42a..4ead81b 100644 --- a/tests/alsaseq-event-variable +++ b/tests/alsaseq-event-variable @@ -11,7 +11,11 @@ from gi.repository import ALSASeq target = ALSASeq.EventVariable() props = () -methods = () +methods = ( + 'new', + 'get_data', + 'set_data', +) signals = () if not test(target, props, methods, signals):