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.

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