Init.cxx 2.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
#include "util/RuntimeError.hxx"

30
#include <cassert>
31
#include <stdexcept>
32

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

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

54 55
		block->SetUsed();

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

77 78
void
input_stream_global_finish() noexcept
79
{
80
	input_plugins_for_each_enabled(plugin)
81
		if (plugin->finish != nullptr)
82
			plugin->finish();
83
}