Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

654 lines
15KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Module.H"
  19. #include <FL/fl_draw.H>
  20. #include <FL/fl_ask.H>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "Module_Parameter_Editor.H"
  25. #include "Chain.H"
  26. #include "JACK_Module.H"
  27. #include "Gain_Module.H"
  28. #include "Mono_Pan_Module.H"
  29. #include "Meter_Module.H"
  30. #include "Plugin_Module.H"
  31. #include <FL/Fl_Menu_Button.H>
  32. #include "FL/test_press.H"
  33. #include "FL/menu_popup.H"
  34. Module *Module::_copied_module_empty = 0;
  35. char *Module::_copied_module_settings = 0;
  36. Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
  37. {
  38. init();
  39. log_create();
  40. }
  41. Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
  42. {
  43. this->is_default( is_default );
  44. init();
  45. log_create();
  46. }
  47. Module::Module ( ) : Fl_Group( 0, 0, 0, 50, "Unnamed" )
  48. {
  49. init();
  50. log_create();
  51. }
  52. Module::~Module ( )
  53. {
  54. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  55. audio_input[i].disconnect();
  56. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  57. audio_output[i].disconnect();
  58. for ( unsigned int i = 0; i < control_input.size(); ++i )
  59. control_input[i].disconnect();
  60. for ( unsigned int i = 0; i < control_output.size(); ++i )
  61. control_output[i].disconnect();
  62. audio_input.clear();
  63. audio_output.clear();
  64. control_input.clear();
  65. control_output.clear();
  66. }
  67. void
  68. Module::init ( void )
  69. {
  70. _is_default = false;
  71. _editor = 0;
  72. _chain = 0;
  73. _instances = 1;
  74. _bypass = 0;
  75. box( FL_UP_BOX );
  76. labeltype( FL_NO_LABEL );
  77. clip_children( 1 );
  78. set_visible_focus();
  79. selection_color( FL_RED );
  80. }
  81. void
  82. Module::get ( Log_Entry &e ) const
  83. {
  84. // e.add( ":name", label() );
  85. // e.add( ":color", (unsigned long)color());
  86. {
  87. char *s = get_parameters();
  88. if ( strlen( s ) )
  89. e.add( ":parameter_values", s );
  90. delete[] s;
  91. }
  92. e.add( ":is_default", is_default() );
  93. e.add( ":chain", chain() );
  94. e.add( ":active", ! bypass() );
  95. }
  96. void
  97. Module::copy ( void ) const
  98. {
  99. Module *m = clone_empty();
  100. if ( ! m )
  101. {
  102. DMESSAGE( "Module \"%s\" doesn't support cloning", name() );
  103. }
  104. Log_Entry *ne = new Log_Entry();
  105. _copied_module_empty = m;
  106. {
  107. Log_Entry e;
  108. get( e );
  109. for ( int i = 0; i < e.size(); ++i )
  110. {
  111. const char *s, *v;
  112. e.get( i, &s, &v );
  113. /* we don't want this module to get added to the current
  114. chain... */
  115. if ( !( !strcmp( s, ":chain" ) ||
  116. !strcmp( s, ":is_default" ) ) )
  117. {
  118. DMESSAGE( "%s = %s", s, v );
  119. ne->add_raw( s, v );
  120. }
  121. }
  122. }
  123. _copied_module_settings = ne->print();
  124. }
  125. void
  126. Module::paste_before ( void )
  127. {
  128. Module *m = _copied_module_empty;
  129. m->chain( chain() );
  130. Log_Entry le( _copied_module_settings );
  131. m->set( le );
  132. if ( ! chain()->insert( this, m ) )
  133. {
  134. fl_alert( "Copied module cannot be inserted at this point in the chain" );
  135. }
  136. free( _copied_module_settings );
  137. _copied_module_settings = NULL;
  138. _copied_module_empty = NULL;
  139. /* set up for another copy */
  140. m->copy();
  141. }
  142. void
  143. Module::set ( Log_Entry &e )
  144. {
  145. for ( int i = 0; i < e.size(); ++i )
  146. {
  147. const char *s, *v;
  148. e.get( i, &s, &v );
  149. if ( ! strcmp( s, ":chain" ) )
  150. {
  151. /* This trickiness is because we may need to know the name of
  152. our chain before we actually get added to it. */
  153. int i;
  154. sscanf( v, "%X", &i );
  155. Chain *t = (Chain*)Loggable::find( i );
  156. assert( t );
  157. chain( t );
  158. }
  159. }
  160. for ( int i = 0; i < e.size(); ++i )
  161. {
  162. const char *s, *v;
  163. e.get( i, &s, &v );
  164. /* if ( ! strcmp( s, ":name" ) ) */
  165. /* label( v ); */
  166. if ( ! strcmp( s, ":parameter_values" ) )
  167. {
  168. set_parameters( v );
  169. }
  170. else if ( ! ( strcmp( s, ":is_default" ) ) )
  171. {
  172. is_default( atoi( v ) );
  173. }
  174. else if ( ! ( strcmp( s, ":active" ) ) )
  175. {
  176. bypass( ! atoi( v ) );
  177. }
  178. else if ( ! strcmp( s, ":chain" ) )
  179. {
  180. int i;
  181. sscanf( v, "%X", &i );
  182. Chain *t = (Chain*)Loggable::find( i );
  183. assert( t );
  184. t->add( this );
  185. }
  186. }
  187. }
  188. /* return a string serializing this module's parameter settings. The
  189. format is 1.0:2.0:... Where 1.0 is the value of the first control
  190. input, 2.0 is the value of the second control input etc.
  191. */
  192. char *
  193. Module::get_parameters ( void ) const
  194. {
  195. char *s = new char[1024];
  196. s[0] = 0;
  197. char *sp = s;
  198. if ( control_input.size() )
  199. {
  200. for ( unsigned int i = 0; i < control_input.size(); ++i )
  201. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  202. *(sp - 1) = '\0';
  203. }
  204. return s;
  205. }
  206. void
  207. Module::set_parameters ( const char *parameters )
  208. {
  209. char *s = strdup( parameters );
  210. char *start = s;
  211. unsigned int i = 0;
  212. for ( char *sp = s; ; ++sp )
  213. {
  214. if ( ':' == *sp || '\0' == *sp )
  215. {
  216. char was = *sp;
  217. *sp = '\0';
  218. DMESSAGE( start );
  219. if ( i < control_input.size() )
  220. control_input[i].control_value( atof( start ) );
  221. else
  222. {
  223. WARNING( "Module has no parameter at index %i", i );
  224. break;
  225. }
  226. i++;
  227. if ( '\0' == was )
  228. break;
  229. start = sp + 1;
  230. }
  231. }
  232. free( s );
  233. }
  234. void
  235. Module::draw_box ( void )
  236. {
  237. fl_color( FL_WHITE );
  238. int tw, th, tx, ty;
  239. tw = w();
  240. th = h();
  241. ty = y();
  242. tx = x();
  243. // bbox( tx, ty, tw, th );
  244. fl_push_clip( tx, ty, tw, th );
  245. Fl_Color c = is_default() ? FL_BLACK : color();
  246. c = active() && ! bypass() ? c : fl_inactive( c );
  247. int spacing = w() / instances();
  248. for ( int i = instances(); i--; )
  249. {
  250. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  251. }
  252. if ( this == Fl::focus() )
  253. {
  254. fl_draw_box( FL_UP_FRAME, x(), y(), w(), h(), selection_color() );
  255. }
  256. if ( audio_input.size() && audio_output.size() )
  257. {
  258. /* maybe draw control indicators */
  259. if ( control_input.size() )
  260. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  261. if ( control_output.size() )
  262. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  263. }
  264. fl_pop_clip();
  265. // box( FL_NO_BOX );
  266. Fl_Group::draw_children();
  267. }
  268. void
  269. Module::draw_label ( void )
  270. {
  271. int tw, th, tx, ty;
  272. bbox( tx, ty, tw, th );
  273. const char *lp = label();
  274. int l = strlen( label() );
  275. Fl_Color c = FL_FOREGROUND_COLOR;
  276. if ( bypass() || ! active() )
  277. c = FL_BLACK;
  278. fl_color( c );
  279. char *s = NULL;
  280. if ( l > 10 )
  281. {
  282. s = new char[l];
  283. char *sp = s;
  284. for ( ; *lp; ++lp )
  285. switch ( *lp )
  286. {
  287. case 'i': case 'e': case 'o': case 'u': case 'a':
  288. break;
  289. default:
  290. *(sp++) = *lp;
  291. }
  292. *sp = '\0';
  293. }
  294. if ( l > 20 )
  295. fl_font( FL_HELVETICA, 10 );
  296. else
  297. fl_font( FL_HELVETICA, 14 );
  298. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  299. if ( s )
  300. delete[] s;
  301. }
  302. void
  303. Module::insert_menu_cb ( const Fl_Menu_ *m )
  304. {
  305. void * v = m->menu()[ m->value() ].user_data();
  306. if ( v )
  307. {
  308. unsigned long id = *((unsigned long *)v);
  309. Module *mod = NULL;
  310. switch ( id )
  311. {
  312. case -1:
  313. mod = new JACK_Module();
  314. break;
  315. case -2:
  316. mod = new Gain_Module();
  317. break;
  318. case -3:
  319. mod = new Meter_Module();
  320. break;
  321. case -4:
  322. mod = new Mono_Pan_Module();
  323. break;
  324. default:
  325. {
  326. Plugin_Module *m = new Plugin_Module();
  327. m->load( id );
  328. mod = m;
  329. }
  330. }
  331. if ( mod )
  332. {
  333. if ( !strcmp( mod->name(), "JACK" ) )
  334. {
  335. DMESSAGE( "Special casing JACK module" );
  336. JACK_Module *jm = (JACK_Module*)mod;
  337. jm->chain( chain() );
  338. jm->configure_inputs( ninputs() );
  339. jm->configure_outputs( ninputs() );
  340. }
  341. if ( ! chain()->insert( this, mod ) )
  342. {
  343. fl_alert( "Cannot insert this module at this point in the chain" );
  344. delete mod;
  345. return;
  346. }
  347. redraw();
  348. }
  349. }
  350. }
  351. void
  352. Module::insert_menu_cb ( Fl_Widget *w, void *v )
  353. {
  354. ((Module*)v)->insert_menu_cb( (Fl_Menu_*) w );
  355. }
  356. void
  357. Module::menu_cb ( const Fl_Menu_ *m )
  358. {
  359. char picked[256];
  360. strncpy( picked, m->mvalue()->label(), sizeof( picked ) );
  361. // m->item_pathname( picked, sizeof( picked ) );
  362. DMESSAGE( "%s", picked );
  363. Logger log( this );
  364. if ( ! strcmp( picked, "Edit Parameters" ) )
  365. command_open_parameter_editor();
  366. else if ( ! strcmp( picked, "Bypass" ) )
  367. bypass( ! ( m->mvalue()->flags & FL_MENU_VALUE ) );
  368. else if ( ! strcmp( picked, "Cut" ) )
  369. {
  370. copy();
  371. chain()->remove( this );
  372. Fl::delete_widget( this );
  373. }
  374. else if ( ! strcmp( picked, "Copy" ) )
  375. {
  376. copy();
  377. }
  378. else if ( ! strcmp( picked, "Paste" ) )
  379. {
  380. paste_before();
  381. }
  382. else if ( ! strcmp( picked, "Remove" ) )
  383. command_remove();
  384. }
  385. void
  386. Module::menu_cb ( Fl_Widget *w, void *v )
  387. {
  388. ((Module*)v)->menu_cb( (Fl_Menu_*) w );
  389. }
  390. /** build the context menu */
  391. Fl_Menu_Button &
  392. Module::menu ( void ) const
  393. {
  394. static Fl_Menu_Button m( 0, 0, 0, 0, "Module" );
  395. static Fl_Menu_Button *insert_menu = NULL;
  396. if ( ! insert_menu )
  397. {
  398. insert_menu = new Fl_Menu_Button( 0, 0, 0, 0 );
  399. insert_menu->add( "Gain", 0, 0, new unsigned long(-2) );
  400. insert_menu->add( "Meter", 0, 0, new unsigned long(-3) );
  401. insert_menu->add( "Mono Pan", 0, 0, new unsigned long(-4) );
  402. Plugin_Module::add_plugins_to_menu( insert_menu );
  403. // menu_set_callback( insert_menu, &Module::insert_menu_cb, (void*)this );
  404. insert_menu->callback( &Module::insert_menu_cb, (void*)this );
  405. }
  406. m.clear();
  407. m.add( "Insert", 0, &Module::menu_cb, (void*)this, 0);
  408. m.add( "Insert", 0, &Module::menu_cb, const_cast< Fl_Menu_Item *>( insert_menu->menu() ), FL_SUBMENU_POINTER );
  409. m.add( "Edit Parameters", ' ', &Module::menu_cb, (void*)this, 0 );
  410. m.add( "Bypass", 'b', &Module::menu_cb, (void*)this, FL_MENU_TOGGLE | ( bypass() ? FL_MENU_VALUE : 0 ) );
  411. m.add( "Cut", FL_CTRL + 'x', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  412. m.add( "Copy", FL_CTRL + 'c', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  413. m.add( "Paste", FL_CTRL + 'v', &Module::menu_cb, (void*)this, _copied_module_empty ? 0 : FL_MENU_INACTIVE );
  414. m.add( "Remove", FL_Delete, &Module::menu_cb, (void*)this );
  415. // menu_set_callback( menu, &Module::menu_cb, (void*)this );
  416. m.callback( &Module::insert_menu_cb, (void*)this );
  417. return m;
  418. }
  419. int
  420. Module::handle ( int m )
  421. {
  422. switch ( m )
  423. {
  424. case FL_KEYBOARD:
  425. {
  426. if ( Fl_Group::handle( m ) )
  427. return 1;
  428. if ( Fl::event_key() == FL_Menu )
  429. {
  430. menu_popup( &menu(), x(), y() );
  431. return 1;
  432. }
  433. else
  434. return menu().test_shortcut() != 0;
  435. }
  436. case FL_PUSH:
  437. {
  438. take_focus();
  439. if ( Fl_Group::handle( m ) )
  440. return 1;
  441. else if ( test_press( FL_BUTTON3 ) )
  442. {
  443. menu_popup( &menu() );
  444. return 1;
  445. }
  446. else if ( test_press( FL_BUTTON1 ) )
  447. {
  448. command_open_parameter_editor();
  449. return 1;
  450. }
  451. else if ( test_press( FL_BUTTON3 | FL_CTRL ) )
  452. {
  453. command_remove();
  454. return 1;
  455. }
  456. else if ( test_press( FL_BUTTON2 ) )
  457. {
  458. bypass( !bypass() );
  459. redraw();
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. case FL_FOCUS:
  465. case FL_UNFOCUS:
  466. redraw();
  467. return 1;
  468. }
  469. return Fl_Group::handle( m );
  470. }
  471. /************/
  472. /* Commands */
  473. /************/
  474. void
  475. Module::command_open_parameter_editor ( void )
  476. {
  477. if ( _editor )
  478. {
  479. _editor->show();
  480. }
  481. else if ( ncontrol_inputs() )
  482. {
  483. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  484. _editor = new Module_Parameter_Editor( this );
  485. _editor->show();
  486. do { Fl::wait(); }
  487. while ( _editor->shown() );
  488. DMESSAGE( "Module parameters for \"%s\" closed",label() );
  489. delete _editor;
  490. _editor = NULL;
  491. }
  492. }
  493. void
  494. Module::command_activate ( void )
  495. {
  496. bypass( false );
  497. }
  498. void
  499. Module::command_deactivate ( void )
  500. {
  501. bypass( true );
  502. }
  503. void
  504. Module::command_remove ( void )
  505. {
  506. if ( is_default() )
  507. fl_alert( "Default modules may not be deleted." );
  508. else
  509. {
  510. chain()->remove( this );
  511. Fl::delete_widget( this );
  512. }
  513. }