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.

255 lines
5.7KB

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