From 9f4b906e6cb61def8afddc2ed9bdf826e52e96d2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 7 Jan 2013 08:59:11 +0100 Subject: volume: convert to C++ --- src/Main.cxx | 2 +- src/OtherCommands.cxx | 2 +- src/PlayerCommands.cxx | 2 +- src/StateFile.cxx | 5 +- src/Volume.cxx | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Volume.hxx | 47 +++++++++++++++++ src/volume.c | 138 ----------------------------------------------- src/volume.h | 48 ----------------- 8 files changed, 192 insertions(+), 193 deletions(-) create mode 100644 src/Volume.cxx create mode 100644 src/Volume.hxx delete mode 100644 src/volume.c delete mode 100644 src/volume.h (limited to 'src') diff --git a/src/Main.cxx b/src/Main.cxx index 371602247..82816fcf9 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -34,6 +34,7 @@ #include "Client.hxx" #include "AllCommands.hxx" #include "Partition.hxx" +#include "Volume.hxx" extern "C" { #include "daemon.h" @@ -45,7 +46,6 @@ extern "C" { #include "sig_handlers.h" #include "audio_config.h" #include "output_all.h" -#include "volume.h" #include "log.h" #include "pcm_resample.h" #include "replay_gain_config.h" diff --git a/src/OtherCommands.cxx b/src/OtherCommands.cxx index 9de616759..d467c30ef 100644 --- a/src/OtherCommands.cxx +++ b/src/OtherCommands.cxx @@ -32,10 +32,10 @@ #include "protocol/ArgParser.hxx" #include "protocol/Result.hxx" #include "ls.hxx" +#include "Volume.hxx" extern "C" { #include "uri.h" -#include "volume.h" #include "stats.h" } diff --git a/src/PlayerCommands.cxx b/src/PlayerCommands.cxx index 2bd6fc4e6..5920863a6 100644 --- a/src/PlayerCommands.cxx +++ b/src/PlayerCommands.cxx @@ -24,12 +24,12 @@ #include "PlaylistPrint.hxx" #include "UpdateGlue.hxx" #include "ClientInternal.hxx" +#include "Volume.hxx" #include "protocol/Result.hxx" #include "protocol/ArgParser.hxx" extern "C" { #include "audio_format.h" -#include "volume.h" #include "replay_gain_config.h" #include "output_all.h" } diff --git a/src/StateFile.cxx b/src/StateFile.cxx index 1d77b77de..29a560c75 100644 --- a/src/StateFile.cxx +++ b/src/StateFile.cxx @@ -23,10 +23,7 @@ #include "PlaylistState.hxx" #include "TextFile.hxx" #include "Partition.hxx" - -extern "C" { -#include "volume.h" -} +#include "Volume.hxx" #include #include diff --git a/src/Volume.cxx b/src/Volume.cxx new file mode 100644 index 000000000..f6643a905 --- /dev/null +++ b/src/Volume.cxx @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + * + * 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. + */ + +#include "config.h" +#include "Volume.hxx" + +extern "C" { +#include "idle.h" +#include "mixer_all.h" +#include "event_pipe.h" +} + +#include + +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "volume" + +#define SW_VOLUME_STATE "sw_volume: " + +static unsigned volume_software_set = 100; + +/** 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; + +/** + * 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); +} + +void volume_finish(void) +{ + g_timer_destroy(hardware_volume_timer); +} + +void volume_init(void) +{ + hardware_volume_timer = g_timer_new(); + + event_pipe_register(PIPE_EVENT_MIXER, mixer_event_callback); +} + +int volume_level_get(void) +{ + 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; + + last_hardware_volume = mixer_all_get_volume(); + g_timer_start(hardware_volume_timer); + return last_hardware_volume; +} + +static bool software_volume_change(unsigned volume) +{ + assert(volume <= 100); + + volume_software_set = volume; + mixer_all_set_software_volume(volume); + + return true; +} + +static bool hardware_volume_change(unsigned volume) +{ + /* reset the cache */ + last_hardware_volume = -1; + + return mixer_all_set_volume(volume); +} + +bool volume_level_change(unsigned volume) +{ + assert(volume <= 100); + + volume_software_set = volume; + + idle_add(IDLE_MIXER); + + return hardware_volume_change(volume); +} + +bool +read_sw_volume_state(const char *line) +{ + char *end = NULL; + long int sv; + + 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; +} + +void save_sw_volume_state(FILE *fp) +{ + fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set); +} + +unsigned +sw_volume_state_get_hash(void) +{ + return volume_software_set; +} diff --git a/src/Volume.hxx b/src/Volume.hxx new file mode 100644 index 000000000..024b2840a --- /dev/null +++ b/src/Volume.hxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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. + * + * 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. + */ + +#ifndef MPD_VOLUME_HXX +#define MPD_VOLUME_HXX + +#include + +void volume_init(void); + +void volume_finish(void); + +int volume_level_get(void); + +bool volume_level_change(unsigned volume); + +bool +read_sw_volume_state(const char *line); + +void save_sw_volume_state(FILE *fp); + +/** + * Generates a hash number for the current state of the software + * volume control. This is used by timer_save_state_file() to + * determine whether the state has changed and the state file should + * be saved. + */ +unsigned +sw_volume_state_get_hash(void); + +#endif diff --git a/src/volume.c b/src/volume.c deleted file mode 100644 index 549feab9b..000000000 --- a/src/volume.c +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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. - * - * 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. - */ - -#include "config.h" -#include "volume.h" -#include "idle.h" -#include "mixer_all.h" -#include "event_pipe.h" - -#include - -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "volume" - -#define SW_VOLUME_STATE "sw_volume: " - -static unsigned volume_software_set = 100; - -/** 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; - -/** - * 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); -} - -void volume_finish(void) -{ - g_timer_destroy(hardware_volume_timer); -} - -void volume_init(void) -{ - hardware_volume_timer = g_timer_new(); - - event_pipe_register(PIPE_EVENT_MIXER, mixer_event_callback); -} - -int volume_level_get(void) -{ - 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; - - last_hardware_volume = mixer_all_get_volume(); - g_timer_start(hardware_volume_timer); - return last_hardware_volume; -} - -static bool software_volume_change(unsigned volume) -{ - assert(volume <= 100); - - volume_software_set = volume; - mixer_all_set_software_volume(volume); - - return true; -} - -static bool hardware_volume_change(unsigned volume) -{ - /* reset the cache */ - last_hardware_volume = -1; - - return mixer_all_set_volume(volume); -} - -bool volume_level_change(unsigned volume) -{ - assert(volume <= 100); - - volume_software_set = volume; - - idle_add(IDLE_MIXER); - - return hardware_volume_change(volume); -} - -bool -read_sw_volume_state(const char *line) -{ - char *end = NULL; - long int sv; - - 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; -} - -void save_sw_volume_state(FILE *fp) -{ - fprintf(fp, SW_VOLUME_STATE "%u\n", volume_software_set); -} - -unsigned -sw_volume_state_get_hash(void) -{ - return volume_software_set; -} diff --git a/src/volume.h b/src/volume.h deleted file mode 100644 index b08899a84..000000000 --- a/src/volume.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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. - * - * 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. - */ - -#ifndef MPD_VOLUME_H -#define MPD_VOLUME_H - -#include -#include - -void volume_init(void); - -void volume_finish(void); - -int volume_level_get(void); - -bool volume_level_change(unsigned volume); - -bool -read_sw_volume_state(const char *line); - -void save_sw_volume_state(FILE *fp); - -/** - * Generates a hash number for the current state of the software - * volume control. This is used by timer_save_state_file() to - * determine whether the state has changed and the state file should - * be saved. - */ -unsigned -sw_volume_state_get_hash(void); - -#endif -- cgit v1.2.3