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.

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