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.

319 lines
7.3KB

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