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.

808 lines
19KB

  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 <FL/Fl_Color_Chooser.H>
  22. #include "Control_Sequence.H"
  23. #include "Track.H"
  24. #include "Engine/Engine.H" // for lock()
  25. #include "Track_Header.H"
  26. #include <list>
  27. using std::list;
  28. #include "Transport.H"
  29. #include "OSC/Endpoint.H"
  30. #include "string_util.h"
  31. #include "FL/event_name.H"
  32. #include "FL/test_press.H"
  33. #include <FL/Fl_Menu_Button.H>
  34. #include "FL/menu_popup.H"
  35. #define DAMAGE_SEQUENCE FL_DAMAGE_USER1
  36. #define DAMAGE_HEADER FL_DAMAGE_USER2
  37. bool Control_Sequence::draw_with_grid = true;
  38. bool Control_Sequence::draw_with_polygon = true;
  39. Fl_Widget * Control_Sequence::_highlighted = 0;
  40. Control_Sequence::Control_Sequence ( ) : Sequence( 0 )
  41. {
  42. init();
  43. }
  44. Control_Sequence::Control_Sequence ( Track *track, const char *name ) : Sequence( 0 )
  45. {
  46. init();
  47. _track = track;
  48. this->name( name );
  49. mode( OSC );
  50. if ( track )
  51. track->add( this );
  52. log_create();
  53. }
  54. Control_Sequence::~Control_Sequence ( )
  55. {
  56. Loggable::block_start();
  57. clear();
  58. log_destroy();
  59. track()->remove( this );
  60. if ( _output )
  61. {
  62. _output->shutdown();
  63. delete _output;
  64. _output = NULL;
  65. }
  66. if ( _osc_output() )
  67. {
  68. OSC::Signal *t = _osc_output();
  69. _osc_output( NULL );
  70. delete t;
  71. }
  72. for ( list<char*>::iterator i = _persistent_osc_connections.begin();
  73. i != _persistent_osc_connections.end();
  74. ++i )
  75. {
  76. free( *i );
  77. }
  78. _persistent_osc_connections.clear();
  79. Loggable::block_end();
  80. }
  81. const char *
  82. Control_Sequence::name ( void ) const
  83. {
  84. return Sequence::name();
  85. }
  86. void
  87. Control_Sequence::name ( const char *s )
  88. {
  89. char *n = track()->get_unique_control_name( s );
  90. Sequence::name( n );
  91. header()->name_input->value( n );
  92. if ( mode() == CV )
  93. update_port_name();
  94. else
  95. update_osc_path();
  96. redraw();
  97. }
  98. void
  99. Control_Sequence::update_osc_path ( void )
  100. {
  101. char *path;
  102. asprintf( &path, "/track/%s/%s", track()->name(), name() );
  103. char *s = escape_url( path );
  104. free( path );
  105. path = s;
  106. if ( !_osc_output() )
  107. {
  108. OSC::Signal *t = timeline->osc->add_signal( path, OSC::Signal::Output, 0, 1, 0, NULL, NULL );
  109. _osc_output( t );
  110. }
  111. else
  112. {
  113. _osc_output()->rename( path );
  114. }
  115. free(path);
  116. }
  117. void
  118. Control_Sequence::update_port_name ( void )
  119. {
  120. bool needs_activation = false;
  121. char s[512];
  122. snprintf( s, sizeof(s), "%s-cv", name() );
  123. if ( ! _output )
  124. {
  125. _output = new JACK::Port( engine, track()->name(), s, JACK::Port::Output, JACK::Port::CV );
  126. _output->terminal( true );
  127. needs_activation = true;
  128. }
  129. if ( name() )
  130. {
  131. _output->trackname( track()->name() );
  132. _output->name( s );
  133. _output->rename();
  134. }
  135. if ( needs_activation )
  136. {
  137. if ( ! _output->activate() )
  138. {
  139. fl_alert( "Could not create JACK port for control output on track \"%s\"", track()->name() );
  140. delete _output;
  141. _output = NULL;
  142. }
  143. }
  144. }
  145. void
  146. Control_Sequence::cb_button ( Fl_Widget *w, void *v )
  147. {
  148. ((Control_Sequence*)v)->cb_button( w );
  149. }
  150. void
  151. Control_Sequence::cb_button ( Fl_Widget *w )
  152. {
  153. Logger log(this);
  154. if ( w == header()->name_input )
  155. {
  156. name( header()->name_input->value() );
  157. }
  158. else if ( w == header()->delete_button )
  159. {
  160. Fl::delete_widget( this );
  161. }
  162. else if ( w == header()->menu_button )
  163. {
  164. menu_popup( &menu(), header()->menu_button->x(), header()->menu_button->y() );
  165. }
  166. /* else if ( w == header()->promote_button ) */
  167. /* { */
  168. /* track()->sequence( this ); */
  169. /* } */
  170. }
  171. void
  172. Control_Sequence::init ( void )
  173. {
  174. timeline->osc->peer_signal_notification_callback( &Control_Sequence::peer_callback, NULL );
  175. labeltype( FL_NO_LABEL );
  176. {
  177. Control_Sequence_Header *o = new Control_Sequence_Header( x(), y(), Track::width(), 52 );
  178. o->name_input->callback( cb_button, this );
  179. o->delete_button->callback( cb_button, this );
  180. o->menu_button->callback( cb_button, this );
  181. /* o->promote_button->callback( cb_button, this ); */
  182. Fl_Group::add( o );
  183. }
  184. resizable(0);
  185. box( FL_NO_BOX );
  186. _track = NULL;
  187. _output = NULL;
  188. __osc_output = NULL;
  189. _mode = (Mode)-1;
  190. interpolation( Linear );
  191. }
  192. void
  193. Control_Sequence::get ( Log_Entry &e ) const
  194. {
  195. e.add( ":track", _track );
  196. e.add( ":name", name() );
  197. e.add( ":color", color() );
  198. }
  199. void
  200. Control_Sequence::get_unjournaled ( Log_Entry &e ) const
  201. {
  202. e.add( ":interpolation", _interpolation );
  203. /* if ( _osc_output() && _osc_output()->connected() ) */
  204. /* { */
  205. /* DMESSAGE( "OSC Output connections: %i", _osc_output()->noutput_connections() ); */
  206. /* for ( int i = 0; i < _osc_output()->noutput_connections(); ++i ) */
  207. /* { */
  208. /* char *s; */
  209. /* s = _osc_output()->get_output_connection_peer_name_and_path(i); */
  210. /* e.add( ":osc-output", s ); */
  211. /* free( s ); */
  212. /* } */
  213. /* } */
  214. e.add( ":mode", mode() );
  215. }
  216. void
  217. Control_Sequence::set ( Log_Entry &e )
  218. {
  219. for ( int i = 0; i < e.size(); ++i )
  220. {
  221. const char *s, *v;
  222. e.get( i, &s, &v );
  223. if ( ! strcmp( ":track", s ) )
  224. {
  225. int i;
  226. sscanf( v, "%X", &i );
  227. Track *t = (Track*)Loggable::find( i );
  228. assert( t );
  229. t->add( this );
  230. }
  231. else if ( ! strcmp( ":name", s ) )
  232. {
  233. name( v );
  234. }
  235. else if ( ! strcmp( ":interpolation", s ) )
  236. {
  237. interpolation( (Curve_Type)atoi( v ) );
  238. }
  239. else if ( ! strcmp( ":mode", s ) )
  240. mode( (Mode)atoi( v ) );
  241. else if ( ! strcmp( ":osc-output", s ) )
  242. {
  243. _persistent_osc_connections.push_back( strdup( v ) );
  244. }
  245. else if ( ! strcmp( ":color", s ) )
  246. {
  247. color( (Fl_Color)atol( v ) );
  248. }
  249. }
  250. }
  251. void
  252. Control_Sequence::mode ( Mode m )
  253. {
  254. if ( CV != m && mode() == CV )
  255. {
  256. if ( _output )
  257. {
  258. _output->shutdown();
  259. JACK::Port *t = _output;
  260. _output = NULL;
  261. delete t;
  262. }
  263. }
  264. else if ( OSC != m && mode() == OSC )
  265. {
  266. if ( _osc_output() )
  267. {
  268. OSC::Signal *t = _osc_output();
  269. _osc_output( NULL );
  270. delete t;
  271. }
  272. }
  273. if ( CV == m && mode() != CV )
  274. {
  275. update_port_name();
  276. header()->outputs_indicator->label( "cv" );
  277. }
  278. else if ( OSC == m && mode() != OSC )
  279. {
  280. update_osc_path();
  281. header()->outputs_indicator->label( "osc" );
  282. }
  283. _mode = m;
  284. }
  285. void
  286. Control_Sequence::draw_curve ( bool filled )
  287. {
  288. const int bx = drawable_x();
  289. const int by = y() + Fl::box_dy( box() );
  290. const int bw = drawable_w();
  291. const int bh = h() - Fl::box_dh( box() );
  292. /* make a copy of the list for drawing and sort it... */
  293. list <Sequence_Widget *> wl;
  294. std::copy( _widgets.begin(), _widgets.end(), std::back_inserter( wl ) );
  295. //= new list <const Sequence_Widget *>(_widgets);
  296. wl.sort( Sequence_Widget::sort_func );
  297. list <Sequence_Widget *>::const_iterator e = wl.end();
  298. e--;
  299. if ( wl.size() )
  300. for ( list <Sequence_Widget *>::const_iterator r = wl.begin(); ; r++ )
  301. {
  302. const int ry = (*r)->y();
  303. const long rx = (*r)->curve_x();
  304. if ( r == wl.begin() )
  305. {
  306. if ( filled )
  307. fl_vertex( bx, bh + by );
  308. fl_vertex( bx, ry );
  309. }
  310. fl_vertex( rx, ry );
  311. if ( r == e )
  312. {
  313. fl_vertex( bx + bw, ry );
  314. if ( filled )
  315. fl_vertex( bx + bw, bh + by );
  316. break;
  317. }
  318. }
  319. }
  320. void
  321. Control_Sequence::draw_box ( void )
  322. {
  323. const int bx = drawable_x();
  324. const int by = y();
  325. const int bw = drawable_w();
  326. const int bh = h();
  327. int X, Y, W, H;
  328. fl_clip_box( bx, by, bw, bh, X, Y, W, H );
  329. // fl_rectf( X, Y, W, H, fl_color_average( FL_BLACK, FL_BACKGROUND_COLOR, 0.3 ) );
  330. // fl_rectf( X,Y,W,H, fl_color_average( FL_BLACK, FL_WHITE, 0.90 ) );
  331. fl_rectf( X,Y,W,H, FL_DARK1 );
  332. if ( draw_with_grid )
  333. {
  334. fl_color( FL_GRAY );
  335. const int inc = bh / 10;
  336. if ( inc )
  337. for ( int gy = 0; gy < bh; gy += inc )
  338. fl_line( X, by + gy, X + W, by + gy );
  339. }
  340. timeline->draw_measure_lines( X, Y, W, H );
  341. }
  342. void
  343. Control_Sequence::draw ( void )
  344. {
  345. fl_push_clip( drawable_x(), y(), drawable_w(), h() );
  346. const int bx = x();
  347. const int by = y() + Fl::box_dy( box() );
  348. const int bw = w();
  349. const int bh = h() - Fl::box_dh( box() );
  350. int X, Y, W, H;
  351. fl_clip_box( bx, by, bw, bh, X, Y, W, H );
  352. bool active = active_r();
  353. const Fl_Color color = active ? this->color() : fl_inactive( this->color() );
  354. // const Fl_Color selection_color = active ? this->selection_color() : fl_inactive( this->selection_color() );
  355. if ( box() != FL_NO_BOX )
  356. draw_box();
  357. if ( interpolation() != None )
  358. {
  359. if ( draw_with_polygon )
  360. {
  361. fl_color( fl_color_add_alpha( color, 60 ) );
  362. fl_begin_complex_polygon();
  363. draw_curve( true );
  364. fl_end_complex_polygon();
  365. }
  366. fl_color( fl_color_average( FL_WHITE, color, 0.5 ) );
  367. fl_line_style( FL_SOLID, 2 );
  368. fl_begin_line();
  369. draw_curve( false );
  370. fl_end_line();
  371. fl_line_style( FL_SOLID, 0 );
  372. }
  373. if ( interpolation() == None || _highlighted == this || Fl::focus() == this )
  374. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  375. if ( (*r)->x() + (*r)->w() >= bx &&
  376. (*r)->x() <= bw + bw )
  377. (*r)->draw_box();
  378. else
  379. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  380. if ( (*r)->selected() )
  381. if ( (*r)->x() + (*r)->w() >= bx &&
  382. (*r)->x() <= bw + bw )
  383. (*r)->draw_box();
  384. fl_pop_clip();
  385. if ( damage() & ~DAMAGE_SEQUENCE )
  386. {
  387. Fl_Group::draw_children();
  388. }
  389. }
  390. void
  391. Control_Sequence::menu_cb ( Fl_Widget *w, void *v )
  392. {
  393. ((Control_Sequence*)v)->menu_cb( (const Fl_Menu_*)w );
  394. }
  395. void
  396. Control_Sequence::menu_cb ( const Fl_Menu_ *m )
  397. {
  398. char picked[1024];
  399. DMESSAGE( "Control_Sequence: menu_cb" );
  400. if ( ! m->mvalue() || m->mvalue()->flags & ( FL_SUBMENU_POINTER | FL_SUBMENU ))
  401. return;
  402. m->item_pathname( picked, sizeof( picked ), m->mvalue() );
  403. if ( ! strncmp( picked, "Connect To/", strlen( "Connect To/" ) ) )
  404. {
  405. char *peer_name = index( picked, '/' ) + 1;
  406. *index( peer_name, '/' ) = 0;
  407. const char *path = ((OSC::Signal*)m->mvalue()->user_data())->path();
  408. /* if ( ! _osc_output()->is_connected_to( ((OSC::Signal*)m->mvalue()->user_data()) ) ) */
  409. {
  410. _persistent_osc_connections.push_back( strdup(path) );
  411. connect_osc();
  412. }
  413. /* else */
  414. /* { */
  415. /* timeline->osc->disconnect_signal( _osc_output(), path ); */
  416. /* for ( std::list<char*>::iterator i = _persistent_osc_connections.begin(); */
  417. /* i != _persistent_osc_connections.end(); */
  418. /* ++i ) */
  419. /* { */
  420. /* if ( !strcmp( *i, path ) ) */
  421. /* { */
  422. /* free( *i ); */
  423. /* i = _persistent_osc_connections.erase( i ); */
  424. /* break; */
  425. /* } */
  426. /* } */
  427. /* //free( path ); */
  428. /* } */
  429. }
  430. else if ( ! strcmp( picked, "Interpolation/Linear" ) )
  431. interpolation( Linear );
  432. else if ( ! strcmp( picked, "Interpolation/None" ) )
  433. interpolation( None );
  434. else if ( ! strcmp( picked, "Mode/Control Signal (OSC)" ))
  435. mode( OSC );
  436. else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
  437. mode( CV );
  438. else if ( ! strcmp( picked, "/Rename" ) )
  439. {
  440. ((Fl_Sometimes_Input*)header()->name_input)->take_focus();
  441. }
  442. else if ( !strcmp( picked, "/Remove" ) )
  443. {
  444. Fl::delete_widget( this );
  445. }
  446. else if ( ! strcmp( picked, "/Color" ) )
  447. {
  448. unsigned char r, g, b;
  449. Fl::get_color( color(), r, g, b );
  450. if ( fl_color_chooser( "Track Color", r, g, b ) )
  451. {
  452. color( fl_rgb_color( r, g, b ) );
  453. }
  454. redraw();
  455. }
  456. }
  457. void
  458. Control_Sequence::connect_osc ( void )
  459. {
  460. timeline->osc_thread->lock();
  461. if ( _persistent_osc_connections.size() )
  462. {
  463. for ( std::list<char*>::iterator i = _persistent_osc_connections.begin();
  464. i != _persistent_osc_connections.end();
  465. ++i )
  466. {
  467. if ( ! timeline->osc->connect_signal( _osc_output(), *i ) )
  468. {
  469. /* WARNING( "Failed to connect output %s to %s", _osc_output()->path(), *i ); */
  470. }
  471. else
  472. {
  473. MESSAGE( "Connected output %s to %s", _osc_output()->path(), *i );
  474. // tooltip( _osc_connected_path );
  475. }
  476. }
  477. }
  478. /* header()->outputs_indicator->value( _osc_output() && _osc_output()->connected() ); */
  479. timeline->osc_thread->unlock();
  480. }
  481. void
  482. Control_Sequence::process_osc ( void )
  483. {
  484. if ( mode() != OSC )
  485. return;
  486. if ( _osc_output() )
  487. {
  488. sample_t buf[1];
  489. play( buf, (nframes_t)transport->frame, (nframes_t) 1 );
  490. _osc_output()->value( (float)buf[0] );
  491. }
  492. }
  493. static Fl_Menu_Button *peer_menu;
  494. static const char *peer_prefix;
  495. void
  496. Control_Sequence::update_osc_connection_state ( void )
  497. {
  498. /* header()->outputs_indicator->value( _osc_output() && _osc_output()->connected() ); */
  499. }
  500. void
  501. Control_Sequence::peer_callback( OSC::Signal *sig, OSC::Signal::State state, void *v )
  502. {
  503. char *s;
  504. /* only show inputs */
  505. if ( sig->direction() != OSC::Signal::Input )
  506. return;
  507. // DMESSAGE( "Paramter limits: %f %f", sig->parameter_limits().min, sig->parameter_limits().max );
  508. /* only list CV signals for now */
  509. if ( ! ( sig->parameter_limits().min == 0.0 &&
  510. sig->parameter_limits().max == 1.0 ) )
  511. return;
  512. if ( ! v )
  513. {
  514. if( state == OSC::Signal::Created )
  515. timeline->connect_osc();
  516. else
  517. timeline->update_osc_connection_state();
  518. }
  519. else
  520. {
  521. /* building menu */
  522. // const char *name = sig->peer_name();
  523. assert( sig->path() );
  524. char *path = strdup( sig->path() );
  525. unescape_url( path );
  526. asprintf( &s, "%s/%s", peer_prefix, path );
  527. peer_menu->add( s, 0, NULL, (void*)( sig ), 0 );
  528. /* FL_MENU_TOGGLE | */
  529. /* ( ((Control_Sequence*)v)->_osc_output()->is_connected_to( sig ) ? FL_MENU_VALUE : 0 ) ); */
  530. free( path );
  531. free( s );
  532. }
  533. }
  534. void
  535. Control_Sequence::add_osc_peers_to_menu ( Fl_Menu_Button *m, const char *prefix )
  536. {
  537. peer_menu = m;
  538. peer_prefix = prefix;
  539. timeline->osc->list_peer_signals( this );
  540. }
  541. Fl_Menu_Button &
  542. Control_Sequence::menu ( void )
  543. {
  544. static Fl_Menu_Button _menu( 0, 0, 0, 0, "Control Sequence" );
  545. _menu.clear();
  546. if ( mode() == OSC )
  547. {
  548. add_osc_peers_to_menu( &_menu, "Connect To" );
  549. }
  550. _menu.add( "Interpolation/None", 0, 0, 0, FL_MENU_RADIO | ( interpolation() == None ? FL_MENU_VALUE : 0 ) );
  551. _menu.add( "Interpolation/Linear", 0, 0, 0, FL_MENU_RADIO | ( interpolation() == Linear ? FL_MENU_VALUE : 0 ) );
  552. _menu.add( "Mode/Control Voltage (JACK)", 0, 0, 0 ,FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) );
  553. _menu.add( "Mode/Control Signal (OSC)", 0, 0, 0 , FL_MENU_RADIO | ( mode() == OSC ? FL_MENU_VALUE : 0 ) );
  554. _menu.add( "Rename", 0, 0, 0 );
  555. _menu.add( "Color", 0, 0, 0 );
  556. _menu.add( "Remove", 0, 0, 0 );
  557. _menu.callback( &Control_Sequence::menu_cb, (void*)this);
  558. return _menu;
  559. }
  560. int
  561. Control_Sequence::handle ( int m )
  562. {
  563. switch ( m )
  564. {
  565. case FL_ENTER:
  566. break;
  567. case FL_LEAVE:
  568. _highlighted = 0;
  569. damage( DAMAGE_SEQUENCE );
  570. fl_cursor( FL_CURSOR_DEFAULT );
  571. break;
  572. case FL_MOVE:
  573. if ( Fl::event_x() > drawable_x() )
  574. {
  575. if ( _highlighted != this )
  576. {
  577. _highlighted = this;
  578. damage( DAMAGE_SEQUENCE );
  579. fl_cursor( FL_CURSOR_CROSS );
  580. }
  581. }
  582. else
  583. {
  584. if ( _highlighted == this )
  585. {
  586. _highlighted = 0;
  587. damage( DAMAGE_SEQUENCE );
  588. fl_cursor( FL_CURSOR_DEFAULT );
  589. }
  590. }
  591. default:
  592. break;
  593. }
  594. Logger log(this);
  595. int r = Sequence::handle( m );
  596. if ( r )
  597. return r;
  598. switch ( m )
  599. {
  600. case FL_PUSH:
  601. {
  602. if ( Fl::event_x() >= drawable_x() &&
  603. test_press( FL_BUTTON1 ) )
  604. {
  605. /* insert new control point */
  606. timeline->sequence_lock.wrlock();
  607. new Control_Point( this, timeline->xoffset + timeline->x_to_ts( Fl::event_x() - drawable_x() ), (float)(Fl::event_y() - y()) / h() );
  608. timeline->sequence_lock.unlock();
  609. return 1;
  610. }
  611. else if ( Fl::event_x() < drawable_x() &&
  612. test_press( FL_BUTTON3 ) )
  613. {
  614. menu_popup( &menu() );
  615. return 1;
  616. }
  617. return Fl_Group::handle( m );
  618. }
  619. default:
  620. return 0;
  621. }
  622. }