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.

349 lines
7.9KB

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