output_init: return GError on error

Do error handling with GError instead of aborting with g_error().
This commit is contained in:
Max Kellermann 2009-03-01 13:31:56 +01:00
parent cb942eeb45
commit f298fcf3a6
3 changed files with 28 additions and 32 deletions

View File

@ -77,6 +77,7 @@ audio_output_all_init(void)
{ {
const struct config_param *param = NULL; const struct config_param *param = NULL;
unsigned int i; unsigned int i;
GError *error = NULL;
notify_init(&audio_output_client_notify); notify_init(&audio_output_client_notify);
@ -93,18 +94,8 @@ audio_output_all_init(void)
/* only allow param to be NULL if there just one audioOutput */ /* only allow param to be NULL if there just one audioOutput */
assert(param || (num_audio_outputs == 1)); assert(param || (num_audio_outputs == 1));
if (!audio_output_init(output, param)) { if (!audio_output_init(output, param, &error))
if (param) g_error("line %i: %s", param->line, error->message);
{
g_error("problems configuring output device "
"defined at line %i\n", param->line);
}
else
{
g_error("No audio_output specified and unable to "
"detect a default audio output device\n");
}
}
/* require output names to be unique: */ /* require output names to be unique: */
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {

View File

@ -19,6 +19,8 @@
#ifndef MPD_OUTPUT_CONTROL_H #ifndef MPD_OUTPUT_CONTROL_H
#define MPD_OUTPUT_CONTROL_H #define MPD_OUTPUT_CONTROL_H
#include <glib.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
@ -27,8 +29,15 @@ struct audio_format;
struct tag; struct tag;
struct config_param; struct config_param;
static inline GQuark
audio_output_quark(void)
{
return g_quark_from_static_string("audio_output");
}
bool bool
audio_output_init(struct audio_output *ao, const struct config_param *param); audio_output_init(struct audio_output *ao, const struct config_param *param,
GError **error);
bool bool
audio_output_open(struct audio_output *ao, audio_output_open(struct audio_output *ao,

View File

@ -42,7 +42,7 @@
} }
static const struct audio_output_plugin * static const struct audio_output_plugin *
audio_output_detect(void) audio_output_detect(GError **error)
{ {
const struct audio_output_plugin *plugin; const struct audio_output_plugin *plugin;
unsigned i; unsigned i;
@ -59,17 +59,19 @@ audio_output_detect(void)
return plugin; return plugin;
} }
g_set_error(error, audio_output_quark(), 0,
"Unable to detect an audio device");
return NULL; return NULL;
} }
bool bool
audio_output_init(struct audio_output *ao, const struct config_param *param) audio_output_init(struct audio_output *ao, const struct config_param *param,
GError **error)
{ {
const char *name = NULL; const char *name = NULL;
char *format = NULL; char *format = NULL;
struct block_param *bp = NULL; struct block_param *bp = NULL;
const struct audio_output_plugin *plugin = NULL; const struct audio_output_plugin *plugin = NULL;
GError *error = NULL;
if (param) { if (param) {
const char *type = NULL; const char *type = NULL;
@ -80,18 +82,18 @@ audio_output_init(struct audio_output *ao, const struct config_param *param)
plugin = audio_output_plugin_get(type); plugin = audio_output_plugin_get(type);
if (plugin == NULL) { if (plugin == NULL) {
g_error("couldn't find audio output plugin for type " g_set_error(error, audio_output_quark(), 0,
"\"%s\" at line %i\n", type, param->line); "No such audio output plugin: %s",
type);
return false;
} }
} else { } else {
g_warning("No \"%s\" defined in config file\n", g_warning("No \"%s\" defined in config file\n",
CONF_AUDIO_OUTPUT); CONF_AUDIO_OUTPUT);
plugin = audio_output_detect(); plugin = audio_output_detect(error);
if (plugin == NULL) { if (plugin == NULL)
g_warning("Unable to detect an audio device");
return false; return false;
}
g_message("Successfully detected a %s audio device", g_message("Successfully detected a %s audio device",
plugin->name); plugin->name);
@ -111,10 +113,9 @@ audio_output_init(struct audio_output *ao, const struct config_param *param)
bool ret; bool ret;
ret = audio_format_parse(&ao->config_audio_format, format, ret = audio_format_parse(&ao->config_audio_format, format,
&error); error);
if (!ret) if (!ret)
g_error("error parsing format at line %i: %s", return false;
bp->line, error->message);
} else } else
audio_format_clear(&ao->config_audio_format); audio_format_clear(&ao->config_audio_format);
@ -124,14 +125,9 @@ audio_output_init(struct audio_output *ao, const struct config_param *param)
ao->data = ao_plugin_init(plugin, ao->data = ao_plugin_init(plugin,
format ? &ao->config_audio_format : NULL, format ? &ao->config_audio_format : NULL,
param, &error); param, error);
if (ao->data == NULL) { if (ao->data == NULL)
g_warning("Failed to initialize \"%s\" [%s]: %s",
ao->name, ao->plugin->name,
error->message);
g_error_free(error);
return false; return false;
}
return true; return true;
} }