Init.cxx 2.32 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
27
#include "config/Block.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
	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 46
		const auto *block =
			config_find_block(ConfigBlockOption::INPUT, "plugin",
47
					  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
		InputPlugin::InitResult result = plugin->init != nullptr
55
			? plugin->init(*block, error)
56 57 58 59
			: InputPlugin::InitResult::SUCCESS;

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

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

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

			break;
77
		}
78
	}
79 80

	return true;
81 82 83 84
}

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