2008-05-04 09:10:15 +02:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
|
/* option) any later version. */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
|
/* more details. */
|
|
|
|
|
/* */
|
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
2008-05-07 01:33:24 +02:00
|
|
|
|
/* Routings for opening/closing/creation of projects. All the actual
|
2008-06-18 06:01:22 +02:00
|
|
|
|
project state belongs to Timeline and other classes. */
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
2008-06-21 06:38:29 +02:00
|
|
|
|
/* Project management routines. */
|
|
|
|
|
|
2008-05-04 09:10:15 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2008-05-04 09:32:54 +02:00
|
|
|
|
#include <sys/types.h>
|
2008-06-18 05:41:15 +02:00
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/fcntl.h>
|
2008-05-29 22:06:22 +02:00
|
|
|
|
#include <errno.h>
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
|
|
|
|
#include "Loggable.H"
|
2008-05-07 01:33:24 +02:00
|
|
|
|
#include "Project.H"
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
2008-06-22 01:46:11 +02:00
|
|
|
|
#include "Timeline.H" // for sample_rate()
|
|
|
|
|
#include "Engine/Engine.H" // for sample_rate()
|
2008-05-15 06:23:16 +02:00
|
|
|
|
#include "TLE.H" // all this just for load and save...
|
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
#include <FL/filename.H>
|
|
|
|
|
|
2008-06-18 06:01:22 +02:00
|
|
|
|
#include "util/debug.h"
|
|
|
|
|
#include "util/file.h"
|
|
|
|
|
|
2008-05-15 06:23:16 +02:00
|
|
|
|
extern TLE *tle;
|
|
|
|
|
|
2008-05-07 06:42:06 +02:00
|
|
|
|
/* FIXME: wrong place for this */
|
2008-05-07 02:41:07 +02:00
|
|
|
|
#define APP_TITLE "Non-DAW"
|
|
|
|
|
|
2008-06-21 23:28:17 +02:00
|
|
|
|
const int PROJECT_VERSION = 1;
|
2008-05-07 06:42:06 +02:00
|
|
|
|
|
2008-05-29 22:06:22 +02:00
|
|
|
|
|
|
|
|
|
|
2008-06-22 01:46:11 +02:00
|
|
|
|
const char *Project::_errstr[] =
|
|
|
|
|
{
|
|
|
|
|
"Not a Non-DAW project",
|
|
|
|
|
"Locked by another process",
|
|
|
|
|
"Access denied",
|
|
|
|
|
"Samplerate mismatch",
|
|
|
|
|
"Incompatible project version"
|
|
|
|
|
};
|
|
|
|
|
|
2008-05-07 01:33:24 +02:00
|
|
|
|
char Project::_name[256];
|
2008-05-17 06:18:00 +02:00
|
|
|
|
char Project::_path[512];
|
2008-05-07 01:33:24 +02:00
|
|
|
|
bool Project::_is_open = false;
|
2008-05-29 22:06:22 +02:00
|
|
|
|
int Project::_lockfd = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-06-19 06:14:14 +02:00
|
|
|
|
/***********/
|
|
|
|
|
/* Private */
|
|
|
|
|
/***********/
|
|
|
|
|
|
2008-05-04 09:10:15 +02:00
|
|
|
|
void
|
2008-05-07 01:33:24 +02:00
|
|
|
|
Project::set_name ( const char *name )
|
2008-05-04 09:10:15 +02:00
|
|
|
|
{
|
2008-05-15 06:23:16 +02:00
|
|
|
|
strcpy( Project::_name, name );
|
|
|
|
|
|
|
|
|
|
if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
|
|
|
|
|
Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
|
|
|
|
|
|
|
|
|
|
char *s = rindex( Project::_name, '/' );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
2008-05-15 06:23:16 +02:00
|
|
|
|
s = s ? s + 1 : Project::_name;
|
|
|
|
|
|
|
|
|
|
memmove( Project::_name, s, strlen( s ) + 1 );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
2008-05-07 01:33:24 +02:00
|
|
|
|
for ( s = Project::_name; *s; ++s )
|
2008-05-04 09:10:15 +02:00
|
|
|
|
if ( *s == '_' || *s == '-' )
|
|
|
|
|
*s = ' ';
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-07 02:41:07 +02:00
|
|
|
|
bool
|
|
|
|
|
Project::write_info ( void )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ( ! open() )
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if ( ! ( fp = fopen( "info", "w" ) ) )
|
|
|
|
|
{
|
|
|
|
|
WARNING( "could not open project info file for writing." );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-21 23:28:17 +02:00
|
|
|
|
fprintf( fp, "created by\n\t%s\nversion\n\t%d\nsample rate\n\t%lu\n",
|
2008-05-07 06:42:06 +02:00
|
|
|
|
APP_TITLE " " VERSION,
|
|
|
|
|
PROJECT_VERSION,
|
2008-05-08 01:19:48 +02:00
|
|
|
|
(unsigned long)timeline->sample_rate() );
|
2008-05-07 02:41:07 +02:00
|
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2008-06-22 01:46:11 +02:00
|
|
|
|
Project::read_info ( int *version, nframes_t *sample_rate )
|
2008-05-07 02:41:07 +02:00
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if ( ! ( fp = fopen( "info", "r" ) ) )
|
|
|
|
|
{
|
|
|
|
|
WARNING( "could not open project info file for reading." );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-21 23:28:17 +02:00
|
|
|
|
char *name, *value;
|
|
|
|
|
|
|
|
|
|
while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
|
|
|
|
|
{
|
|
|
|
|
MESSAGE( "Info: %s = %s", name, value );
|
|
|
|
|
|
|
|
|
|
if ( ! strcmp( name, "sample rate" ) )
|
2008-06-22 01:46:11 +02:00
|
|
|
|
*sample_rate = atoll( value );
|
2008-06-21 23:28:17 +02:00
|
|
|
|
else if ( ! strcmp( name, "version" ) )
|
2008-06-22 01:46:11 +02:00
|
|
|
|
*version = atoi( value );
|
2008-06-21 23:28:17 +02:00
|
|
|
|
|
|
|
|
|
free( name );
|
|
|
|
|
free( value );
|
|
|
|
|
}
|
2008-05-07 02:41:07 +02:00
|
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-19 06:14:14 +02:00
|
|
|
|
/**********/
|
|
|
|
|
/* Public */
|
|
|
|
|
/**********/
|
|
|
|
|
|
|
|
|
|
/** Close the project (reclaiming all memory) */
|
|
|
|
|
bool
|
|
|
|
|
Project::close ( void )
|
|
|
|
|
{
|
|
|
|
|
if ( ! open() )
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
tle->save_timeline_settings();
|
|
|
|
|
|
|
|
|
|
Loggable::close();
|
|
|
|
|
|
|
|
|
|
write_info();
|
|
|
|
|
|
|
|
|
|
_is_open = false;
|
|
|
|
|
|
|
|
|
|
*Project::_name = '\0';
|
|
|
|
|
|
|
|
|
|
release_lock( &_lockfd, ".lock" );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Ensure a project is valid before opening it... */
|
2008-05-04 09:10:15 +02:00
|
|
|
|
bool
|
2008-05-17 06:18:00 +02:00
|
|
|
|
Project::validate ( const char *name )
|
2008-05-04 09:10:15 +02:00
|
|
|
|
{
|
2008-05-17 06:18:00 +02:00
|
|
|
|
bool r = true;
|
|
|
|
|
|
|
|
|
|
char pwd[512];
|
|
|
|
|
|
|
|
|
|
fl_filename_absolute( pwd, sizeof( pwd ), "." );
|
|
|
|
|
|
2008-05-04 09:10:15 +02:00
|
|
|
|
if ( chdir( name ) )
|
|
|
|
|
{
|
2008-05-07 01:33:24 +02:00
|
|
|
|
WARNING( "Cannot change to project dir \"%s\"", name );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
if ( ! exists( "info" ) ||
|
|
|
|
|
! exists( "history" ) ||
|
2008-05-04 09:10:15 +02:00
|
|
|
|
! exists( "sources" ) )
|
|
|
|
|
// ! exists( "options" ) )
|
|
|
|
|
{
|
2008-05-07 01:33:24 +02:00
|
|
|
|
WARNING( "Not a Non-DAW project: \"%s\"", name );
|
2008-05-17 06:18:00 +02:00
|
|
|
|
r = false;
|
2008-05-04 09:10:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
chdir( pwd );
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-19 06:14:14 +02:00
|
|
|
|
/** Try to open project /name/. Returns 0 if sucsessful, an error code
|
|
|
|
|
* otherwise */
|
2008-06-16 04:44:45 +02:00
|
|
|
|
int
|
2008-05-17 06:18:00 +02:00
|
|
|
|
Project::open ( const char *name )
|
|
|
|
|
{
|
|
|
|
|
if ( ! validate( name ) )
|
2008-06-16 04:44:45 +02:00
|
|
|
|
return E_INVALID;
|
2008-05-17 06:18:00 +02:00
|
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
|
|
chdir( name );
|
|
|
|
|
|
2008-06-18 05:41:15 +02:00
|
|
|
|
if ( ! acquire_lock( &_lockfd, ".lock" ) )
|
2008-06-22 01:46:11 +02:00
|
|
|
|
return E_LOCKED;
|
|
|
|
|
|
|
|
|
|
int version;
|
|
|
|
|
nframes_t rate;
|
2008-05-29 22:06:22 +02:00
|
|
|
|
|
2008-06-22 01:46:11 +02:00
|
|
|
|
if ( ! read_info( &version, &rate ) )
|
2008-06-21 23:28:17 +02:00
|
|
|
|
return E_INVALID;
|
|
|
|
|
|
2008-06-22 01:46:11 +02:00
|
|
|
|
if ( version != PROJECT_VERSION )
|
|
|
|
|
return E_VERSION;
|
|
|
|
|
|
2008-05-04 09:10:15 +02:00
|
|
|
|
if ( ! Loggable::open( "history" ) )
|
2008-06-22 01:46:11 +02:00
|
|
|
|
return E_INVALID;
|
|
|
|
|
|
|
|
|
|
timeline->sample_rate( rate );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
|
|
|
|
set_name( name );
|
2008-05-04 09:32:54 +02:00
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
*_path = '\0';
|
|
|
|
|
fl_filename_absolute( _path, sizeof( _path ), "." );
|
|
|
|
|
|
2008-05-06 06:33:41 +02:00
|
|
|
|
_is_open = true;
|
|
|
|
|
|
2008-05-15 06:23:16 +02:00
|
|
|
|
tle->load_timeline_settings();
|
|
|
|
|
|
2008-05-29 22:46:17 +02:00
|
|
|
|
timeline->zoom_fit();
|
|
|
|
|
|
2008-06-18 05:41:15 +02:00
|
|
|
|
MESSAGE( "Loaded project \"%s\"", name );
|
|
|
|
|
|
2008-06-16 04:44:45 +02:00
|
|
|
|
return 0;
|
2008-05-04 09:10:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-19 06:14:14 +02:00
|
|
|
|
/** Create a new project /name/ from existing template
|
|
|
|
|
* /template_name/ */
|
2008-05-04 09:10:15 +02:00
|
|
|
|
bool
|
2008-05-07 01:33:24 +02:00
|
|
|
|
Project::create ( const char *name, const char *template_name )
|
2008-05-04 09:10:15 +02:00
|
|
|
|
{
|
|
|
|
|
if ( exists( name ) )
|
|
|
|
|
{
|
2008-05-07 01:33:24 +02:00
|
|
|
|
WARNING( "Project already exists" );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
close();
|
|
|
|
|
|
2008-05-04 09:10:15 +02:00
|
|
|
|
if ( mkdir( name, 0777 ) )
|
|
|
|
|
{
|
2008-05-07 01:33:24 +02:00
|
|
|
|
WARNING( "Cannot create project directory" );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( chdir( name ) )
|
2008-05-07 01:33:24 +02:00
|
|
|
|
FATAL( "WTF? Cannot change to new project directory" );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
|
|
|
|
mkdir( "sources", 0777 );
|
|
|
|
|
|
2008-05-17 06:18:00 +02:00
|
|
|
|
creat( "info", 0666 );
|
2008-05-04 09:32:54 +02:00
|
|
|
|
creat( "history", 0666 );
|
2008-05-04 09:10:15 +02:00
|
|
|
|
|
|
|
|
|
/* TODO: copy template */
|
|
|
|
|
|
2008-06-16 04:44:45 +02:00
|
|
|
|
if ( open( name ) == 0 )
|
2008-05-08 00:55:01 +02:00
|
|
|
|
{
|
2008-05-17 06:18:00 +02:00
|
|
|
|
write_info();
|
|
|
|
|
|
2008-05-08 00:55:01 +02:00
|
|
|
|
/* add the bare essentials */
|
|
|
|
|
timeline->beats_per_minute( 0, 120 );
|
|
|
|
|
timeline->time( 0, 4, 4 );
|
|
|
|
|
|
2008-06-18 05:41:15 +02:00
|
|
|
|
MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
|
2008-05-08 00:55:01 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
2008-06-18 05:41:15 +02:00
|
|
|
|
{
|
|
|
|
|
WARNING( "Failed to open newly created project" );
|
2008-05-08 00:55:01 +02:00
|
|
|
|
return false;
|
2008-06-18 05:41:15 +02:00
|
|
|
|
}
|
2008-05-04 09:10:15 +02:00
|
|
|
|
}
|
2008-06-19 06:14:14 +02:00
|
|
|
|
|
|
|
|
|
/** Replace the journal with a snapshot of the current state */
|
|
|
|
|
void
|
|
|
|
|
Project::compact ( void )
|
|
|
|
|
{
|
|
|
|
|
Loggable::compact();
|
|
|
|
|
}
|