Init.cxx 2.17 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
 * 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#include "Init.hxx"
#include "Registry.hxx"
22
#include "InputPlugin.hxx"
23
#include "config/Data.hxx"
24
#include "config/Option.hxx"
25
#include "config/Block.hxx"
26
#include "Log.hxx"
27
#include "PluginUnavailable.hxx"
28 29 30
#include "util/RuntimeError.hxx"

#include <stdexcept>
31

32
#include <assert.h>
33

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

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

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

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

55 56
		block->SetUsed();

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

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