Main.cxx 16.5 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 "thread/Id.hxx"
58
#include "thread/Slack.hxx"
59
#include "lib/icu/Init.hxx"
60 61 62 63
#include "config/ConfigGlobal.hxx"
#include "config/ConfigData.hxx"
#include "config/ConfigDefaults.hxx"
#include "config/ConfigOption.hxx"
64
#include "config/ConfigError.hxx"
65
#include "Stats.hxx"
Max Kellermann's avatar
Max Kellermann committed
66

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

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

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

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

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

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

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

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

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

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

120 121
#include <limits.h>

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

125 126 127 128
#ifdef ANDROID
Context *context;
#endif

129
Instance *instance;
130

131 132
static StateFile *state_file;

133 134
#ifndef ANDROID

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

142 143
	daemonize_init(config_get_string(CONF_USER, nullptr),
		       config_get_string(CONF_GROUP, nullptr),
144
		       std::move(pid_file));
145 146 147

	if (options->kill)
		daemonize_kill();
148 149

	return true;
150 151
}

152 153
#endif

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

161 162 163
	mapper_init(std::move(playlist_dir));
	return true;
}
164

165 166
#ifdef ENABLE_DATABASE

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

	assert(!error.IsDefined());

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

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

201 202 203
	if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
		if (!InitStorage(error))
			FatalError(error);
204

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

220
	if (!instance->database->Open(error))
221
		FatalError(error);
222

223
	if (!instance->database->IsPlugin(simple_db_plugin))
224 225
		return true;

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

231
	/* run database update after daemonization? */
232
	return db.FileExists();
Warren Dukes's avatar
Warren Dukes committed
233
}
Warren Dukes's avatar
Warren Dukes committed
234

235 236 237 238 239 240 241
static bool
InitDatabaseAndStorage()
{
	const bool create_db = !glue_db_init_and_load();
	return create_db;
}

242 243
#endif

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

259
	if (!sticker_global_init(std::move(sticker_file), error))
260
		FatalError(error);
261 262 263
#endif
}

264
static bool
265
glue_state_file_init(Error &error)
266
{
267
	auto path_fs = config_get_path(CONF_STATE_FILE, error);
268 269 270 271 272 273 274 275 276 277 278 279 280 281
	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
	}
282

283 284 285 286
	unsigned interval = config_get_unsigned(CONF_STATE_FILE_INTERVAL,
						StateFile::DEFAULT_INTERVAL);

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

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

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

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

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

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

	buffer_size *= 1024;

335
	const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
Max Kellermann's avatar
Max Kellermann committed
336 337

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

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

355
	unsigned buffered_before_play = (perc / 100) * buffered_chunks;
Max Kellermann's avatar
Max Kellermann committed
356 357 358
	if (buffered_before_play > buffered_chunks)
		buffered_before_play = buffered_chunks;

359 360 361 362
	const unsigned max_length =
		config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
				    DEFAULT_PLAYLIST_MAX_LENGTH);

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

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

381 382
	if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
	    state_file != nullptr)
383
		state_file->CheckModified();
384 385
}

386 387
#ifdef WIN32

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

397 398
#endif

Max Kellermann's avatar
Max Kellermann committed
399 400
#ifndef ANDROID

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

Max Kellermann's avatar
Max Kellermann committed
410 411
#endif

412 413
static int mpd_main_after_fork(struct options);

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

422
#ifndef ANDROID
423 424
	daemonize_close_stdin();

425
#ifdef HAVE_LOCALE_H
426 427 428 429
	/* initialize locale */
	setlocale(LC_CTYPE,"");
#endif

430
#ifdef HAVE_GLIB
431 432
	g_set_application_name("Music Player Daemon");

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

440
	if (!IcuInit(error)) {
441 442 443 444
		LogError(error);
		return EXIT_FAILURE;
	}

445
	winsock_init();
446
	io_thread_init();
447
	config_global_init();
Warren Dukes's avatar
Warren Dukes committed
448

449 450 451
#ifdef ANDROID
	(void)argc;
	(void)argv;
452

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

471
	if (!glue_daemonize_init(&options, error)) {
472
		LogError(error);
473 474
		return EXIT_FAILURE;
	}
475
#endif
476

Max Kellermann's avatar
Max Kellermann committed
477
	stats_global_init();
478
	TagLoadConfig();
479

480
	if (!log_init(options.verbose, options.log_stderr, error)) {
481
		LogError(error);
482 483
		return EXIT_FAILURE;
	}
484

485
	instance = new Instance();
486
	instance->event_loop = new EventLoop();
487

488 489 490 491 492 493 494 495 496 497 498 499 500
#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

501
	const unsigned max_clients = config_get_positive(CONF_MAX_CONN, 10);
502
	instance->client_list = new ClientList(max_clients);
503

504 505
	initialize_decoder_and_player();

506 507
	if (!listen_global_init(*instance->event_loop, *instance->partition,
				error)) {
508
		LogError(error);
509 510
		return EXIT_FAILURE;
	}
511

512
#ifndef ANDROID
513
	daemonize_set_user();
514
	daemonize_begin(options.daemon);
515
#endif
Warren Dukes's avatar
Warren Dukes committed
516

517
#ifdef __BLOCKS__
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	/* 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;

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

544
	ConfigureFS();
545

546
	if (!glue_mapper_init(error)) {
547
		LogError(error);
548 549 550
		return EXIT_FAILURE;
	}

551
	initPermissions();
552
	playlist_global_init();
553
	spl_global_init();
554
#ifdef ENABLE_ARCHIVE
555
	archive_plugin_init_all();
556
#endif
557

558
	if (!pcm_convert_global_init(error)) {
559
		LogError(error);
560 561 562
		return EXIT_FAILURE;
	}

563
	decoder_plugin_init_all();
564

565
#ifdef ENABLE_DATABASE
566
	const bool create_db = InitDatabaseAndStorage();
567
#endif
568

569
	glue_sticker_init();
570

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

578
	if (!input_stream_global_init(error)) {
579
		LogError(error);
580 581 582
		return EXIT_FAILURE;
	}

583
	playlist_list_global_init();
584

585
#ifndef ANDROID
586
	daemonize_commit();
587

588
	setup_log_output(options.log_stderr);
Warren Dukes's avatar
Warren Dukes committed
589

590
	SignalHandlersInit(*instance->event_loop);
591
#endif
592

593
	io_thread_start();
594

595 596 597 598 599 600
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr &&
	    !instance->neighbors->Open(error))
		FatalError(error);
#endif

601
	ZeroconfInit(*instance->event_loop);
602

603
	StartPlayerThread(instance->partition->pc);
604

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

615
	if (!glue_state_file_init(error)) {
616
		LogError(error);
617 618
		return EXIT_FAILURE;
	}
619

620
	instance->partition->outputs.SetReplayGainMode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));
621

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

639 640
	config_global_check();

641 642
	/* enable all audio outputs (if not already done by
	   playlist_state_restore() */
643
	instance->partition->pc.UpdateAudio();
644

645 646 647
#ifdef WIN32
	win32_app_started();
#endif
648

649 650 651 652
	/* 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);

653
	/* run the main loop */
654
	instance->event_loop->Run();
655

656 657 658 659
#ifdef WIN32
	win32_app_stopping();
#endif

660 661
	/* cleanup */

662
#if defined(ENABLE_DATABASE) && defined(ENABLE_INOTIFY)
663
	mpd_inotify_finish();
664 665 666

	if (instance->update != nullptr)
		instance->update->CancelAllAsync();
667
#endif
668

669 670 671 672 673
	if (state_file != nullptr) {
		state_file->Write();
		delete state_file;
	}

674
	instance->partition->pc.Kill();
675
	ZeroconfDeinit();
Max Kellermann's avatar
Max Kellermann committed
676
	listen_global_finish();
677
	delete instance->client_list;
678

679 680 681 682 683 684 685
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr) {
		instance->neighbors->Close();
		delete instance->neighbors;
	}
#endif

686
#ifdef ENABLE_DATABASE
687
	delete instance->update;
688 689 690 691 692

	if (instance->database != nullptr) {
		instance->database->Close();
		delete instance->database;
	}
693 694

	delete instance->storage;
695
#endif
696

697 698 699 700
#ifdef ENABLE_SQLITE
	sticker_global_finish();
#endif

701
	GlobalEvents::Deinitialize();
702

703
	playlist_list_global_finish();
704
	input_stream_global_finish();
705 706

#ifdef ENABLE_DATABASE
707
	mapper_finish();
708 709
#endif

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

731
	IcuFinish();
732

733
	log_deinit();
734
	return EXIT_SUCCESS;
Warren Dukes's avatar
Warren Dukes committed
735
}
Max Kellermann's avatar
Max Kellermann committed
736 737 738 739 740

#ifdef ANDROID

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

747 748
	context = new Context(env, _context);

Max Kellermann's avatar
Max Kellermann committed
749
	mpd_main(0, nullptr);
750

751
	delete context;
752
	Environment::Deinitialise(env);
Max Kellermann's avatar
Max Kellermann committed
753 754
}

755 756 757 758 759 760 761 762
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
763
#endif