aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c169
1 files changed, 120 insertions, 49 deletions
diff --git a/src/main.c b/src/main.c
index 5035a4836..a40dae369 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,6 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "config.h"
#include "main.h"
#include "daemon.h"
#include "client.h"
@@ -33,7 +34,6 @@
#include "path.h"
#include "mapper.h"
#include "chunk.h"
-#include "decoder_control.h"
#include "player_control.h"
#include "stats.h"
#include "sig_handlers.h"
@@ -44,18 +44,21 @@
#include "permission.h"
#include "replay_gain.h"
#include "decoder_list.h"
-#include "input_stream.h"
+#include "input_init.h"
+#include "playlist_list.h"
#include "state_file.h"
#include "tag.h"
#include "dbUtils.h"
-#include "config.h"
-#include "normalize.h"
#include "zeroconf.h"
#include "event_pipe.h"
#include "dirvec.h"
#include "songvec.h"
#include "tag_pool.h"
+#ifdef ENABLE_INOTIFY
+#include "inotify_update.h"
+#endif
+
#ifdef ENABLE_SQLITE
#include "sticker.h"
#endif
@@ -88,7 +91,34 @@ enum {
GThread *main_task;
GMainLoop *main_loop;
-struct notify main_notify;
+GCond *main_cond;
+
+static void
+glue_daemonize_init(const struct options *options)
+{
+ daemonize_init(config_get_string(CONF_USER, NULL),
+ config_get_string(CONF_GROUP, NULL),
+ config_get_path(CONF_PID_FILE));
+
+ if (options->kill)
+ daemonize_kill();
+}
+
+static void
+glue_mapper_init(void)
+{
+ const char *music_dir, *playlist_dir;
+
+ music_dir = config_get_path(CONF_MUSIC_DIR);
+#if GLIB_CHECK_VERSION(2,14,0)
+ if (music_dir == NULL)
+ music_dir = g_get_user_special_dir(G_USER_DIRECTORY_MUSIC);
+#endif
+
+ playlist_dir = config_get_path(CONF_PLAYLIST_DIR);
+
+ mapper_init(music_dir, playlist_dir);
+}
/**
* Returns the database. If this function returns false, this has not
@@ -96,7 +126,7 @@ struct notify main_notify;
* process has been daemonized.
*/
static bool
-openDB(const Options *options)
+glue_db_init_and_load(void)
{
const char *path = config_get_path(CONF_DB_FILE);
bool ret;
@@ -115,19 +145,11 @@ openDB(const Options *options)
db_init(path);
- if (options->createDB > 0)
- /* don't attempt to load the old database */
- return false;
-
ret = db_load(&error);
if (!ret) {
g_warning("Failed to load database: %s", error->message);
g_error_free(error);
- if (options->createDB < 0)
- g_error("can't open db file and using "
- "\"--no-create-db\" command line option");
-
if (!db_check())
exit(EXIT_FAILURE);
@@ -141,11 +163,34 @@ openDB(const Options *options)
}
/**
+ * Configure and initialize the sticker subsystem.
+ */
+static void
+glue_sticker_init(void)
+{
+#ifdef ENABLE_SQLITE
+ bool success;
+ GError *error = NULL;
+
+ success = sticker_global_init(config_get_path(CONF_STICKER_FILE),
+ &error);
+ if (!success)
+ g_error("%s", error->message);
+#endif
+}
+
+static void
+glue_state_file_init(void)
+{
+ state_file_init(config_get_path(CONF_STATE_FILE));
+}
+
+/**
* Windows-only initialization of the Winsock2 library.
*/
-#ifdef WIN32
static void winsock_init(void)
{
+#ifdef WIN32
WSADATA sockinfo;
int retval;
@@ -161,9 +206,8 @@ static void winsock_init(void)
g_error("We use Winsock2 but your version is either too new or "
"old; please install Winsock 2.x\n");
}
-
-}
#endif
+}
/**
* Initialize the decoder and player core, including the music pipe.
@@ -210,7 +254,6 @@ initialize_decoder_and_player(void)
buffered_before_play = buffered_chunks;
pc_init(buffered_chunks, buffered_before_play);
- dc_init();
}
/**
@@ -228,9 +271,11 @@ idle_event_emitted(void)
int main(int argc, char *argv[])
{
- Options options;
+ struct options options;
clock_t start;
bool create_db;
+ GError *error = NULL;
+ bool success;
daemonize_close_stdin();
@@ -239,45 +284,51 @@ int main(int argc, char *argv[])
setlocale(LC_CTYPE,"");
#endif
+ g_set_application_name("Music Player Daemon");
+
/* enable GLib's thread safety code */
g_thread_init(NULL);
-#ifdef WIN32
winsock_init();
-#endif
idle_init();
dirvec_init();
songvec_init();
tag_pool_init();
config_global_init();
- parseOptions(argc, argv, &options);
-
- daemonize_init(config_get_string(CONF_USER, NULL),
- config_get_path(CONF_PID_FILE));
+ success = parse_cmdline(argc, argv, &options, &error);
+ if (!success) {
+ g_warning("%s", error->message);
+ g_error_free(error);
+ return EXIT_FAILURE;
+ }
- if (options.kill)
- daemonize_kill();
+ glue_daemonize_init(&options);
stats_global_init();
tag_lib_init();
- log_init(options.verbose, options.stdOutput);
+ log_init(options.verbose, options.log_stderr);
- listen_global_init();
+ success = listen_global_init(&error);
+ if (!success) {
+ g_warning("%s", error->message);
+ g_error_free(error);
+ return EXIT_FAILURE;
+ }
daemonize_set_user();
main_task = g_thread_self();
main_loop = g_main_loop_new(NULL, FALSE);
- notify_init(&main_notify);
+ main_cond = g_cond_new();
event_pipe_init();
event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted);
path_global_init();
- mapper_init();
+ glue_mapper_init();
initPermissions();
- initPlaylist();
+ playlist_global_init();
spl_global_init();
#ifdef ENABLE_ARCHIVE
archive_plugin_init_all();
@@ -285,11 +336,9 @@ int main(int argc, char *argv[])
decoder_plugin_init_all();
update_global_init();
- create_db = !openDB(&options);
+ create_db = !glue_db_init_and_load();
-#ifdef ENABLE_SQLITE
- sticker_global_init(config_get_path(CONF_STICKER_FILE));
-#endif
+ glue_sticker_init();
command_init();
initialize_decoder_and_player();
@@ -298,12 +347,18 @@ int main(int argc, char *argv[])
audio_output_all_init();
client_manager_init();
replay_gain_global_init();
- initNormalization();
- input_stream_global_init();
+
+ if (!input_stream_global_init(&error)) {
+ g_warning("%s", error->message);
+ g_error_free(error);
+ return EXIT_FAILURE;
+ }
+
+ playlist_list_global_init();
daemonize(options.daemon);
- setup_log_output(options.stdOutput);
+ setup_log_output(options.log_stderr);
initSigHandlers();
@@ -312,15 +367,29 @@ int main(int argc, char *argv[])
player_create();
if (create_db) {
- /* the database failed to load, or MPD was started
- with --create-db: recreate a new database */
- unsigned job = directory_update_init(NULL);
+ /* the database failed to load: recreate the
+ database */
+ unsigned job = update_enqueue(NULL, true);
if (job == 0)
g_error("directory update failed");
}
+ glue_state_file_init();
- state_file_init(config_get_path(CONF_STATE_FILE));
+ success = config_get_bool(CONF_AUTO_UPDATE, false);
+#ifdef ENABLE_INOTIFY
+ if (success && mapper_has_music_directory())
+ mpd_inotify_init();
+#else
+ if (success)
+ g_warning("inotify: auto_update was disabled. enable during compilation phase");
+#endif
+
+ config_global_check();
+
+ /* enable all audio outputs (if not already done by
+ playlist_state_restore() */
+ pc_update_audio();
/* run the main loop */
@@ -330,12 +399,16 @@ int main(int argc, char *argv[])
g_main_loop_unref(main_loop);
+#ifdef ENABLE_INOTIFY
+ mpd_inotify_finish();
+#endif
+
state_file_finish();
- playerKill();
+ pc_kill();
finishZeroconf();
client_manager_deinit();
listen_global_finish();
- finishPlaylist();
+ playlist_global_finish();
start = clock();
db_finish();
@@ -346,18 +419,16 @@ int main(int argc, char *argv[])
sticker_global_finish();
#endif
- notify_deinit(&main_notify);
+ g_cond_free(main_cond);
event_pipe_deinit();
+ playlist_list_global_finish();
input_stream_global_finish();
- finishNormalization();
audio_output_all_finish();
- finishAudioConfig();
volume_finish();
mapper_finish();
path_global_finish();
finishPermissions();
- dc_deinit();
pc_deinit();
command_finish();
update_global_finish();