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.

404 lines
9.1KB

  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 "debug.h"
  36. #include "file.h"
  37. #include "Block_Timer.H"
  38. #include "Transport.H"
  39. extern Transport *transport;
  40. extern TLE *tle;
  41. const int PROJECT_VERSION = 2;
  42. extern char *instance_name;
  43. const char *Project::_errstr[] =
  44. {
  45. "Not a Non-DAW project",
  46. "Locked by another process",
  47. "Access denied",
  48. "Samplerate mismatch",
  49. "Incompatible project version"
  50. };
  51. char Project::_name[256];
  52. char Project::_created_on[40];
  53. char Project::_path[512];
  54. bool Project::_is_open = false;
  55. int Project::_lockfd = 0;
  56. /***********/
  57. /* Private */
  58. /***********/
  59. void
  60. Project::set_name ( const char *name )
  61. {
  62. strcpy( Project::_name, name );
  63. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  64. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  65. char *s = rindex( Project::_name, '/' );
  66. s = s ? s + 1 : Project::_name;
  67. memmove( Project::_name, s, strlen( s ) + 1 );
  68. for ( s = Project::_name; *s; ++s )
  69. if ( *s == '_' || *s == '-' )
  70. *s = ' ';
  71. }
  72. bool
  73. Project::write_info ( void )
  74. {
  75. FILE *fp;
  76. if ( ! ( fp = fopen( "info", "w" ) ) )
  77. {
  78. WARNING( "could not open project info file for writing." );
  79. return false;
  80. }
  81. char s[40];
  82. if ( ! *_created_on )
  83. {
  84. time_t t = time( NULL );
  85. ctime_r( &t, s );
  86. s[ strlen( s ) - 1 ] = '\0';
  87. }
  88. else
  89. strcpy( s, _created_on );
  90. fprintf( fp, "created by\n\t%s\ncreated on\n\t%s\nversion\n\t%d\nsample rate\n\t%lu\n",
  91. APP_NAME " " VERSION,
  92. s,
  93. PROJECT_VERSION,
  94. (unsigned long)timeline->sample_rate() );
  95. fclose( fp );
  96. return true;
  97. }
  98. void
  99. Project::undo ( void )
  100. {
  101. timeline->wrlock();
  102. Loggable::undo();
  103. timeline->unlock();
  104. }
  105. bool
  106. Project::read_info ( int *version, nframes_t *sample_rate, char **creation_date, char **created_by )
  107. {
  108. FILE *fp;
  109. if ( ! ( fp = fopen( "info", "r" ) ) )
  110. {
  111. WARNING( "could not open project info file for reading." );
  112. return false;
  113. }
  114. *version = 0;
  115. *sample_rate = 0;
  116. *creation_date = 0;
  117. *created_by = 0;
  118. char *name, *value;
  119. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  120. {
  121. MESSAGE( "Info: %s = %s", name, value );
  122. if ( ! strcmp( name, "sample rate" ) )
  123. *sample_rate = atoll( value );
  124. else if ( ! strcmp( name, "version" ) )
  125. *version = atoi( value );
  126. else if ( ! strcmp( name, "created on" ) )
  127. *creation_date = strdup( value );
  128. else if ( ! strcmp( name, "created by" ) )
  129. *created_by = strdup( value );
  130. free( name );
  131. free( value );
  132. }
  133. fclose( fp );
  134. return true;
  135. }
  136. /**********/
  137. /* Public */
  138. /**********/
  139. /** Save out any settings and unjournaled state... */
  140. bool
  141. Project::save ( void )
  142. {
  143. if ( ! open() )
  144. return true;
  145. tle->save_timeline_settings();
  146. return Loggable::save_unjournaled_state();
  147. }
  148. /** Close the project (reclaiming all memory) */
  149. bool
  150. Project::close ( void )
  151. {
  152. if ( ! open() )
  153. return true;
  154. if ( ! save() )
  155. return false;
  156. timeline->wrlock();
  157. Loggable::close();
  158. timeline->unlock();
  159. // write_info();
  160. _is_open = false;
  161. *Project::_name = '\0';
  162. *Project::_created_on = '\0';
  163. release_lock( &_lockfd, ".lock" );
  164. delete engine;
  165. engine = NULL;
  166. return true;
  167. }
  168. /** Ensure a project is valid before opening it... */
  169. bool
  170. Project::validate ( const char *name )
  171. {
  172. bool r = true;
  173. char pwd[512];
  174. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  175. if ( chdir( name ) )
  176. {
  177. WARNING( "Cannot change to project dir \"%s\"", name );
  178. return false;
  179. }
  180. if ( ! exists( "info" ) ||
  181. ! exists( "history" ) ||
  182. ! exists( "sources" ) )
  183. // ! exists( "options" ) )
  184. {
  185. WARNING( "Not a Non-DAW project: \"%s\"", name );
  186. r = false;
  187. }
  188. chdir( pwd );
  189. return r;
  190. }
  191. void
  192. Project::make_engine ( void )
  193. {
  194. if ( engine )
  195. FATAL( "Engine should be null!" );
  196. engine = new Engine;
  197. if ( ! engine->init( instance_name, JACK::Client::SLOW_SYNC | JACK::Client::TIMEBASE_MASTER ))
  198. FATAL( "Could not connect to JACK!" );
  199. timeline->sample_rate( engine->sample_rate() );
  200. /* always start stopped (please imagine for me a realistic
  201. * scenario requiring otherwise */
  202. transport->stop();
  203. }
  204. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  205. * otherwise */
  206. int
  207. Project::open ( const char *name )
  208. {
  209. if ( ! validate( name ) )
  210. return E_INVALID;
  211. close();
  212. chdir( name );
  213. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  214. return E_LOCKED;
  215. int version;
  216. nframes_t rate;
  217. char *creation_date;
  218. char *created_by;
  219. if ( ! read_info( &version, &rate, &creation_date, &created_by ) )
  220. return E_INVALID;
  221. if ( strncmp( created_by, APP_TITLE, strlen( APP_TITLE ) ) &&
  222. strncmp( created_by, APP_NAME, strlen( APP_NAME ) ) )
  223. return E_INVALID;
  224. if ( version > PROJECT_VERSION )
  225. return E_VERSION;
  226. if ( version < 2 )
  227. {
  228. /* FIXME: Version 1->2 adds Cursor_Sequence and Cursor_Point to default project... */
  229. }
  230. /* normally, engine will be NULL after a close or on an initial open,
  231. but 'new' will have already created it to get the sample rate. */
  232. if ( ! engine )
  233. make_engine();
  234. timeline->wrlock();
  235. {
  236. Block_Timer timer( "Replayed journal" );
  237. if ( ! Loggable::open( "history" ) )
  238. return E_INVALID;
  239. }
  240. timeline->unlock();
  241. /* /\* really a good idea? *\/ */
  242. /* timeline->sample_rate( rate ); */
  243. if ( creation_date )
  244. {
  245. strcpy( _created_on, creation_date );
  246. free( creation_date );
  247. }
  248. else
  249. *_created_on = 0;
  250. set_name( name );
  251. *_path = '\0';
  252. fl_filename_absolute( _path, sizeof( _path ), "." );
  253. _is_open = true;
  254. tle->load_timeline_settings();
  255. timeline->zoom_fit();
  256. MESSAGE( "Loaded project \"%s\"", name );
  257. return 0;
  258. }
  259. /** Create a new project /name/ from existing template
  260. * /template_name/ */
  261. bool
  262. Project::create ( const char *name, const char *template_name )
  263. {
  264. if ( exists( name ) )
  265. {
  266. WARNING( "Project already exists" );
  267. return false;
  268. }
  269. close();
  270. if ( mkdir( name, 0777 ) )
  271. {
  272. WARNING( "Cannot create project directory" );
  273. return false;
  274. }
  275. if ( chdir( name ) )
  276. FATAL( "WTF? Cannot change to new project directory" );
  277. mkdir( "sources", 0777 );
  278. creat( "history", 0666 );
  279. if ( ! engine )
  280. make_engine();
  281. /* TODO: copy template */
  282. write_info();
  283. if ( open( name ) == 0 )
  284. {
  285. /* add the bare essentials */
  286. timeline->beats_per_minute( 0, 120 );
  287. timeline->time( 0, 4, 4 );
  288. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  289. return true;
  290. }
  291. else
  292. {
  293. WARNING( "Failed to open newly created project" );
  294. return false;
  295. }
  296. }
  297. /** Replace the journal with a snapshot of the current state */
  298. void
  299. Project::compact ( void )
  300. {
  301. Block_Timer timer( "Compacted journal" );
  302. Loggable::compact();
  303. }