Init.cxx 2.19 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "config/Data.hxx"
25
#include "config/Option.hxx"
26
#include "config/Block.hxx"
27
#include "Log.hxx"
28
#include "PluginUnavailable.hxx"
29 30 31
#include "util/RuntimeError.hxx"

#include <stdexcept>
32

33
#include <assert.h>
34

35
void
36
input_stream_global_init(const ConfigData &config, EventLoop &event_loop)
37
{
38
	const ConfigBlock empty;
39

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

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

47
		const auto *block =
48 49
			config.FindBlock(ConfigBlockOption::INPUT, "plugin",
					 plugin->name);
50 51 52
		if (block == nullptr) {
			block = &empty;
		} else if (!block->GetBlockValue("enabled", true))
53 54 55
			/* the plugin is disabled in mpd.conf */
			continue;

56 57
		block->SetUsed();

58
		try {
59
			if (plugin->init != nullptr)
60
				plugin->init(event_loop, *block);
61
			input_plugins_enabled[i] = true;
62 63 64 65 66
		} catch (const PluginUnavailable &e) {
			FormatError(e,
				    "Input plugin '%s' is unavailable",
				    plugin->name);
			continue;
67
		} catch (...) {
68 69 70
			std::throw_with_nested(FormatRuntimeError("Failed to initialize input plugin '%s'",
								  plugin->name));
		}
71 72 73
	}
}

74 75
void
input_stream_global_finish() noexcept
76
{
77
	input_plugins_for_each_enabled(plugin)
78
		if (plugin->finish != nullptr)
79
			plugin->finish();
80
}