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.

293 lines
6.9KB

  1. /**********************************************************************************/
  2. /* Copyright (C) 2007,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 <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "non.H"
  23. // #include "gui/input.H"
  24. #include "gui/ui.H"
  25. #include "jack.H"
  26. #include "NSM.H"
  27. #include "transport.H"
  28. #include "pattern.H"
  29. #include "phrase.H"
  30. #include <signal.h>
  31. #include <MIDI/midievent.H>
  32. using namespace MIDI;
  33. // extern const char *BUILD_ID;
  34. // extern const char *VERSION;
  35. const double NSM_CHECK_INTERVAL = 0.25f;
  36. sequence *playlist;
  37. global_settings config;
  38. song_settings song;
  39. NSM_Client *nsm;
  40. char *instance_name;
  41. /* default to pattern mode */
  42. UI *ui;
  43. void
  44. quit ( void )
  45. {
  46. /* clean up, only for valgrind's sake */
  47. ui->save_settings();
  48. delete ui;
  49. midi_all_sound_off();
  50. // wait for it...
  51. sleep( 1 );
  52. midi_shutdown();
  53. MESSAGE( "Your fun is over" );
  54. exit( 0 );
  55. }
  56. void
  57. clear_song ( void )
  58. {
  59. // song.filename = NULL;
  60. ui->pattern_canvas_widget->grid( NULL );
  61. ui->phrase_canvas_widget->grid( NULL );
  62. playlist->reset();
  63. playlist->insert( 0, 1 );
  64. ui->pattern_canvas_widget->grid( new pattern );
  65. ui->phrase_canvas_widget->grid( new phrase );
  66. song.dirty( false );
  67. }
  68. void
  69. init_song ( void )
  70. {
  71. if ( ! midi_is_active() )
  72. setup_jack();
  73. if ( !( nsm && nsm->is_active() ) )
  74. song.filename = NULL;
  75. clear_song();
  76. if ( nsm && nsm->is_active() )
  77. save_song( song.filename );
  78. }
  79. void
  80. handle_midi_input ( void )
  81. {
  82. midievent e;
  83. while ( ( midi_input_event( PERFORMANCE, &e ) ) )
  84. {
  85. pattern::record_event( &e );
  86. }
  87. }
  88. bool
  89. load_song ( const char *name )
  90. {
  91. if ( ! midi_is_active() )
  92. setup_jack();
  93. MESSAGE( "loading song \"%s\"", name );
  94. Grid *pattern_grid = ui->pattern_canvas_widget->grid();
  95. Grid *phrase_grid = ui->phrase_canvas_widget->grid();
  96. ui->pattern_canvas_widget->grid( NULL );
  97. ui->phrase_canvas_widget->grid( NULL );
  98. if ( ! playlist->load( name ) )
  99. {
  100. WARNING( "failed to load song file" );
  101. goto failed;
  102. }
  103. ui->pattern_canvas_widget->grid( pattern::pattern_by_number( 1 ) );
  104. ui->phrase_canvas_widget->grid( phrase::phrase_by_number( 1 ) );
  105. song.filename = strdup( name );
  106. song.dirty( false );
  107. return true;
  108. failed:
  109. ui->pattern_canvas_widget->grid( pattern_grid );
  110. ui->phrase_canvas_widget->grid( phrase_grid );
  111. return false;
  112. }
  113. bool
  114. save_song ( const char *name )
  115. {
  116. playlist->save( name );
  117. song.filename = strdup( name );
  118. song.dirty( false );
  119. return true;
  120. }
  121. void
  122. setup_jack ( )
  123. {
  124. const char *jack_name;
  125. jack_name = midi_init( instance_name );
  126. if ( ! jack_name )
  127. ASSERTION( "Could not initialize MIDI system! (is Jack running and with MIDI ports enabled?)" );
  128. if ( ! transport.valid )
  129. {
  130. if ( transport.master )
  131. ASSERTION( "The version of JACK you are using does not appear to be capable of passing BBT positional information." );
  132. else
  133. ASSERTION( "Either the version of JACK you are using does pass BBT information, or the current timebase master does not provide it." );
  134. }
  135. }
  136. static int got_sigterm = 0;
  137. void
  138. sigterm_handler ( int )
  139. {
  140. got_sigterm = 1;
  141. Fl::awake();
  142. }
  143. void
  144. check_sigterm ( void * )
  145. {
  146. if ( got_sigterm )
  147. {
  148. MESSAGE( "Got SIGTERM, quitting..." );
  149. quit();
  150. }
  151. }
  152. void
  153. check_nsm ( void * v )
  154. {
  155. nsm->check();
  156. Fl::repeat_timeout( NSM_CHECK_INTERVAL, check_nsm, v );
  157. }
  158. int
  159. main ( int argc, char **argv )
  160. {
  161. printf( "%s %s %s -- %s\n", APP_TITLE, VERSION, "", COPYRIGHT );
  162. if ( ! Fl::visual( FL_DOUBLE | FL_RGB ) )
  163. {
  164. WARNING( "Xdbe not supported, FLTK will fake double buffering." );
  165. }
  166. ::signal( SIGTERM, sigterm_handler );
  167. ::signal( SIGHUP, sigterm_handler );
  168. ::signal( SIGINT, sigterm_handler );
  169. config.follow_playhead = true;
  170. config.record_mode = MERGE;
  171. song.play_mode = PATTERN;
  172. asprintf( &config.user_config_dir, "%s/%s", getenv( "HOME" ), USER_CONFIG_DIR );
  173. mkdir( config.user_config_dir, 0777 );
  174. playlist = new sequence;
  175. nsm = new NSM_Client;
  176. song.filename = NULL;
  177. ui = new UI;
  178. pattern::signal_create_destroy.connect( mem_fun( ui->phrase_canvas_widget, &Canvas::v_zoom_fit ) );
  179. pattern::signal_create_destroy.connect( mem_fun( song, &song_settings::set_dirty ) );
  180. phrase::signal_create_destroy.connect( mem_fun( song, &song_settings::set_dirty ) );
  181. song.dirty( false );
  182. clear_song();
  183. #ifdef HAVE_XPM
  184. ui->main_window->icon((char *)p);
  185. #endif
  186. ui->main_window->show( 0, 0 );
  187. instance_name = strdup( APP_NAME );
  188. const char *nsm_url = getenv( "NSM_URL" );
  189. if ( nsm_url )
  190. {
  191. if ( ! nsm->init( nsm_url ) )
  192. {
  193. nsm->announce( APP_NAME, ":switch:dirty:", argv[0] );
  194. song.signal_dirty.connect( sigc::mem_fun( nsm, &NSM_Client::is_dirty ) );
  195. song.signal_clean.connect( sigc::mem_fun( nsm, &NSM_Client::is_clean ) );
  196. // poll so we can keep OSC handlers running in the GUI thread and avoid extra sync
  197. Fl::add_timeout( NSM_CHECK_INTERVAL, check_nsm, NULL );
  198. }
  199. else
  200. WARNING( "Error initializing NSM" );
  201. }
  202. else
  203. {
  204. setup_jack();
  205. if ( argc > 1 )
  206. {
  207. /* maybe a filename on the commandline */
  208. if ( ! load_song( argv[ 1 ] ) )
  209. ASSERTION( "Could not load song \"%s\" specified on command line", argv[ 1 ] );
  210. }
  211. }
  212. MESSAGE( "Initializing GUI" );
  213. Fl::add_check( check_sigterm );
  214. ui->load_settings();
  215. ui->run();
  216. return 0;
  217. }