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.

343 lines
7.9KB

  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 <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "Module_Parameter_Editor.H"
  24. #include "Chain.H"
  25. Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
  26. {
  27. init();
  28. log_create();
  29. }
  30. Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
  31. {
  32. this->is_default( is_default );
  33. init();
  34. log_create();
  35. }
  36. Module::Module ( ) : Fl_Group( 0, 0, 0, 50, "Unnamed" )
  37. {
  38. init();
  39. log_create();
  40. }
  41. Module::~Module ( )
  42. {
  43. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  44. audio_input[i].disconnect();
  45. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  46. audio_output[i].disconnect();
  47. for ( unsigned int i = 0; i < control_input.size(); ++i )
  48. control_input[i].disconnect();
  49. for ( unsigned int i = 0; i < control_output.size(); ++i )
  50. control_output[i].disconnect();
  51. audio_input.clear();
  52. audio_output.clear();
  53. control_input.clear();
  54. control_output.clear();
  55. }
  56. void
  57. Module::init ( void )
  58. {
  59. _is_default = false;
  60. _editor = 0;
  61. _chain = 0;
  62. _instances = 1;
  63. box( FL_UP_BOX );
  64. labeltype( FL_NO_LABEL );
  65. clip_children( 1 );
  66. }
  67. void
  68. Module::get ( Log_Entry &e ) const
  69. {
  70. // e.add( ":name", label() );
  71. // e.add( ":color", (unsigned long)color());
  72. {
  73. char *s = get_parameters();
  74. if ( strlen( s ) )
  75. e.add( ":parameter_values", s );
  76. delete[] s;
  77. }
  78. e.add( ":is_default", is_default() );
  79. e.add( ":chain", chain() );
  80. }
  81. void
  82. Module::set ( Log_Entry &e )
  83. {
  84. for ( int i = 0; i < e.size(); ++i )
  85. {
  86. const char *s, *v;
  87. e.get( i, &s, &v );
  88. if ( ! strcmp( s, ":chain" ) )
  89. {
  90. /* This trickiness is because we may need to know the name of
  91. our chain before we actually get added to it. */
  92. int i;
  93. sscanf( v, "%X", &i );
  94. Chain *t = (Chain*)Loggable::find( i );
  95. assert( t );
  96. chain( t );
  97. }
  98. }
  99. for ( int i = 0; i < e.size(); ++i )
  100. {
  101. const char *s, *v;
  102. e.get( i, &s, &v );
  103. /* if ( ! strcmp( s, ":name" ) ) */
  104. /* label( v ); */
  105. if ( ! strcmp( s, ":parameter_values" ) )
  106. {
  107. set_parameters( v );
  108. }
  109. else if ( ! ( strcmp( s, ":is_default" ) ) )
  110. {
  111. is_default( atoi( v ) );
  112. }
  113. else if ( ! strcmp( s, ":chain" ) )
  114. {
  115. int i;
  116. sscanf( v, "%X", &i );
  117. Chain *t = (Chain*)Loggable::find( i );
  118. assert( t );
  119. t->add( this );
  120. }
  121. }
  122. }
  123. /* return a string serializing this module's parameter settings. The
  124. format is 1.0:2.0:... Where 1.0 is the value of the first control
  125. input, 2.0 is the value of the second control input etc.
  126. */
  127. char *
  128. Module::get_parameters ( void ) const
  129. {
  130. char *s = new char[1024];
  131. s[0] = 0;
  132. char *sp = s;
  133. if ( control_input.size() )
  134. {
  135. for ( unsigned int i = 0; i < control_input.size(); ++i )
  136. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  137. *(sp - 1) = '\0';
  138. }
  139. return s;
  140. }
  141. void
  142. Module::set_parameters ( const char *parameters )
  143. {
  144. char *s = strdup( parameters );
  145. char *sp = s;
  146. char *start = s;
  147. int i = 0;
  148. for ( char *sp = s; ; ++sp )
  149. {
  150. if ( ':' == *sp || '\0' == *sp )
  151. {
  152. char was = *sp;
  153. *sp = '\0';
  154. DMESSAGE( start );
  155. if ( i < control_input.size() )
  156. control_input[i].control_value( atof( start ) );
  157. else
  158. {
  159. WARNING( "Module has no parameter at index %i", i );
  160. break;
  161. }
  162. i++;
  163. if ( '\0' == was )
  164. break;
  165. start = sp + 1;
  166. }
  167. }
  168. free( s );
  169. }
  170. void
  171. Module::draw_box ( void )
  172. {
  173. fl_color( FL_WHITE );
  174. int tw, th, tx, ty;
  175. tw = w();
  176. th = h();
  177. ty = y();
  178. tx = x();
  179. // bbox( tx, ty, tw, th );
  180. fl_push_clip( tx, ty, tw, th );
  181. Fl_Color c = is_default() ? FL_BLACK : color();
  182. int spacing = w() / instances();
  183. for ( int i = instances(); i--; )
  184. {
  185. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  186. }
  187. if ( audio_input.size() && audio_output.size() )
  188. {
  189. /* maybe draw control indicators */
  190. if ( control_input.size() )
  191. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  192. if ( control_output.size() )
  193. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  194. }
  195. fl_pop_clip();
  196. // box( FL_NO_BOX );
  197. Fl_Group::draw_children();
  198. }
  199. void
  200. Module::draw_label ( void )
  201. {
  202. int tw, th, tx, ty;
  203. bbox( tx, ty, tw, th );
  204. const char *lp = label();
  205. int l = strlen( label() );
  206. fl_color( FL_FOREGROUND_COLOR );
  207. char *s = NULL;
  208. if ( l > 10 )
  209. {
  210. s = new char[l];
  211. char *sp = s;
  212. for ( ; *lp; ++lp )
  213. switch ( *lp )
  214. {
  215. case 'i': case 'e': case 'o': case 'u': case 'a':
  216. break;
  217. default:
  218. *(sp++) = *lp;
  219. }
  220. *sp = '\0';
  221. }
  222. if ( l > 20 )
  223. fl_font( FL_HELVETICA, 10 );
  224. else
  225. fl_font( FL_HELVETICA, 14 );
  226. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  227. if ( s )
  228. delete[] s;
  229. }
  230. #include "FL/test_press.H"
  231. int
  232. Module::handle ( int m )
  233. {
  234. switch ( m )
  235. {
  236. case FL_PUSH:
  237. {
  238. if ( test_press( FL_BUTTON1 ) )
  239. {
  240. if ( _editor )
  241. {
  242. _editor->show();
  243. }
  244. else if ( ncontrol_inputs() )
  245. {
  246. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  247. _editor = new Module_Parameter_Editor( this );
  248. _editor->show();
  249. do { Fl::wait(); }
  250. while ( _editor->shown() );
  251. DMESSAGE( "Module parameters for \"%s\" closed",label() );
  252. delete _editor;
  253. _editor = NULL;
  254. }
  255. return 1;
  256. }
  257. break;
  258. }
  259. }
  260. return Fl_Group::handle( m );
  261. }