2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2008-12-31 16:46:41 +01:00
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
#include "Memento.hxx"
|
2014-01-27 08:20:25 +01:00
|
|
|
#include "output/MultipleOutputs.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2021-12-03 14:02:07 +01:00
|
|
|
#include "io/BufferedOutputStream.hxx"
|
2008-04-12 06:19:26 +02:00
|
|
|
|
2022-07-13 14:06:28 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2009-01-03 14:52:56 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2006-07-31 01:32:54 +02:00
|
|
|
#define SW_VOLUME_STATE "sw_volume: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
int
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::GetVolume(const MultipleOutputs &outputs) noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-28 21:12:15 +01:00
|
|
|
if (last_hardware_volume >= 0 &&
|
2016-12-27 22:47:20 +01:00
|
|
|
!hardware_volume_clock.CheckUpdate(std::chrono::seconds(1)))
|
2009-02-28 21:12:15 +01:00
|
|
|
/* throttle access to hardware mixers */
|
|
|
|
return last_hardware_volume;
|
|
|
|
|
2014-01-27 08:20:25 +01:00
|
|
|
last_hardware_volume = outputs.GetVolume();
|
2009-03-14 11:35:40 +01:00
|
|
|
return last_hardware_volume;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
inline bool
|
|
|
|
MixerMemento::SetSoftwareVolume(MultipleOutputs &outputs, unsigned volume)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-06 21:52:10 +02:00
|
|
|
assert(volume <= 100);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-07-06 21:51:24 +02:00
|
|
|
volume_software_set = volume;
|
2014-01-27 08:20:25 +01:00
|
|
|
outputs.SetSoftwareVolume(volume);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-03-14 11:10:21 +01:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
inline void
|
|
|
|
MixerMemento::SetHardwareVolume(MultipleOutputs &outputs, unsigned volume)
|
2008-12-31 16:46:41 +01:00
|
|
|
{
|
2009-02-28 21:12:15 +01:00
|
|
|
/* reset the cache */
|
|
|
|
last_hardware_volume = -1;
|
|
|
|
|
2022-07-08 10:46:15 +02:00
|
|
|
outputs.SetVolume(volume);
|
2008-12-31 16:46:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-08 10:46:15 +02:00
|
|
|
void
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::SetVolume(MultipleOutputs &outputs, unsigned volume)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-06 21:52:10 +02:00
|
|
|
assert(volume <= 100);
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
volume_software_set = volume;
|
|
|
|
|
2022-08-08 23:15:09 +02:00
|
|
|
SetHardwareVolume(outputs, volume);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-31 01:32:54 +02:00
|
|
|
|
2009-07-15 16:57:37 +02:00
|
|
|
bool
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::LoadSoftwareVolumeState(const char *line, MultipleOutputs &outputs)
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
char *end = nullptr;
|
2006-07-31 01:32:54 +02:00
|
|
|
long int sv;
|
|
|
|
|
2015-11-11 15:34:36 +01:00
|
|
|
line = StringAfterPrefix(line, SW_VOLUME_STATE);
|
|
|
|
if (line == nullptr)
|
2009-07-15 16:57:37 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
sv = strtol(line, &end, 10);
|
|
|
|
if (*end == 0 && sv >= 0 && sv <= 100)
|
2022-08-08 23:15:09 +02:00
|
|
|
SetSoftwareVolume(outputs, sv);
|
2022-08-08 23:13:03 +02:00
|
|
|
|
2009-07-15 16:57:37 +02:00
|
|
|
return true;
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 20:58:14 +02:00
|
|
|
void
|
2022-08-08 23:15:09 +02:00
|
|
|
MixerMemento::SaveSoftwareVolumeState(BufferedOutputStream &os) const
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
2022-07-13 12:54:58 +02:00
|
|
|
os.Fmt(FMT_STRING(SW_VOLUME_STATE "{}\n"), volume_software_set);
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|