From c8336345e553f0bea818312d5109339cd8dd8754 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Sun, 9 Feb 2020 12:20:53 +0900 Subject: [PATCH] timer: add global method to get sysname for ALSA Timer Signed-off-by: Takashi Sakamoto --- src/timer/alsatimer.map | 2 ++ src/timer/query.c | 60 +++++++++++++++++++++++++++++++++++++++++ src/timer/query.h | 2 ++ 3 files changed, 64 insertions(+) diff --git a/src/timer/alsatimer.map b/src/timer/alsatimer.map index 9fd4420..5051462 100644 --- a/src/timer/alsatimer.map +++ b/src/timer/alsatimer.map @@ -6,6 +6,8 @@ ALSA_GOBJECT_0_0_0 { "alsatimer_device_info_flag_get_type"; "alsatimer_instance_param_flag_get_type"; "alsatimer_event_type_get_type"; + + "alsatimer_get_sysname"; local: *; }; diff --git a/src/timer/query.c b/src/timer/query.c index d112a5f..60741cf 100644 --- a/src/timer/query.c +++ b/src/timer/query.c @@ -2,5 +2,65 @@ #include "query.h" #include "privates.h" +#include +#include + +#include + +#define TIMER_SYSNAME_TEMPLATE "timer" + // For error handling. G_DEFINE_QUARK("alsatimer-error", alsatimer_error) + +static bool check_existence(char *sysname, GError **error) +{ + struct udev *ctx; + struct udev_device *dev; + bool result; + + ctx = udev_new(); + if (ctx == NULL) { + generate_error(error, errno); + return false; + } + + dev = udev_device_new_from_subsystem_sysname(ctx, "sound", sysname); + if (dev == NULL) { + generate_error(error, errno); + result = false; + } else { + result = true; + } + udev_device_unref(dev); + + udev_unref(ctx); + + return result; +} + +/** + * alsatimer_get_sysname: + * @sysname: (out): The string for sysname of ALSA Timer. + * @error: A #GError. + * + * Allocate sysname for ALSA Timer and return it when it exists. + */ +void alsatimer_get_sysname(char **sysname, GError **error) +{ + char *name; + + g_return_if_fail(sysname != NULL); + + name = strdup(TIMER_SYSNAME_TEMPLATE); + if (name == NULL) { + generate_error(error, ENOMEM); + return; + } + + if (!check_existence(name, error)) { + g_free(name); + return; + } + + *sysname = name; +} diff --git a/src/timer/query.h b/src/timer/query.h index ec3d941..bc2d12a 100644 --- a/src/timer/query.h +++ b/src/timer/query.h @@ -7,6 +7,8 @@ G_BEGIN_DECLS +void alsatimer_get_sysname(char **sysname, GError **error); + G_END_DECLS #endif -- 2.47.3