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.

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