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