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.

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