Init.cxx 2.29 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
bool
33
input_stream_global_init(Error &error)
34
{
35
	const ConfigBlock empty;
36

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

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

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

53
		InputPlugin::InitResult result = plugin->init != nullptr
54
			? plugin->init(*block, error)
55 56 57 58
			: InputPlugin::InitResult::SUCCESS;

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

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

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

			break;
76
		}
77
	}
78 79

	return true;
80 81 82 83
}

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