From 5b1b5b7f34c872216bba1e81f46a24865c811245 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Fri, 12 Jun 2020 22:24:02 +0900 Subject: [PATCH] timer: user_instance: add API to choose the type of event to read Currently ALSATimer.UserInstance has any attach API to decide the type of event to read. In design of ALSA Timer core, the attach API has two functionality; to attach or to detach with error for absent timer device. Current implementation has inconvenient for the functionality due to execution to choose the type of event in advance. The execution fails when the instance is already attached. This commit adds a new API to choose the type of event and split the call from any attach API. Signed-off-by: Takashi Sakamoto --- src/timer/alsatimer.map | 1 + src/timer/user-instance.c | 55 ++++++++++++++++++++++------------- src/timer/user-instance.h | 6 ++-- tests/alsatimer-user-instance | 1 + 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/timer/alsatimer.map b/src/timer/alsatimer.map index 616660c..f811304 100644 --- a/src/timer/alsatimer.map +++ b/src/timer/alsatimer.map @@ -32,6 +32,7 @@ ALSA_GOBJECT_0_0_0 { "alsatimer_user_instance_get_type"; "alsatimer_user_instance_new"; "alsatimer_user_instance_open"; + "alsatimer_user_instance_choose_event_data_type"; "alsatimer_user_instance_attach"; "alsatimer_user_instance_attach_as_slave"; "alsatimer_user_instance_get_info"; diff --git a/src/timer/user-instance.c b/src/timer/user-instance.c index e025b42..3d7db23 100644 --- a/src/timer/user-instance.c +++ b/src/timer/user-instance.c @@ -139,35 +139,55 @@ ALSATimerUserInstance *alsatimer_user_instance_new() return g_object_new(ALSATIMER_TYPE_USER_INSTANCE, NULL); } +/** + * alsatimer_user_instance_choose_event_data_type: + * @self: A #ALSATimerUserInstance. + * @event_data_type: The type of event data, one of ALSATimerEventDataType. + * @error: A #GError. + * + * Choose the type of event data to receive. + * + * The call of function is successful just before the instance is not attached + * yet. ALSATIMER_EVENT_DATA_TYPE_TICK is used as a default if the function is + * not called for ALSATIMER_EVENT_DATA_TYPE_TSTAMP explicitly. + */ +void alsatimer_user_instance_choose_event_data_type(ALSATimerUserInstance *self, + ALSATimerEventDataType event_data_type, + GError **error) +{ + ALSATimerUserInstancePrivate *priv; + int tread; + + g_return_if_fail(ALSATIMER_IS_USER_INSTANCE(self)); + priv = alsatimer_user_instance_get_instance_private(self); + + tread = (int)event_data_type; + if (ioctl(priv->fd, SNDRV_TIMER_IOCTL_TREAD, &tread) < 0) + generate_error(error, errno); + else + priv->event_data_type = event_data_type; +} + /** * alsatimer_user_instance_attach: * @self: A #ALSATimerUserInstance. * @device_id: A #ALSATimerDeviceId to which the instance is attached. - * @event_data_type: The type of event data, one of ALSATimerEventDataType. * @error: A #GError. * - * Attach the instance to the timer device. + * Attach the instance to the timer device. If the given device_id is for + * absent timer device, the instance can be detached with error. */ void alsatimer_user_instance_attach(ALSATimerUserInstance *self, ALSATimerDeviceId *device_id, - ALSATimerEventDataType event_data_type, GError **error) { ALSATimerUserInstancePrivate *priv; - int tread; struct snd_timer_select sel = {0}; g_return_if_fail(ALSATIMER_IS_USER_INSTANCE(self)); g_return_if_fail(device_id != NULL); priv = alsatimer_user_instance_get_instance_private(self); - tread = (int)event_data_type; - if (ioctl(priv->fd, SNDRV_TIMER_IOCTL_TREAD, &tread) < 0) { - generate_error(error, errno); - return; - } - priv->event_data_type = event_data_type; - sel.id = *device_id; if (ioctl(priv->fd, SNDRV_TIMER_IOCTL_SELECT, &sel) < 0) generate_error(error, errno); @@ -179,7 +199,6 @@ void alsatimer_user_instance_attach(ALSATimerUserInstance *self, * @slave_class: The class identifier of master instance, one of * #ALSATimerSlaveClass. * @slave_id: The numerical identifier of master instance. - * @event_data_type: The type of event data, one of ALSATimerEventDataType. * @error: A #GError. * * Attach the instance to timer device as an slave to another instance indicated @@ -188,27 +207,21 @@ void alsatimer_user_instance_attach(ALSATimerUserInstance *self, * application process which owns the instance of timer. If the slave_class is * for ALSA sequencer (=ALSATIMER_SLAVE_CLASS_SEQUENCER), the slave_id is the * numerical ID of queue bound for timer device. + * + * Attach the instance to the timer device. If the given device_id is for + * absent timer device, the instance can be detached with error. */ void alsatimer_user_instance_attach_as_slave(ALSATimerUserInstance *self, ALSATimerSlaveClass slave_class, int slave_id, - ALSATimerEventDataType event_data_type, GError **error) { ALSATimerUserInstancePrivate *priv; - int tread; struct snd_timer_select sel = {0}; g_return_if_fail(ALSATIMER_IS_USER_INSTANCE(self)); priv = alsatimer_user_instance_get_instance_private(self); - tread = (int)event_data_type; - if (ioctl(priv->fd, SNDRV_TIMER_IOCTL_TREAD, &tread) < 0) { - generate_error(error, errno); - return; - } - priv->event_data_type = event_data_type; - sel.id.dev_class = SNDRV_TIMER_CLASS_SLAVE; sel.id.dev_sclass = slave_class; sel.id.device = slave_id; diff --git a/src/timer/user-instance.h b/src/timer/user-instance.h index dfdce98..7ceab6f 100644 --- a/src/timer/user-instance.h +++ b/src/timer/user-instance.h @@ -77,15 +77,17 @@ ALSATimerUserInstance *alsatimer_user_instance_new(); void alsatimer_user_instance_open(ALSATimerUserInstance *self, gint open_flag, GError **error); +void alsatimer_user_instance_choose_event_data_type(ALSATimerUserInstance *self, + ALSATimerEventDataType event_data_type, + GError **error); + void alsatimer_user_instance_attach(ALSATimerUserInstance *self, ALSATimerDeviceId *device_id, - ALSATimerEventDataType event_data_type, GError **error); void alsatimer_user_instance_attach_as_slave(ALSATimerUserInstance *self, ALSATimerSlaveClass slave_class, int slave_id, - ALSATimerEventDataType event_data_type, GError **error); void alsatimer_user_instance_get_info(ALSATimerUserInstance *self, diff --git a/tests/alsatimer-user-instance b/tests/alsatimer-user-instance index 48de47e..49e925a 100644 --- a/tests/alsatimer-user-instance +++ b/tests/alsatimer-user-instance @@ -14,6 +14,7 @@ props = () methods = ( 'new', 'open', + 'choose_event_data_type', 'attach', 'attach_as_slave', 'get_info', -- 2.47.3