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.

553 lines
13KB

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