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
7.2KB

  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 <FL/Fl.H>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <sys/stat.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <getopt.h>
  26. /* for registrations */
  27. #include "Audio_Region.H"
  28. #include "Sequence.H"
  29. #include "Audio_Sequence.H"
  30. #include "Timeline.H"
  31. #include "Tempo_Sequence.H"
  32. #include "Time_Sequence.H"
  33. #include "Annotation_Sequence.H"
  34. #include "Control_Sequence.H"
  35. #include "Cursor_Sequence.H"
  36. #include "Track.H"
  37. #include "TLE.H"
  38. #include "Timeline.H"
  39. #include "Project.H"
  40. #include "Transport.H"
  41. #include "Engine/Engine.H"
  42. #include "Thread.H"
  43. #include <nsm.h>
  44. extern void set_nsm_callbacks ( nsm_client_t *nsm );
  45. Engine *engine;
  46. Timeline *timeline;
  47. Transport *transport;
  48. TLE *tle;
  49. nsm_client_t *nsm;
  50. char *instance_name = NULL;
  51. /* TODO: put these in a header */
  52. #define USER_CONFIG_DIR ".non-daw/"
  53. const char *APP_NAME = "Non-Timeline";
  54. const char *APP_TITLE = "The Non-Timeline";
  55. const char COPYRIGHT[] = "Copyright (C) 2008-2013 Jonathan Moore Liles";
  56. const double NSM_CHECK_INTERVAL = 0.25f;
  57. #define PACKAGE "non"
  58. #include "const.h"
  59. #include "debug.h"
  60. char *user_config_dir;
  61. #include <errno.h>
  62. static int
  63. ensure_dirs ( void )
  64. {
  65. asprintf( &user_config_dir, "%s/%s", getenv( "HOME" ), USER_CONFIG_DIR );
  66. int r = mkdir( user_config_dir, 0777 );
  67. return r == 0 || errno == EEXIST;
  68. }
  69. #include <FL/Fl_Shared_Image.H>
  70. #include <signal.h>
  71. void
  72. shift ( char **argv, int *argc, int n )
  73. {
  74. int i;
  75. for ( i = 0; i < *argc; ++i )
  76. argv[ i ] = argv[ i + n ];
  77. argv[ i ] = 0;
  78. argc -= n;
  79. }
  80. extern Timeline *timeline;
  81. void
  82. check_nsm ( void * v )
  83. {
  84. nsm_check_nowait( nsm );
  85. Fl::repeat_timeout( NSM_CHECK_INTERVAL, check_nsm, v );
  86. }
  87. static int got_sigterm = 0;
  88. void
  89. sigterm_handler ( int )
  90. {
  91. got_sigterm = 1;
  92. Fl::awake();
  93. }
  94. void
  95. check_sigterm ( void * )
  96. {
  97. if ( got_sigterm )
  98. {
  99. MESSAGE( "Got SIGTERM, quitting..." );
  100. timeline->command_quit();
  101. }
  102. }
  103. int
  104. main ( int argc, char **argv )
  105. {
  106. if ( !strcmp( argv[0], "non-daw" ) )
  107. {
  108. /* use old app name and title */
  109. APP_NAME = "Non-DAW";
  110. APP_TITLE = "The Non-DAW";
  111. }
  112. printf( "%s %s -- %s\n", APP_TITLE, VERSION, COPYRIGHT );
  113. if ( ! Fl::visual( FL_DOUBLE | FL_RGB ) )
  114. {
  115. WARNING( "Xdbe not supported, FLTK will fake double buffering." );
  116. }
  117. Thread::init();
  118. Thread thread( "UI" );
  119. thread.set();
  120. signal( SIGTERM, sigterm_handler );
  121. signal( SIGHUP, sigterm_handler );
  122. signal( SIGINT, sigterm_handler );
  123. fl_register_images();
  124. /* welcome to C++ */
  125. LOG_REGISTER_CREATE( Annotation_Point );
  126. LOG_REGISTER_CREATE( Annotation_Region );
  127. LOG_REGISTER_CREATE( Annotation_Sequence );
  128. LOG_REGISTER_CREATE( Audio_Region );
  129. LOG_REGISTER_CREATE( Audio_Sequence );
  130. LOG_REGISTER_CREATE( Control_Point );
  131. LOG_REGISTER_CREATE( Control_Sequence );
  132. LOG_REGISTER_CREATE( Tempo_Point );
  133. LOG_REGISTER_CREATE( Time_Point );
  134. LOG_REGISTER_CREATE( Cursor_Point );
  135. LOG_REGISTER_CREATE( Cursor_Region );
  136. LOG_REGISTER_CREATE( Track );
  137. signal( SIGPIPE, SIG_IGN );
  138. if ( ! ensure_dirs() )
  139. FATAL( "Cannot create required directories" );
  140. instance_name = strdup( APP_NAME );
  141. bool instance_override = false;
  142. const char *osc_port = NULL;
  143. static struct option long_options[] =
  144. {
  145. { "help", no_argument, 0, '?' },
  146. { "instance", required_argument, 0, 'i' },
  147. { "osc-port", required_argument, 0, 'p' },
  148. { 0, 0, 0, 0 }
  149. };
  150. int option_index = 0;
  151. int c = 0;
  152. while ( ( c = getopt_long_only( argc, argv, "", long_options, &option_index ) ) != -1 )
  153. {
  154. switch ( c )
  155. {
  156. case 'p':
  157. DMESSAGE( "Using OSC port %s", optarg );
  158. osc_port = optarg;
  159. break;
  160. case 'i':
  161. DMESSAGE( "Using instance name %s", optarg );
  162. free( instance_name );
  163. instance_name = strdup( optarg );
  164. instance_override = true;
  165. break;
  166. case '?':
  167. printf( "\nUsage: %s [--instance instance_name] [--osc-port portnum] [path_to_project]\n\n", argv[0] );
  168. exit(0);
  169. break;
  170. }
  171. }
  172. /* we don't really need a pointer for this */
  173. // will be created on project new/open
  174. engine = NULL;
  175. tle = new TLE;
  176. nsm = nsm_new();
  177. set_nsm_callbacks( nsm );
  178. MESSAGE( "Starting GUI" );
  179. tle->run();
  180. timeline->init_osc( osc_port );
  181. tle->main_window->show( 0, NULL );
  182. char *nsm_url = getenv( "NSM_URL" );
  183. if ( nsm_url )
  184. {
  185. if ( ! nsm_init( nsm, nsm_url ) )
  186. {
  187. if ( instance_override )
  188. WARNING( "--instance option is not available when running under session management, ignoring." );
  189. if ( optind < argc )
  190. WARNING( "Loading files from the command-line is incompatible with session management, ignoring." );
  191. nsm_send_announce( nsm, APP_NAME, ":progress:switch:", argv[0] );
  192. /* poll so we can keep OSC handlers running in the GUI thread and avoid extra sync */
  193. Fl::add_timeout( NSM_CHECK_INTERVAL, check_nsm, NULL );
  194. }
  195. }
  196. else
  197. {
  198. if ( optind < argc )
  199. {
  200. MESSAGE( "Loading \"%s\"", argv[optind] );
  201. Fl::lock();
  202. tle->open( argv[optind] );
  203. Fl::unlock();
  204. }
  205. }
  206. Fl::add_check( check_sigterm );
  207. Fl::run();
  208. /* cleanup for valgrind's sake */
  209. if ( engine )
  210. {
  211. delete engine;
  212. engine = NULL;
  213. }
  214. delete timeline;
  215. timeline = NULL;
  216. delete tle;
  217. tle = NULL;
  218. nsm_free( nsm );
  219. nsm = NULL;
  220. MESSAGE( "Your fun is over" );
  221. }