ClientIdle.cxx 1.87 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.
 */

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

#include <assert.h>

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

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

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

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

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

50
	timeout_event.Schedule(client_timeout);
51 52
}

53
void
54
Client::IdleAdd(unsigned flags) noexcept
55
{
56
	if (IsExpired())
57 58
		return;

59 60 61
	idle_flags |= flags;
	if (idle_waiting && (idle_flags & idle_subscriptions))
		IdleNotify();
62 63
}

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

69 70
	idle_waiting = true;
	idle_subscriptions = flags;
71

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