2009-05-13 20:15:38 -04:00
/*
2009-10-14 12:10:01 -04:00
Copyright ( C ) 1999 - 2006 Paul Davis
2009-05-13 20:15:38 -04: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 .
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 . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include <unistd.h>
# include <cstdio> /* for snprintf, grrr */
2009-10-14 12:10:01 -04:00
# include <glib.h>
2009-05-13 20:15:38 -04:00
# include <glib/gstdio.h> /* for g_stat() */
# include <glibmm/miscutils.h>
# include "pbd/xml++.h"
# include "pbd/file_utils.h"
2013-08-13 21:59:05 -04:00
# include "ardour/audioengine.h"
2009-05-13 20:15:38 -04:00
# include "ardour/control_protocol_manager.h"
2012-05-24 02:09:29 -04:00
# include "ardour/diskstream.h"
2009-05-13 20:15:38 -04:00
# include "ardour/filesystem_paths.h"
2013-08-13 21:59:05 -04:00
# include "ardour/port.h"
2012-05-24 02:09:29 -04:00
# include "ardour/rc_configuration.h"
2012-04-18 13:46:29 -04:00
# include "ardour/session_metadata.h"
2009-05-13 20:15:38 -04:00
# include "i18n.h"
using namespace ARDOUR ;
using namespace std ;
using namespace PBD ;
/* this is global so that we do not have to indirect through an object pointer
to reference it .
*/
namespace ARDOUR {
float speed_quietning = 0.251189 ; // -12dB reduction for ffwd or rewind
}
2014-09-04 12:31:30 -04:00
static const char * user_config_file_name = " config " ;
static const char * system_config_file_name = " system_config " ;
2009-05-13 20:15:38 -04:00
RCConfiguration : : RCConfiguration ( )
:
/* construct variables */
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-05-13 20:15:38 -04:00
# define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
# define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
# include "ardour/rc_configuration_vars.h"
# undef CONFIG_VARIABLE
# undef CONFIG_VARIABLE_SPECIAL
_control_protocol_state ( 0 )
{
}
2010-02-23 15:25:53 -05:00
RCConfiguration : : ~ RCConfiguration ( )
{
delete _control_protocol_state ;
}
2009-05-13 20:15:38 -04:00
int
RCConfiguration : : load_state ( )
{
2012-06-23 01:06:54 -04:00
std : : string rcfile ;
2013-08-04 10:36:07 -04:00
GStatBuf statbuf ;
2009-05-13 20:15:38 -04:00
/* load system configuration first */
2009-10-14 12:10:01 -04:00
2014-09-04 12:31:30 -04:00
if ( find_file ( ardour_config_search_path ( ) , system_config_file_name , rcfile ) ) {
2009-05-13 20:15:38 -04:00
/* stupid XML Parser hates empty files */
2009-10-14 12:10:01 -04:00
2009-05-13 20:15:38 -04:00
if ( g_stat ( rcfile . c_str ( ) , & statbuf ) ) {
return - 1 ;
}
if ( statbuf . st_size ! = 0 ) {
2009-10-22 16:05:40 -04:00
info < < string_compose ( _ ( " Loading system configuration file %1 " ) , rcfile ) < < endl ;
2009-10-14 12:10:01 -04:00
2010-02-23 15:25:53 -05:00
XMLTree tree ;
2009-05-13 20:15:38 -04:00
if ( ! tree . read ( rcfile . c_str ( ) ) ) {
2010-03-13 14:22:34 -05:00
error < < string_compose ( _ ( " %1: cannot read system configuration file \" %2 \" " ) , PROGRAM_NAME , rcfile ) < < endmsg ;
2009-05-13 20:15:38 -04:00
return - 1 ;
}
2009-10-14 12:10:01 -04:00
2009-10-15 14:56:11 -04:00
if ( set_state ( * tree . root ( ) , Stateful : : current_state_version ) ) {
2010-03-13 14:22:34 -05:00
error < < string_compose ( _ ( " %1: system configuration file \" %2 \" not loaded successfully. " ) , PROGRAM_NAME , rcfile ) < < endmsg ;
2009-05-13 20:15:38 -04:00
return - 1 ;
}
} else {
2013-01-09 10:05:04 -05:00
error < < string_compose ( _ ( " Your system %1 configuration file is empty. This probably means that there was an error installing %1 " ) , PROGRAM_NAME ) < < endmsg ;
2009-05-13 20:15:38 -04:00
}
}
/* now load configuration file for user */
2014-09-04 12:31:30 -04:00
if ( find_file ( ardour_config_search_path ( ) , user_config_file_name , rcfile ) ) {
2009-05-13 20:15:38 -04:00
/* stupid XML parser hates empty files */
if ( g_stat ( rcfile . c_str ( ) , & statbuf ) ) {
return - 1 ;
}
if ( statbuf . st_size ! = 0 ) {
2009-10-22 16:05:40 -04:00
info < < string_compose ( _ ( " Loading user configuration file %1 " ) , rcfile ) < < endl ;
2009-10-14 12:10:01 -04:00
2010-02-23 15:25:53 -05:00
XMLTree tree ;
2009-05-13 20:15:38 -04:00
if ( ! tree . read ( rcfile ) ) {
2010-03-13 14:22:34 -05:00
error < < string_compose ( _ ( " %1: cannot read configuration file \" %2 \" " ) , PROGRAM_NAME , rcfile ) < < endmsg ;
2009-05-13 20:15:38 -04:00
return - 1 ;
}
2009-10-14 12:10:01 -04:00
2009-10-15 14:56:11 -04:00
if ( set_state ( * tree . root ( ) , Stateful : : current_state_version ) ) {
2010-03-13 14:22:34 -05:00
error < < string_compose ( _ ( " %1: user configuration file \" %2 \" not loaded successfully. " ) , PROGRAM_NAME , rcfile ) < < endmsg ;
2009-05-13 20:15:38 -04:00
return - 1 ;
}
} else {
2010-03-13 14:22:34 -05:00
warning < < string_compose ( _ ( " your %1 configuration file is empty. This is not normal. " ) , PROGRAM_NAME ) < < endmsg ;
2009-10-14 12:10:01 -04:00
}
2009-05-13 20:15:38 -04:00
}
return 0 ;
}
int
RCConfiguration : : save_state ( )
{
2014-09-04 12:31:30 -04:00
const std : : string rcfile = Glib : : build_filename ( user_config_directory ( ) , user_config_file_name ) ;
2009-05-13 20:15:38 -04:00
// this test seems bogus?
2010-03-30 11:18:43 -04:00
if ( ! rcfile . empty ( ) ) {
2010-02-23 15:25:53 -05:00
XMLTree tree ;
2009-05-13 20:15:38 -04:00
tree . set_root ( & get_state ( ) ) ;
if ( ! tree . write ( rcfile . c_str ( ) ) ) {
error < < string_compose ( _ ( " Config file %1 not saved " ) , rcfile ) < < endmsg ;
return - 1 ;
}
}
return 0 ;
}
void
RCConfiguration : : add_instant_xml ( XMLNode & node )
{
Stateful : : add_instant_xml ( node , user_config_directory ( ) ) ;
}
XMLNode *
RCConfiguration : : instant_xml ( const string & node_name )
{
return Stateful : : instant_xml ( node_name , user_config_directory ( ) ) ;
}
XMLNode &
RCConfiguration : : get_state ( )
{
XMLNode * root ;
2015-01-19 08:29:25 -05:00
LocaleGuard lg ( X_ ( " C " ) ) ;
2009-05-13 20:15:38 -04:00
root = new XMLNode ( " Ardour " ) ;
root - > add_child_nocopy ( get_variables ( ) ) ;
2009-10-14 12:10:01 -04:00
2012-04-18 13:46:29 -04:00
root - > add_child_nocopy ( SessionMetadata : : Metadata ( ) - > get_user_state ( ) ) ;
2009-05-13 20:15:38 -04:00
if ( _extra_xml ) {
root - > add_child_copy ( * _extra_xml ) ;
}
2009-10-14 12:10:01 -04:00
2009-05-13 20:15:38 -04:00
root - > add_child_nocopy ( ControlProtocolManager : : instance ( ) . get_state ( ) ) ;
2009-10-14 12:10:01 -04:00
2009-05-13 20:15:38 -04:00
return * root ;
}
XMLNode &
RCConfiguration : : get_variables ( )
{
XMLNode * node ;
2015-01-19 08:29:25 -05:00
LocaleGuard lg ( X_ ( " C " ) ) ;
2009-05-13 20:15:38 -04:00
node = new XMLNode ( " Config " ) ;
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-05-13 20:15:38 -04:00
# define CONFIG_VARIABLE(type,var,Name,value) \
var . add_to_node ( * node ) ;
# define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
var . add_to_node ( * node ) ;
# include "ardour/rc_configuration_vars.h"
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-05-13 20:15:38 -04:00
return * node ;
}
int
2012-04-18 13:46:29 -04:00
RCConfiguration : : set_state ( const XMLNode & root , int version )
2009-05-13 20:15:38 -04:00
{
if ( root . name ( ) ! = " Ardour " ) {
return - 1 ;
}
XMLNodeList nlist = root . children ( ) ;
XMLNodeConstIterator niter ;
XMLNode * node ;
2011-06-11 11:35:34 -04:00
Stateful : : save_extra_xml ( root ) ;
2009-05-13 20:15:38 -04:00
for ( niter = nlist . begin ( ) ; niter ! = nlist . end ( ) ; + + niter ) {
node = * niter ;
2010-07-06 20:40:58 -04:00
if ( node - > name ( ) = = " Config " ) {
2009-05-13 20:15:38 -04:00
set_variables ( * node ) ;
2012-04-18 13:46:29 -04:00
} else if ( node - > name ( ) = = " Metadata " ) {
SessionMetadata : : Metadata ( ) - > set_state ( * node , version ) ;
2009-05-13 20:15:38 -04:00
} else if ( node - > name ( ) = = ControlProtocolManager : : state_node_name ) {
_control_protocol_state = new XMLNode ( * node ) ;
}
}
2015-01-22 12:52:06 -05:00
Diskstream : : set_disk_read_chunk_frames ( minimum_disk_read_bytes . get ( ) / sizeof ( Sample ) ) ;
Diskstream : : set_disk_write_chunk_frames ( minimum_disk_write_bytes . get ( ) / sizeof ( Sample ) ) ;
2009-05-13 20:15:38 -04:00
return 0 ;
}
void
RCConfiguration : : set_variables ( const XMLNode & node )
{
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-05-13 20:15:38 -04:00
# define CONFIG_VARIABLE(type,var,name,value) \
if ( var . set_from_node ( node ) ) { \
ParameterChanged ( name ) ; \
}
# define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
if ( var . set_from_node ( node ) ) { \
ParameterChanged ( name ) ; \
}
# include "ardour/rc_configuration_vars.h"
# undef CONFIG_VARIABLE
# undef CONFIG_VARIABLE_SPECIAL
2009-10-14 12:10:01 -04:00
2009-05-13 20:15:38 -04:00
}
void
2009-12-12 10:02:15 -05:00
RCConfiguration : : map_parameters ( boost : : function < void ( std : : string ) > & functor )
2009-05-13 20:15:38 -04:00
{
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-12-12 10:02:15 -05:00
# define CONFIG_VARIABLE(type,var,name,value) functor (name);
# define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
2009-05-13 20:15:38 -04:00
# include "ardour/rc_configuration_vars.h"
# undef CONFIG_VARIABLE
2009-10-14 12:10:01 -04:00
# undef CONFIG_VARIABLE_SPECIAL
2009-05-13 20:15:38 -04:00
}
2015-01-22 12:52:06 -05:00