Main.cxx 16.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Main.hxx"
22
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "CommandLine.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "PlaylistFile.hxx"
25
#include "PlaylistGlobal.hxx"
26
#include "MusicChunk.hxx"
27
#include "StateFile.hxx"
28
#include "PlayerThread.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "Mapper.hxx"
30
#include "Permission.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "Listen.hxx"
32 33
#include "client/Client.hxx"
#include "client/ClientList.hxx"
34
#include "command/AllCommands.hxx"
35
#include "Partition.hxx"
36
#include "tag/TagConfig.hxx"
37
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "Idle.hxx"
39
#include "Log.hxx"
40
#include "LogInit.hxx"
41
#include "GlobalEvents.hxx"
Max Kellermann's avatar
Max Kellermann committed
42
#include "input/Init.hxx"
43
#include "event/Loop.hxx"
44
#include "IOThread.hxx"
45
#include "fs/AllocatedPath.hxx"
46
#include "fs/Config.hxx"
47
#include "playlist/PlaylistRegistry.hxx"
48
#include "zeroconf/ZeroconfGlue.hxx"
49
#include "decoder/DecoderList.hxx"
50
#include "AudioConfig.hxx"
51
#include "pcm/PcmConvert.hxx"
52 53
#include "unix/SignalHandlers.hxx"
#include "unix/Daemon.hxx"
54
#include "system/FatalError.hxx"
55
#include "util/UriUtil.hxx"
56
#include "util/Error.hxx"
57
#include "util/Domain.hxx"
58
#include "thread/Id.hxx"
59
#include "thread/Slack.hxx"
60
#include "lib/icu/Init.hxx"
61 62 63 64
#include "config/ConfigGlobal.hxx"
#include "config/ConfigData.hxx"
#include "config/ConfigDefaults.hxx"
#include "config/ConfigOption.hxx"
65
#include "config/ConfigError.hxx"
66
#include "Stats.hxx"
Max Kellermann's avatar
Max Kellermann committed
67

68 69
#ifdef ENABLE_DATABASE
#include "db/update/Service.hxx"
70
#include "db/Configured.hxx"
71
#include "db/DatabasePlugin.hxx"
72
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
73
#include "storage/Configured.hxx"
74
#include "storage/CompositeStorage.hxx"
75 76 77
#ifdef ENABLE_INOTIFY
#include "db/update/InotifyUpdate.hxx"
#endif
78 79
#endif

80 81 82 83
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Glue.hxx"
#endif

84
#ifdef ENABLE_SQLITE
85
#include "sticker/StickerDatabase.hxx"
86 87
#endif

88
#ifdef ENABLE_ARCHIVE
89
#include "archive/ArchiveList.hxx"
90
#endif
Max Kellermann's avatar
Max Kellermann committed
91

Max Kellermann's avatar
Max Kellermann committed
92
#ifdef ANDROID
93 94
#include "java/Global.hxx"
#include "java/File.hxx"
95
#include "android/Environment.hxx"
96
#include "android/Context.hxx"
97
#include "fs/StandardDirectory.hxx"
98
#include "fs/FileSystem.hxx"
Max Kellermann's avatar
Max Kellermann committed
99 100 101
#include "org_musicpd_Bridge.h"
#endif

102
#ifdef HAVE_GLIB
103
#include <glib.h>
104
#endif
105

Max Kellermann's avatar
Max Kellermann committed
106
#include <stdlib.h>
107

108
#ifdef HAVE_LOCALE_H
109 110 111
#include <locale.h>
#endif

112 113 114 115 116
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

117
#ifdef __BLOCKS__
118 119 120
#include <dispatch/dispatch.h>
#endif

121 122
#include <limits.h>

123
static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096;
124
static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
Max Kellermann's avatar
Max Kellermann committed
125

126 127
static constexpr Domain main_domain("main");

128 129 130 131
#ifdef ANDROID
Context *context;
#endif

132
Instance *instance;
133

134 135
static StateFile *state_file;

136 137
#ifndef ANDROID

138
static bool
139
glue_daemonize_init(const struct options *options, Error &error)
140
{
141
	auto pid_file = config_get_path(CONF_PID_FILE, error);
142
	if (pid_file.IsNull() && error.IsDefined())
143 144
		return false;

145 146
	daemonize_init(config_get_string(CONF_USER, nullptr),
		       config_get_string(CONF_GROUP, nullptr),
147
		       std::move(pid_file));
148 149 150

	if (options->kill)
		daemonize_kill();
151 152

	return true;
153 154
}

155 156
#endif

157
static bool
158
glue_mapper_init(Error &error)
159
{
160
	auto playlist_dir = config_get_path(CONF_PLAYLIST_DIR, error);
161
	if (playlist_dir.IsNull() && error.IsDefined())
162
		return false;
163

164 165 166
	mapper_init(std::move(playlist_dir));
	return true;
}
167

168 169
#ifdef ENABLE_DATABASE

170 171 172
static bool
InitStorage(Error &error)
{
173
	Storage *storage = CreateConfiguredStorage(io_thread_get(), error);
174 175 176 177 178 179 180 181 182
	if (storage == nullptr)
		return !error.IsDefined();

	assert(!error.IsDefined());

	CompositeStorage *composite = new CompositeStorage();
	instance->storage = composite;
	composite->Mount("", storage);
	return true;
183 184
}

185 186 187 188 189 190
/**
 * 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
191
glue_db_init_and_load(void)
192
{
193
	Error error;
194 195
	instance->database =
		CreateConfiguredDatabase(*instance->event_loop, *instance,
196
					 error);
197 198 199 200 201 202
	if (instance->database == nullptr) {
		if (error.IsDefined())
			FatalError(error);
		else
			return true;
	}
203

204 205 206
	if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
		if (!InitStorage(error))
			FatalError(error);
207

208 209 210 211 212 213 214 215 216 217 218 219 220
		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");
221 222
	}

223
	if (!instance->database->Open(error))
224
		FatalError(error);
225

226
	if (!instance->database->IsPlugin(simple_db_plugin))
227 228
		return true;

229
	SimpleDatabase &db = *(SimpleDatabase *)instance->database;
230
	instance->update = new UpdateService(*instance->event_loop, db,
Max Kellermann's avatar
Max Kellermann committed
231
					     static_cast<CompositeStorage &>(*instance->storage),
232
					     *instance);
233

234
	/* run database update after daemonization? */
235
	return db.FileExists();
Warren Dukes's avatar
Warren Dukes committed
236
}
Warren Dukes's avatar
Warren Dukes committed
237

238 239 240 241 242 243 244
static bool
InitDatabaseAndStorage()
{
	const bool create_db = !glue_db_init_and_load();
	return create_db;
}

245 246
#endif

247 248 249 250 251 252 253
/**
 * Configure and initialize the sticker subsystem.
 */
static void
glue_sticker_init(void)
{
#ifdef ENABLE_SQLITE
254
	Error error;
255 256 257 258 259 260
	auto sticker_file = config_get_path(CONF_STICKER_FILE, error);
	if (sticker_file.IsNull()) {
		if (error.IsDefined())
			FatalError(error);
		return;
	}
261

262
	if (!sticker_global_init(std::move(sticker_file), error))
263
		FatalError(error);
264 265 266
#endif
}

267
static bool
268
glue_state_file_init(Error &error)
269
{
270
	auto path_fs = config_get_path(CONF_STATE_FILE, error);
271 272 273 274 275 276 277 278 279 280 281 282 283 284
	if (path_fs.IsNull()) {
		if (error.IsDefined())
			return false;

#ifdef ANDROID
		const auto cache_dir = GetUserCacheDir();
		if (cache_dir.IsNull())
			return true;

		path_fs = AllocatedPath::Build(cache_dir, "state");
#else
		return true;
#endif
	}
285

286 287 288 289
	unsigned interval = config_get_unsigned(CONF_STATE_FILE_INTERVAL,
						StateFile::DEFAULT_INTERVAL);

	state_file = new StateFile(std::move(path_fs), interval,
290 291
				   *instance->partition,
				   *instance->event_loop);
292
	state_file->Read();
293
	return true;
294 295
}

296 297 298 299 300
/**
 * Windows-only initialization of the Winsock2 library.
 */
static void winsock_init(void)
{
301
#ifdef WIN32
302 303
	WSADATA sockinfo;

304
	int retval = WSAStartup(MAKEWORD(2, 2), &sockinfo);
305
	if(retval != 0)
306 307
		FormatFatalError("Attempt to open Winsock2 failed; error code %d",
				 retval);
308 309

	if (LOBYTE(sockinfo.wVersion) != 2)
310 311
		FatalError("We use Winsock2 but your version is either too new "
			   "or old; please install Winsock 2.x");
312
#endif
313
}
314

Max Kellermann's avatar
Max Kellermann committed
315 316 317 318 319 320
/**
 * Initialize the decoder and player core, including the music pipe.
 */
static void
initialize_decoder_and_player(void)
{
321
	const struct config_param *param;
Max Kellermann's avatar
Max Kellermann committed
322

323
	size_t buffer_size;
Max Kellermann's avatar
Max Kellermann committed
324
	param = config_get_param(CONF_AUDIO_BUFFER_SIZE);
325
	if (param != nullptr) {
326
		char *test;
327
		long tmp = strtol(param->value.c_str(), &test, 10);
328
		if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
329 330
			FormatFatalError("buffer size \"%s\" is not a "
					 "positive integer, line %i",
331
					 param->value.c_str(), param->line);
332
		buffer_size = tmp;
Max Kellermann's avatar
Max Kellermann committed
333 334 335 336 337
	} else
		buffer_size = DEFAULT_BUFFER_SIZE;

	buffer_size *= 1024;

338
	const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
Max Kellermann's avatar
Max Kellermann committed
339 340

	if (buffered_chunks >= 1 << 15)
341 342
		FormatFatalError("buffer size \"%lu\" is too big",
				 (unsigned long)buffer_size);
Max Kellermann's avatar
Max Kellermann committed
343

344
	float perc;
Max Kellermann's avatar
Max Kellermann committed
345
	param = config_get_param(CONF_BUFFER_BEFORE_PLAY);
346
	if (param != nullptr) {
347
		char *test;
348
		perc = strtod(param->value.c_str(), &test);
Max Kellermann's avatar
Max Kellermann committed
349
		if (*test != '%' || perc < 0 || perc > 100) {
350 351 352
			FormatFatalError("buffered before play \"%s\" is not "
					 "a positive percentage and less "
					 "than 100 percent, line %i",
353
					 param->value.c_str(), param->line);
Max Kellermann's avatar
Max Kellermann committed
354 355 356 357
		}
	} else
		perc = DEFAULT_BUFFER_BEFORE_PLAY;

358
	unsigned buffered_before_play = (perc / 100) * buffered_chunks;
Max Kellermann's avatar
Max Kellermann committed
359 360 361
	if (buffered_before_play > buffered_chunks)
		buffered_before_play = buffered_chunks;

362 363 364 365
	const unsigned max_length =
		config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
				    DEFAULT_PLAYLIST_MAX_LENGTH);

366 367 368 369
	instance->partition = new Partition(*instance,
					    max_length,
					    buffered_chunks,
					    buffered_before_play);
Max Kellermann's avatar
Max Kellermann committed
370 371
}

372
/**
373
 * Handler for GlobalEvents::IDLE.
374 375 376
 */
static void
idle_event_emitted(void)
377
{
Max Kellermann's avatar
Max Kellermann committed
378
	/* send "idle" notifications to all subscribed
379
	   clients */
380
	unsigned flags = idle_get();
381
	if (flags != 0)
382
		instance->client_list->IdleAdd(flags);
383

384 385
	if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
	    state_file != nullptr)
386
		state_file->CheckModified();
387 388
}

389 390
#ifdef WIN32

391
/**
392
 * Handler for GlobalEvents::SHUTDOWN.
393 394 395 396
 */
static void
shutdown_event_emitted(void)
{
397
	instance->event_loop->Break();
398 399
}

400 401
#endif

Max Kellermann's avatar
Max Kellermann committed
402 403
#ifndef ANDROID

404
int main(int argc, char *argv[])
405 406 407 408 409 410 411 412
{
#ifdef WIN32
	return win32_main(argc, argv);
#else
	return mpd_main(argc, argv);
#endif
}

Max Kellermann's avatar
Max Kellermann committed
413 414
#endif

415 416
static int mpd_main_after_fork(struct options);

417 418 419
#ifdef ANDROID
static inline
#endif
420
int mpd_main(int argc, char *argv[])
421
{
Max Kellermann's avatar
Max Kellermann committed
422
	struct options options;
423
	Error error;
Warren Dukes's avatar
Warren Dukes committed
424

425
#ifndef ANDROID
426 427
	daemonize_close_stdin();

428
#ifdef HAVE_LOCALE_H
429 430 431 432
	/* initialize locale */
	setlocale(LC_CTYPE,"");
#endif

433
#ifdef HAVE_GLIB
434 435
	g_set_application_name("Music Player Daemon");

436
#if !GLIB_CHECK_VERSION(2,32,0)
Max Kellermann's avatar
Max Kellermann committed
437
	/* enable GLib's thread safety code */
438
	g_thread_init(nullptr);
439
#endif
440
#endif
441
#endif
Max Kellermann's avatar
Max Kellermann committed
442

443
	if (!IcuInit(error)) {
444 445 446 447
		LogError(error);
		return EXIT_FAILURE;
	}

448
	winsock_init();
449
	io_thread_init();
450
	config_global_init();
Warren Dukes's avatar
Warren Dukes committed
451

452 453 454
#ifdef ANDROID
	(void)argc;
	(void)argv;
455

456 457 458 459 460
	{
		const auto sdcard = Environment::getExternalStorageDirectory();
		if (!sdcard.IsNull()) {
			const auto config_path =
				AllocatedPath::Build(sdcard, "mpd.conf");
461 462
			if (FileExists(config_path) &&
			    !ReadConfigFile(config_path, error)) {
463 464 465 466 467
				LogError(error);
				return EXIT_FAILURE;
			}
		}
	}
468
#else
469
	if (!parse_cmdline(argc, argv, &options, error)) {
470
		LogError(error);
471 472
		return EXIT_FAILURE;
	}
Warren Dukes's avatar
Warren Dukes committed
473

474
	if (!glue_daemonize_init(&options, error)) {
475
		LogError(error);
476 477
		return EXIT_FAILURE;
	}
478
#endif
479

Max Kellermann's avatar
Max Kellermann committed
480
	stats_global_init();
481
	TagLoadConfig();
482

483
	if (!log_init(options.verbose, options.log_stderr, error)) {
484
		LogError(error);
485 486
		return EXIT_FAILURE;
	}
487

488
	instance = new Instance();
489
	instance->event_loop = new EventLoop();
490

491 492 493 494 495 496 497 498 499 500 501 502 503
#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

504
	const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10);
505
	instance->client_list = new ClientList(max_clients);
506

507 508
	initialize_decoder_and_player();

509 510
	if (!listen_global_init(*instance->event_loop, *instance->partition,
				error)) {
511
		LogError(error);
512 513
		return EXIT_FAILURE;
	}
514

515
#ifndef ANDROID
516
	daemonize_set_user();
517
	daemonize_begin(options.daemon);
518
#endif
Warren Dukes's avatar
Warren Dukes committed
519

520
#ifdef __BLOCKS__
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	/* 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
}

static int mpd_main_after_fork(struct options options)
{
	Error error;

541
	GlobalEvents::Initialize(*instance->event_loop);
542
	GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
543
#ifdef WIN32
544
	GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted);
545
#endif
546

547
	ConfigureFS();
548

549
	if (!glue_mapper_init(error)) {
550
		LogError(error);
551 552 553
		return EXIT_FAILURE;
	}

554
	initPermissions();
555
	playlist_global_init();
556
	spl_global_init();
557
#ifdef ENABLE_ARCHIVE
558
	archive_plugin_init_all();
559
#endif
560

561
	if (!pcm_convert_global_init(error)) {
562
		LogError(error);
563 564 565
		return EXIT_FAILURE;
	}

566
	decoder_plugin_init_all();
567

568
#ifdef ENABLE_DATABASE
569
	const bool create_db = InitDatabaseAndStorage();
570
#endif
571

572
	glue_sticker_init();
573

Max Kellermann's avatar
Max Kellermann committed
574
	command_init();
575
	initAudioConfig();
576
	instance->partition->outputs.Configure(*instance->event_loop,
577
					       instance->partition->pc);
578
	client_manager_init();
579
	replay_gain_global_init();
580

581
	if (!input_stream_global_init(error)) {
582
		LogError(error);
583 584 585
		return EXIT_FAILURE;
	}

586
	playlist_list_global_init();
587

588
#ifndef ANDROID
589
	daemonize_commit();
590

591
	setup_log_output(options.log_stderr);
Warren Dukes's avatar
Warren Dukes committed
592

593
	SignalHandlersInit(*instance->event_loop);
594
#endif
595

596
	io_thread_start();
597

598 599 600 601 602 603
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr &&
	    !instance->neighbors->Open(error))
		FatalError(error);
#endif

604
	ZeroconfInit(*instance->event_loop);
605

606
	StartPlayerThread(instance->partition->pc);
607

608
#ifdef ENABLE_DATABASE
609
	if (create_db) {
610 611
		/* the database failed to load: recreate the
		   database */
612
		unsigned job = instance->update->Enqueue("", true);
613
		if (job == 0)
614
			FatalError("directory update failed");
615
	}
616
#endif
617

618
	if (!glue_state_file_init(error)) {
619
		LogError(error);
620 621
		return EXIT_FAILURE;
	}
622

623
	instance->partition->outputs.SetReplayGainMode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));
624

625
#ifdef ENABLE_DATABASE
626
	if (config_get_bool(CONF_AUTO_UPDATE, false)) {
627
#ifdef ENABLE_INOTIFY
628
		if (instance->storage != nullptr &&
629
		    instance->update != nullptr)
630 631 632
			mpd_inotify_init(*instance->event_loop,
					 *instance->storage,
					 *instance->update,
633
					 config_get_unsigned(CONF_AUTO_UPDATE_DEPTH,
634
							     INT_MAX));
635
#else
636 637
		FormatWarning(main_domain,
			      "inotify: auto_update was disabled. enable during compilation phase");
638
#endif
639
	}
640
#endif
641

642 643
	config_global_check();

644 645
	/* enable all audio outputs (if not already done by
	   playlist_state_restore() */
646
	instance->partition->pc.UpdateAudio();
647

648 649 650
#ifdef WIN32
	win32_app_started();
#endif
651

652 653 654 655
	/* 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);

656
	/* run the main loop */
657
	instance->event_loop->Run();
658

659 660 661 662
#ifdef WIN32
	win32_app_stopping();
#endif

663 664
	/* cleanup */

665
#if defined(ENABLE_DATABASE) && defined(ENABLE_INOTIFY)
666
	mpd_inotify_finish();
667 668 669

	if (instance->update != nullptr)
		instance->update->CancelAllAsync();
670
#endif
671

672 673 674 675 676
	if (state_file != nullptr) {
		state_file->Write();
		delete state_file;
	}

677
	instance->partition->pc.Kill();
678
	ZeroconfDeinit();
Max Kellermann's avatar
Max Kellermann committed
679
	listen_global_finish();
680
	delete instance->client_list;
681

682 683 684 685 686 687 688
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr) {
		instance->neighbors->Close();
		delete instance->neighbors;
	}
#endif

689
#ifdef ENABLE_DATABASE
690
	delete instance->update;
691 692 693 694 695

	if (instance->database != nullptr) {
		instance->database->Close();
		delete instance->database;
	}
696 697

	delete instance->storage;
698
#endif
699

700 701 702 703
#ifdef ENABLE_SQLITE
	sticker_global_finish();
#endif

704
	GlobalEvents::Deinitialize();
705

706
	playlist_list_global_finish();
707
	input_stream_global_finish();
708 709

#ifdef ENABLE_DATABASE
710
	mapper_finish();
711 712
#endif

713
	delete instance->partition;
Max Kellermann's avatar
Max Kellermann committed
714
	command_finish();
715
	decoder_plugin_deinit_all();
716
#ifdef ENABLE_ARCHIVE
717
	archive_plugin_deinit_all();
718
#endif
719
	config_global_finish();
720
	io_thread_deinit();
721
#ifndef ANDROID
722
	SignalHandlersFinish();
723
#endif
724
	delete instance->event_loop;
725
	delete instance;
726
	instance = nullptr;
727
#ifndef ANDROID
728
	daemonize_finish();
729
#endif
730 731 732
#ifdef WIN32
	WSACleanup();
#endif
733

734
	IcuFinish();
735

736
	log_deinit();
737
	return EXIT_SUCCESS;
Warren Dukes's avatar
Warren Dukes committed
738
}
Max Kellermann's avatar
Max Kellermann committed
739 740 741 742 743

#ifdef ANDROID

gcc_visibility_default
JNIEXPORT void JNICALL
744
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context)
Max Kellermann's avatar
Max Kellermann committed
745
{
746 747
	Java::Init(env);
	Java::File::Initialise(env);
748
	Environment::Initialise(env);
749

750 751
	context = new Context(env, _context);

Max Kellermann's avatar
Max Kellermann committed
752
	mpd_main(0, nullptr);
753

754
	delete context;
755
	Environment::Deinitialise(env);
Max Kellermann's avatar
Max Kellermann committed
756 757
}

758 759 760 761 762 763 764 765
gcc_visibility_default
JNIEXPORT void JNICALL
Java_org_musicpd_Bridge_shutdown(JNIEnv *, jclass)
{
	if (instance != nullptr)
		instance->event_loop->Break();
}

Max Kellermann's avatar
Max Kellermann committed
766
#endif