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.

767 lines
18KB

  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. /* This is the main mixer group. It contains and manages Mixer_Strips. */
  19. #include "const.h"
  20. #include "Mixer.H"
  21. #include "Mixer_Strip.H"
  22. #include <FL/Fl_Pack.H>
  23. #include <FL/Fl_Scroll.H>
  24. #include <FL/Fl_Menu_Bar.H>
  25. #include <FL/fl_ask.H>
  26. #include <FL/Fl.H>
  27. #include "New_Project_Dialog.H"
  28. #include "Engine/Engine.H"
  29. #include "FL/Fl_Flowpack.H"
  30. #include "Project.H"
  31. #include "FL/Fl_Menu_Settings.H"
  32. #include "About_Dialog.H"
  33. #include <FL/Fl_File_Chooser.H>
  34. #include "file.h"
  35. #include <string.h>
  36. #include "debug.h"
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include "FL/color_scheme.H"
  40. #include "OSC/Endpoint.H"
  41. #include <lo/lo.h>
  42. #include "FL/Fl_Blinker.H"
  43. #include "OSC/Endpoint.H"
  44. const double STATUS_UPDATE_FREQ = 0.2f;
  45. const double OSC_INTERVAL = 1.0 / 20.0; /* 20 hz */
  46. extern char *user_config_dir;
  47. extern char *instance_name;
  48. #include "debug.h"
  49. #include "NSM.H"
  50. extern NSM_Client *nsm;
  51. /* static void update_cb( void *v ) { */
  52. /* Fl::repeat_timeout( STATUS_UPDATE_FREQ, update_cb, v ); */
  53. /* ((Mixer*)v)->update(); */
  54. /* } */
  55. /************************/
  56. /* OSC Message Handlers */
  57. /************************/
  58. #undef OSC_REPLY_OK
  59. #undef OSC_REPLY_ERR
  60. #undef OSC_REPLY
  61. #define OSC_REPLY_OK() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, 0, "OK" )
  62. #define OSC_REPLY( value ) ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, value )
  63. #define OSC_REPLY_ERR(errcode, value) ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path,errcode, value )
  64. OSC_HANDLER( add_strip )
  65. {
  66. OSC_DMSG();
  67. ((Mixer*)(OSC_ENDPOINT())->owner)->command_add_strip();
  68. OSC_REPLY_OK();
  69. return 0;
  70. }
  71. void
  72. Mixer::reply_to_finger ( lo_message msg )
  73. {
  74. int argc = lo_message_get_argc( msg );
  75. lo_arg **argv = lo_message_get_argv( msg );
  76. if ( argc < 1 )
  77. return;
  78. lo_address to = lo_address_new_from_url( &argv[0]->s );
  79. osc_endpoint->send( to,
  80. "/non/hello",
  81. osc_endpoint->url(),
  82. APP_NAME,
  83. VERSION,
  84. instance_name );
  85. lo_address_free( to );
  86. }
  87. void
  88. Mixer::say_hello ( void )
  89. {
  90. lo_message m = lo_message_new();
  91. lo_message_add( m, "sssss",
  92. "/non/hello",
  93. osc_endpoint->url(),
  94. APP_NAME,
  95. VERSION,
  96. instance_name );
  97. nsm->broadcast( m );
  98. lo_message_free( m );
  99. }
  100. static
  101. Fl_Menu_Item *
  102. find_item( Fl_Menu_ *menu, const char *path )
  103. {
  104. return const_cast<Fl_Menu_Item*>(menu->find_item( path ));
  105. }
  106. void
  107. Mixer::sm_active ( bool b )
  108. {
  109. sm_blinker->value( b );
  110. sm_blinker->tooltip( nsm->session_manager_name() );
  111. if ( b )
  112. {
  113. find_item( menubar, "&Project/&Open" )->deactivate();
  114. find_item( menubar, "&Project/&New" )->deactivate();
  115. }
  116. }
  117. void Mixer::cb_menu(Fl_Widget* o) {
  118. Fl_Menu_Bar *menu = (Fl_Menu_Bar*)o;
  119. /* const Fl_Menu_Item *mi = &menu->menu()[menu->value()]; */
  120. char picked[256];
  121. // const char *picked = menu->text();
  122. menu->item_pathname( picked, sizeof( picked ) );
  123. if (! strcmp( picked, "&Project/&New") )
  124. {
  125. DMESSAGE( "New project" );
  126. const char *templates[] = { "Default", NULL };
  127. char *default_path;
  128. char *selected_template;
  129. read_line( user_config_dir, "default_path", &default_path );
  130. char *path = new_project_chooser( templates, &default_path, &selected_template );
  131. if ( path )
  132. {
  133. if ( ! Project::create( path, selected_template ) )
  134. fl_alert( "Error creating project!" );
  135. free( path );
  136. free( selected_template );
  137. }
  138. update_menu();
  139. if ( default_path )
  140. {
  141. write_line( user_config_dir, "default_path", default_path );
  142. free( default_path );
  143. }
  144. }
  145. else if (! strcmp( picked, "&Project/&Open" ) )
  146. {
  147. char *path = NULL;
  148. // read_line( user_config_dir, "default_path", &path );
  149. const char *name = fl_dir_chooser( "Open Project", path, NULL );
  150. free( path );
  151. mixer->hide();
  152. if ( int err = Project::open( name ) )
  153. {
  154. fl_alert( "Error opening project: %s", Project::errstr( err ) );
  155. }
  156. update_menu();
  157. mixer->show();
  158. }
  159. else if (! strcmp( picked, "&Project/&Save" ) )
  160. {
  161. command_save();
  162. }
  163. else if (! strcmp( picked, "&Project/&Quit") )
  164. {
  165. command_quit();
  166. }
  167. else if ( !strcmp( picked, "&Mixer/&Add Strip" ) )
  168. {
  169. command_add_strip();
  170. }
  171. else if ( !strcmp( picked, "&Mixer/Add &N Strips" ) )
  172. {
  173. const char *s = fl_input( "Enter number of strips to add" );
  174. if ( s )
  175. {
  176. for ( int i = atoi( s ); i > 0; i-- )
  177. command_add_strip();
  178. }
  179. }
  180. else if (! strcmp( picked, "&Mixer/&Rows/One") )
  181. {
  182. rows( 1 );
  183. }
  184. else if (! strcmp( picked, "&Mixer/&Rows/Two") )
  185. {
  186. rows( 2 );
  187. }
  188. else if (! strcmp( picked, "&Mixer/&Rows/Three") )
  189. {
  190. rows( 3 );
  191. }
  192. else if (! strcmp( picked, "&Options/&Display/&Style/&Default") )
  193. {
  194. Fl::scheme( "plastic" );
  195. }
  196. else if (! strcmp( picked, "&Options/&Display/&Style/&Flat") )
  197. {
  198. Fl::scheme( "gtk+" );
  199. }
  200. else if (! strcmp( picked, "&Options/&Display/&Colors/&System") )
  201. {
  202. color_scheme( "system" );
  203. }
  204. else if (! strcmp( picked, "&Options/&Display/&Colors/&Dark") )
  205. {
  206. color_scheme( "dark" );
  207. }
  208. else if (! strcmp( picked, "&Options/&Display/&Colors/&Very Dark") )
  209. {
  210. color_scheme( "very dark" );
  211. }
  212. else if (! strcmp( picked, "&Options/&Display/&Colors/&Light") )
  213. {
  214. color_scheme( "light" );
  215. }
  216. else if ( ! strcmp( picked, "&Help/&About" ) )
  217. {
  218. About_Dialog ab( PIXMAP_PATH "/non-mixer/logo.png" );
  219. ab.logo_box->label( VERSION );
  220. ab.title->label( "The Non Mixer" );
  221. ab.copyright->label( "Copyright (C) 2008-2010 Jonathan Moore Liles" );
  222. ab.credits->label(
  223. "Non-Mixer was written from scratch by\n"
  224. "Jonathan Moore Liles for his own use\n"
  225. "(see the manual).\n"
  226. "\n"
  227. "Nobody planned. Nobody helped.\n"
  228. "You can help now by donating time, money,\n"
  229. "and/or replacing the rest of Linux Audio\n"
  230. "with fast, light, reliable alternatives.\n" );
  231. ab.website_url->label( "http://non-mixer.tuxfamily.org" );
  232. ab.run();
  233. }
  234. else if ( !strcmp( picked, "&Help/&Manual" ))
  235. {
  236. char *pat;
  237. asprintf( &pat, "file://%s.html", DOCUMENT_PATH "/non-mixer/MANUAL" );
  238. open_url( pat );
  239. free( pat );
  240. }
  241. }
  242. void Mixer::cb_menu(Fl_Widget* o, void* v) {
  243. ((Mixer*)(v))->cb_menu(o);
  244. }
  245. Mixer::Mixer ( int X, int Y, int W, int H, const char *L ) :
  246. Fl_Group( X, Y, W, H, L )
  247. {
  248. get_system_colors();
  249. Fl::scheme( "plastic" );
  250. color_scheme( "dark" );
  251. _rows = 1;
  252. box( FL_NO_BOX );
  253. labelsize( 96 );
  254. { Fl_Menu_Bar *o = menubar = new Fl_Menu_Bar( X, Y, W, 24 );
  255. o->add( "&Project/&New" );
  256. o->add( "&Project/&Open" );
  257. o->add( "&Project/&Save", FL_CTRL + 's', 0, 0 );
  258. o->add( "&Project/&Quit", FL_CTRL + 'q', 0, 0 );
  259. o->add( "&Mixer/&Add Strip", 'a', 0, 0 );
  260. o->add( "&Mixer/Add &N Strips" );
  261. o->add( "&Mixer/&Rows/One", '1', 0, 0 );
  262. o->add( "&Mixer/&Rows/Two", '2', 0, 0 );
  263. o->add( "&Mixer/&Rows/Three", '3', 0, 0 );
  264. o->add( "_&Options/&Display/&Style/&Default", 0, 0, 0, FL_MENU_RADIO | FL_MENU_VALUE );
  265. o->add( "_&Options/&Display/&Style/&Flat", 0, 0, 0, FL_MENU_RADIO );
  266. o->add( "_&Options/&Display/&Colors/&Dark", 0, 0, 0, FL_MENU_RADIO | FL_MENU_VALUE );
  267. o->add( "_&Options/&Display/&Colors/&Very Dark", 0, 0, 0, FL_MENU_RADIO );
  268. o->add( "_&Options/&Display/&Colors/&Light", 0, 0, 0, FL_MENU_RADIO );
  269. o->add( "_&Options/&Display/&Colors/&System", 0, 0, 0, FL_MENU_RADIO );
  270. o->add( "&Help/&Manual" );
  271. o->add( "&Help/&About" );
  272. o->callback( cb_menu, this );
  273. }
  274. { Fl_Box *o = project_name = new Fl_Box( X + 150, Y, W, 24 );
  275. o->labelfont( FL_HELVETICA_ITALIC );
  276. o->label( 0 );
  277. o->align( FL_ALIGN_INSIDE | FL_ALIGN_CENTER );
  278. o->labeltype( FL_SHADOW_LABEL );
  279. }
  280. { sm_blinker = new Fl_Blinker( ( X + W) - 52, Y + 4, 50, 15, "SM");
  281. sm_blinker->box(FL_ROUNDED_BOX);
  282. sm_blinker->down_box(FL_ROUNDED_BOX);
  283. sm_blinker->color((Fl_Color)75);
  284. sm_blinker->selection_color((Fl_Color)86);
  285. sm_blinker->labeltype(FL_NORMAL_LABEL);
  286. sm_blinker->labelfont(2);
  287. sm_blinker->labelsize(14);
  288. sm_blinker->labelcolor(FL_DARK3);
  289. sm_blinker->align(Fl_Align(FL_ALIGN_CENTER));
  290. sm_blinker->when(FL_WHEN_RELEASE);
  291. sm_blinker->deactivate();
  292. } // Fl_Blinker* sm_blinker
  293. { Fl_Scroll *o = scroll = new Fl_Scroll( X, Y + 24, W, H - 24 );
  294. o->box( FL_NO_BOX );
  295. // o->type( Fl_Scroll::HORIZONTAL_ALWAYS );
  296. // o->box( Fl_Scroll::BOTH );
  297. {
  298. Fl_Flowpack *o = mixer_strips = new Fl_Flowpack( X, Y + 24, W, H - 18 - 24 );
  299. label( "Non-Mixer" );
  300. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE) );
  301. o->box( FL_NO_BOX );
  302. o->type( Fl_Pack::HORIZONTAL );
  303. o->hspacing( 2 );
  304. o->vspacing( 2 );
  305. o->end();
  306. Fl_Group::current()->resizable( o );
  307. }
  308. o->end();
  309. Fl_Group::current()->resizable( o );
  310. }
  311. end();
  312. // Fl::add_timeout( STATUS_UPDATE_FREQ, update_cb, this );
  313. update_menu();
  314. load_options();
  315. }
  316. int
  317. Mixer::init_osc ( const char *osc_port )
  318. {
  319. osc_endpoint = new OSC::Endpoint();
  320. if ( int r = osc_endpoint->init( LO_UDP, osc_port ) )
  321. return r;
  322. osc_endpoint->owner = this;
  323. printf( "OSC=%s\n", osc_endpoint->url() );
  324. osc_endpoint->add_method( "/non/mixer/add_strip", "", OSC_NAME( add_strip ), osc_endpoint, "" );
  325. // osc_endpoint->start();
  326. /* poll so we can keep OSC handlers running in the GUI thread and avoid extra sync */
  327. Fl::add_timeout( OSC_INTERVAL, check_osc, this );
  328. return 0;
  329. }
  330. void
  331. Mixer::check_osc ( void * v )
  332. {
  333. ((Mixer*)v)->osc_endpoint->check();
  334. Fl::repeat_timeout( OSC_INTERVAL, check_osc, v );
  335. }
  336. Mixer::~Mixer ( )
  337. {
  338. DMESSAGE( "Destroying mixer" );
  339. save_options();
  340. /* FIXME: teardown */
  341. mixer_strips->clear();
  342. }
  343. void Mixer::resize ( int X, int Y, int W, int H )
  344. {
  345. Fl_Group::resize( X, Y, W, H );
  346. mixer_strips->resize( X, Y + 24, W, H - 18 - 24 );
  347. scroll->resize( X, Y + 24, W, H - 24 );
  348. rows( _rows );
  349. }
  350. void Mixer::add ( Mixer_Strip *ms )
  351. {
  352. MESSAGE( "Add mixer strip \"%s\"", ms->name() );
  353. mixer_strips->add( ms );
  354. ms->take_focus();
  355. rows( _rows );
  356. // scroll->redraw();
  357. }
  358. void
  359. Mixer::quit ( void )
  360. {
  361. /* TODO: save project? */
  362. while ( Fl::first_window() ) Fl::first_window()->hide();
  363. }
  364. void
  365. Mixer::insert ( Mixer_Strip *ms, Mixer_Strip *before )
  366. {
  367. mixer_strips->remove( ms );
  368. mixer_strips->insert( *ms, before );
  369. scroll->redraw();
  370. }
  371. void
  372. Mixer::insert ( Mixer_Strip *ms, int i )
  373. {
  374. Mixer_Strip *before = (Mixer_Strip*)mixer_strips->child( i );
  375. insert( ms, before);
  376. }
  377. void
  378. Mixer::move_left ( Mixer_Strip *ms )
  379. {
  380. int i = mixer_strips->find( ms );
  381. if ( i > 0 )
  382. insert( ms, i - 1 );
  383. }
  384. void
  385. Mixer::move_right ( Mixer_Strip *ms )
  386. {
  387. int i = mixer_strips->find( ms );
  388. if ( i < mixer_strips->children() - 1 )
  389. insert( ms, i + 2 );
  390. }
  391. void Mixer::remove ( Mixer_Strip *ms )
  392. {
  393. MESSAGE( "Remove mixer strip \"%s\"", ms->name() );
  394. mixer_strips->remove( ms );
  395. if ( parent() )
  396. parent()->redraw();
  397. }
  398. bool
  399. Mixer::contains ( Mixer_Strip *ms )
  400. {
  401. return ms->parent() == mixer_strips;
  402. }
  403. /* set the ideal number of rows... All may not actually fit. */
  404. void
  405. Mixer::rows ( int ideal_rows )
  406. {
  407. int sh;
  408. int actual_rows = 1;
  409. if ( ideal_rows > 1 )
  410. {
  411. sh = (scroll->h() / ideal_rows ) - (mixer_strips->vspacing() * (ideal_rows - 1));
  412. mixer_strips->flow( true );
  413. if ( sh < Mixer_Strip::min_h() )
  414. {
  415. int can_fit = ( scroll->h() - 18 ) / Mixer_Strip::min_h();
  416. actual_rows = can_fit > 0 ? can_fit : 1;
  417. }
  418. else
  419. actual_rows = ideal_rows;
  420. }
  421. else
  422. actual_rows = 1;
  423. if ( 1 == actual_rows )
  424. {
  425. sh = (scroll->h() - 18);
  426. mixer_strips->flow( false );
  427. actual_rows = 1;
  428. }
  429. int tw = 0;
  430. for ( int i = 0; i < mixer_strips->children(); ++i )
  431. {
  432. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  433. t->size( t->w(), sh );
  434. tw += t->w() + mixer_strips->hspacing();
  435. }
  436. if ( actual_rows > 1 )
  437. mixer_strips->size( scroll->w() - 18, mixer_strips->h() );
  438. else
  439. mixer_strips->size( tw, mixer_strips->h() );
  440. _rows = ideal_rows;
  441. scroll->redraw();
  442. }
  443. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  444. Mixer_Strip *
  445. Mixer::track_by_name ( const char *name )
  446. {
  447. for ( int i = mixer_strips->children(); i-- ; )
  448. {
  449. Mixer_Strip *t = (Mixer_Strip*)mixer_strips->child( i );
  450. if ( ! strcmp( name, t->name() ) )
  451. return t;
  452. }
  453. return NULL;
  454. }
  455. /** return a malloc'd string representing a unique name for a new track */
  456. char *
  457. Mixer::get_unique_track_name ( const char *name )
  458. {
  459. char pat[256];
  460. strcpy( pat, name );
  461. for ( int i = 1; track_by_name( pat ); ++i )
  462. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  463. return strdup( pat );
  464. }
  465. void
  466. Mixer::snapshot ( void )
  467. {
  468. for ( int i = 0; i < mixer_strips->children(); ++i )
  469. ((Mixer_Strip*)mixer_strips->child( i ))->log_children();
  470. }
  471. void
  472. Mixer::new_strip ( void )
  473. {
  474. add( new Mixer_Strip( get_unique_track_name( "Unnamed" ) ) );
  475. }
  476. bool
  477. Mixer::save ( void )
  478. {
  479. MESSAGE( "Saving state" );
  480. Loggable::snapshot_callback( &Mixer::snapshot, this );
  481. Loggable::snapshot( "snapshot" );
  482. return true;
  483. }
  484. static const char options_filename[] = "options";
  485. void
  486. Mixer::load_options ( void )
  487. {
  488. // save options
  489. char *path;
  490. asprintf( &path, "%s/options", user_config_dir );
  491. ((Fl_Menu_Settings*)menubar)->load( menubar->find_item( "&Options" ), path );
  492. free( path );
  493. }
  494. void
  495. Mixer::save_options ( void )
  496. {
  497. char *path;
  498. asprintf( &path, "%s/%s", user_config_dir, options_filename );
  499. ((Fl_Menu_Settings*)menubar)->dump( menubar->find_item( "&Options" ), path );
  500. free( path );
  501. }
  502. void
  503. Mixer::update_menu ( void )
  504. {
  505. bool b = Project::open();
  506. if ( b )
  507. {
  508. ((Fl_Menu_Item*)menubar->find_item( "&Mixer" ))->flags &= ~FL_MENU_INACTIVE;
  509. ((Fl_Menu_Item*)menubar->find_item( "&Project/&Save" ))->flags &= ~FL_MENU_INACTIVE;
  510. mixer_strips->activate();
  511. }
  512. else
  513. {
  514. ((Fl_Menu_Item*)menubar->find_item( "&Mixer" ))->flags |= FL_MENU_INACTIVE;
  515. ((Fl_Menu_Item*)menubar->find_item( "&Project/&Save" ))->flags |= FL_MENU_INACTIVE;
  516. mixer_strips->deactivate();
  517. }
  518. project_name->label( Project::name() );
  519. }
  520. int
  521. Mixer::handle ( int m )
  522. {
  523. if ( Fl_Group::handle( m ) )
  524. return 1;
  525. switch ( m )
  526. {
  527. case FL_ENTER:
  528. case FL_LEAVE:
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. void
  534. Mixer::discover_peers ( void )
  535. {
  536. if ( nsm->is_active() )
  537. {
  538. lo_message m = lo_message_new();
  539. lo_message_add_string( m, "/non/finger" );
  540. lo_message_add_string( m, osc_endpoint->url() );
  541. nsm->broadcast( m );
  542. lo_message_free( m );
  543. }
  544. }
  545. /************/
  546. /* Commands */
  547. /************/
  548. bool
  549. Mixer::command_save ( void )
  550. {
  551. return Project::save();
  552. }
  553. bool
  554. Mixer::command_load ( const char *path, const char *display_name )
  555. {
  556. mixer->hide();
  557. if ( int err = Project::open( path ) )
  558. {
  559. // fl_alert( "Error opening project specified on commandline: %s", Project::errstr( err ) );
  560. return false;
  561. }
  562. if ( display_name )
  563. Project::name( display_name );
  564. update_menu();
  565. mixer->show();
  566. return true;
  567. }
  568. bool
  569. Mixer::command_new ( const char *path, const char *display_name )
  570. {
  571. if ( ! Project::create( path, "" ) )
  572. return false;
  573. if ( display_name )
  574. Project::name( display_name );
  575. update_menu();
  576. return true;
  577. // fl_alert( "Error creating project!" );
  578. }
  579. void
  580. Mixer::command_quit ( void )
  581. {
  582. if ( Loggable::dirty() )
  583. {
  584. int i = fl_choice( "There have been changes since the last save. Quitting now will discard them", "Discard", "Cancel", NULL );
  585. if ( i != 0 )
  586. return;
  587. }
  588. quit();
  589. }
  590. /* */
  591. void
  592. Mixer::command_add_strip ( void )
  593. {
  594. new_strip();
  595. }