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.

643 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. }
  79. void
  80. Module::get ( Log_Entry &e ) const
  81. {
  82. // e.add( ":name", label() );
  83. // e.add( ":color", (unsigned long)color());
  84. {
  85. char *s = get_parameters();
  86. if ( strlen( s ) )
  87. e.add( ":parameter_values", s );
  88. delete[] s;
  89. }
  90. e.add( ":is_default", is_default() );
  91. e.add( ":chain", chain() );
  92. e.add( ":active", ! bypass() );
  93. }
  94. void
  95. Module::copy ( void ) const
  96. {
  97. Module *m = clone_empty();
  98. if ( ! m )
  99. {
  100. DMESSAGE( "Module \"%s\" doesn't support cloning", name() );
  101. }
  102. Log_Entry *ne = new Log_Entry();
  103. _copied_module_empty = m;
  104. {
  105. Log_Entry e;
  106. get( e );
  107. for ( int i = 0; i < e.size(); ++i )
  108. {
  109. const char *s, *v;
  110. e.get( i, &s, &v );
  111. /* we don't want this module to get added to the current
  112. chain... */
  113. if ( !( !strcmp( s, ":chain" ) ||
  114. !strcmp( s, ":is_default" ) ) )
  115. {
  116. DMESSAGE( "%s = %s", s, v );
  117. ne->add_raw( s, v );
  118. }
  119. }
  120. }
  121. _copied_module_settings = ne->print();
  122. }
  123. void
  124. Module::paste_before ( void )
  125. {
  126. Module *m = _copied_module_empty;
  127. m->chain( chain() );
  128. Log_Entry le( _copied_module_settings );
  129. m->set( le );
  130. if ( ! chain()->insert( this, m ) )
  131. {
  132. fl_alert( "Copied module cannot be inserted at this point in the chain" );
  133. }
  134. free( _copied_module_settings );
  135. _copied_module_settings = NULL;
  136. _copied_module_empty = NULL;
  137. /* set up for another copy */
  138. m->copy();
  139. }
  140. void
  141. Module::set ( Log_Entry &e )
  142. {
  143. for ( int i = 0; i < e.size(); ++i )
  144. {
  145. const char *s, *v;
  146. e.get( i, &s, &v );
  147. if ( ! strcmp( s, ":chain" ) )
  148. {
  149. /* This trickiness is because we may need to know the name of
  150. our chain before we actually get added to it. */
  151. int i;
  152. sscanf( v, "%X", &i );
  153. Chain *t = (Chain*)Loggable::find( i );
  154. assert( t );
  155. chain( t );
  156. }
  157. }
  158. for ( int i = 0; i < e.size(); ++i )
  159. {
  160. const char *s, *v;
  161. e.get( i, &s, &v );
  162. /* if ( ! strcmp( s, ":name" ) ) */
  163. /* label( v ); */
  164. if ( ! strcmp( s, ":parameter_values" ) )
  165. {
  166. set_parameters( v );
  167. }
  168. else if ( ! ( strcmp( s, ":is_default" ) ) )
  169. {
  170. is_default( atoi( v ) );
  171. }
  172. else if ( ! ( strcmp( s, ":active" ) ) )
  173. {
  174. bypass( ! atoi( v ) );
  175. }
  176. else if ( ! strcmp( s, ":chain" ) )
  177. {
  178. int i;
  179. sscanf( v, "%X", &i );
  180. Chain *t = (Chain*)Loggable::find( i );
  181. assert( t );
  182. t->add( this );
  183. }
  184. }
  185. }
  186. /* return a string serializing this module's parameter settings. The
  187. format is 1.0:2.0:... Where 1.0 is the value of the first control
  188. input, 2.0 is the value of the second control input etc.
  189. */
  190. char *
  191. Module::get_parameters ( void ) const
  192. {
  193. char *s = new char[1024];
  194. s[0] = 0;
  195. char *sp = s;
  196. if ( control_input.size() )
  197. {
  198. for ( unsigned int i = 0; i < control_input.size(); ++i )
  199. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  200. *(sp - 1) = '\0';
  201. }
  202. return s;
  203. }
  204. void
  205. Module::set_parameters ( const char *parameters )
  206. {
  207. char *s = strdup( parameters );
  208. char *start = s;
  209. unsigned int i = 0;
  210. for ( char *sp = s; ; ++sp )
  211. {
  212. if ( ':' == *sp || '\0' == *sp )
  213. {
  214. char was = *sp;
  215. *sp = '\0';
  216. DMESSAGE( start );
  217. if ( i < control_input.size() )
  218. control_input[i].control_value( atof( start ) );
  219. else
  220. {
  221. WARNING( "Module has no parameter at index %i", i );
  222. break;
  223. }
  224. i++;
  225. if ( '\0' == was )
  226. break;
  227. start = sp + 1;
  228. }
  229. }
  230. free( s );
  231. }
  232. void
  233. Module::draw_box ( void )
  234. {
  235. fl_color( FL_WHITE );
  236. int tw, th, tx, ty;
  237. tw = w();
  238. th = h();
  239. ty = y();
  240. tx = x();
  241. // bbox( tx, ty, tw, th );
  242. fl_push_clip( tx, ty, tw, th );
  243. Fl_Color c = is_default() ? FL_BLACK : color();
  244. c = active() && ! bypass() ? c : fl_inactive( c );
  245. int spacing = w() / instances();
  246. for ( int i = instances(); i--; )
  247. {
  248. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  249. }
  250. if ( audio_input.size() && audio_output.size() )
  251. {
  252. /* maybe draw control indicators */
  253. if ( control_input.size() )
  254. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  255. if ( control_output.size() )
  256. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  257. }
  258. fl_pop_clip();
  259. // box( FL_NO_BOX );
  260. Fl_Group::draw_children();
  261. }
  262. void
  263. Module::draw_label ( void )
  264. {
  265. int tw, th, tx, ty;
  266. bbox( tx, ty, tw, th );
  267. const char *lp = label();
  268. int l = strlen( label() );
  269. Fl_Color c = FL_FOREGROUND_COLOR;
  270. if ( bypass() || ! active() )
  271. c = FL_BLACK;
  272. fl_color( c );
  273. char *s = NULL;
  274. if ( l > 10 )
  275. {
  276. s = new char[l];
  277. char *sp = s;
  278. for ( ; *lp; ++lp )
  279. switch ( *lp )
  280. {
  281. case 'i': case 'e': case 'o': case 'u': case 'a':
  282. break;
  283. default:
  284. *(sp++) = *lp;
  285. }
  286. *sp = '\0';
  287. }
  288. if ( l > 20 )
  289. fl_font( FL_HELVETICA, 10 );
  290. else
  291. fl_font( FL_HELVETICA, 14 );
  292. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  293. if ( s )
  294. delete[] s;
  295. }
  296. void
  297. Module::insert_menu_cb ( const Fl_Menu_ *m )
  298. {
  299. void * v = m->menu()[ m->value() ].user_data();
  300. if ( v )
  301. {
  302. unsigned long id = *((unsigned long *)v);
  303. Module *mod = NULL;
  304. switch ( id )
  305. {
  306. case -1:
  307. mod = new JACK_Module();
  308. break;
  309. case -2:
  310. mod = new Gain_Module();
  311. break;
  312. case -3:
  313. mod = new Meter_Module();
  314. break;
  315. case -4:
  316. mod = new Mono_Pan_Module();
  317. break;
  318. default:
  319. {
  320. Plugin_Module *m = new Plugin_Module();
  321. m->load( id );
  322. mod = m;
  323. }
  324. }
  325. if ( mod )
  326. {
  327. if ( !strcmp( mod->name(), "JACK" ) )
  328. {
  329. DMESSAGE( "Special casing JACK module" );
  330. JACK_Module *jm = (JACK_Module*)mod;
  331. jm->chain( chain() );
  332. jm->configure_inputs( ninputs() );
  333. jm->configure_outputs( ninputs() );
  334. }
  335. if ( ! chain()->insert( this, mod ) )
  336. {
  337. fl_alert( "Cannot insert this module at this point in the chain" );
  338. delete mod;
  339. return;
  340. }
  341. redraw();
  342. }
  343. }
  344. }
  345. void
  346. Module::insert_menu_cb ( Fl_Widget *w, void *v )
  347. {
  348. ((Module*)v)->insert_menu_cb( (Fl_Menu_*) w );
  349. }
  350. void
  351. Module::menu_cb ( const Fl_Menu_ *m )
  352. {
  353. char picked[256];
  354. strncpy( picked, m->mvalue()->label(), sizeof( picked ) );
  355. // m->item_pathname( picked, sizeof( picked ) );
  356. DMESSAGE( "%s", picked );
  357. Logger log( this );
  358. if ( ! strcmp( picked, "Edit Parameters" ) )
  359. command_open_parameter_editor();
  360. else if ( ! strcmp( picked, "Activate" ) )
  361. command_activate();
  362. else if ( ! strcmp( picked, "Deactivate" ) )
  363. command_deactivate();
  364. else if ( ! strcmp( picked, "Cut" ) )
  365. {
  366. copy();
  367. chain()->remove( this );
  368. Fl::delete_widget( this );
  369. }
  370. else if ( ! strcmp( picked, "Copy" ) )
  371. {
  372. copy();
  373. }
  374. else if ( ! strcmp( picked, "Paste" ) )
  375. {
  376. paste_before();
  377. }
  378. else if ( ! strcmp( picked, "Remove" ) )
  379. command_remove();
  380. }
  381. void
  382. Module::menu_cb ( Fl_Widget *w, void *v )
  383. {
  384. ((Module*)v)->menu_cb( (Fl_Menu_*) w );
  385. }
  386. /** build the context menu */
  387. Fl_Menu_Button &
  388. Module::menu ( void ) const
  389. {
  390. static Fl_Menu_Button m( 0, 0, 0, 0, "Module" );
  391. static Fl_Menu_Button *insert_menu = NULL;
  392. if ( ! insert_menu )
  393. {
  394. insert_menu = new Fl_Menu_Button( 0, 0, 0, 0 );
  395. insert_menu->add( "Gain", 0, 0, new unsigned long(-2) );
  396. insert_menu->add( "Meter", 0, 0, new unsigned long(-3) );
  397. insert_menu->add( "Mono Pan", 0, 0, new unsigned long(-4) );
  398. Plugin_Module::add_plugins_to_menu( insert_menu );
  399. // menu_set_callback( insert_menu, &Module::insert_menu_cb, (void*)this );
  400. insert_menu->callback( &Module::insert_menu_cb, (void*)this );
  401. }
  402. m.clear();
  403. m.add( "Insert", 0, &Module::menu_cb, (void*)this, 0);
  404. m.add( "Insert", 0, &Module::menu_cb, const_cast< Fl_Menu_Item *>( insert_menu->menu() ), FL_SUBMENU_POINTER );
  405. m.add( "Edit Parameters", 0, &Module::menu_cb, (void*)this, 0 );
  406. m.add( "Activate", 0, &Module::menu_cb, (void*)this, ! bypass() ? FL_MENU_INACTIVE : 0 );
  407. m.add( "Deactivate", 0, &Module::menu_cb, (void*)this, bypass() ? FL_MENU_INACTIVE : 0 );
  408. m.add( "Cut", FL_CTRL + 'x', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  409. m.add( "Copy", FL_CTRL + 'c', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  410. m.add( "Paste", FL_CTRL + 'v', &Module::menu_cb, (void*)this, _copied_module_empty ? 0 : FL_MENU_INACTIVE );
  411. m.add( "Remove", 0, &Module::menu_cb, (void*)this );
  412. // menu_set_callback( menu, &Module::menu_cb, (void*)this );
  413. m.callback( &Module::insert_menu_cb, (void*)this );
  414. return m;
  415. }
  416. int
  417. Module::handle ( int m )
  418. {
  419. switch ( m )
  420. {
  421. case FL_KEYBOARD:
  422. {
  423. if ( Fl_Group::handle( m ) )
  424. return 1;
  425. if ( Fl::event_key() == FL_Menu )
  426. {
  427. menu_popup( &menu(), x(), y() );
  428. return 1;
  429. }
  430. else
  431. return menu().test_shortcut() != 0;
  432. }
  433. case FL_PUSH:
  434. {
  435. if ( Fl_Group::handle( m ) )
  436. return 1;
  437. else if ( test_press( FL_BUTTON3 ) )
  438. {
  439. menu_popup( &menu() );
  440. return 1;
  441. }
  442. else if ( test_press( FL_BUTTON1 ) )
  443. {
  444. command_open_parameter_editor();
  445. return 1;
  446. }
  447. else if ( test_press( FL_BUTTON3 | FL_CTRL ) )
  448. {
  449. command_remove();
  450. return 1;
  451. }
  452. else if ( test_press( FL_BUTTON2 ) )
  453. {
  454. bypass( !bypass() );
  455. redraw();
  456. return 1;
  457. }
  458. return 0;
  459. }
  460. }
  461. return Fl_Group::handle( m );
  462. }
  463. /************/
  464. /* Commands */
  465. /************/
  466. void
  467. Module::command_open_parameter_editor ( void )
  468. {
  469. if ( _editor )
  470. {
  471. _editor->show();
  472. }
  473. else if ( ncontrol_inputs() )
  474. {
  475. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  476. _editor = new Module_Parameter_Editor( this );
  477. _editor->show();
  478. do { Fl::wait(); }
  479. while ( _editor->shown() );
  480. DMESSAGE( "Module parameters for \"%s\" closed",label() );
  481. delete _editor;
  482. _editor = NULL;
  483. }
  484. }
  485. void
  486. Module::command_activate ( void )
  487. {
  488. bypass( false );
  489. }
  490. void
  491. Module::command_deactivate ( void )
  492. {
  493. bypass( true );
  494. }
  495. void
  496. Module::command_remove ( void )
  497. {
  498. if ( is_default() )
  499. fl_alert( "Default modules may not be deleted." );
  500. else
  501. {
  502. chain()->remove( this );
  503. Fl::delete_widget( this );
  504. }
  505. }