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.

337 lines
7.5KB

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