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.

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