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.

307 lines
7.0KB

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