2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2013-04-17 22:58:33 +02:00
|
|
|
#include "Instance.hxx"
|
2013-01-03 17:31:38 +01:00
|
|
|
#include "CommandLine.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "PlaylistFile.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicChunk.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "StateFile.hxx"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "player/Thread.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
|
|
|
#include "client/ClientList.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/AllCommands.hxx"
|
2013-01-04 22:42:05 +01:00
|
|
|
#include "Partition.hxx"
|
2013-09-05 18:23:15 +02:00
|
|
|
#include "tag/TagConfig.hxx"
|
2016-11-25 11:13:08 +01:00
|
|
|
#include "ReplayGainGlobal.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-09 22:33:06 +01:00
|
|
|
#include "Log.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "LogInit.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/Init.hxx"
|
2013-01-10 19:13:00 +01:00
|
|
|
#include "event/Loop.hxx"
|
2013-01-10 10:33:20 +01:00
|
|
|
#include "IOThread.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-10-17 22:13:54 +02:00
|
|
|
#include "fs/Config.hxx"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "playlist/PlaylistRegistry.hxx"
|
2014-01-24 00:14:54 +01:00
|
|
|
#include "zeroconf/ZeroconfGlue.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "decoder/DecoderList.hxx"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioConfig.hxx"
|
2013-11-29 10:06:20 +01:00
|
|
|
#include "pcm/PcmConvert.hxx"
|
2014-01-24 16:12:10 +01:00
|
|
|
#include "unix/SignalHandlers.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2014-01-30 18:37:30 +01:00
|
|
|
#include "thread/Slack.hxx"
|
2014-06-10 22:38:00 +02:00
|
|
|
#include "lib/icu/Init.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigDefaults.hxx"
|
|
|
|
#include "config/ConfigOption.hxx"
|
2014-02-08 00:04:29 +01:00
|
|
|
#include "config/ConfigError.hxx"
|
2013-10-02 12:14:07 +02:00
|
|
|
#include "Stats.hxx"
|
2016-12-03 13:56:25 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
|
|
|
#include "unix/Daemon.hxx"
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
#include "db/update/Service.hxx"
|
2014-02-17 21:47:15 +01:00
|
|
|
#include "db/Configured.hxx"
|
2014-02-17 20:57:56 +01:00
|
|
|
#include "db/DatabasePlugin.hxx"
|
2014-02-26 09:17:41 +01:00
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
2014-02-09 07:21:25 +01:00
|
|
|
#include "storage/Configured.hxx"
|
2014-02-09 07:44:07 +01:00
|
|
|
#include "storage/CompositeStorage.hxx"
|
2014-02-18 21:05:04 +01:00
|
|
|
#ifdef ENABLE_INOTIFY
|
|
|
|
#include "db/update/InotifyUpdate.hxx"
|
|
|
|
#endif
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
|
|
|
#include "neighbor/Glue.hxx"
|
|
|
|
#endif
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
2014-01-24 16:15:41 +01:00
|
|
|
#include "sticker/StickerDatabase.hxx"
|
2009-01-19 18:51:57 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2014-01-24 00:09:37 +01:00
|
|
|
#include "archive/ArchiveList.hxx"
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2012-09-28 00:40:00 +02:00
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#ifdef ANDROID
|
2014-03-01 09:19:32 +01:00
|
|
|
#include "java/Global.hxx"
|
|
|
|
#include "java/File.hxx"
|
2014-03-01 17:46:59 +01:00
|
|
|
#include "android/Environment.hxx"
|
2014-03-01 20:20:29 +01:00
|
|
|
#include "android/Context.hxx"
|
2014-03-02 00:35:37 +01:00
|
|
|
#include "fs/StandardDirectory.hxx"
|
2014-07-30 10:22:22 +02:00
|
|
|
#include "fs/FileSystem.hxx"
|
2014-02-18 08:33:06 +01:00
|
|
|
#include "org_musicpd_Bridge.h"
|
|
|
|
#endif
|
|
|
|
|
2015-03-21 14:18:15 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
#include <systemd/sd-daemon.h>
|
|
|
|
#endif
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:43:00 +01:00
|
|
|
|
2009-01-05 09:31:09 +01:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2008-11-05 18:37:39 +01:00
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-27 18:02:31 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2014-11-11 04:38:22 +01:00
|
|
|
#ifdef __BLOCKS__
|
2014-10-13 00:29:43 +02:00
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-15 12:32:15 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
2013-10-30 21:46:34 +01:00
|
|
|
static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096;
|
2013-10-20 13:46:32 +02:00
|
|
|
static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
|
2009-01-18 17:32:43 +01:00
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
Context *context;
|
|
|
|
#endif
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
Instance *instance;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2014-02-21 23:12:51 +01:00
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
|
|
|
glue_daemonize_init(const struct options *options)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2015-01-21 22:36:13 +01:00
|
|
|
daemonize_init(config_get_string(ConfigOption::USER, nullptr),
|
|
|
|
config_get_string(ConfigOption::GROUP, nullptr),
|
2016-11-02 10:01:29 +01:00
|
|
|
config_get_path(ConfigOption::PID_FILE));
|
2009-07-15 18:58:12 +02:00
|
|
|
|
|
|
|
if (options->kill)
|
|
|
|
daemonize_kill();
|
|
|
|
}
|
|
|
|
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
|
|
|
glue_mapper_init()
|
2009-07-15 18:58:19 +02:00
|
|
|
{
|
2016-11-02 10:01:29 +01:00
|
|
|
mapper_init(config_get_path(ConfigOption::PLAYLIST_DIR));
|
2014-02-07 23:37:39 +01:00
|
|
|
}
|
2014-02-07 23:13:24 +01:00
|
|
|
|
2014-02-07 22:54:18 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
static void
|
|
|
|
InitStorage()
|
2014-02-07 23:37:39 +01:00
|
|
|
{
|
2016-10-27 08:40:40 +02:00
|
|
|
Storage *storage = CreateConfiguredStorage(io_thread_get());
|
2014-02-09 07:44:07 +01:00
|
|
|
if (storage == nullptr)
|
2016-10-27 08:40:40 +02:00
|
|
|
return;
|
2014-02-09 07:44:07 +01:00
|
|
|
|
|
|
|
CompositeStorage *composite = new CompositeStorage();
|
|
|
|
instance->storage = composite;
|
|
|
|
composite->Mount("", storage);
|
2009-07-15 18:58:19 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 16:57:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the database. If this function returns false, this has not
|
|
|
|
* succeeded, and the caller should create the database after the
|
|
|
|
* process has been daemonized.
|
|
|
|
*/
|
|
|
|
static bool
|
2009-09-20 23:31:35 +02:00
|
|
|
glue_db_init_and_load(void)
|
2006-07-19 20:04:15 +02:00
|
|
|
{
|
2014-02-17 21:47:15 +01:00
|
|
|
instance->database =
|
2016-10-28 23:00:10 +02:00
|
|
|
CreateConfiguredDatabase(instance->event_loop, *instance);
|
|
|
|
if (instance->database == nullptr)
|
|
|
|
return true;
|
2012-08-08 08:36:14 +02:00
|
|
|
|
2014-02-17 20:57:56 +01:00
|
|
|
if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
|
2016-10-27 08:40:40 +02:00
|
|
|
InitStorage();
|
2014-02-17 22:12:33 +01:00
|
|
|
|
2014-02-17 20:57:56 +01:00
|
|
|
if (instance->storage == nullptr) {
|
|
|
|
delete instance->database;
|
|
|
|
instance->database = nullptr;
|
|
|
|
LogDefault(config_domain,
|
|
|
|
"Found database setting without "
|
|
|
|
"music_directory - disabling database");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IsStorageConfigured())
|
|
|
|
LogDefault(config_domain,
|
|
|
|
"Ignoring the storage configuration "
|
|
|
|
"because the database does not need it");
|
2014-02-17 22:12:33 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 22:17:46 +01:00
|
|
|
instance->database->Open();
|
2009-02-02 16:57:24 +01:00
|
|
|
|
2014-02-19 23:17:21 +01:00
|
|
|
if (!instance->database->IsPlugin(simple_db_plugin))
|
2014-02-04 10:09:09 +01:00
|
|
|
return true;
|
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
SimpleDatabase &db = *(SimpleDatabase *)instance->database;
|
2016-03-05 20:17:47 +01:00
|
|
|
instance->update = new UpdateService(instance->event_loop, db,
|
2014-02-26 08:39:44 +01:00
|
|
|
static_cast<CompositeStorage &>(*instance->storage),
|
2014-02-04 23:58:03 +01:00
|
|
|
*instance);
|
2014-02-04 10:09:09 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
/* run database update after daemonization? */
|
2014-02-01 00:26:34 +01:00
|
|
|
return db.FileExists();
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2014-02-17 21:42:58 +01:00
|
|
|
static bool
|
|
|
|
InitDatabaseAndStorage()
|
|
|
|
{
|
|
|
|
const bool create_db = !glue_db_init_and_load();
|
|
|
|
return create_db;
|
|
|
|
}
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2009-07-15 18:58:12 +02:00
|
|
|
/**
|
|
|
|
* Configure and initialize the sticker subsystem.
|
|
|
|
*/
|
|
|
|
static void
|
2016-11-02 10:01:29 +01:00
|
|
|
glue_sticker_init()
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_SQLITE
|
2016-11-02 10:01:29 +01:00
|
|
|
auto sticker_file = config_get_path(ConfigOption::STICKER_FILE);
|
|
|
|
if (sticker_file.IsNull())
|
2013-10-17 21:59:35 +02:00
|
|
|
return;
|
2009-07-15 18:58:12 +02:00
|
|
|
|
2016-10-27 07:50:08 +02:00
|
|
|
sticker_global_init(std::move(sticker_file));
|
2009-07-15 18:58:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
static void
|
|
|
|
glue_state_file_init()
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2016-11-02 10:01:29 +01:00
|
|
|
auto path_fs = config_get_path(ConfigOption::STATE_FILE);
|
2014-03-02 00:35:37 +01:00
|
|
|
if (path_fs.IsNull()) {
|
|
|
|
#ifdef ANDROID
|
|
|
|
const auto cache_dir = GetUserCacheDir();
|
|
|
|
if (cache_dir.IsNull())
|
2016-11-02 10:01:29 +01:00
|
|
|
return;
|
2014-03-02 00:35:37 +01:00
|
|
|
|
|
|
|
path_fs = AllocatedPath::Build(cache_dir, "state");
|
|
|
|
#else
|
2016-11-02 10:01:29 +01:00
|
|
|
return;
|
2014-03-02 00:35:37 +01:00
|
|
|
#endif
|
|
|
|
}
|
2013-01-27 08:26:17 +01:00
|
|
|
|
2015-01-21 22:36:13 +01:00
|
|
|
const unsigned interval =
|
|
|
|
config_get_unsigned(ConfigOption::STATE_FILE_INTERVAL,
|
|
|
|
StateFile::DEFAULT_INTERVAL);
|
2014-08-24 12:59:45 +02:00
|
|
|
|
2016-03-05 21:00:38 +01:00
|
|
|
instance->state_file = new StateFile(std::move(path_fs), interval,
|
|
|
|
*instance->partition,
|
|
|
|
instance->event_loop);
|
|
|
|
instance->state_file->Read();
|
2009-07-15 18:58:12 +02:00
|
|
|
}
|
|
|
|
|
2009-03-27 18:02:31 +01:00
|
|
|
/**
|
|
|
|
* Windows-only initialization of the Winsock2 library.
|
|
|
|
*/
|
|
|
|
static void winsock_init(void)
|
|
|
|
{
|
2009-10-22 17:12:28 +02:00
|
|
|
#ifdef WIN32
|
2009-03-27 18:02:31 +01:00
|
|
|
WSADATA sockinfo;
|
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
int retval = WSAStartup(MAKEWORD(2, 2), &sockinfo);
|
2009-03-27 18:02:31 +01:00
|
|
|
if(retval != 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FormatFatalError("Attempt to open Winsock2 failed; error code %d",
|
|
|
|
retval);
|
2009-03-27 18:02:31 +01:00
|
|
|
|
|
|
|
if (LOBYTE(sockinfo.wVersion) != 2)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError("We use Winsock2 but your version is either too new "
|
|
|
|
"or old; please install Winsock 2.x");
|
2009-03-27 18:02:31 +01:00
|
|
|
#endif
|
2009-10-22 17:12:28 +02:00
|
|
|
}
|
2009-03-27 18:02:31 +01:00
|
|
|
|
2009-01-18 17:32:43 +01:00
|
|
|
/**
|
|
|
|
* Initialize the decoder and player core, including the music pipe.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
initialize_decoder_and_player(void)
|
|
|
|
{
|
2016-10-28 11:38:37 +02:00
|
|
|
const ConfigParam *param;
|
2009-01-18 17:32:43 +01:00
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
size_t buffer_size;
|
2015-01-21 22:36:13 +01:00
|
|
|
param = config_get_param(ConfigOption::AUDIO_BUFFER_SIZE);
|
2013-09-26 17:31:50 +02:00
|
|
|
if (param != nullptr) {
|
2014-01-18 11:34:13 +01:00
|
|
|
char *test;
|
2013-10-15 22:32:39 +02:00
|
|
|
long tmp = strtol(param->value.c_str(), &test, 10);
|
2012-02-15 20:35:06 +01:00
|
|
|
if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
|
2013-08-07 09:35:30 +02:00
|
|
|
FormatFatalError("buffer size \"%s\" is not a "
|
|
|
|
"positive integer, line %i",
|
2013-10-15 22:32:39 +02:00
|
|
|
param->value.c_str(), param->line);
|
2012-02-15 20:35:06 +01:00
|
|
|
buffer_size = tmp;
|
2009-01-18 17:32:43 +01:00
|
|
|
} else
|
|
|
|
buffer_size = DEFAULT_BUFFER_SIZE;
|
|
|
|
|
|
|
|
buffer_size *= 1024;
|
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
|
2009-01-18 17:32:43 +01:00
|
|
|
|
|
|
|
if (buffered_chunks >= 1 << 15)
|
2013-08-07 09:35:30 +02:00
|
|
|
FormatFatalError("buffer size \"%lu\" is too big",
|
|
|
|
(unsigned long)buffer_size);
|
2009-01-18 17:32:43 +01:00
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
float perc;
|
2015-01-21 22:36:13 +01:00
|
|
|
param = config_get_param(ConfigOption::BUFFER_BEFORE_PLAY);
|
2013-09-26 17:31:50 +02:00
|
|
|
if (param != nullptr) {
|
2014-01-18 11:34:13 +01:00
|
|
|
char *test;
|
2013-10-15 22:32:39 +02:00
|
|
|
perc = strtod(param->value.c_str(), &test);
|
2009-01-18 17:32:43 +01:00
|
|
|
if (*test != '%' || perc < 0 || perc > 100) {
|
2013-08-07 09:35:30 +02:00
|
|
|
FormatFatalError("buffered before play \"%s\" is not "
|
|
|
|
"a positive percentage and less "
|
|
|
|
"than 100 percent, line %i",
|
2013-10-15 22:32:39 +02:00
|
|
|
param->value.c_str(), param->line);
|
2009-01-18 17:32:43 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
perc = DEFAULT_BUFFER_BEFORE_PLAY;
|
|
|
|
|
2014-01-18 11:34:13 +01:00
|
|
|
unsigned buffered_before_play = (perc / 100) * buffered_chunks;
|
2009-01-18 17:32:43 +01:00
|
|
|
if (buffered_before_play > buffered_chunks)
|
|
|
|
buffered_before_play = buffered_chunks;
|
|
|
|
|
2013-01-04 22:42:05 +01:00
|
|
|
const unsigned max_length =
|
2015-01-21 22:36:13 +01:00
|
|
|
config_get_positive(ConfigOption::MAX_PLAYLIST_LENGTH,
|
2013-01-04 22:42:05 +01:00
|
|
|
DEFAULT_PLAYLIST_MAX_LENGTH);
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->partition = new Partition(*instance,
|
|
|
|
max_length,
|
|
|
|
buffered_chunks,
|
2016-12-03 13:14:06 +01:00
|
|
|
buffered_before_play,
|
|
|
|
replay_gain_config);
|
2016-12-03 13:56:25 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
param = config_get_param(ConfigOption::REPLAYGAIN);
|
|
|
|
if (param != nullptr)
|
|
|
|
instance->partition->replay_gain_mode =
|
|
|
|
FromString(param->value.c_str());
|
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
|
|
|
|
param->line));
|
|
|
|
}
|
2009-01-18 17:32:43 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:27:07 +01:00
|
|
|
void
|
|
|
|
Instance::OnIdle(unsigned flags)
|
2008-12-30 19:24:39 +01:00
|
|
|
{
|
2013-01-16 23:23:30 +01:00
|
|
|
/* send "idle" notifications to all subscribed
|
2008-12-30 19:24:39 +01:00
|
|
|
clients */
|
2016-03-10 09:27:07 +01:00
|
|
|
client_list->IdleAdd(flags);
|
2013-04-08 22:31:51 +02:00
|
|
|
|
2013-07-30 08:30:56 +02:00
|
|
|
if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
|
2016-03-10 09:27:07 +01:00
|
|
|
state_file != nullptr)
|
|
|
|
state_file->CheckModified();
|
2008-12-30 19:24:39 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#ifndef ANDROID
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
int main(int argc, char *argv[])
|
2010-09-21 03:28:08 +02:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return win32_main(argc, argv);
|
|
|
|
#else
|
|
|
|
return mpd_main(argc, argv);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#endif
|
|
|
|
|
2014-10-13 00:29:43 +02:00
|
|
|
static int mpd_main_after_fork(struct options);
|
|
|
|
|
2014-03-01 19:09:31 +01:00
|
|
|
#ifdef ANDROID
|
|
|
|
static inline
|
|
|
|
#endif
|
2010-09-21 03:28:08 +02:00
|
|
|
int mpd_main(int argc, char *argv[])
|
2016-10-28 21:20:35 +02:00
|
|
|
try {
|
2009-07-15 18:22:49 +02:00
|
|
|
struct options options;
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2008-12-30 16:28:13 +01:00
|
|
|
daemonize_close_stdin();
|
2014-11-20 20:38:37 +01:00
|
|
|
#endif
|
2008-12-30 16:28:13 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifndef ANDROID
|
2009-01-05 09:31:09 +01:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2008-11-05 18:37:39 +01:00
|
|
|
/* initialize locale */
|
|
|
|
setlocale(LC_CTYPE,"");
|
2015-06-23 13:26:45 +02:00
|
|
|
setlocale(LC_COLLATE, "");
|
2008-11-05 18:37:39 +01:00
|
|
|
#endif
|
2013-04-17 01:53:10 +02:00
|
|
|
#endif
|
2008-11-05 18:21:49 +01:00
|
|
|
|
2016-11-02 09:54:13 +01:00
|
|
|
IcuInit();
|
2014-02-23 19:27:08 +01:00
|
|
|
|
2009-03-27 18:02:31 +01:00
|
|
|
winsock_init();
|
2013-11-26 13:10:18 +01:00
|
|
|
io_thread_init();
|
2009-01-17 20:23:27 +01:00
|
|
|
config_global_init();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2014-02-21 23:12:51 +01:00
|
|
|
#ifdef ANDROID
|
2016-10-28 21:20:35 +02:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
|
|
|
const auto sdcard = Environment::getExternalStorageDirectory();
|
|
|
|
if (!sdcard.IsNull()) {
|
|
|
|
const auto config_path =
|
|
|
|
AllocatedPath::Build(sdcard, "mpd.conf");
|
|
|
|
if (FileExists(config_path))
|
|
|
|
ReadConfigFile(config_path);
|
|
|
|
}
|
2014-02-21 23:12:51 +01:00
|
|
|
#else
|
2016-11-02 10:30:46 +01:00
|
|
|
ParseCommandLine(argc, argv, &options);
|
2016-10-28 21:20:35 +02:00
|
|
|
#endif
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2016-11-02 10:01:29 +01:00
|
|
|
glue_daemonize_init(&options);
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2005-03-09 04:13:33 +01:00
|
|
|
|
2009-01-18 15:22:26 +01:00
|
|
|
stats_global_init();
|
2013-09-05 18:23:15 +02:00
|
|
|
TagLoadConfig();
|
2016-12-03 13:27:59 +01:00
|
|
|
replay_gain_global_init();
|
2011-09-09 21:40:28 +02:00
|
|
|
|
2016-11-02 09:59:08 +01:00
|
|
|
log_init(options.verbose, options.log_stderr);
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
instance = new Instance();
|
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
|
|
|
instance->neighbors = new NeighborGlue();
|
2016-09-05 10:53:54 +02:00
|
|
|
instance->neighbors->Init(io_thread_get(), *instance);
|
2014-01-18 16:36:42 +01:00
|
|
|
|
|
|
|
if (instance->neighbors->IsEmpty()) {
|
|
|
|
delete instance->neighbors;
|
|
|
|
instance->neighbors = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 22:36:13 +01:00
|
|
|
const unsigned max_clients =
|
|
|
|
config_get_positive(ConfigOption::MAX_CONN, 10);
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->client_list = new ClientList(max_clients);
|
2013-01-16 22:55:33 +01:00
|
|
|
|
2014-02-20 00:03:49 +01:00
|
|
|
initialize_decoder_and_player();
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
listen_global_init(instance->event_loop, *instance->partition);
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2009-01-18 17:15:34 +01:00
|
|
|
daemonize_set_user();
|
2014-01-18 12:01:09 +01:00
|
|
|
daemonize_begin(options.daemon);
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2014-11-11 04:38:22 +01:00
|
|
|
#ifdef __BLOCKS__
|
2014-10-13 00:29:43 +02:00
|
|
|
/* Runs the OS X native event loop in the main thread, and runs
|
|
|
|
the rest of mpd_main on a new thread. This lets CoreAudio receive
|
|
|
|
route change notifications (e.g. plugging or unplugging headphones).
|
|
|
|
All hardware output on OS X ultimately uses CoreAudio internally.
|
|
|
|
This must be run after forking; if dispatch is called before forking,
|
|
|
|
the child process will have a broken internal dispatch state. */
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
exit(mpd_main_after_fork(options));
|
|
|
|
});
|
|
|
|
dispatch_main();
|
|
|
|
return EXIT_FAILURE; // unreachable, because dispatch_main never returns
|
|
|
|
#else
|
|
|
|
return mpd_main_after_fork(options);
|
|
|
|
#endif
|
2016-10-28 21:20:35 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LogError(e);
|
|
|
|
return EXIT_FAILURE;
|
2014-10-13 00:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mpd_main_after_fork(struct options options)
|
2016-02-07 12:42:03 +01:00
|
|
|
try {
|
2016-04-12 22:18:36 +02:00
|
|
|
ConfigureFS();
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
glue_mapper_init();
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2006-08-06 08:40:11 +02:00
|
|
|
initPermissions();
|
2009-01-25 13:53:16 +01:00
|
|
|
spl_global_init();
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:34 +01:00
|
|
|
archive_plugin_init_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2011-10-08 13:14:29 +02:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
pcm_convert_global_init();
|
2011-10-08 13:14:29 +02:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
decoder_plugin_init_all();
|
2006-08-06 08:40:11 +02:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2014-02-17 21:42:58 +01:00
|
|
|
const bool create_db = InitDatabaseAndStorage();
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2014-01-29 20:16:43 +01:00
|
|
|
|
2009-07-15 18:58:12 +02:00
|
|
|
glue_sticker_init();
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
command_init();
|
2005-11-16 15:43:04 +01:00
|
|
|
initAudioConfig();
|
2016-03-05 20:17:47 +01:00
|
|
|
instance->partition->outputs.Configure(instance->event_loop,
|
2016-11-25 13:54:55 +01:00
|
|
|
replay_gain_config,
|
2014-02-05 00:02:02 +01:00
|
|
|
instance->partition->pc);
|
2008-08-28 20:03:02 +02:00
|
|
|
client_manager_init();
|
2016-09-05 12:05:54 +02:00
|
|
|
input_stream_global_init();
|
2009-10-13 18:01:11 +02:00
|
|
|
playlist_list_global_init();
|
2006-07-21 19:48:54 +02:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifdef ENABLE_DAEMON
|
2014-01-18 12:01:09 +01:00
|
|
|
daemonize_commit();
|
2014-11-20 20:38:37 +01:00
|
|
|
#endif
|
2005-11-16 15:43:04 +01:00
|
|
|
|
2014-11-20 20:38:37 +01:00
|
|
|
#ifndef ANDROID
|
2009-07-22 13:40:19 +02:00
|
|
|
setup_log_output(options.log_stderr);
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2016-03-05 20:17:47 +01:00
|
|
|
SignalHandlersInit(instance->event_loop);
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2005-11-18 13:09:05 +01:00
|
|
|
|
2013-09-03 11:28:47 +02:00
|
|
|
io_thread_start();
|
2011-08-24 02:34:28 +02:00
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
2016-09-05 10:53:54 +02:00
|
|
|
if (instance->neighbors != nullptr)
|
|
|
|
instance->neighbors->Open();
|
2014-01-18 16:36:42 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-05 20:17:47 +01:00
|
|
|
ZeroconfInit(instance->event_loop);
|
2007-06-04 19:41:18 +02:00
|
|
|
|
2014-08-16 10:33:19 +02:00
|
|
|
StartPlayerThread(instance->partition->pc);
|
2009-01-18 18:09:50 +01:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2009-02-02 16:57:24 +01:00
|
|
|
if (create_db) {
|
2009-09-20 23:31:35 +02:00
|
|
|
/* the database failed to load: recreate the
|
|
|
|
database */
|
2014-01-29 20:16:43 +01:00
|
|
|
unsigned job = instance->update->Enqueue("", true);
|
2009-02-02 16:57:24 +01:00
|
|
|
if (job == 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError("directory update failed");
|
2009-02-02 16:57:24 +01:00
|
|
|
}
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2009-02-02 16:57:24 +01:00
|
|
|
|
2016-11-02 10:01:29 +01:00
|
|
|
glue_state_file_init();
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2014-02-18 21:05:04 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2015-01-21 22:36:13 +01:00
|
|
|
if (config_get_bool(ConfigOption::AUTO_UPDATE, false)) {
|
2009-10-24 18:24:29 +02:00
|
|
|
#ifdef ENABLE_INOTIFY
|
2014-02-07 22:54:18 +01:00
|
|
|
if (instance->storage != nullptr &&
|
2014-01-29 20:16:43 +01:00
|
|
|
instance->update != nullptr)
|
2016-03-05 20:17:47 +01:00
|
|
|
mpd_inotify_init(instance->event_loop,
|
2014-02-07 22:16:37 +01:00
|
|
|
*instance->storage,
|
|
|
|
*instance->update,
|
2015-01-21 22:36:13 +01:00
|
|
|
config_get_unsigned(ConfigOption::AUTO_UPDATE_DEPTH,
|
2014-02-24 18:56:57 +01:00
|
|
|
INT_MAX));
|
2009-10-26 19:14:40 +01:00
|
|
|
#else
|
2015-09-17 23:15:48 +02:00
|
|
|
FormatWarning(config_domain,
|
2013-09-27 22:31:24 +02:00
|
|
|
"inotify: auto_update was disabled. enable during compilation phase");
|
2009-10-24 18:24:29 +02:00
|
|
|
#endif
|
2014-01-18 11:20:20 +01:00
|
|
|
}
|
2014-02-18 21:05:04 +01:00
|
|
|
#endif
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2009-06-19 09:02:14 +02:00
|
|
|
config_global_check();
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
/* enable all audio outputs (if not already done by
|
|
|
|
playlist_state_restore() */
|
2015-11-11 16:50:57 +01:00
|
|
|
instance->partition->pc.LockUpdateAudio();
|
2009-10-23 10:55:52 +02:00
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
win32_app_started();
|
|
|
|
#endif
|
2008-10-14 22:38:14 +02:00
|
|
|
|
2014-01-30 18:37:30 +01:00
|
|
|
/* the MPD frontend does not care about timer slack; set it to
|
|
|
|
a huge value to allow the kernel to reduce CPU wakeups */
|
|
|
|
SetThreadTimerSlackMS(100);
|
|
|
|
|
2015-03-21 14:18:15 +01:00
|
|
|
#ifdef ENABLE_SYSTEMD_DAEMON
|
|
|
|
sd_notify(0, "READY=1");
|
|
|
|
#endif
|
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
/* run the main loop */
|
2016-03-05 20:17:47 +01:00
|
|
|
instance->event_loop.Run();
|
2008-11-21 18:36:33 +01:00
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
win32_app_stopping();
|
|
|
|
#endif
|
|
|
|
|
2008-12-30 19:24:39 +01:00
|
|
|
/* cleanup */
|
|
|
|
|
2014-02-18 21:05:04 +01:00
|
|
|
#if defined(ENABLE_DATABASE) && defined(ENABLE_INOTIFY)
|
2009-09-25 18:32:00 +02:00
|
|
|
mpd_inotify_finish();
|
2014-02-27 16:36:11 +01:00
|
|
|
|
|
|
|
if (instance->update != nullptr)
|
|
|
|
instance->update->CancelAllAsync();
|
2009-10-24 18:24:29 +02:00
|
|
|
#endif
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2016-03-05 21:00:38 +01:00
|
|
|
if (instance->state_file != nullptr) {
|
|
|
|
instance->state_file->Write();
|
|
|
|
delete instance->state_file;
|
2013-01-14 11:00:22 +01:00
|
|
|
}
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->partition->pc.Kill();
|
2013-01-27 22:18:45 +01:00
|
|
|
ZeroconfDeinit();
|
2009-02-24 17:42:36 +01:00
|
|
|
listen_global_finish();
|
2013-04-17 22:58:33 +02:00
|
|
|
delete instance->client_list;
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
|
|
|
if (instance->neighbors != nullptr) {
|
|
|
|
instance->neighbors->Close();
|
|
|
|
delete instance->neighbors;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2014-01-29 20:16:43 +01:00
|
|
|
delete instance->update;
|
2014-02-17 22:13:39 +01:00
|
|
|
|
|
|
|
if (instance->database != nullptr) {
|
|
|
|
instance->database->Close();
|
|
|
|
delete instance->database;
|
|
|
|
}
|
2014-06-10 22:51:48 +02:00
|
|
|
|
|
|
|
delete instance->storage;
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
|
|
|
sticker_global_finish();
|
|
|
|
#endif
|
|
|
|
|
2009-10-13 18:01:11 +02:00
|
|
|
playlist_list_global_finish();
|
2008-10-26 19:30:13 +01:00
|
|
|
input_stream_global_finish();
|
2014-01-30 20:29:48 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
2008-10-15 19:36:33 +02:00
|
|
|
mapper_finish();
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2014-11-29 23:35:18 +01:00
|
|
|
DeinitFS();
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
delete instance->partition;
|
2008-10-22 21:40:44 +02:00
|
|
|
command_finish();
|
2008-08-26 08:27:09 +02:00
|
|
|
decoder_plugin_deinit_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:34 +01:00
|
|
|
archive_plugin_deinit_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2009-01-17 20:23:27 +01:00
|
|
|
config_global_finish();
|
2011-08-24 02:34:28 +02:00
|
|
|
io_thread_deinit();
|
2014-02-21 23:12:51 +01:00
|
|
|
#ifndef ANDROID
|
2013-08-06 21:31:59 +02:00
|
|
|
SignalHandlersFinish();
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2013-04-17 22:58:33 +02:00
|
|
|
delete instance;
|
2014-09-15 14:03:19 +02:00
|
|
|
instance = nullptr;
|
2014-11-20 20:38:37 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DAEMON
|
2009-01-18 18:29:27 +01:00
|
|
|
daemonize_finish();
|
2014-02-21 23:12:51 +01:00
|
|
|
#endif
|
2014-11-20 20:38:37 +01:00
|
|
|
|
2009-03-27 18:02:31 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2014-06-10 22:38:00 +02:00
|
|
|
IcuFinish();
|
2014-02-23 19:27:08 +01:00
|
|
|
|
2012-08-14 23:16:46 +02:00
|
|
|
log_deinit();
|
2006-07-19 20:04:15 +02:00
|
|
|
return EXIT_SUCCESS;
|
2016-02-07 12:42:03 +01:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LogError(e);
|
|
|
|
return EXIT_FAILURE;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
|
|
|
|
gcc_visibility_default
|
|
|
|
JNIEXPORT void JNICALL
|
2014-03-01 20:20:29 +01:00
|
|
|
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context)
|
2014-02-18 08:33:06 +01:00
|
|
|
{
|
2014-03-01 09:19:32 +01:00
|
|
|
Java::Init(env);
|
|
|
|
Java::File::Initialise(env);
|
2014-03-01 17:46:59 +01:00
|
|
|
Environment::Initialise(env);
|
2014-03-01 09:19:32 +01:00
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
context = new Context(env, _context);
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
mpd_main(0, nullptr);
|
2014-03-01 17:46:59 +01:00
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
delete context;
|
2014-03-01 17:46:59 +01:00
|
|
|
Environment::Deinitialise(env);
|
2014-02-18 08:33:06 +01:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:03:19 +02:00
|
|
|
gcc_visibility_default
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_musicpd_Bridge_shutdown(JNIEnv *, jclass)
|
|
|
|
{
|
|
|
|
if (instance != nullptr)
|
2016-03-05 20:20:06 +01:00
|
|
|
instance->Shutdown();
|
2014-09-15 14:03:19 +02:00
|
|
|
}
|
|
|
|
|
2014-02-18 08:33:06 +01:00
|
|
|
#endif
|