From ef26b219575d7a13cfc97f926806ae22741aed4c Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Fri, 3 Jun 2022 12:36:48 +0900 Subject: [PATCH] hwdep: add error domain for implementators of DeviceCommon interface The implementator of DeviceCommon interface should operate character device for ALSA HwDep interface, thus it can receive any type of error as a result of system call. Some of the error may be common. This commit adds error domain for the case. Signed-off-by: Takashi Sakamoto --- src/hwdep/alsahwdep-enum-types.h | 22 +++++++++++++- src/hwdep/alsahwdep.map | 4 +++ src/hwdep/device-common.c | 50 ++++++++++++++++++++++++++++++++ src/hwdep/device-common.h | 4 +++ tests/alsahwdep-enums | 10 +++++++ 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/hwdep/alsahwdep-enum-types.h b/src/hwdep/alsahwdep-enum-types.h index a8c84d0..a58e134 100644 --- a/src/hwdep/alsahwdep-enum-types.h +++ b/src/hwdep/alsahwdep-enum-types.h @@ -7,7 +7,7 @@ G_BEGIN_DECLS /** - *ALSAHwdepIfaceType: + * ALSAHwdepIfaceType: * @ALSAHWDEP_IFACE_TYPE_OPL2: For OPL2 sound chip. * @ALSAHWDEP_IFACE_TYPE_OPL3: For OPL3 sound chip. * @ALSAHWDEP_IFACE_TYPE_OPL4: For OPL4 sound chip. @@ -68,6 +68,26 @@ typedef enum { ALSAHWDEP_IFACE_TYPE_FW_FIREFACE, } ALSAHwdepIfaceType; +/** + * ALSAHwdepDeviceCommonError: + * @ALSAHWDEP_DEVICE_COMMON_ERROR_FAILED: The operation failed due to unspecified reason. + * @ALSAHWDEP_DEVICE_COMMON_ERROR_IS_OPENED: The instance is already associated to character device. + * @ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_OPENED: The instance is not associated to character device yet. + * @ALSAHWDEP_DEVICE_COMMON_ERROR_IS_USED: The character device is already used. + * @ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_SUPPORTED: The HwDep device associated to the character device is not supported. + * @ALSAHWDEP_DEVICE_COMMON_ERROR_IS_DISCONNECTED: The sound card is under disconnected state. + * + * A set of enumerations for code of ALSAHwDep.DeviceCommonError error domain. + */ +typedef enum { + ALSAHWDEP_DEVICE_COMMON_ERROR_FAILED = 1, + ALSAHWDEP_DEVICE_COMMON_ERROR_IS_OPENED, + ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_OPENED, + ALSAHWDEP_DEVICE_COMMON_ERROR_IS_USED, + ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_SUPPORTED, + ALSAHWDEP_DEVICE_COMMON_ERROR_IS_DISCONNECTED, +} ALSAHwdepDeviceCommonError; + G_END_DECLS #endif diff --git a/src/hwdep/alsahwdep.map b/src/hwdep/alsahwdep.map index a37cca4..ea361f1 100644 --- a/src/hwdep/alsahwdep.map +++ b/src/hwdep/alsahwdep.map @@ -19,4 +19,8 @@ ALSA_GOBJECT_0_3_0 { "alsahwdep_device_common_get_protocol_version"; "alsahwdep_device_common_get_device_info"; "alsahwdep_device_common_create_source"; + + "alsahwdep_device_common_error_quark"; + "alsahwdep_device_common_error_get_type"; + "alsahwdep_device_common_error_to_label"; } ALSA_GOBJECT_0_0_0; diff --git a/src/hwdep/device-common.c b/src/hwdep/device-common.c index d6f9f75..a20ea40 100644 --- a/src/hwdep/device-common.c +++ b/src/hwdep/device-common.c @@ -14,6 +14,56 @@ static void alsahwdep_device_common_default_init(ALSAHwdepDeviceCommonInterface G_DEFINE_INTERFACE(ALSAHwdepDeviceCommon, alsahwdep_device_common, G_TYPE_OBJECT) +/** + * alsahwdep_device_common_error_quark: + * + * Return the [alias@GLib.Quark] for [struct@GLib.Error] with code in ALSAHwdep.DeviceCommonError + * enumerations. + * + * Returns: A [alias@GLib.Quark]. + */ +G_DEFINE_QUARK(alsahwdep-device-common-error-quark, alsahwdep_device_common_error) + +/** + * alsahwdep_device_common_error_to_label: + * @code: A ALSAHwdep.DeviceCommonError. + * @label: (out) (transfer none): The string label of error. + * + * Retrieve the string label of error from code. + */ +void alsahwdep_device_common_error_to_label(ALSAHwdepDeviceCommonError code, const char **label) +{ + static const char *const labels[] = { + [ALSAHWDEP_DEVICE_COMMON_ERROR_FAILED] = + "The operation failed due to unspecified reason.", + [ALSAHWDEP_DEVICE_COMMON_ERROR_IS_OPENED] = + "The instance is already associated to character device.", + [ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_OPENED] = + "The instance is not associated to character device yet.", + [ALSAHWDEP_DEVICE_COMMON_ERROR_IS_USED] = + "The character device is already used.", + [ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_SUPPORTED] = + "The HwDep device associated to the character device is not supported.", + [ALSAHWDEP_DEVICE_COMMON_ERROR_IS_DISCONNECTED] = + "The sound card is under disconnected state.", + }; + + switch (code) { + case ALSAHWDEP_DEVICE_COMMON_ERROR_FAILED: + case ALSAHWDEP_DEVICE_COMMON_ERROR_IS_OPENED: + case ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_OPENED: + case ALSAHWDEP_DEVICE_COMMON_ERROR_IS_USED: + case ALSAHWDEP_DEVICE_COMMON_ERROR_IS_NOT_SUPPORTED: + case ALSAHWDEP_DEVICE_COMMON_ERROR_IS_DISCONNECTED: + break; + default: + code = ALSAHWDEP_DEVICE_COMMON_ERROR_FAILED; + break; + } + + *label = labels[code]; +} + static void alsahwdep_device_common_default_init(ALSAHwdepDeviceCommonInterface *iface) { /** diff --git a/src/hwdep/device-common.h b/src/hwdep/device-common.h index 247f419..39382db 100644 --- a/src/hwdep/device-common.h +++ b/src/hwdep/device-common.h @@ -10,6 +10,10 @@ G_BEGIN_DECLS G_DECLARE_INTERFACE(ALSAHwdepDeviceCommon, alsahwdep_device_common, ALSAHWDEP, DEVICE_COMMON, GObject) +GQuark alsahwdep_device_common_error_quark(); + +void alsahwdep_device_common_error_to_label(ALSAHwdepDeviceCommonError code, const char **label); + struct _ALSAHwdepDeviceCommonInterface { GTypeInterface parent_iface; diff --git a/tests/alsahwdep-enums b/tests/alsahwdep-enums index d6b0747..5ad287b 100644 --- a/tests/alsahwdep-enums +++ b/tests/alsahwdep-enums @@ -35,8 +35,18 @@ iface_types = ( 'FW_FIREFACE', ) +device_common_error_types = ( + 'FAILED', + 'IS_OPENED', + 'IS_NOT_OPENED', + 'IS_USED', + 'IS_NOT_SUPPORTED', + 'IS_DISCONNECTED', +) + types = { ALSAHwdep.IfaceType: iface_types, + ALSAHwdep.DeviceCommonError: device_common_error_types, } for obj, types in types.items(): -- 2.47.3