2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-02 19:22:15 +01:00
|
|
|
* Copyright (C) 2003-2013 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-09 22:23:37 +01:00
|
|
|
#include "PlaylistGlobal.hxx"
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "UpdateGlue.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicChunk.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "StateFile.hxx"
|
2013-01-02 20:36:28 +01:00
|
|
|
#include "PlayerThread.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-01-03 00:24:45 +01:00
|
|
|
#include "DatabaseGlue.hxx"
|
2013-01-03 00:25:15 +01:00
|
|
|
#include "DatabaseSimple.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2013-01-03 11:05:44 +01:00
|
|
|
#include "Listen.hxx"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "Client.hxx"
|
2013-01-16 22:55:33 +01:00
|
|
|
#include "ClientList.hxx"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "AllCommands.hxx"
|
2013-01-04 22:42:05 +01:00
|
|
|
#include "Partition.hxx"
|
2013-01-07 08:59:11 +01:00
|
|
|
#include "Volume.hxx"
|
2013-01-07 08:54:26 +01:00
|
|
|
#include "OutputAll.hxx"
|
2013-09-05 18:23:15 +02:00
|
|
|
#include "tag/TagConfig.hxx"
|
2013-01-07 09:38:02 +01:00
|
|
|
#include "replay_gain_config.h"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-09 22:33:06 +01:00
|
|
|
#include "SignalHandlers.hxx"
|
|
|
|
#include "Log.hxx"
|
2013-01-09 23:12:53 +01:00
|
|
|
#include "GlobalEvents.hxx"
|
2013-01-10 10:15:09 +01:00
|
|
|
#include "InputInit.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-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-26 01:04:02 +01:00
|
|
|
#include "PlaylistRegistry.hxx"
|
2013-01-27 22:18:45 +01:00
|
|
|
#include "ZeroconfGlue.hxx"
|
2013-01-30 17:18:48 +01:00
|
|
|
#include "DecoderList.hxx"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioConfig.hxx"
|
2013-07-29 23:18:55 +02:00
|
|
|
#include "pcm/PcmResample.hxx"
|
2013-08-07 09:53:33 +02:00
|
|
|
#include "Daemon.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigGlobal.hxx"
|
|
|
|
#include "ConfigData.hxx"
|
2013-09-05 08:42:08 +02:00
|
|
|
#include "ConfigDefaults.hxx"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigOption.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "stats.h"
|
2012-09-28 00:40:00 +02:00
|
|
|
}
|
|
|
|
|
2009-10-24 18:24:29 +02:00
|
|
|
#ifdef ENABLE_INOTIFY
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "InotifyUpdate.hxx"
|
2009-10-24 18:24:29 +02:00
|
|
|
#endif
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
2013-01-02 22:25:17 +01:00
|
|
|
#include "StickerDatabase.hxx"
|
2009-01-19 18:51:57 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2013-01-24 19:18:58 +01:00
|
|
|
#include "ArchiveList.hxx"
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2012-09-28 00:40:00 +02:00
|
|
|
|
2008-10-28 20:33:56 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.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
|
|
|
|
|
2009-01-18 17:32:43 +01:00
|
|
|
enum {
|
|
|
|
DEFAULT_BUFFER_SIZE = 2048,
|
|
|
|
DEFAULT_BUFFER_BEFORE_PLAY = 10,
|
|
|
|
};
|
|
|
|
|
2009-01-03 13:19:01 +01:00
|
|
|
GThread *main_task;
|
2013-01-10 19:13:00 +01:00
|
|
|
EventLoop *main_loop;
|
2008-12-30 19:50:22 +01:00
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
Instance *instance;
|
2009-11-03 21:08:48 +01:00
|
|
|
|
2013-01-14 11:00:22 +01:00
|
|
|
static StateFile *state_file;
|
|
|
|
|
2013-01-27 08:26:17 +01:00
|
|
|
static inline GQuark main_quark()
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string ("main");
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:32:12 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
glue_daemonize_init(const struct options *options, Error &error)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Path pid_file = config_get_path(CONF_PID_FILE, error);
|
|
|
|
if (pid_file.IsNull() && error.IsDefined())
|
2011-09-09 21:32:12 +02:00
|
|
|
return false;
|
|
|
|
|
2009-07-15 18:58:12 +02:00
|
|
|
daemonize_init(config_get_string(CONF_USER, NULL),
|
2009-07-19 08:17:55 +02:00
|
|
|
config_get_string(CONF_GROUP, NULL),
|
2013-08-07 19:54:38 +02:00
|
|
|
std::move(pid_file));
|
2009-07-15 18:58:12 +02:00
|
|
|
|
|
|
|
if (options->kill)
|
|
|
|
daemonize_kill();
|
2011-09-09 21:32:12 +02:00
|
|
|
|
|
|
|
return true;
|
2009-07-15 18:58:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:32:12 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
glue_mapper_init(Error &error)
|
2009-07-15 18:58:19 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Path music_dir = config_get_path(CONF_MUSIC_DIR, error);
|
|
|
|
if (music_dir.IsNull() && error.IsDefined())
|
2011-09-09 21:32:12 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Path playlist_dir = config_get_path(CONF_PLAYLIST_DIR, error);
|
|
|
|
if (playlist_dir.IsNull() && error.IsDefined())
|
2011-09-09 21:32:12 +02:00
|
|
|
return false;
|
2009-07-15 18:58:19 +02:00
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
if (music_dir.IsNull())
|
|
|
|
music_dir = Path::FromUTF8(g_get_user_special_dir(G_USER_DIRECTORY_MUSIC));
|
2009-07-15 18:58:19 +02:00
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
mapper_init(std::move(music_dir), std::move(playlist_dir));
|
2011-09-09 21:32:12 +02:00
|
|
|
return true;
|
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
|
|
|
{
|
2013-01-30 17:52:51 +01:00
|
|
|
const struct config_param *param = config_get_param(CONF_DATABASE);
|
2011-09-05 23:03:05 +02:00
|
|
|
const struct config_param *path = config_get_param(CONF_DB_FILE);
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2012-08-08 08:34:59 +02:00
|
|
|
if (param != NULL && path != NULL)
|
2013-01-30 17:52:51 +01:00
|
|
|
g_message("Found both 'database' and 'db_file' setting - ignoring the latter");
|
2012-08-08 08:34:59 +02:00
|
|
|
|
2009-01-18 16:56:07 +01:00
|
|
|
if (!mapper_has_music_directory()) {
|
2012-08-08 08:34:59 +02:00
|
|
|
if (param != NULL)
|
|
|
|
g_message("Found database setting without "
|
2013-01-30 17:52:51 +01:00
|
|
|
"music_directory - disabling database");
|
2009-01-18 17:54:46 +01:00
|
|
|
if (path != NULL)
|
2013-01-30 17:52:51 +01:00
|
|
|
g_message("Found db_file setting without "
|
|
|
|
"music_directory - disabling database");
|
2009-02-02 16:57:24 +01:00
|
|
|
return true;
|
2009-01-18 16:56:07 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 08:34:59 +02:00
|
|
|
struct config_param *allocated = NULL;
|
2009-01-18 17:54:46 +01:00
|
|
|
|
2012-08-08 08:34:59 +02:00
|
|
|
if (param == NULL && path != NULL) {
|
2013-01-30 22:00:32 +01:00
|
|
|
allocated = new config_param("database", path->line);
|
2013-01-30 22:25:17 +01:00
|
|
|
allocated->AddBlockParam("path", path->value, path->line);
|
2012-08-08 08:34:59 +02:00
|
|
|
param = allocated;
|
|
|
|
}
|
2012-08-08 08:36:14 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
if (!DatabaseGlobalInit(*param, error))
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError(error);
|
2009-01-04 21:18:16 +01:00
|
|
|
|
2013-01-30 22:00:32 +01:00
|
|
|
delete allocated;
|
2012-08-08 08:36:14 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!DatabaseGlobalOpen(error))
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError(error);
|
2009-02-02 16:57:24 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
/* run database update after daemonization? */
|
2012-08-08 08:34:59 +02:00
|
|
|
return !db_is_simple() || db_exists();
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-07-15 18:58:12 +02:00
|
|
|
/**
|
|
|
|
* Configure and initialize the sticker subsystem.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
glue_sticker_init(void)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_SQLITE
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
Path sticker_file = config_get_path(CONF_STICKER_FILE, error);
|
|
|
|
if (sticker_file.IsNull() && error.IsDefined())
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError(error);
|
2009-07-15 18:58:12 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!sticker_global_init(std::move(sticker_file), error))
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError(error);
|
2009-07-15 18:58:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-09-09 21:32:12 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
glue_state_file_init(Error &error)
|
2009-07-15 18:58:12 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Path path_fs = config_get_path(CONF_STATE_FILE, error);
|
|
|
|
if (path_fs.IsNull())
|
|
|
|
return !error.IsDefined();
|
2013-01-27 08:26:17 +01:00
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
state_file = new StateFile(std::move(path_fs),
|
2013-04-17 22:58:33 +02:00
|
|
|
*instance->partition, *main_loop);
|
2013-01-14 11:00:22 +01:00
|
|
|
state_file->Read();
|
2011-09-09 21:32:12 +02:00
|
|
|
return true;
|
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;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = WSAStartup(MAKEWORD(2, 2), &sockinfo);
|
|
|
|
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)
|
|
|
|
{
|
2009-01-25 16:03:49 +01:00
|
|
|
const struct config_param *param;
|
2009-01-18 17:32:43 +01:00
|
|
|
char *test;
|
|
|
|
size_t buffer_size;
|
|
|
|
float perc;
|
|
|
|
unsigned buffered_chunks;
|
|
|
|
unsigned buffered_before_play;
|
|
|
|
|
|
|
|
param = config_get_param(CONF_AUDIO_BUFFER_SIZE);
|
|
|
|
if (param != NULL) {
|
2012-02-15 20:35:06 +01:00
|
|
|
long tmp = strtol(param->value, &test, 10);
|
|
|
|
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",
|
|
|
|
param->value, 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;
|
|
|
|
|
|
|
|
buffered_chunks = buffer_size / CHUNK_SIZE;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
param = config_get_param(CONF_BUFFER_BEFORE_PLAY);
|
|
|
|
if (param != NULL) {
|
|
|
|
perc = strtod(param->value, &test);
|
|
|
|
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",
|
|
|
|
param->value, param->line);
|
2009-01-18 17:32:43 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
perc = DEFAULT_BUFFER_BEFORE_PLAY;
|
|
|
|
|
|
|
|
buffered_before_play = (perc / 100) * buffered_chunks;
|
|
|
|
if (buffered_before_play > buffered_chunks)
|
|
|
|
buffered_before_play = buffered_chunks;
|
|
|
|
|
2013-01-04 22:42:05 +01:00
|
|
|
const unsigned max_length =
|
|
|
|
config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
|
|
|
|
DEFAULT_PLAYLIST_MAX_LENGTH);
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->partition = new Partition(*instance,
|
|
|
|
max_length,
|
|
|
|
buffered_chunks,
|
|
|
|
buffered_before_play);
|
2009-01-18 17:32:43 +01:00
|
|
|
}
|
|
|
|
|
2009-01-01 18:22:11 +01:00
|
|
|
/**
|
2013-01-09 23:12:53 +01:00
|
|
|
* Handler for GlobalEvents::IDLE.
|
2009-01-01 18:22:11 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
idle_event_emitted(void)
|
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 */
|
2009-01-01 18:22:11 +01:00
|
|
|
unsigned flags = idle_get();
|
2008-12-30 19:24:39 +01:00
|
|
|
if (flags != 0)
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->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) &&
|
|
|
|
state_file != nullptr)
|
2013-04-08 22:31:51 +02:00
|
|
|
state_file->CheckModified();
|
2008-12-30 19:24:39 +01:00
|
|
|
}
|
|
|
|
|
2013-08-06 21:31:59 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
/**
|
2013-01-09 23:12:53 +01:00
|
|
|
* Handler for GlobalEvents::SHUTDOWN.
|
2010-09-21 03:28:08 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
shutdown_event_emitted(void)
|
|
|
|
{
|
2013-01-10 19:13:00 +01:00
|
|
|
main_loop->Break();
|
2010-09-21 03:28:08 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 21:31:59 +02:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
int mpd_main(int argc, char *argv[])
|
2006-07-19 20:04:15 +02:00
|
|
|
{
|
2009-07-15 18:22:49 +02:00
|
|
|
struct options options;
|
2006-08-07 22:49:20 +02:00
|
|
|
clock_t start;
|
2009-02-02 16:57:24 +01:00
|
|
|
bool create_db;
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2009-09-24 21:40:04 +02:00
|
|
|
bool success;
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2008-12-30 16:28:13 +01:00
|
|
|
daemonize_close_stdin();
|
|
|
|
|
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,"");
|
|
|
|
#endif
|
|
|
|
|
2009-10-20 21:02:00 +02:00
|
|
|
g_set_application_name("Music Player Daemon");
|
|
|
|
|
2013-04-17 01:53:10 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2,32,0)
|
2008-11-05 18:21:49 +01:00
|
|
|
/* enable GLib's thread safety code */
|
|
|
|
g_thread_init(NULL);
|
2013-04-17 01:53:10 +02:00
|
|
|
#endif
|
2008-11-05 18:21:49 +01:00
|
|
|
|
2011-08-24 02:34:28 +02:00
|
|
|
io_thread_init();
|
2009-03-27 18:02:31 +01:00
|
|
|
winsock_init();
|
2009-01-17 20:23:27 +01:00
|
|
|
config_global_init();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
success = parse_cmdline(argc, argv, &options, error);
|
2009-09-24 21:40:05 +02:00
|
|
|
if (!success) {
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2009-09-24 21:40:05 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!glue_daemonize_init(&options, error)) {
|
|
|
|
g_printerr("%s\n", error.GetMessage());
|
2011-09-09 21:32:12 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
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();
|
2011-09-09 21:40:28 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!log_init(options.verbose, options.log_stderr, error)) {
|
|
|
|
g_warning("%s", error.GetMessage());
|
2011-09-09 21:40:28 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2013-01-15 22:50:49 +01:00
|
|
|
main_task = g_thread_self();
|
|
|
|
main_loop = new EventLoop(EventLoop::Default());
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
instance = new Instance();
|
|
|
|
|
2013-01-16 22:55:33 +01:00
|
|
|
const unsigned max_clients = config_get_positive(CONF_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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
success = listen_global_init(error);
|
2009-09-24 21:40:04 +02:00
|
|
|
if (!success) {
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2009-09-24 21:40:04 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2009-01-18 17:15:34 +01:00
|
|
|
daemonize_set_user();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2013-08-07 19:38:10 +02:00
|
|
|
GlobalEvents::Initialize(*main_loop);
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
|
2013-08-06 21:31:59 +02:00
|
|
|
#ifdef WIN32
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted);
|
2013-08-06 21:31:59 +02:00
|
|
|
#endif
|
2009-01-01 18:22:11 +01:00
|
|
|
|
2013-01-27 19:07:31 +01:00
|
|
|
Path::GlobalInit();
|
2011-09-09 21:32:12 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!glue_mapper_init(error)) {
|
|
|
|
g_printerr("%s\n", error.GetMessage());
|
2011-09-09 21:32:12 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-08-06 08:40:11 +02:00
|
|
|
initPermissions();
|
2013-01-04 22:42:05 +01:00
|
|
|
playlist_global_init();
|
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
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!pcm_resample_global_init(error)) {
|
|
|
|
g_warning("%s", error.GetMessage());
|
2011-10-08 13:14:29 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
decoder_plugin_init_all();
|
2008-11-27 19:34:54 +01:00
|
|
|
update_global_init();
|
2006-08-06 08:40:11 +02:00
|
|
|
|
2009-09-20 23:31:35 +02:00
|
|
|
create_db = !glue_db_init_and_load();
|
2006-08-06 08:40:11 +02: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();
|
2009-01-18 17:32:43 +01:00
|
|
|
initialize_decoder_and_player();
|
2009-01-25 17:38:02 +01:00
|
|
|
volume_init();
|
2005-11-16 15:43:04 +01:00
|
|
|
initAudioConfig();
|
2013-04-17 22:58:33 +02:00
|
|
|
audio_output_all_init(&instance->partition->pc);
|
2008-08-28 20:03:02 +02:00
|
|
|
client_manager_init();
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_global_init();
|
2009-12-14 22:29:46 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!input_stream_global_init(error)) {
|
|
|
|
g_warning("%s", error.GetMessage());
|
2009-12-14 22:29:46 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-10-13 18:01:11 +02:00
|
|
|
playlist_list_global_init();
|
2006-07-21 19:48:54 +02:00
|
|
|
|
2009-01-18 18:29:30 +01:00
|
|
|
daemonize(options.daemon);
|
2005-11-16 15:43:04 +01:00
|
|
|
|
2009-07-22 13:40:19 +02:00
|
|
|
setup_log_output(options.log_stderr);
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2013-08-06 21:31:59 +02:00
|
|
|
SignalHandlersInit(*main_loop);
|
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
|
|
|
|
2013-01-27 22:32:10 +01:00
|
|
|
ZeroconfInit(*main_loop);
|
2007-06-04 19:41:18 +02:00
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
player_create(&instance->partition->pc);
|
2009-01-18 18:09:50 +01:00
|
|
|
|
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 */
|
2009-09-24 21:55:40 +02:00
|
|
|
unsigned job = update_enqueue(NULL, 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
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!glue_state_file_init(error)) {
|
|
|
|
g_printerr("%s\n", error.GetMessage());
|
2011-09-09 21:32:12 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
audio_output_all_set_replay_gain_mode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));
|
2013-01-05 00:05:57 +01:00
|
|
|
|
2009-10-26 19:14:40 +01:00
|
|
|
success = config_get_bool(CONF_AUTO_UPDATE, false);
|
2009-10-24 18:24:29 +02:00
|
|
|
#ifdef ENABLE_INOTIFY
|
2009-10-26 19:14:40 +01:00
|
|
|
if (success && mapper_has_music_directory())
|
2010-04-13 21:01:19 +02:00
|
|
|
mpd_inotify_init(config_get_unsigned(CONF_AUTO_UPDATE_DEPTH,
|
|
|
|
G_MAXUINT));
|
2009-10-26 19:14:40 +01:00
|
|
|
#else
|
|
|
|
if (success)
|
|
|
|
g_warning("inotify: auto_update was disabled. enable during compilation phase");
|
2009-10-24 18:24:29 +02: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() */
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->partition->pc.UpdateAudio();
|
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
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
/* run the main loop */
|
2013-01-10 19:13:00 +01:00
|
|
|
main_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 */
|
|
|
|
|
2009-10-24 18:24:29 +02:00
|
|
|
#ifdef ENABLE_INOTIFY
|
2009-09-25 18:32:00 +02:00
|
|
|
mpd_inotify_finish();
|
2009-10-24 18:24:29 +02:00
|
|
|
#endif
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-14 11:00:22 +01:00
|
|
|
if (state_file != nullptr) {
|
|
|
|
state_file->Write();
|
|
|
|
delete state_file;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-08-07 22:49:20 +02:00
|
|
|
start = clock();
|
2013-01-03 00:24:45 +01:00
|
|
|
DatabaseGlobalDeinit();
|
2008-12-29 17:35:40 +01:00
|
|
|
g_debug("db_finish took %f seconds",
|
|
|
|
((float)(clock()-start))/CLOCKS_PER_SEC);
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
|
|
|
sticker_global_finish();
|
|
|
|
#endif
|
|
|
|
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Deinitialize();
|
2008-09-24 07:14:11 +02:00
|
|
|
|
2009-10-13 18:01:11 +02:00
|
|
|
playlist_list_global_finish();
|
2008-10-26 19:30:13 +01:00
|
|
|
input_stream_global_finish();
|
2009-02-10 18:51:51 +01:00
|
|
|
audio_output_all_finish();
|
2008-12-31 16:46:41 +01:00
|
|
|
volume_finish();
|
2008-10-15 19:36:33 +02:00
|
|
|
mapper_finish();
|
2013-04-17 22:58:33 +02:00
|
|
|
delete instance->partition;
|
2008-10-22 21:40:44 +02:00
|
|
|
command_finish();
|
2008-11-27 19:34:54 +01:00
|
|
|
update_global_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();
|
2009-01-18 15:40:53 +01:00
|
|
|
stats_global_finish();
|
2011-08-24 02:34:28 +02:00
|
|
|
io_thread_deinit();
|
2013-08-06 21:31:59 +02:00
|
|
|
SignalHandlersFinish();
|
2013-04-17 22:58:33 +02:00
|
|
|
delete instance;
|
2013-01-30 15:08:03 +01:00
|
|
|
delete main_loop;
|
2009-01-18 18:29:27 +01:00
|
|
|
daemonize_finish();
|
2009-03-27 18:02:31 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2012-08-14 23:16:46 +02:00
|
|
|
log_deinit();
|
2006-07-19 20:04:15 +02:00
|
|
|
return EXIT_SUCCESS;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|