diff options
Diffstat (limited to '')
-rw-r--r-- | src/Main.cxx | 146 |
1 files changed, 84 insertions, 62 deletions
diff --git a/src/Main.cxx b/src/Main.cxx index b45e2c3ae..f790ec574 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -23,67 +23,71 @@ #include "CommandLine.hxx" #include "PlaylistFile.hxx" #include "PlaylistGlobal.hxx" -#include "UpdateGlue.hxx" +#include "db/update/UpdateGlue.hxx" #include "MusicChunk.hxx" #include "StateFile.hxx" #include "PlayerThread.hxx" #include "Mapper.hxx" -#include "DatabaseGlue.hxx" -#include "DatabaseSimple.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabaseSimple.hxx" #include "Permission.hxx" #include "Listen.hxx" -#include "Client.hxx" -#include "ClientList.hxx" +#include "client/Client.hxx" +#include "client/ClientList.hxx" #include "command/AllCommands.hxx" #include "Partition.hxx" -#include "Volume.hxx" -#include "OutputAll.hxx" +#include "mixer/Volume.hxx" +#include "output/OutputAll.hxx" #include "tag/TagConfig.hxx" #include "ReplayGainConfig.hxx" #include "Idle.hxx" -#include "SignalHandlers.hxx" #include "Log.hxx" #include "LogInit.hxx" #include "GlobalEvents.hxx" -#include "InputInit.hxx" +#include "input/Init.hxx" #include "event/Loop.hxx" #include "IOThread.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Config.hxx" -#include "PlaylistRegistry.hxx" -#include "ZeroconfGlue.hxx" -#include "DecoderList.hxx" +#include "fs/StandardDirectory.hxx" +#include "playlist/PlaylistRegistry.hxx" +#include "zeroconf/ZeroconfGlue.hxx" +#include "decoder/DecoderList.hxx" #include "AudioConfig.hxx" -#include "pcm/PcmResample.hxx" -#include "Daemon.hxx" +#include "pcm/PcmConvert.hxx" +#include "unix/SignalHandlers.hxx" +#include "unix/Daemon.hxx" #include "system/FatalError.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "thread/Id.hxx" -#include "ConfigGlobal.hxx" -#include "ConfigData.hxx" -#include "ConfigDefaults.hxx" -#include "ConfigOption.hxx" +#include "config/ConfigGlobal.hxx" +#include "config/ConfigData.hxx" +#include "config/ConfigDefaults.hxx" +#include "config/ConfigOption.hxx" #include "Stats.hxx" +#ifdef ENABLE_NEIGHBOR_PLUGINS +#include "neighbor/Glue.hxx" +#endif + #ifdef ENABLE_INOTIFY -#include "InotifyUpdate.hxx" +#include "db/update/InotifyUpdate.hxx" #endif #ifdef ENABLE_SQLITE -#include "StickerDatabase.hxx" +#include "sticker/StickerDatabase.hxx" #endif #ifdef ENABLE_ARCHIVE -#include "ArchiveList.hxx" +#include "archive/ArchiveList.hxx" #endif +#ifdef HAVE_GLIB #include <glib.h> +#endif -#include <unistd.h> #include <stdlib.h> -#include <errno.h> -#include <string.h> #ifdef HAVE_LOCALE_H #include <locale.h> @@ -94,6 +98,8 @@ #include <ws2tcpip.h> #endif +#include <limits.h> + static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096; static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10; @@ -135,13 +141,9 @@ glue_mapper_init(Error &error) return false; if (music_dir.IsNull()) { - const char *path = - g_get_user_special_dir(G_USER_DIRECTORY_MUSIC); - if (path != nullptr) { - music_dir = AllocatedPath::FromUTF8(path, error); - if (music_dir.IsNull()) - return false; - } + music_dir = GetUserMusicDir(); + if (music_dir.IsNull()) + return true; } mapper_init(std::move(music_dir), std::move(playlist_dir)); @@ -188,7 +190,7 @@ glue_db_init_and_load(void) return true; Error error; - if (!DatabaseGlobalInit(*param, error)) + if (!DatabaseGlobalInit(*main_loop, *instance, *param, error)) FatalError(error); delete allocated; @@ -240,9 +242,8 @@ static void winsock_init(void) { #ifdef WIN32 WSADATA sockinfo; - int retval; - retval = WSAStartup(MAKEWORD(2, 2), &sockinfo); + int retval = WSAStartup(MAKEWORD(2, 2), &sockinfo); if(retval != 0) FormatFatalError("Attempt to open Winsock2 failed; error code %d", retval); @@ -260,14 +261,11 @@ static void initialize_decoder_and_player(void) { const struct config_param *param; - char *test; - size_t buffer_size; - float perc; - unsigned buffered_chunks; - unsigned buffered_before_play; + size_t buffer_size; param = config_get_param(CONF_AUDIO_BUFFER_SIZE); if (param != nullptr) { + char *test; long tmp = strtol(param->value.c_str(), &test, 10); if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX) FormatFatalError("buffer size \"%s\" is not a " @@ -279,14 +277,16 @@ initialize_decoder_and_player(void) buffer_size *= 1024; - buffered_chunks = buffer_size / CHUNK_SIZE; + const unsigned buffered_chunks = buffer_size / CHUNK_SIZE; if (buffered_chunks >= 1 << 15) FormatFatalError("buffer size \"%lu\" is too big", (unsigned long)buffer_size); + float perc; param = config_get_param(CONF_BUFFER_BEFORE_PLAY); if (param != nullptr) { + char *test; perc = strtod(param->value.c_str(), &test); if (*test != '%' || perc < 0 || perc > 100) { FormatFatalError("buffered before play \"%s\" is not " @@ -297,7 +297,7 @@ initialize_decoder_and_player(void) } else perc = DEFAULT_BUFFER_BEFORE_PLAY; - buffered_before_play = (perc / 100) * buffered_chunks; + unsigned buffered_before_play = (perc / 100) * buffered_chunks; if (buffered_before_play > buffered_chunks) buffered_before_play = buffered_chunks; @@ -353,10 +353,7 @@ int main(int argc, char *argv[]) int mpd_main(int argc, char *argv[]) { struct options options; - clock_t start; - bool create_db; Error error; - bool success; daemonize_close_stdin(); @@ -365,19 +362,20 @@ int mpd_main(int argc, char *argv[]) setlocale(LC_CTYPE,""); #endif +#ifdef HAVE_GLIB g_set_application_name("Music Player Daemon"); #if !GLIB_CHECK_VERSION(2,32,0) /* enable GLib's thread safety code */ g_thread_init(nullptr); #endif +#endif - io_thread_init(); winsock_init(); + io_thread_init(); config_global_init(); - success = parse_cmdline(argc, argv, &options, error); - if (!success) { + if (!parse_cmdline(argc, argv, &options, error)) { LogError(error); return EXIT_FAILURE; } @@ -400,16 +398,29 @@ int mpd_main(int argc, char *argv[]) instance = new Instance(); +#ifdef ENABLE_NEIGHBOR_PLUGINS + instance->neighbors = new NeighborGlue(); + if (!instance->neighbors->Init(io_thread_get(), *instance, error)) { + LogError(error); + return EXIT_FAILURE; + } + + if (instance->neighbors->IsEmpty()) { + delete instance->neighbors; + instance->neighbors = nullptr; + } +#endif + const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10); instance->client_list = new ClientList(max_clients); - success = listen_global_init(error); - if (!success) { + if (!listen_global_init(error)) { LogError(error); return EXIT_FAILURE; } daemonize_set_user(); + daemonize_begin(options.daemon); GlobalEvents::Initialize(*main_loop); GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted); @@ -431,7 +442,7 @@ int mpd_main(int argc, char *argv[]) archive_plugin_init_all(); #endif - if (!pcm_resample_global_init(error)) { + if (!pcm_convert_global_init(error)) { LogError(error); return EXIT_FAILURE; } @@ -439,7 +450,7 @@ int mpd_main(int argc, char *argv[]) decoder_plugin_init_all(); update_global_init(); - create_db = !glue_db_init_and_load(); + const bool create_db = !glue_db_init_and_load(); glue_sticker_init(); @@ -458,7 +469,7 @@ int mpd_main(int argc, char *argv[]) playlist_list_global_init(); - daemonize(options.daemon); + daemonize_commit(); setup_log_output(options.log_stderr); @@ -466,6 +477,12 @@ int mpd_main(int argc, char *argv[]) io_thread_start(); +#ifdef ENABLE_NEIGHBOR_PLUGINS + if (instance->neighbors != nullptr && + !instance->neighbors->Open(error)) + FatalError(error); +#endif + ZeroconfInit(*main_loop); player_create(instance->partition->pc); @@ -479,22 +496,22 @@ int mpd_main(int argc, char *argv[]) } if (!glue_state_file_init(error)) { - g_printerr("%s\n", error.GetMessage()); + LogError(error); return EXIT_FAILURE; } audio_output_all_set_replay_gain_mode(replay_gain_get_real_mode(instance->partition->playlist.queue.random)); - success = config_get_bool(CONF_AUTO_UPDATE, false); + if (config_get_bool(CONF_AUTO_UPDATE, false)) { #ifdef ENABLE_INOTIFY - if (success && mapper_has_music_directory()) - mpd_inotify_init(config_get_unsigned(CONF_AUTO_UPDATE_DEPTH, - G_MAXUINT)); + if (mapper_has_music_directory()) + mpd_inotify_init(config_get_unsigned(CONF_AUTO_UPDATE_DEPTH, + G_MAXUINT)); #else - if (success) FormatWarning(main_domain, "inotify: auto_update was disabled. enable during compilation phase"); #endif + } config_global_check(); @@ -529,7 +546,14 @@ int mpd_main(int argc, char *argv[]) listen_global_finish(); delete instance->client_list; - start = clock(); +#ifdef ENABLE_NEIGHBOR_PLUGINS + if (instance->neighbors != nullptr) { + instance->neighbors->Close(); + delete instance->neighbors; + } +#endif + + const clock_t start = clock(); DatabaseGlobalDeinit(); FormatDebug(main_domain, "db_finish took %f seconds", @@ -544,7 +568,6 @@ int mpd_main(int argc, char *argv[]) playlist_list_global_finish(); input_stream_global_finish(); audio_output_all_finish(); - volume_finish(); mapper_finish(); delete instance->partition; command_finish(); @@ -554,7 +577,6 @@ int mpd_main(int argc, char *argv[]) archive_plugin_deinit_all(); #endif config_global_finish(); - stats_global_finish(); io_thread_deinit(); SignalHandlersFinish(); delete instance; |