Init.cxx 2.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "Init.hxx"
#include "Registry.hxx"
23
#include "InputPlugin.hxx"
24
#include "util/Error.hxx"
25 26 27
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "config/ConfigData.hxx"
28
#include "Log.hxx"
29

30
#include <assert.h>
31 32
#include <string.h>

33
bool
34
input_stream_global_init(Error &error)
35
{
36 37
	const config_param empty;

38
	for (unsigned i = 0; input_plugins[i] != nullptr; ++i) {
39
		const InputPlugin *plugin = input_plugins[i];
40

41
		assert(plugin->name != nullptr);
42
		assert(*plugin->name != 0);
43
		assert(plugin->open != nullptr);
44

45
		const struct config_param *param =
46
			config_find_block(CONF_INPUT, "plugin", plugin->name);
47 48 49
		if (param == nullptr) {
			param = &empty;
		} else if (!param->GetBlockValue("enabled", true))
50 51 52
			/* the plugin is disabled in mpd.conf */
			continue;

53 54 55 56 57 58
		InputPlugin::InitResult result = plugin->init != nullptr
			? plugin->init(*param, error)
			: InputPlugin::InitResult::SUCCESS;

		switch (result) {
		case InputPlugin::InitResult::SUCCESS:
59
			input_plugins_enabled[i] = true;
60 61 62
			break;

		case InputPlugin::InitResult::ERROR:
63 64
			error.FormatPrefix("Failed to initialize input plugin '%s': ",
					   plugin->name);
65
			return false;
66 67 68 69

		case InputPlugin::InitResult::UNAVAILABLE:
			if (error.IsDefined()) {
				FormatError(error,
70
					    "Input plugin '%s' is unavailable",
71 72 73 74 75
					    plugin->name);
				error.Clear();
			}

			break;
76
		}
77
	}
78 79

	return true;
80 81 82 83
}

void input_stream_global_finish(void)
{
84
	input_plugins_for_each_enabled(plugin)
85
		if (plugin->finish != nullptr)
86
			plugin->finish();
87
}