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.

275 lines
6.0KB

  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 "TLE.H" // all this just for load and save...
  32. #include <FL/filename.H>
  33. #include "util/debug.h"
  34. #include "util/file.h"
  35. extern TLE *tle;
  36. /* FIXME: wrong place for this */
  37. #define APP_TITLE "Non-DAW"
  38. #define PROJECT_VERSION "0.28.0"
  39. char Project::_name[256];
  40. char Project::_path[512];
  41. bool Project::_is_open = false;
  42. int Project::_lockfd = 0;
  43. /***********/
  44. /* Private */
  45. /***********/
  46. void
  47. Project::set_name ( const char *name )
  48. {
  49. strcpy( Project::_name, name );
  50. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  51. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  52. char *s = rindex( Project::_name, '/' );
  53. s = s ? s + 1 : Project::_name;
  54. memmove( Project::_name, s, strlen( s ) + 1 );
  55. for ( s = Project::_name; *s; ++s )
  56. if ( *s == '_' || *s == '-' )
  57. *s = ' ';
  58. }
  59. bool
  60. Project::write_info ( void )
  61. {
  62. if ( ! open() )
  63. return true;
  64. FILE *fp;
  65. if ( ! ( fp = fopen( "info", "w" ) ) )
  66. {
  67. WARNING( "could not open project info file for writing." );
  68. return false;
  69. }
  70. fprintf( fp, "created by\n\t%s\nversion\n\t%s\nsample rate\n\t%lu\n",
  71. APP_TITLE " " VERSION,
  72. PROJECT_VERSION,
  73. (unsigned long)timeline->sample_rate() );
  74. fclose( fp );
  75. return true;
  76. }
  77. bool
  78. Project::read_info ( void )
  79. {
  80. FILE *fp;
  81. if ( ! ( fp = fopen( "info", "r" ) ) )
  82. {
  83. WARNING( "could not open project info file for reading." );
  84. return false;
  85. }
  86. /* TODO: something */
  87. fclose( fp );
  88. return true;
  89. }
  90. /**********/
  91. /* Public */
  92. /**********/
  93. /** Close the project (reclaiming all memory) */
  94. bool
  95. Project::close ( void )
  96. {
  97. if ( ! open() )
  98. return true;
  99. tle->save_timeline_settings();
  100. Loggable::close();
  101. write_info();
  102. _is_open = false;
  103. *Project::_name = '\0';
  104. release_lock( &_lockfd, ".lock" );
  105. return true;
  106. }
  107. /** Ensure a project is valid before opening it... */
  108. bool
  109. Project::validate ( const char *name )
  110. {
  111. bool r = true;
  112. char pwd[512];
  113. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  114. if ( chdir( name ) )
  115. {
  116. WARNING( "Cannot change to project dir \"%s\"", name );
  117. return false;
  118. }
  119. if ( ! exists( "info" ) ||
  120. ! exists( "history" ) ||
  121. ! exists( "sources" ) )
  122. // ! exists( "options" ) )
  123. {
  124. WARNING( "Not a Non-DAW project: \"%s\"", name );
  125. r = false;
  126. }
  127. chdir( pwd );
  128. return r;
  129. }
  130. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  131. * otherwise */
  132. int
  133. Project::open ( const char *name )
  134. {
  135. if ( ! validate( name ) )
  136. return E_INVALID;
  137. close();
  138. chdir( name );
  139. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  140. {
  141. WARNING( "Could not open project: locked by another process!" );
  142. return Project::E_LOCKED;
  143. }
  144. if ( ! Loggable::open( "history" ) )
  145. FATAL( "error opening journal" );
  146. set_name( name );
  147. *_path = '\0';
  148. fl_filename_absolute( _path, sizeof( _path ), "." );
  149. read_info();
  150. _is_open = true;
  151. tle->load_timeline_settings();
  152. timeline->zoom_fit();
  153. MESSAGE( "Loaded project \"%s\"", name );
  154. return 0;
  155. }
  156. /** Create a new project /name/ from existing template
  157. * /template_name/ */
  158. bool
  159. Project::create ( const char *name, const char *template_name )
  160. {
  161. if ( exists( name ) )
  162. {
  163. WARNING( "Project already exists" );
  164. return false;
  165. }
  166. close();
  167. if ( mkdir( name, 0777 ) )
  168. {
  169. WARNING( "Cannot create project directory" );
  170. return false;
  171. }
  172. if ( chdir( name ) )
  173. FATAL( "WTF? Cannot change to new project directory" );
  174. mkdir( "sources", 0777 );
  175. creat( "info", 0666 );
  176. creat( "history", 0666 );
  177. /* TODO: copy template */
  178. if ( open( name ) == 0 )
  179. {
  180. write_info();
  181. /* add the bare essentials */
  182. timeline->beats_per_minute( 0, 120 );
  183. timeline->time( 0, 4, 4 );
  184. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  185. return true;
  186. }
  187. else
  188. {
  189. WARNING( "Failed to open newly created project" );
  190. return false;
  191. }
  192. }
  193. /** Replace the journal with a snapshot of the current state */
  194. void
  195. Project::compact ( void )
  196. {
  197. Loggable::compact();
  198. }