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.

631 lines
16KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 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 "const.h"
  19. #include "debug.h"
  20. #include <FL/fl_ask.H>
  21. #include "Control_Sequence.H"
  22. #include "Track.H"
  23. #include "Engine/Engine.H" // for lock()
  24. #include <list>
  25. using std::list;
  26. #include "Transport.H"
  27. #include "OSC/Endpoint.H"
  28. bool Control_Sequence::draw_with_gradient = true;
  29. bool Control_Sequence::draw_with_polygon = true;
  30. bool Control_Sequence::draw_with_grid = true;
  31. Control_Sequence::Control_Sequence ( Track *track ) : Sequence( 0 )
  32. {
  33. init();
  34. _track = track;
  35. _osc_output = 0;
  36. _output = 0;
  37. _mode = CV;
  38. mode( OSC );
  39. if ( track )
  40. track->add( this );
  41. log_create();
  42. }
  43. Control_Sequence::~Control_Sequence ( )
  44. {
  45. // Fl::remove_timeout( &Control_Sequence::process_osc, this );
  46. Loggable::block_start();
  47. clear();
  48. log_destroy();
  49. engine->lock();
  50. track()->remove( this );
  51. engine->unlock();
  52. if ( _output )
  53. {
  54. _output->shutdown();
  55. delete _output;
  56. _output = NULL;
  57. }
  58. if ( _osc_output )
  59. {
  60. delete _osc_output;
  61. _osc_output = NULL;
  62. }
  63. for ( list<char*>::iterator i = _persistent_osc_connections.begin();
  64. i != _persistent_osc_connections.end();
  65. ++i )
  66. {
  67. free( *i );
  68. }
  69. _persistent_osc_connections.clear();
  70. Loggable::block_end();
  71. }
  72. void
  73. Control_Sequence::init ( void )
  74. {
  75. _track = NULL;
  76. _highlighted = false;
  77. _output = NULL;
  78. _osc_output = NULL;
  79. color( fl_darker( FL_YELLOW ) );
  80. interpolation( Linear );
  81. }
  82. void
  83. Control_Sequence::get ( Log_Entry &e ) const
  84. {
  85. e.add( ":track", _track );
  86. e.add( ":name", name() );
  87. }
  88. void
  89. Control_Sequence::get_unjournaled ( Log_Entry &e ) const
  90. {
  91. e.add( ":interpolation", _interpolation );
  92. if ( _osc_output && _osc_output->connected() )
  93. {
  94. DMESSAGE( "OSC Output connections: %i", _osc_output->noutput_connections() );
  95. for ( int i = 0; i < _osc_output->noutput_connections(); ++i )
  96. {
  97. char *s;
  98. s = _osc_output->get_output_connection_peer_name_and_path(i);
  99. e.add( ":osc-output", s );
  100. free( s );
  101. }
  102. }
  103. e.add( ":mode", mode() );
  104. }
  105. void
  106. Control_Sequence::set ( Log_Entry &e )
  107. {
  108. for ( int i = 0; i < e.size(); ++i )
  109. {
  110. const char *s, *v;
  111. e.get( i, &s, &v );
  112. if ( ! strcmp( ":track", s ) )
  113. {
  114. int i;
  115. sscanf( v, "%X", &i );
  116. Track *t = (Track*)Loggable::find( i );
  117. assert( t );
  118. _output = new JACK::Port( engine, JACK::Port::Output, t->name(), t->ncontrols(), "cv" );
  119. if ( ! _output->activate() )
  120. {
  121. FATAL( "could not create JACK port" );
  122. }
  123. t->add( this );
  124. }
  125. else if ( ! strcmp( ":name", s ) )
  126. {
  127. name( v );
  128. }
  129. else if ( ! strcmp( ":interpolation", s ) )
  130. {
  131. interpolation( (Curve_Type)atoi( v ) );
  132. }
  133. else if ( ! strcmp( ":mode", s ) )
  134. mode( (Mode)atoi( v ) );
  135. else if ( ! strcmp( ":osc-output", s ) )
  136. {
  137. _persistent_osc_connections.push_back( strdup( v ) );
  138. }
  139. }
  140. }
  141. void
  142. Control_Sequence::mode ( Mode m )
  143. {
  144. if ( CV != m && mode() == CV )
  145. {
  146. if ( _output )
  147. {
  148. _output->shutdown();
  149. delete _output;
  150. _output = NULL;
  151. }
  152. }
  153. else if ( OSC != m && mode() == OSC )
  154. {
  155. if ( _osc_output )
  156. {
  157. delete _osc_output;
  158. _osc_output = NULL;
  159. }
  160. }
  161. if ( CV == m && mode() != CV )
  162. {
  163. _output = new JACK::Port( engine, JACK::Port::Output, track()->name(), track()->ncontrols(), "cv" );
  164. if ( ! _output->activate() )
  165. {
  166. fl_alert( "Could not create JACK port for control output on %s", track()->name() );
  167. delete _output;
  168. _output = NULL;
  169. }
  170. }
  171. else if ( OSC == m && mode() != OSC )
  172. {
  173. char *path;
  174. asprintf( &path, "/track/%s/control/%i", track()->name(), track()->ncontrols() );
  175. _osc_output = timeline->osc->add_signal( path, OSC::Signal::Output, 0, 1, 0, NULL, NULL );
  176. free( path );
  177. connect_osc();
  178. }
  179. _mode = m;
  180. }
  181. void
  182. Control_Sequence::draw_curve ( bool flip, bool filled )
  183. {
  184. const int bx = x();
  185. const int by = y() + Fl::box_dy( box() );
  186. const int bw = w();
  187. const int bh = h() - Fl::box_dh( box() );
  188. list <Sequence_Widget *>::const_iterator e = _widgets.end();
  189. e--;
  190. if ( _widgets.size() )
  191. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); ; r++ )
  192. {
  193. const int ry = (*r)->y();
  194. if ( r == _widgets.begin() )
  195. {
  196. if ( flip )
  197. {
  198. if ( filled )
  199. fl_vertex( bx, by );
  200. fl_vertex( bx, ry );
  201. }
  202. else
  203. {
  204. if ( filled )
  205. fl_vertex( bx, bh + by );
  206. fl_vertex( bx, ry );
  207. }
  208. }
  209. fl_vertex( (*r)->line_x(), ry );
  210. if ( r == e )
  211. {
  212. if ( flip )
  213. {
  214. fl_vertex( bx + bw, ry );
  215. if ( filled )
  216. fl_vertex( bx + bw, by );
  217. }
  218. else
  219. {
  220. fl_vertex( bx + bw, ry );
  221. if ( filled )
  222. fl_vertex( bx + bw, bh + by );
  223. }
  224. break;
  225. }
  226. }
  227. }
  228. void
  229. Control_Sequence::draw ( void )
  230. {
  231. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  232. return;
  233. fl_push_clip( x(), y(), w(), h() );
  234. /* draw the box with the ends cut off. */
  235. draw_box( box(), x() - Fl::box_dx( box() ), y(), w() + Fl::box_dw( box() ) + 1, h(), color() );
  236. const int bx = x();
  237. const int by = y() + Fl::box_dy( box() );
  238. const int bw = w();
  239. const int bh = h() - Fl::box_dh( box() );
  240. int X, Y, W, H;
  241. fl_clip_box( bx, by, bw, bh, X, Y, W, H );
  242. bool active = active_r();
  243. const Fl_Color color = active ? this->color() : fl_inactive( this->color() );
  244. const Fl_Color selection_color = active ? this->selection_color() : fl_inactive( this->selection_color() );
  245. if ( draw_with_gradient )
  246. {
  247. /* const Fl_Color c2 = fl_color_average( selection_color, FL_WHITE, 0.90f ); */
  248. /* const Fl_Color c1 = fl_color_average( color, c2, 0.60f ); */
  249. const Fl_Color c1 = fl_color_average( selection_color, FL_WHITE, 0.90f );
  250. const Fl_Color c2 = fl_color_average( color, c1, 0.60f );
  251. for ( int gy = 0; gy < bh; gy++ )
  252. {
  253. fl_color( fl_color_average( c1, c2, gy / (float)bh) );
  254. fl_line( X, by + gy, X + W, by + gy );
  255. }
  256. }
  257. if ( draw_with_grid )
  258. {
  259. fl_color( fl_darker( color ) );
  260. const int inc = bh / 10;
  261. if ( inc )
  262. for ( int gy = 0; gy < bh; gy += inc )
  263. fl_line( X, by + gy, X + W, by + gy );
  264. }
  265. if ( interpolation() != None )
  266. {
  267. if ( draw_with_polygon )
  268. {
  269. fl_color( draw_with_gradient ? color : fl_color_average( color, selection_color, 0.45f ) );
  270. fl_begin_complex_polygon();
  271. draw_curve( draw_with_gradient, true );
  272. fl_end_complex_polygon();
  273. fl_color( selection_color );
  274. fl_line_style( FL_SOLID, 2 );
  275. fl_begin_line();
  276. draw_curve( draw_with_gradient, false );
  277. fl_end_line();
  278. }
  279. else
  280. {
  281. // fl_color( fl_color_average( selection_color, color, 0.70f ) );
  282. fl_color( selection_color );
  283. fl_line_style( FL_SOLID, 2 );
  284. fl_begin_line();
  285. draw_curve( draw_with_gradient, false );
  286. fl_end_line();
  287. }
  288. fl_line_style( FL_SOLID, 0 );
  289. }
  290. timeline->draw_measure_lines( x(), y(), w(), h(), color );
  291. if ( interpolation() == None || _highlighted || Fl::focus() == this )
  292. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  293. (*r)->draw_box();
  294. else
  295. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  296. if ( (*r)->selected() )
  297. (*r)->draw_box();
  298. fl_pop_clip();
  299. }
  300. #include "FL/menu_popup.H"
  301. void
  302. Control_Sequence::menu_cb ( Fl_Widget *w, void *v )
  303. {
  304. ((Control_Sequence*)v)->menu_cb( (const Fl_Menu_*)w );
  305. }
  306. void
  307. Control_Sequence::menu_cb ( const Fl_Menu_ *m )
  308. {
  309. char picked[1024];
  310. if ( ! m->mvalue() ) // || m->mvalue()->flags & FL_SUBMENU_POINTER || m->mvalue()->flags & FL_SUBMENU )
  311. return;
  312. m->item_pathname( picked, sizeof( picked ), m->mvalue() );
  313. if ( ! strncmp( picked, "Connect To/", strlen( "Connect To/" ) ) )
  314. {
  315. char *peer_name = index( picked, '/' ) + 1;
  316. *index( peer_name, '/' ) = 0;
  317. const char *path = ((OSC::Signal*)m->mvalue()->user_data())->path();
  318. char *peer_and_path;
  319. asprintf( &peer_and_path, "%s:%s", peer_name, path );
  320. if ( ! _osc_output->is_connected_to( ((OSC::Signal*)m->mvalue()->user_data()) ) )
  321. {
  322. _persistent_osc_connections.push_back( peer_and_path );
  323. connect_osc();
  324. }
  325. else
  326. {
  327. timeline->osc->disconnect_signal( _osc_output, peer_name, path );
  328. for ( std::list<char*>::iterator i = _persistent_osc_connections.begin();
  329. i != _persistent_osc_connections.end();
  330. ++i )
  331. {
  332. if ( !strcmp( *i, peer_and_path ) )
  333. {
  334. free( *i );
  335. i = _persistent_osc_connections.erase( i );
  336. break;
  337. }
  338. }
  339. free( peer_and_path );
  340. }
  341. }
  342. else if ( ! strcmp( picked, "Interpolation/Linear" ) )
  343. interpolation( Linear );
  344. else if ( ! strcmp( picked, "Interpolation/None" ) )
  345. interpolation( None );
  346. else if ( ! strcmp( picked, "Mode/Control Signal (OSC)" ))
  347. mode( OSC );
  348. else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
  349. mode( CV );
  350. else if ( ! strcmp( picked, "/Rename" ) )
  351. {
  352. const char *s = fl_input( "Input new name for control sequence:", name() );
  353. if ( s )
  354. name( s );
  355. redraw();
  356. }
  357. else if ( !strcmp( picked, "/Remove" ) )
  358. {
  359. Fl::delete_widget( this );
  360. }
  361. }
  362. void
  363. Control_Sequence::connect_osc ( void )
  364. {
  365. if ( _persistent_osc_connections.size() )
  366. {
  367. for ( std::list<char*>::iterator i = _persistent_osc_connections.begin();
  368. i != _persistent_osc_connections.end();
  369. ++i )
  370. {
  371. if ( ! timeline->osc->connect_signal( _osc_output, *i ) )
  372. {
  373. // MESSAGE( "Failed to connect output %s to ", _osc_output->path(), *i );
  374. }
  375. else
  376. {
  377. MESSAGE( "Connected output %s to %s", _osc_output->path(), *i );
  378. // tooltip( _osc_connected_path );
  379. }
  380. }
  381. }
  382. }
  383. void
  384. Control_Sequence::process_osc ( void *v )
  385. {
  386. ((Control_Sequence*)v)->process_osc();
  387. }
  388. void
  389. Control_Sequence::process_osc ( void )
  390. {
  391. if ( _osc_output && _osc_output->connected() )
  392. {
  393. sample_t buf[1];
  394. play( buf, (nframes_t)transport->frame, (nframes_t) 1 );
  395. _osc_output->value( (float)buf[0] );
  396. }
  397. }
  398. void
  399. Control_Sequence::peer_callback( const char *name, const OSC::Signal *sig, void *v )
  400. {
  401. ((Control_Sequence*)v)->peer_callback( name, sig );
  402. }
  403. static Fl_Menu_Button *peer_menu;
  404. static const char *peer_prefix;
  405. void
  406. Control_Sequence::peer_callback( const char *name, const OSC::Signal *sig )
  407. {
  408. char *s;
  409. /* only show inputs */
  410. if ( sig->direction() != OSC::Signal::Input )
  411. return;
  412. /* only list CV signals for now */
  413. if ( ! ( sig->parameter_limits().min == 0.0 &&
  414. sig->parameter_limits().max == 1.0 ) )
  415. return;
  416. asprintf( &s, "%s/%s%s", peer_prefix, name, sig->path() );
  417. peer_menu->add( s, 0, NULL, (void*)( sig ),
  418. FL_MENU_TOGGLE |
  419. ( _osc_output->is_connected_to( sig ) ? FL_MENU_VALUE : 0 ) );
  420. free( s );
  421. connect_osc();
  422. }
  423. void
  424. Control_Sequence::add_osc_peers_to_menu ( Fl_Menu_Button *m, const char *prefix )
  425. {
  426. peer_menu = m;
  427. peer_prefix = prefix;
  428. timeline->osc->list_peer_signals( &Control_Sequence::peer_callback, this );
  429. }
  430. int
  431. Control_Sequence::handle ( int m )
  432. {
  433. switch ( m )
  434. {
  435. case FL_ENTER:
  436. _highlighted = true;
  437. fl_cursor( cursor() );
  438. redraw();
  439. return 1;
  440. case FL_LEAVE:
  441. _highlighted = false;
  442. redraw();
  443. return 1;
  444. default:
  445. break;
  446. }
  447. int r = Sequence::handle( m );
  448. if ( r )
  449. return r;
  450. switch ( m )
  451. {
  452. case FL_PUSH:
  453. {
  454. Logger log( this );
  455. if ( Fl::event_button1() )
  456. {
  457. Control_Point *r = new Control_Point( this, timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() ), (float)(Fl::event_y() - y()) / h() );
  458. add( r );
  459. }
  460. else if ( Fl::event_button3() && ! ( Fl::event_state() & ( FL_ALT | FL_SHIFT | FL_CTRL ) ) )
  461. {
  462. Fl_Menu_Button menu( 0, 0, 0, 0, "Control Sequence" );
  463. menu.clear();
  464. if ( mode() == OSC )
  465. {
  466. add_osc_peers_to_menu( &menu, "Connect To" );
  467. }
  468. menu.add( "Interpolation/None", 0, 0, 0, FL_MENU_RADIO | ( interpolation() == None ? FL_MENU_VALUE : 0 ) );
  469. menu.add( "Interpolation/Linear", 0, 0, 0, FL_MENU_RADIO | ( interpolation() == Linear ? FL_MENU_VALUE : 0 ) );
  470. menu.add( "Mode/Control Voltage (JACK)", 0, 0, 0 ,FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) );
  471. menu.add( "Mode/Control Signal (OSC)", 0, 0, 0 , FL_MENU_RADIO | ( mode() == OSC ? FL_MENU_VALUE : 0 ) );
  472. menu.add( "Rename", 0, 0, 0 );
  473. menu.add( "Remove", 0, 0, 0 );
  474. menu.callback( &Control_Sequence::menu_cb, (void*)this);
  475. menu_popup( &menu, x(), y() );
  476. return 1;
  477. }
  478. return 1;
  479. }
  480. default:
  481. return 0;
  482. }
  483. }