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.

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