configure.ac: Don't allow UNIX IPC to be configured for a native Windows build.
[mpd-mk.git] / src / volume.c
blobd7b72dd5611211c279bd8ada7f4801eac0c4960e
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "volume.h"
22 #include "conf.h"
23 #include "player_control.h"
24 #include "idle.h"
25 #include "pcm_volume.h"
26 #include "output_all.h"
27 #include "mixer_control.h"
28 #include "mixer_all.h"
29 #include "mixer_type.h"
30 #include "event_pipe.h"
32 #include <glib.h>
34 #include <assert.h>
35 #include <math.h>
36 #include <string.h>
37 #include <stdlib.h>
39 #undef G_LOG_DOMAIN
40 #define G_LOG_DOMAIN "volume"
42 #define SW_VOLUME_STATE "sw_volume: "
44 static unsigned volume_software_set = 100;
46 /** the cached hardware mixer value; invalid if negative */
47 static int last_hardware_volume = -1;
48 /** the age of #last_hardware_volume */
49 static GTimer *hardware_volume_timer;
51 /**
52 * Handler for #PIPE_EVENT_MIXER.
54 static void
55 mixer_event_callback(void)
57 /* flush the hardware volume cache */
58 last_hardware_volume = -1;
60 /* notify clients */
61 idle_add(IDLE_MIXER);
64 void volume_finish(void)
66 g_timer_destroy(hardware_volume_timer);
69 void volume_init(void)
71 hardware_volume_timer = g_timer_new();
73 event_pipe_register(PIPE_EVENT_MIXER, mixer_event_callback);
76 int volume_level_get(void)
78 assert(hardware_volume_timer != NULL);
80 if (last_hardware_volume >= 0 &&
81 g_timer_elapsed(hardware_volume_timer, NULL) < 1.0)
82 /* throttle access to hardware mixers */
83 return last_hardware_volume;
85 last_hardware_volume = mixer_all_get_volume();
86 g_timer_start(hardware_volume_timer);
87 return last_hardware_volume;
90 static bool software_volume_change(unsigned volume)
92 assert(volume <= 100);
94 volume_software_set = volume;
95 mixer_all_set_software_volume(volume);
97 return true;
100 static bool hardware_volume_change(unsigned volume)
102 /* reset the cache */
103 last_hardware_volume = -1;
105 return mixer_all_set_volume(volume);
108 bool volume_level_change(unsigned volume)
110 assert(volume <= 100);
112 volume_software_set = volume;
114 idle_add(IDLE_MIXER);
116 return hardware_volume_change(volume);
119 bool
120 read_sw_volume_state(const char *line)
122 char *end = NULL;
123 long int sv;
125 if (!g_str_has_prefix(line, SW_VOLUME_STATE))
126 return false;
128 line += sizeof(SW_VOLUME_STATE) - 1;
129 sv = strtol(line, &end, 10);
130 if (*end == 0 && sv >= 0 && sv <= 100)
131 software_volume_change(sv);
132 else
133 g_warning("Can't parse software volume: %s\n", line);
134 return true;
137 void save_sw_volume_state(FILE *fp)
139 fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set);
142 unsigned
143 sw_volume_state_get_hash(void)
145 return volume_software_set;