2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
2008-12-31 16:46:41 +01:00
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-01-03 14:52:56 +01:00
|
|
|
#include "volume.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2009-01-07 18:05:38 +01:00
|
|
|
#include "pcm_volume.h"
|
2009-02-10 18:51:49 +01:00
|
|
|
#include "output_all.h"
|
2009-03-14 11:36:50 +01:00
|
|
|
#include "mixer_control.h"
|
2009-03-14 11:35:40 +01:00
|
|
|
#include "mixer_all.h"
|
2009-07-06 07:37:37 +02:00
|
|
|
#include "mixer_type.h"
|
2009-10-21 09:48:37 +02:00
|
|
|
#include "event_pipe.h"
|
2008-04-12 06:19:26 +02:00
|
|
|
|
2008-10-28 20:33:56 +01:00
|
|
|
#include <glib.h>
|
2009-01-03 14:52:56 +01:00
|
|
|
|
2009-02-28 21:12:15 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2009-01-03 14:52:56 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2008-12-29 17:29:18 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "volume"
|
|
|
|
|
2006-07-31 01:32:54 +02:00
|
|
|
#define SW_VOLUME_STATE "sw_volume: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-07-06 21:52:10 +02:00
|
|
|
static unsigned volume_software_set = 100;
|
2006-07-16 18:53:04 +02:00
|
|
|
|
2009-02-28 21:12:15 +01:00
|
|
|
/** the cached hardware mixer value; invalid if negative */
|
|
|
|
static int last_hardware_volume = -1;
|
|
|
|
/** the age of #last_hardware_volume */
|
|
|
|
static GTimer *hardware_volume_timer;
|
|
|
|
|
2009-10-21 09:48:37 +02:00
|
|
|
/**
|
|
|
|
* Handler for #PIPE_EVENT_MIXER.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mixer_event_callback(void)
|
|
|
|
{
|
|
|
|
/* flush the hardware volume cache */
|
|
|
|
last_hardware_volume = -1;
|
|
|
|
|
|
|
|
/* notify clients */
|
|
|
|
idle_add(IDLE_MIXER);
|
|
|
|
}
|
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
void volume_finish(void)
|
2008-11-14 18:05:13 +01:00
|
|
|
{
|
2009-07-06 22:00:50 +02:00
|
|
|
g_timer_destroy(hardware_volume_timer);
|
2008-11-14 18:05:13 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 16:46:41 +01:00
|
|
|
void volume_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-07-06 22:00:50 +02:00
|
|
|
hardware_volume_timer = g_timer_new();
|
2009-10-21 09:48:37 +02:00
|
|
|
|
|
|
|
event_pipe_register(PIPE_EVENT_MIXER, mixer_event_callback);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
int volume_level_get(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-28 21:12:15 +01:00
|
|
|
assert(hardware_volume_timer != NULL);
|
|
|
|
|
|
|
|
if (last_hardware_volume >= 0 &&
|
|
|
|
g_timer_elapsed(hardware_volume_timer, NULL) < 1.0)
|
|
|
|
/* throttle access to hardware mixers */
|
|
|
|
return last_hardware_volume;
|
|
|
|
|
2009-03-14 11:35:40 +01:00
|
|
|
last_hardware_volume = mixer_all_get_volume();
|
|
|
|
g_timer_start(hardware_volume_timer);
|
|
|
|
return last_hardware_volume;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-07-06 21:52:10 +02:00
|
|
|
static bool software_volume_change(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;
|
2009-07-06 22:00:50 +02:00
|
|
|
mixer_all_set_software_volume(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
|
|
|
}
|
|
|
|
|
2009-07-06 21:52:10 +02:00
|
|
|
static bool hardware_volume_change(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;
|
|
|
|
|
2009-07-06 21:51:24 +02:00
|
|
|
return mixer_all_set_volume(volume);
|
2008-12-31 16:46:41 +01:00
|
|
|
}
|
|
|
|
|
2009-07-06 21:52:10 +02:00
|
|
|
bool volume_level_change(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;
|
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_MIXER);
|
|
|
|
|
2009-07-06 22:00:50 +02:00
|
|
|
return hardware_volume_change(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
|
|
|
|
read_sw_volume_state(const char *line)
|
2006-07-31 01:32:54 +02:00
|
|
|
{
|
|
|
|
char *end = NULL;
|
|
|
|
long int sv;
|
|
|
|
|
2009-07-15 16:57:37 +02:00
|
|
|
if (!g_str_has_prefix(line, SW_VOLUME_STATE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
line += sizeof(SW_VOLUME_STATE) - 1;
|
|
|
|
sv = strtol(line, &end, 10);
|
|
|
|
if (*end == 0 && sv >= 0 && sv <= 100)
|
|
|
|
software_volume_change(sv);
|
|
|
|
else
|
|
|
|
g_warning("Can't parse software volume: %s\n", line);
|
|
|
|
return true;
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void save_sw_volume_state(FILE *fp)
|
|
|
|
{
|
2009-07-06 22:00:50 +02:00
|
|
|
fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set);
|
2006-07-31 01:32:54 +02:00
|
|
|
}
|
2009-10-08 15:22:39 +02:00
|
|
|
|
|
|
|
unsigned
|
|
|
|
sw_volume_state_get_hash(void)
|
|
|
|
{
|
|
|
|
return volume_software_set;
|
|
|
|
}
|