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.

326 lines
7.3KB

  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::_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. /** Close the project (reclaiming all memory) */
  127. bool
  128. Project::close ( void )
  129. {
  130. if ( ! open() )
  131. return true;
  132. tle->save_timeline_settings();
  133. Loggable::close();
  134. // write_info();
  135. _is_open = false;
  136. *Project::_name = '\0';
  137. *Project::_created_on = '\0';
  138. release_lock( &_lockfd, ".lock" );
  139. return true;
  140. }
  141. /** Ensure a project is valid before opening it... */
  142. bool
  143. Project::validate ( const char *name )
  144. {
  145. bool r = true;
  146. char pwd[512];
  147. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  148. if ( chdir( name ) )
  149. {
  150. WARNING( "Cannot change to project dir \"%s\"", name );
  151. return false;
  152. }
  153. if ( ! exists( "info" ) ||
  154. ! exists( "history" ) ||
  155. ! exists( "sources" ) )
  156. // ! exists( "options" ) )
  157. {
  158. WARNING( "Not a Non-DAW project: \"%s\"", name );
  159. r = false;
  160. }
  161. chdir( pwd );
  162. return r;
  163. }
  164. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  165. * otherwise */
  166. int
  167. Project::open ( const char *name )
  168. {
  169. if ( ! validate( name ) )
  170. return E_INVALID;
  171. close();
  172. chdir( name );
  173. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  174. return E_LOCKED;
  175. int version;
  176. nframes_t rate;
  177. char *creation_date;
  178. if ( ! read_info( &version, &rate, &creation_date ) )
  179. return E_INVALID;
  180. if ( version != PROJECT_VERSION )
  181. return E_VERSION;
  182. if ( ! Loggable::open( "history" ) )
  183. return E_INVALID;
  184. timeline->sample_rate( rate );
  185. if ( creation_date )
  186. {
  187. strcpy( _created_on, creation_date );
  188. free( creation_date );
  189. }
  190. else
  191. *_created_on = 0;
  192. set_name( name );
  193. *_path = '\0';
  194. fl_filename_absolute( _path, sizeof( _path ), "." );
  195. _is_open = true;
  196. tle->load_timeline_settings();
  197. timeline->zoom_fit();
  198. MESSAGE( "Loaded project \"%s\"", name );
  199. return 0;
  200. }
  201. /** Create a new project /name/ from existing template
  202. * /template_name/ */
  203. bool
  204. Project::create ( const char *name, const char *template_name )
  205. {
  206. if ( exists( name ) )
  207. {
  208. WARNING( "Project already exists" );
  209. return false;
  210. }
  211. close();
  212. if ( mkdir( name, 0777 ) )
  213. {
  214. WARNING( "Cannot create project directory" );
  215. return false;
  216. }
  217. if ( chdir( name ) )
  218. FATAL( "WTF? Cannot change to new project directory" );
  219. mkdir( "sources", 0777 );
  220. creat( "history", 0666 );
  221. /* TODO: copy template */
  222. write_info();
  223. if ( open( name ) == 0 )
  224. {
  225. /* add the bare essentials */
  226. timeline->beats_per_minute( 0, 120 );
  227. timeline->time( 0, 4, 4 );
  228. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  229. return true;
  230. }
  231. else
  232. {
  233. WARNING( "Failed to open newly created project" );
  234. return false;
  235. }
  236. }
  237. /** Replace the journal with a snapshot of the current state */
  238. void
  239. Project::compact ( void )
  240. {
  241. Loggable::compact();
  242. }