Idle.cxx 1.74 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
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.
18 19 20 21 22 23 24
 */

/*
 * Support library for the "idle" command.
 *
 */

25
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Idle.hxx"
27
#include "GlobalEvents.hxx"
28
#include "util/ASCII.hxx"
29

30 31
#include <atomic>

32 33
#include <assert.h>

34
static std::atomic_uint idle_flags;
35

36 37 38 39 40 41 42 43
static const char *const idle_names[] = {
	"database",
	"stored_playlist",
	"playlist",
	"player",
	"mixer",
	"output",
	"options",
44
	"sticker",
45
	"update",
46 47
	"subscription",
	"message",
48
	"neighbor",
49
	"mount",
50
	nullptr
51 52
};

53 54 55 56 57
void
idle_add(unsigned flags)
{
	assert(flags != 0);

58
	unsigned old_flags = idle_flags.fetch_or(flags);
59

60 61
	if ((old_flags & flags) != flags)
		GlobalEvents::Emit(GlobalEvents::IDLE);
62 63 64 65 66
}

unsigned
idle_get(void)
{
67
	return idle_flags.exchange(0);
68
}
69 70 71 72 73 74

const char*const*
idle_get_names(void)
{
        return idle_names;
}
75 76 77 78 79 80 81 82 83 84 85 86

unsigned
idle_parse_name(const char *name)
{
	assert(name != nullptr);

	for (unsigned i = 0; idle_names[i] != nullptr; ++i)
		if (StringEqualsCaseASCII(name, idle_names[i]))
			return 1 << i;

	return 0;
}