Init.cxx 2.16 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 25
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.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(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 48
		const auto *block =
			config_find_block(ConfigBlockOption::INPUT, "plugin",
49
					  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
		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 PluginUnavailable &e) {
			FormatError(e,
				    "Input plugin '%s' is unavailable",
				    plugin->name);
			continue;
65
		} catch (...) {
66 67 68
			std::throw_with_nested(FormatRuntimeError("Failed to initialize input plugin '%s'",
								  plugin->name));
		}
69 70 71
	}
}

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