Volume.cxx 3.11 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 "Volume.hxx"
22
#include "output/MultipleOutputs.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "Idle.hxx"
24
#include "GlobalEvents.hxx"
25
#include "util/StringUtil.hxx"
26
#include "util/Domain.hxx"
27
#include "system/PeriodClock.hxx"
28
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
29

30
#include <assert.h>
31
#include <stdlib.h>
Max Kellermann's avatar
Max Kellermann committed
32

33
#define SW_VOLUME_STATE                         "sw_volume: "
Warren Dukes's avatar
Warren Dukes committed
34

35 36
static constexpr Domain volume_domain("volume");

37
static unsigned volume_software_set = 100;
38

39 40 41
/** the cached hardware mixer value; invalid if negative */
static int last_hardware_volume = -1;
/** the age of #last_hardware_volume */
42
static PeriodClock hardware_volume_clock;
43

44
/**
45
 * Handler for #GlobalEvents::MIXER.
46 47 48 49 50 51 52 53 54 55 56
 */
static void
mixer_event_callback(void)
{
	/* flush the hardware volume cache */
	last_hardware_volume = -1;

	/* notify clients */
	idle_add(IDLE_MIXER);
}

57
void volume_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
58
{
59
	GlobalEvents::Register(GlobalEvents::MIXER, mixer_event_callback);
Warren Dukes's avatar
Warren Dukes committed
60 61
}

62 63
int
volume_level_get(const MultipleOutputs &outputs)
Avuton Olrich's avatar
Avuton Olrich committed
64
{
65
	if (last_hardware_volume >= 0 &&
66
	    !hardware_volume_clock.CheckUpdate(1000))
67 68 69
		/* throttle access to hardware mixers */
		return last_hardware_volume;

70
	last_hardware_volume = outputs.GetVolume();
71
	return last_hardware_volume;
Warren Dukes's avatar
Warren Dukes committed
72 73
}

74 75
static bool
software_volume_change(MultipleOutputs &outputs, unsigned volume)
Avuton Olrich's avatar
Avuton Olrich committed
76
{
77
	assert(volume <= 100);
Warren Dukes's avatar
Warren Dukes committed
78

79
	volume_software_set = volume;
80
	outputs.SetSoftwareVolume(volume);
Warren Dukes's avatar
Warren Dukes committed
81

82
	return true;
Warren Dukes's avatar
Warren Dukes committed
83 84
}

85 86
static bool
hardware_volume_change(MultipleOutputs &outputs, unsigned volume)
87
{
88 89 90
	/* reset the cache */
	last_hardware_volume = -1;

91
	return outputs.SetVolume(volume);
92 93
}

94 95
bool
volume_level_change(MultipleOutputs &outputs, unsigned volume)
Avuton Olrich's avatar
Avuton Olrich committed
96
{
97 98
	assert(volume <= 100);

99 100
	volume_software_set = volume;

101 102
	idle_add(IDLE_MIXER);

103
	return hardware_volume_change(outputs, volume);
Warren Dukes's avatar
Warren Dukes committed
104
}
105

106
bool
107
read_sw_volume_state(const char *line, MultipleOutputs &outputs)
108
{
109
	char *end = nullptr;
110 111
	long int sv;

112
	if (!StringStartsWith(line, SW_VOLUME_STATE))
113 114 115 116 117
		return false;

	line += sizeof(SW_VOLUME_STATE) - 1;
	sv = strtol(line, &end, 10);
	if (*end == 0 && sv >= 0 && sv <= 100)
118
		software_volume_change(outputs, sv);
119
	else
120 121
		FormatWarning(volume_domain,
			      "Can't parse software volume: %s", line);
122
	return true;
123 124 125 126
}

void save_sw_volume_state(FILE *fp)
{
127
	fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set);
128
}
129 130 131 132 133 134

unsigned
sw_volume_state_get_hash(void)
{
	return volume_software_set;
}