OutputCommand.cxx 2.45 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
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 25 26
 */

/*
 * Glue functions for controlling the audio outputs over the MPD
 * protocol.  These functions perform extra validation on all
 * parameters, because they might be from an untrusted source.
 *
 */

27
#include "OutputCommand.hxx"
28
#include "MultipleOutputs.hxx"
29
#include "Client.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "mixer/MixerControl.hxx"
31
#include "mixer/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "Idle.hxx"
33

34 35
extern unsigned audio_output_state_version;

36
bool
37
audio_output_enable_index(MultipleOutputs &outputs, unsigned idx)
38
{
39
	if (idx >= outputs.Size())
40 41
		return false;

42 43
	auto &ao = outputs.Get(idx);
	if (!ao.LockSetEnabled(true))
44
		return true;
45 46 47

	idle_add(IDLE_OUTPUT);

48
	if (ao.GetMixer() != nullptr) {
49 50 51 52
		InvalidateHardwareVolume();
		idle_add(IDLE_MIXER);
	}

53
	ao.GetClient().ApplyEnabled();
54

55 56
	++audio_output_state_version;

57 58 59 60
	return true;
}

bool
61
audio_output_disable_index(MultipleOutputs &outputs, unsigned idx)
62
{
63
	if (idx >= outputs.Size())
64 65
		return false;

66 67
	auto &ao = outputs.Get(idx);
	if (!ao.LockSetEnabled(false))
68
		return true;
69 70 71

	idle_add(IDLE_OUTPUT);

72
	auto *mixer = ao.GetMixer();
73
	if (mixer != nullptr) {
74
		mixer_close(mixer);
75
		InvalidateHardwareVolume();
76 77 78
		idle_add(IDLE_MIXER);
	}

79
	ao.GetClient().ApplyEnabled();
80

81 82
	++audio_output_state_version;

83 84
	return true;
}
85 86

bool
87
audio_output_toggle_index(MultipleOutputs &outputs, unsigned idx)
88
{
89
	if (idx >= outputs.Size())
90 91
		return false;

92 93
	auto &ao = outputs.Get(idx);
	const bool enabled = ao.LockToggleEnabled();
94 95 96
	idle_add(IDLE_OUTPUT);

	if (!enabled) {
97
		auto *mixer = ao.GetMixer();
98 99
		if (mixer != nullptr) {
			mixer_close(mixer);
100
			InvalidateHardwareVolume();
101 102 103 104
			idle_add(IDLE_MIXER);
		}
	}

105
	ao.GetClient().ApplyEnabled();
106 107 108 109 110

	++audio_output_state_version;

	return true;
}