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.

297 lines
6.6KB

  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. FILE *fp;
  72. if ( ! ( fp = fopen( "info", "w" ) ) )
  73. {
  74. WARNING( "could not open project info file for writing." );
  75. return false;
  76. }
  77. fprintf( fp, "created by\n\t%s\nversion\n\t%d\nsample rate\n\t%lu\n",
  78. APP_TITLE " " VERSION,
  79. PROJECT_VERSION,
  80. (unsigned long)timeline->sample_rate() );
  81. fclose( fp );
  82. return true;
  83. }
  84. bool
  85. Project::read_info ( int *version, nframes_t *sample_rate )
  86. {
  87. FILE *fp;
  88. if ( ! ( fp = fopen( "info", "r" ) ) )
  89. {
  90. WARNING( "could not open project info file for reading." );
  91. return false;
  92. }
  93. char *name, *value;
  94. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  95. {
  96. MESSAGE( "Info: %s = %s", name, value );
  97. if ( ! strcmp( name, "sample rate" ) )
  98. *sample_rate = atoll( value );
  99. else if ( ! strcmp( name, "version" ) )
  100. *version = atoi( value );
  101. free( name );
  102. free( value );
  103. }
  104. fclose( fp );
  105. return true;
  106. }
  107. /**********/
  108. /* Public */
  109. /**********/
  110. /** Close the project (reclaiming all memory) */
  111. bool
  112. Project::close ( void )
  113. {
  114. if ( ! open() )
  115. return true;
  116. tle->save_timeline_settings();
  117. Loggable::close();
  118. write_info();
  119. _is_open = false;
  120. *Project::_name = '\0';
  121. release_lock( &_lockfd, ".lock" );
  122. return true;
  123. }
  124. /** Ensure a project is valid before opening it... */
  125. bool
  126. Project::validate ( const char *name )
  127. {
  128. bool r = true;
  129. char pwd[512];
  130. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  131. if ( chdir( name ) )
  132. {
  133. WARNING( "Cannot change to project dir \"%s\"", name );
  134. return false;
  135. }
  136. if ( ! exists( "info" ) ||
  137. ! exists( "history" ) ||
  138. ! exists( "sources" ) )
  139. // ! exists( "options" ) )
  140. {
  141. WARNING( "Not a Non-DAW project: \"%s\"", name );
  142. r = false;
  143. }
  144. chdir( pwd );
  145. return r;
  146. }
  147. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  148. * otherwise */
  149. int
  150. Project::open ( const char *name )
  151. {
  152. if ( ! validate( name ) )
  153. return E_INVALID;
  154. close();
  155. chdir( name );
  156. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  157. return E_LOCKED;
  158. int version;
  159. nframes_t rate;
  160. if ( ! read_info( &version, &rate ) )
  161. return E_INVALID;
  162. if ( version != PROJECT_VERSION )
  163. return E_VERSION;
  164. if ( ! Loggable::open( "history" ) )
  165. return E_INVALID;
  166. timeline->sample_rate( rate );
  167. set_name( name );
  168. *_path = '\0';
  169. fl_filename_absolute( _path, sizeof( _path ), "." );
  170. _is_open = true;
  171. tle->load_timeline_settings();
  172. timeline->zoom_fit();
  173. MESSAGE( "Loaded project \"%s\"", name );
  174. return 0;
  175. }
  176. /** Create a new project /name/ from existing template
  177. * /template_name/ */
  178. bool
  179. Project::create ( const char *name, const char *template_name )
  180. {
  181. if ( exists( name ) )
  182. {
  183. WARNING( "Project already exists" );
  184. return false;
  185. }
  186. close();
  187. if ( mkdir( name, 0777 ) )
  188. {
  189. WARNING( "Cannot create project directory" );
  190. return false;
  191. }
  192. if ( chdir( name ) )
  193. FATAL( "WTF? Cannot change to new project directory" );
  194. mkdir( "sources", 0777 );
  195. creat( "history", 0666 );
  196. /* TODO: copy template */
  197. write_info();
  198. if ( open( name ) == 0 )
  199. {
  200. /* add the bare essentials */
  201. timeline->beats_per_minute( 0, 120 );
  202. timeline->time( 0, 4, 4 );
  203. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  204. return true;
  205. }
  206. else
  207. {
  208. WARNING( "Failed to open newly created project" );
  209. return false;
  210. }
  211. }
  212. /** Replace the journal with a snapshot of the current state */
  213. void
  214. Project::compact ( void )
  215. {
  216. Loggable::compact();
  217. }