Idle.cxx 1.85 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

20 21
#include "Client.hxx"
#include "Config.hxx"
22
#include "Response.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "Idle.hxx"
24 25 26

#include <assert.h>

27
static void
28
WriteIdleResponse(Response &r, unsigned flags) noexcept
29 30 31 32
{
	const char *const*idle_names = idle_get_names();
	for (unsigned i = 0; idle_names[i]; ++i) {
		if (flags & (1 << i))
33
			r.Format("changed: %s\n", idle_names[i]);
34 35
	}

36
	r.Write("OK\n");
37 38
}

39
void
40
Client::IdleNotify() noexcept
41
{
42 43
	assert(idle_waiting);
	assert(idle_flags != 0);
44

45
	unsigned flags = std::exchange(idle_flags, 0) & idle_subscriptions;
46
	idle_waiting = false;
47

48 49
	Response r(*this, 0);
	WriteIdleResponse(r, flags);
50

51
	timeout_event.Schedule(client_timeout);
52 53
}

54
void
55
Client::IdleAdd(unsigned flags) noexcept
56
{
57 58 59
	idle_flags |= flags;
	if (idle_waiting && (idle_flags & idle_subscriptions))
		IdleNotify();
60 61
}

62
bool
63
Client::IdleWait(unsigned flags) noexcept
64
{
65
	assert(!idle_waiting);
66

67 68
	idle_waiting = true;
	idle_subscriptions = flags;
69

70 71
	if (idle_flags & idle_subscriptions) {
		IdleNotify();
72
		return true;
73 74
	} else {
		/* disable timeouts while in "idle" */
75
		timeout_event.Cancel();
76
		return false;
77
	}
78
}