non/Mixer/Module.C

657 lines
15 KiB
C++
Raw Normal View History

2009-12-25 01:59:39 +01:00
/*******************************************************************************/
/* Copyright (C) 2009 Jonathan Moore Liles */
/* */
/* 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; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
#include "Module.H"
#include <FL/fl_draw.H>
2010-01-28 05:39:27 +01:00
#include <FL/fl_ask.H>
2009-12-25 01:59:39 +01:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2009-12-25 01:59:39 +01:00
#include "Module_Parameter_Editor.H"
#include "Chain.H"
2009-12-25 01:59:39 +01:00
#include "JACK_Module.H"
#include "Gain_Module.H"
#include "Mono_Pan_Module.H"
#include "Meter_Module.H"
#include "Plugin_Module.H"
2010-01-28 05:39:27 +01:00
#include <FL/Fl_Menu_Button.H>
#include "FL/test_press.H"
#include "FL/menu_popup.H"
2009-12-25 01:59:39 +01:00
Module *Module::_copied_module_empty = 0;
char *Module::_copied_module_settings = 0;
Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
{
init();
log_create();
}
Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
{
this->is_default( is_default );
init();
log_create();
}
Module::Module ( ) : Fl_Group( 0, 0, 0, 50, "Unnamed" )
{
init();
log_create();
}
2009-12-25 01:59:39 +01:00
Module::~Module ( )
{
for ( unsigned int i = 0; i < audio_input.size(); ++i )
audio_input[i].disconnect();
for ( unsigned int i = 0; i < audio_output.size(); ++i )
audio_output[i].disconnect();
for ( unsigned int i = 0; i < control_input.size(); ++i )
control_input[i].disconnect();
for ( unsigned int i = 0; i < control_output.size(); ++i )
control_output[i].disconnect();
audio_input.clear();
audio_output.clear();
control_input.clear();
control_output.clear();
}
void
Module::init ( void )
{
_is_default = false;
_editor = 0;
_chain = 0;
_instances = 1;
2010-01-28 05:39:27 +01:00
_bypass = 0;
2010-02-01 01:25:14 +01:00
box( FL_UP_BOX );
labeltype( FL_NO_LABEL );
clip_children( 1 );
2010-02-01 01:25:14 +01:00
set_visible_focus();
selection_color( FL_RED );
}
void
Module::get ( Log_Entry &e ) const
{
// e.add( ":name", label() );
// e.add( ":color", (unsigned long)color());
{
char *s = get_parameters();
if ( strlen( s ) )
e.add( ":parameter_values", s );
delete[] s;
}
e.add( ":is_default", is_default() );
e.add( ":chain", chain() );
e.add( ":active", ! bypass() );
}
void
Module::copy ( void ) const
{
Module *m = clone_empty();
if ( ! m )
{
DMESSAGE( "Module \"%s\" doesn't support cloning", name() );
}
Log_Entry *ne = new Log_Entry();
_copied_module_empty = m;
{
Log_Entry e;
get( e );
for ( int i = 0; i < e.size(); ++i )
{
const char *s, *v;
e.get( i, &s, &v );
/* we don't want this module to get added to the current
chain... */
if ( !( !strcmp( s, ":chain" ) ||
!strcmp( s, ":is_default" ) ) )
{
DMESSAGE( "%s = %s", s, v );
ne->add_raw( s, v );
}
}
}
_copied_module_settings = ne->print();
}
void
Module::paste_before ( void )
{
Module *m = _copied_module_empty;
m->chain( chain() );
Log_Entry le( _copied_module_settings );
m->set( le );
if ( ! chain()->insert( this, m ) )
{
fl_alert( "Copied module cannot be inserted at this point in the chain" );
}
free( _copied_module_settings );
_copied_module_settings = NULL;
_copied_module_empty = NULL;
/* set up for another copy */
m->copy();
}
void
Module::set ( Log_Entry &e )
{
for ( int i = 0; i < e.size(); ++i )
{
const char *s, *v;
e.get( i, &s, &v );
if ( ! strcmp( s, ":chain" ) )
{
/* This trickiness is because we may need to know the name of
2010-01-28 05:39:27 +01:00
our chain before we actually get added to it. */
int i;
sscanf( v, "%X", &i );
Chain *t = (Chain*)Loggable::find( i );
assert( t );
chain( t );
}
}
for ( int i = 0; i < e.size(); ++i )
{
const char *s, *v;
e.get( i, &s, &v );
/* if ( ! strcmp( s, ":name" ) ) */
/* label( v ); */
if ( ! strcmp( s, ":parameter_values" ) )
{
set_parameters( v );
}
else if ( ! ( strcmp( s, ":is_default" ) ) )
{
is_default( atoi( v ) );
}
else if ( ! ( strcmp( s, ":active" ) ) )
{
bypass( ! atoi( v ) );
}
else if ( ! strcmp( s, ":chain" ) )
{
int i;
sscanf( v, "%X", &i );
Chain *t = (Chain*)Loggable::find( i );
assert( t );
t->add( this );
}
}
}
2009-12-25 01:59:39 +01:00
/* return a string serializing this module's parameter settings. The
format is 1.0:2.0:... Where 1.0 is the value of the first control
input, 2.0 is the value of the second control input etc.
*/
char *
Module::get_parameters ( void ) const
{
char *s = new char[1024];
s[0] = 0;
char *sp = s;
if ( control_input.size() )
{
for ( unsigned int i = 0; i < control_input.size(); ++i )
sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
*(sp - 1) = '\0';
}
return s;
}
void
Module::set_parameters ( const char *parameters )
{
char *s = strdup( parameters );
char *start = s;
unsigned int i = 0;
for ( char *sp = s; ; ++sp )
{
if ( ':' == *sp || '\0' == *sp )
{
char was = *sp;
*sp = '\0';
DMESSAGE( start );
if ( i < control_input.size() )
control_input[i].control_value( atof( start ) );
else
{
WARNING( "Module has no parameter at index %i", i );
break;
}
i++;
if ( '\0' == was )
break;
start = sp + 1;
}
}
free( s );
}
2009-12-25 01:59:39 +01:00
void
Module::draw_box ( void )
{
fl_color( FL_WHITE );
int tw, th, tx, ty;
tw = w();
th = h();
ty = y();
tx = x();
// bbox( tx, ty, tw, th );
fl_push_clip( tx, ty, tw, th );
Fl_Color c = is_default() ? FL_BLACK : color();
2010-01-28 05:39:27 +01:00
c = active() && ! bypass() ? c : fl_inactive( c );
2009-12-25 01:59:39 +01:00
int spacing = w() / instances();
for ( int i = instances(); i--; )
{
fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
2009-12-25 01:59:39 +01:00
}
2010-02-01 01:25:14 +01:00
if ( this == Fl::focus() )
{
fl_draw_box( FL_UP_FRAME, x(), y(), w(), h(), selection_color() );
}
2009-12-25 01:59:39 +01:00
if ( audio_input.size() && audio_output.size() )
{
/* maybe draw control indicators */
if ( control_input.size() )
fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
if ( control_output.size() )
fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
}
fl_pop_clip();
// box( FL_NO_BOX );
Fl_Group::draw_children();
}
void
Module::draw_label ( void )
{
int tw, th, tx, ty;
bbox( tx, ty, tw, th );
const char *lp = label();
int l = strlen( label() );
Fl_Color c = FL_FOREGROUND_COLOR;
2010-01-28 05:39:27 +01:00
if ( bypass() || ! active() )
c = FL_BLACK;
fl_color( c );
2009-12-25 01:59:39 +01:00
char *s = NULL;
if ( l > 10 )
{
s = new char[l];
char *sp = s;
for ( ; *lp; ++lp )
switch ( *lp )
{
case 'i': case 'e': case 'o': case 'u': case 'a':
break;
default:
*(sp++) = *lp;
}
*sp = '\0';
}
if ( l > 20 )
fl_font( FL_HELVETICA, 10 );
else
fl_font( FL_HELVETICA, 14 );
2010-01-13 08:07:08 +01:00
fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
2009-12-25 01:59:39 +01:00
if ( s )
delete[] s;
}
2010-01-28 05:39:27 +01:00
void
Module::insert_menu_cb ( const Fl_Menu_ *m )
{
2010-01-28 05:39:27 +01:00
void * v = m->menu()[ m->value() ].user_data();
2010-01-28 05:39:27 +01:00
if ( v )
{
unsigned long id = *((unsigned long *)v);
2010-01-28 05:39:27 +01:00
Module *mod = NULL;
2010-01-28 05:39:27 +01:00
switch ( id )
{
case -1:
mod = new JACK_Module();
break;
case -2:
mod = new Gain_Module();
break;
case -3:
mod = new Meter_Module();
break;
case -4:
mod = new Mono_Pan_Module();
break;
default:
{
Plugin_Module *m = new Plugin_Module();
2010-01-28 05:39:27 +01:00
m->load( id );
2010-01-28 05:39:27 +01:00
mod = m;
}
}
2010-01-28 05:39:27 +01:00
if ( mod )
{
if ( !strcmp( mod->name(), "JACK" ) )
{
DMESSAGE( "Special casing JACK module" );
JACK_Module *jm = (JACK_Module*)mod;
jm->chain( chain() );
jm->configure_inputs( ninputs() );
jm->configure_outputs( ninputs() );
}
2010-01-28 05:39:27 +01:00
if ( ! chain()->insert( this, mod ) )
{
fl_alert( "Cannot insert this module at this point in the chain" );
delete mod;
return;
}
2010-01-28 05:39:27 +01:00
redraw();
}
}
2010-01-28 05:39:27 +01:00
}
2010-01-28 05:39:27 +01:00
void
Module::insert_menu_cb ( Fl_Widget *w, void *v )
{
((Module*)v)->insert_menu_cb( (Fl_Menu_*) w );
}
void
Module::menu_cb ( const Fl_Menu_ *m )
{
char picked[256];
strncpy( picked, m->mvalue()->label(), sizeof( picked ) );
// m->item_pathname( picked, sizeof( picked ) );
DMESSAGE( "%s", picked );
Logger log( this );
if ( ! strcmp( picked, "Edit Parameters" ) )
command_open_parameter_editor();
else if ( ! strcmp( picked, "Activate" ) )
command_activate();
else if ( ! strcmp( picked, "Deactivate" ) )
command_deactivate();
else if ( ! strcmp( picked, "Cut" ) )
{
copy();
chain()->remove( this );
Fl::delete_widget( this );
}
else if ( ! strcmp( picked, "Copy" ) )
{
copy();
}
else if ( ! strcmp( picked, "Paste" ) )
{
paste_before();
}
2010-01-28 05:39:27 +01:00
else if ( ! strcmp( picked, "Remove" ) )
command_remove();
}
2010-01-28 05:39:27 +01:00
void
Module::menu_cb ( Fl_Widget *w, void *v )
{
((Module*)v)->menu_cb( (Fl_Menu_*) w );
}
2010-01-28 05:39:27 +01:00
/** build the context menu */
Fl_Menu_Button &
Module::menu ( void ) const
{
static Fl_Menu_Button m( 0, 0, 0, 0, "Module" );
static Fl_Menu_Button *insert_menu = NULL;
if ( ! insert_menu )
{
2010-01-28 05:39:27 +01:00
insert_menu = new Fl_Menu_Button( 0, 0, 0, 0 );
insert_menu->add( "Gain", 0, 0, new unsigned long(-2) );
insert_menu->add( "Meter", 0, 0, new unsigned long(-3) );
insert_menu->add( "Mono Pan", 0, 0, new unsigned long(-4) );
Plugin_Module::add_plugins_to_menu( insert_menu );
// menu_set_callback( insert_menu, &Module::insert_menu_cb, (void*)this );
insert_menu->callback( &Module::insert_menu_cb, (void*)this );
}
2010-01-28 05:39:27 +01:00
m.clear();
m.add( "Insert", 0, &Module::menu_cb, (void*)this, 0);
m.add( "Insert", 0, &Module::menu_cb, const_cast< Fl_Menu_Item *>( insert_menu->menu() ), FL_SUBMENU_POINTER );
m.add( "Edit Parameters", 0, &Module::menu_cb, (void*)this, 0 );
m.add( "Activate", 0, &Module::menu_cb, (void*)this, ! bypass() ? FL_MENU_INACTIVE : 0 );
m.add( "Deactivate", 0, &Module::menu_cb, (void*)this, bypass() ? FL_MENU_INACTIVE : 0 );
m.add( "Cut", FL_CTRL + 'x', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
m.add( "Copy", FL_CTRL + 'c', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
m.add( "Paste", FL_CTRL + 'v', &Module::menu_cb, (void*)this, _copied_module_empty ? 0 : FL_MENU_INACTIVE );
2010-01-28 05:39:27 +01:00
m.add( "Remove", 0, &Module::menu_cb, (void*)this );
// menu_set_callback( menu, &Module::menu_cb, (void*)this );
m.callback( &Module::insert_menu_cb, (void*)this );
return m;
}
2009-12-25 01:59:39 +01:00
int
Module::handle ( int m )
{
switch ( m )
{
2010-01-28 05:39:27 +01:00
case FL_KEYBOARD:
{
if ( Fl_Group::handle( m ) )
return 1;
2010-01-30 09:38:15 +01:00
if ( Fl::event_key() == FL_Menu )
2010-01-28 05:39:27 +01:00
{
menu_popup( &menu(), x(), y() );
return 1;
}
else
return menu().test_shortcut() != 0;
}
2009-12-25 01:59:39 +01:00
case FL_PUSH:
{
2010-02-01 01:25:14 +01:00
take_focus();
2010-01-28 05:39:27 +01:00
if ( Fl_Group::handle( m ) )
return 1;
else if ( test_press( FL_BUTTON3 ) )
{
menu_popup( &menu() );
return 1;
}
else if ( test_press( FL_BUTTON1 ) )
{
command_open_parameter_editor();
return 1;
}
else if ( test_press( FL_BUTTON3 | FL_CTRL ) )
2009-12-25 01:59:39 +01:00
{
2010-01-28 05:39:27 +01:00
command_remove();
return 1;
}
else if ( test_press( FL_BUTTON2 ) )
{
bypass( !bypass() );
redraw();
return 1;
}
2010-01-28 05:39:27 +01:00
return 0;
}
2010-02-01 01:25:14 +01:00
case FL_FOCUS:
case FL_UNFOCUS:
redraw();
return 1;
2010-01-28 05:39:27 +01:00
}
return Fl_Group::handle( m );
}
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
/************/
/* Commands */
/************/
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
void
Module::command_open_parameter_editor ( void )
{
if ( _editor )
{
_editor->show();
}
else if ( ncontrol_inputs() )
{
DMESSAGE( "Opening module parameters for \"%s\"", label() );
_editor = new Module_Parameter_Editor( this );
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
_editor->show();
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
do { Fl::wait(); }
while ( _editor->shown() );
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
DMESSAGE( "Module parameters for \"%s\" closed",label() );
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
delete _editor;
_editor = NULL;
2009-12-25 01:59:39 +01:00
}
2010-01-28 05:39:27 +01:00
}
2009-12-25 01:59:39 +01:00
2010-01-28 05:39:27 +01:00
void
Module::command_activate ( void )
{
bypass( false );
}
void
Module::command_deactivate ( void )
{
bypass( true );
}
void
Module::command_remove ( void )
{
if ( is_default() )
fl_alert( "Default modules may not be deleted." );
else
{
chain()->remove( this );
Fl::delete_widget( this );
}
2009-12-25 01:59:39 +01:00
}