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.

303 lines
6.7KB

  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. /* Routings for opening/closing/creation of projects. All the actual
  19. project state belongs to Timeline and other classes. */
  20. /* Project management routines. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/fcntl.h>
  27. #include <errno.h>
  28. #include "Loggable.H"
  29. #include "Project.H"
  30. #include "Timeline.H" // for sample_rate()
  31. #include "Engine/Engine.H" // for sample_rate()
  32. #include "TLE.H" // all this just for load and save...
  33. #include <FL/filename.H>
  34. #include "util/debug.h"
  35. #include "util/file.h"
  36. extern TLE *tle;
  37. /* FIXME: wrong place for this */
  38. #define APP_TITLE "Non-DAW"
  39. const int PROJECT_VERSION = 1;
  40. const char *Project::_errstr[] =
  41. {
  42. "Not a Non-DAW project",
  43. "Locked by another process",
  44. "Access denied",
  45. "Samplerate mismatch",
  46. "Incompatible project version"
  47. };
  48. char Project::_name[256];
  49. char Project::_path[512];
  50. bool Project::_is_open = false;
  51. int Project::_lockfd = 0;
  52. /***********/
  53. /* Private */
  54. /***********/
  55. void
  56. Project::set_name ( const char *name )
  57. {
  58. strcpy( Project::_name, name );
  59. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  60. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  61. char *s = rindex( Project::_name, '/' );
  62. s = s ? s + 1 : Project::_name;
  63. memmove( Project::_name, s, strlen( s ) + 1 );
  64. for ( s = Project::_name; *s; ++s )
  65. if ( *s == '_' || *s == '-' )
  66. *s = ' ';
  67. }
  68. bool
  69. Project::write_info ( void )
  70. {
  71. if ( ! open() )
  72. return true;
  73. FILE *fp;
  74. if ( ! ( fp = fopen( "info", "w" ) ) )
  75. {
  76. WARNING( "could not open project info file for writing." );
  77. return false;
  78. }
  79. fprintf( fp, "created by\n\t%s\nversion\n\t%d\nsample rate\n\t%lu\n",
  80. APP_TITLE " " VERSION,
  81. PROJECT_VERSION,
  82. (unsigned long)timeline->sample_rate() );
  83. fclose( fp );
  84. return true;
  85. }
  86. bool
  87. Project::read_info ( int *version, nframes_t *sample_rate )
  88. {
  89. FILE *fp;
  90. if ( ! ( fp = fopen( "info", "r" ) ) )
  91. {
  92. WARNING( "could not open project info file for reading." );
  93. return false;
  94. }
  95. char *name, *value;
  96. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  97. {
  98. MESSAGE( "Info: %s = %s", name, value );
  99. if ( ! strcmp( name, "sample rate" ) )
  100. *sample_rate = atoll( value );
  101. else if ( ! strcmp( name, "version" ) )
  102. *version = atoi( value );
  103. free( name );
  104. free( value );
  105. }
  106. fclose( fp );
  107. return true;
  108. }
  109. /**********/
  110. /* Public */
  111. /**********/
  112. /** Close the project (reclaiming all memory) */
  113. bool
  114. Project::close ( void )
  115. {
  116. if ( ! open() )
  117. return true;
  118. tle->save_timeline_settings();
  119. Loggable::close();
  120. write_info();
  121. _is_open = false;
  122. *Project::_name = '\0';
  123. release_lock( &_lockfd, ".lock" );
  124. return true;
  125. }
  126. /** Ensure a project is valid before opening it... */
  127. bool
  128. Project::validate ( const char *name )
  129. {
  130. bool r = true;
  131. char pwd[512];
  132. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  133. if ( chdir( name ) )
  134. {
  135. WARNING( "Cannot change to project dir \"%s\"", name );
  136. return false;
  137. }
  138. if ( ! exists( "info" ) ||
  139. ! exists( "history" ) ||
  140. ! exists( "sources" ) )
  141. // ! exists( "options" ) )
  142. {
  143. WARNING( "Not a Non-DAW project: \"%s\"", name );
  144. r = false;
  145. }
  146. chdir( pwd );
  147. return r;
  148. }
  149. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  150. * otherwise */
  151. int
  152. Project::open ( const char *name )
  153. {
  154. if ( ! validate( name ) )
  155. return E_INVALID;
  156. close();
  157. chdir( name );
  158. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  159. return E_LOCKED;
  160. int version;
  161. nframes_t rate;
  162. if ( ! read_info( &version, &rate ) )
  163. return E_INVALID;
  164. if ( version != PROJECT_VERSION )
  165. return E_VERSION;
  166. if ( ! Loggable::open( "history" ) )
  167. return E_INVALID;
  168. timeline->sample_rate( rate );
  169. set_name( name );
  170. *_path = '\0';
  171. fl_filename_absolute( _path, sizeof( _path ), "." );
  172. _is_open = true;
  173. tle->load_timeline_settings();
  174. timeline->zoom_fit();
  175. MESSAGE( "Loaded project \"%s\"", name );
  176. return 0;
  177. }
  178. /** Create a new project /name/ from existing template
  179. * /template_name/ */
  180. bool
  181. Project::create ( const char *name, const char *template_name )
  182. {
  183. if ( exists( name ) )
  184. {
  185. WARNING( "Project already exists" );
  186. return false;
  187. }
  188. close();
  189. if ( mkdir( name, 0777 ) )
  190. {
  191. WARNING( "Cannot create project directory" );
  192. return false;
  193. }
  194. if ( chdir( name ) )
  195. FATAL( "WTF? Cannot change to new project directory" );
  196. mkdir( "sources", 0777 );
  197. creat( "info", 0666 );
  198. creat( "history", 0666 );
  199. /* TODO: copy template */
  200. if ( open( name ) == 0 )
  201. {
  202. write_info();
  203. /* add the bare essentials */
  204. timeline->beats_per_minute( 0, 120 );
  205. timeline->time( 0, 4, 4 );
  206. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  207. return true;
  208. }
  209. else
  210. {
  211. WARNING( "Failed to open newly created project" );
  212. return false;
  213. }
  214. }
  215. /** Replace the journal with a snapshot of the current state */
  216. void
  217. Project::compact ( void )
  218. {
  219. Loggable::compact();
  220. }