From 793fb0517605f30c1f86e582f90ef03e39feb12d Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Fri, 18 Apr 2008 20:05:57 -0500 Subject: [PATCH] Work on making journal replay at startup. --- Timeline/Loggable.C | 32 +++++++++++- Timeline/Loggable.H | 3 ++ Timeline/Makefile | 2 + Timeline/Tempo_Point.C | 112 +++++++++++++++++++++++++++++++++++++++++ Timeline/Tempo_Point.H | 107 ++++----------------------------------- Timeline/Tempo_Track.H | 3 +- Timeline/Time_Point.C | 69 +++++++++++++++++++++++++ Timeline/Time_Point.H | 59 +--------------------- Timeline/Timeline.C | 38 ++++++++------ Timeline/Timeline.H | 13 ++--- Timeline/main.C | 5 +- 11 files changed, 265 insertions(+), 178 deletions(-) create mode 100644 Timeline/Tempo_Point.C create mode 100644 Timeline/Time_Point.C diff --git a/Timeline/Loggable.C b/Timeline/Loggable.C index 6b078ab..23db748 100644 --- a/Timeline/Loggable.C +++ b/Timeline/Loggable.C @@ -44,6 +44,21 @@ Loggable::open ( const char *filename ) return false; } + /* TODO: replay log here */ + + + /* FIXME: handle transactions!!! */ + + { + char buf[BUFSIZ]; + + while ( fscanf( _fp, "%[^\n]\n", buf ) == 1 ) + { + do_this( buf, false ); + } + } + + return true; } @@ -116,7 +131,9 @@ bool Loggable::do_this ( const char *s, bool reverse ) { int id; - sscanf( s, "%*s %X ", &id ); + if ( ! ( sscanf( s, "%*s %X ", &id ) > 0 ) ) + return false; + Loggable *l = find( id ); // assert( l ); @@ -328,6 +345,19 @@ Loggable::log ( const char *fmt, ... ) void Loggable::flush ( void ) { + if ( ! _fp ) + { + printf( "error: no log file open!\n" ); + + while ( ! _transaction.empty() ) + { + free( _transaction.front() ); + _transaction.pop(); + } + + return; + } + int n = _transaction.size(); if ( n > 1 ) diff --git a/Timeline/Loggable.H b/Timeline/Loggable.H index a1b336e..087a49f 100644 --- a/Timeline/Loggable.H +++ b/Timeline/Loggable.H @@ -128,6 +128,9 @@ public: _id = id; + /* make sure it'll fit */ + _loggables.reserve( _id ); + assert( ! _loggables[ _id - 1 ] ); _loggables[ _id - 1 ] = this; diff --git a/Timeline/Makefile b/Timeline/Makefile index e74be8f..1b9ac15 100644 --- a/Timeline/Makefile +++ b/Timeline/Makefile @@ -8,6 +8,8 @@ SRCS= \ Timeline.C \ Track_Header.C \ Track_Widget.C \ + Tempo_Point.C \ + Time_Point.C \ Peaks.C \ Audio_File.C \ Audio_File_SF.C \ diff --git a/Timeline/Tempo_Point.C b/Timeline/Tempo_Point.C new file mode 100644 index 0000000..a04b4ef --- /dev/null +++ b/Timeline/Tempo_Point.C @@ -0,0 +1,112 @@ + +/*******************************************************************************/ +/* 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. */ +/*******************************************************************************/ + + +#include "Tempo_Point.H" +#include "Tempo_Track.H" +#include "Timeline.H" // for timeline->tempo_track + +char ** +Tempo_Point::get ( void ) +{ + char **sa = (char**)malloc( sizeof( char* ) * 4 ); + + int i = 0; + + asprintf( &sa[i++], ":x %lu", _r->offset ); + asprintf( &sa[i++], ":tempo %.2f", _tempo ); + + sa[i] = NULL; + + return sa; +} + +void +Tempo_Point::set ( char **sa ) +{ + for ( int i = 0; sa[i]; ++i ) + { + char *s = sa[i]; + + strtok( s, " " ); + + char *v = s + strlen( s ) + 1; + + if ( ! strcmp( s, ":x" ) ) + _r->offset = atol( v ); + else if ( ! strcmp( s, ":tempo" ) ) + _tempo = atof( v ); + + /* FIXME: we need to add this to the time track on creation!!! */ + timeline->tempo_track->add( this ); + + free( s ); + } + + free( sa ); + + timeline->redraw(); + + _make_label(); +} + + +/* for loggable */ +Loggable * +Tempo_Point::create ( char **sa ) +{ + Tempo_Point *r = new Tempo_Point; + + r->set( sa ); + + return (Loggable *)r; +} + + +Tempo_Point::Tempo_Point ( nframes_t when, float bpm ) +{ + _tempo = bpm; + _r->offset = when; + + _make_label(); + + log_create(); +} + + + +Tempo_Point::~Tempo_Point ( ) +{ + if ( _label ) delete[] _label; + log_destroy(); +} + + +int +Tempo_Point::handle ( int m ) +{ + int r = Track_Widget::handle( m ); + + if ( m == FL_RELEASE ) + { + _track->sort(); + timeline->redraw(); + } + return r; +} diff --git a/Timeline/Tempo_Point.H b/Timeline/Tempo_Point.H index fb995d9..39ab3e8 100644 --- a/Timeline/Tempo_Point.H +++ b/Timeline/Tempo_Point.H @@ -20,8 +20,7 @@ #pragma once #include "Track_Point.H" - -#define __CLASS__ "Tempo_Point" +// #include "Tempo_Track.H" class Tempo_Point : public Track_Point { @@ -40,88 +39,20 @@ protected: const char *class_name ( void ) { return "Tempo_Point"; } - char ** get ( void ) - { - char **sa = (char**)malloc( sizeof( char* ) * 4 ); - - int i = 0; - - asprintf( &sa[i++], ":track 0x%X", _track ? _track->id() : 0 ); - asprintf( &sa[i++], ":x %lu", _r->offset ); - asprintf( &sa[i++], ":tempo %.2f", _tempo ); - - sa[i] = NULL; - - return sa; - } - - void - set ( char **sa ) - { - for ( int i = 0; sa[i]; ++i ) - { - char *s = sa[i]; - - strtok( s, " " ); - - char *v = s + strlen( s ) + 1; - - if ( ! strcmp( s, ":x" ) ) - _r->offset = atol( v ); - else - if ( ! strcmp( s, ":tempo" ) ) - _tempo = atof( v ); - else - if ( ! strcmp( s, ":track" ) ) - { - int i; - sscanf( v, "%X", &i ); - Track *t = (Track*)Loggable::find( i ); - - assert( t ); - - t->add( this ); - } - - - free( s ); - } - - free( sa ); - - timeline->redraw(); - - _make_label(); - } + char ** get ( void ); + void set ( char **sa ); Tempo_Point ( ) { - } public: - /* for loggable */ - static Loggable * - create ( char **sa ) - { - Tempo_Point *r = new Tempo_Point; + static Loggable * create ( char **sa ); - r->set( sa ); + Tempo_Point ( nframes_t when, float bpm ); - return (Loggable *)r; - } - - - Tempo_Point ( nframes_t when, float bpm ) - { - _tempo = bpm; - _r->offset = when; - - _make_label(); - - log_create(); - } + ~Tempo_Point ( ); Tempo_Point ( const Tempo_Point &rhs ) { @@ -134,27 +65,11 @@ public: return new Tempo_Point( *(Tempo_Point*)r ); } - ~Tempo_Point ( ) - { - if ( _label ) delete[] _label; - log_destroy(); - } - float tempo ( void ) const { return _tempo; } - void tempo ( float v ) { _tempo = v; } - - int - handle ( int m ) - { - int r = Track_Widget::handle( m ); - - if ( m == FL_RELEASE ) - { - _track->sort(); - timeline->redraw(); - } - return r; - } + float tempo ( void ) const + { return _tempo; } + void tempo ( float v ) + { _tempo = v; } + int handle ( int m ); }; -#undef __CLASS__ diff --git a/Timeline/Tempo_Track.H b/Timeline/Tempo_Track.H index 6873c49..c70a347 100644 --- a/Timeline/Tempo_Track.H +++ b/Timeline/Tempo_Track.H @@ -23,7 +23,6 @@ #include "Tempo_Point.H" #include -using std::list; class Tempo_Track : public Track { @@ -40,7 +39,7 @@ public: { // sort(); - for ( list ::const_reverse_iterator i = _widgets.rbegin(); + for ( std::list ::const_reverse_iterator i = _widgets.rbegin(); i != _widgets.rend(); i++ ) { if ( (*i)->offset() < when ) diff --git a/Timeline/Time_Point.C b/Timeline/Time_Point.C new file mode 100644 index 0000000..629f16c --- /dev/null +++ b/Timeline/Time_Point.C @@ -0,0 +1,69 @@ + +/*******************************************************************************/ +/* 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. */ +/*******************************************************************************/ + +#include "Time_Point.H" +#include "Time_Track.H" +#include "Timeline.H" // for timeline->time_track + +char ** +Time_Point::get ( void ) +{ + char **sa = (char**)malloc( sizeof( char* ) * 5 ); + + int i = 0; + + asprintf( &sa[i++], ":x %lu", _r->offset ); + asprintf( &sa[i++], ":beats_per_bar %d", _time.beats_per_bar ); + asprintf( &sa[i++], ":beat_type %d", _time.beat_type ); + + sa[i] = NULL; + + return sa; +} + +void +Time_Point::set ( char **sa ) +{ + for ( int i = 0; sa[i]; ++i ) + { + char *s = sa[i]; + + strtok( s, " " ); + + char *v = s + strlen( s ) + 1; + + if ( ! strcmp( s, ":x" ) ) + _r->offset = atol( v ); + else if ( ! strcmp( s, ":beats_per_bar" ) ) + _time.beats_per_bar = atoi( v ); + else if ( ! strcmp( s, ":beat_type" ) ) + _time.beat_type = atoi( v ); + + /* FIXME: we need to add this to the time track on creation!!! */ + timeline->time_track->add( this ); + + free( s ); + } + + free( sa ); + + timeline->redraw(); + + _make_label(); +} diff --git a/Timeline/Time_Point.H b/Timeline/Time_Point.H index 3a96fe5..ebccc68 100644 --- a/Timeline/Time_Point.H +++ b/Timeline/Time_Point.H @@ -65,63 +65,8 @@ protected: const char *class_name ( void ) { return "Time_Point"; } - char ** get ( void ) - { - char **sa = (char**)malloc( sizeof( char* ) * 5 ); - - int i = 0; - - asprintf( &sa[i++], ":track 0x%X", _track ? _track->id() : 0 ); - asprintf( &sa[i++], ":x %lu", _r->offset ); - asprintf( &sa[i++], ":beats_per_bar %d", _time.beats_per_bar ); - asprintf( &sa[i++], ":beat_type %d", _time.beat_type ); - - sa[i] = NULL; - - return sa; - } - - void - set ( char **sa ) - { - for ( int i = 0; sa[i]; ++i ) - { - char *s = sa[i]; - - strtok( s, " " ); - - char *v = s + strlen( s ) + 1; - - if ( ! strcmp( s, ":x" ) ) - _r->offset = atol( v ); - else - if ( ! strcmp( s, ":beats_per_bar" ) ) - _time.beats_per_bar = atoi( v ); - else - if ( ! strcmp( s, ":beat_type" ) ) - _time.beat_type = atoi( v ); - else - if ( ! strcmp( s, ":track" ) ) - { - int i; - sscanf( v, "%X", &i ); - Track *t = (Track*)Loggable::find( i ); - - assert( t ); - - t->add( this ); - } - - - free( s ); - } - - free( sa ); - - timeline->redraw(); - - _make_label(); - } + char ** get ( void ); + void set ( char **sa ); public: diff --git a/Timeline/Timeline.C b/Timeline/Timeline.C index 3863f48..acbc711 100644 --- a/Timeline/Timeline.C +++ b/Timeline/Timeline.C @@ -178,18 +178,18 @@ Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Wi o->type( Fl_Pack::VERTICAL ); o->spacing( 0 ); - for ( int i = 1; i--; ) - { -// Track_Header *t = new Track_Header( 0, 0, W, 75 ); - Track_Header *t = new Track_Header( 0, 0, W, 30 ); - Track *o = new Audio_Track( 0, 0, 1, 100 ); +/* for ( int i = 1; i--; ) */ +/* { */ +/* // Track_Header *t = new Track_Header( 0, 0, W, 75 ); */ +/* Track_Header *t = new Track_Header( 0, 0, W, 30 ); */ +/* Track *o = new Audio_Track( 0, 0, 1, 100 ); */ - t->track( o ); - t->add( new Audio_Track( 0, 0, 1, 100 ) ); - t->add( new Audio_Track( 0, 0, 1, 100 ) ); - t->add_control( new Control_Track( 0, 0, 1, 100 ) ); - t->color( (Fl_Color)rand() ); - } +/* t->track( o ); */ +/* t->add( new Audio_Track( 0, 0, 1, 100 ) ); */ +/* t->add( new Audio_Track( 0, 0, 1, 100 ) ); */ +/* t->add_control( new Control_Track( 0, 0, 1, 100 ) ); */ +/* t->color( (Fl_Color)rand() ); */ +/* } */ tracks = o; o->end(); @@ -443,13 +443,21 @@ Timeline::draw ( void ) { int X, Y, W, H; - X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1; + int bdx = 0; + int bdw = 0; + + /* FIXME: hack to avoid clobbering the box corners of tracks. */ + if ( tracks->children() ) + { + bdx = Fl::box_dx( tracks->child( 0 )->box() ); + bdw = Fl::box_dw( tracks->child( 0 )->box() ); + } + + X = tracks->x() + bdx + 1; Y = tracks->y(); - W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1; + W = tracks->w() - bdw - 1; H = tracks->h(); - - /* if ( damage() & FL_DAMAGE_USER1 ) */ /* { */ diff --git a/Timeline/Timeline.H b/Timeline/Timeline.H index 4853031..281dc27 100644 --- a/Timeline/Timeline.H +++ b/Timeline/Timeline.H @@ -46,8 +46,8 @@ class Tempo_Track; class Time_Track; class Ruler_Track; -#include -using std::list; +/* #include */ +/* using std::list; */ // disables double-buffering to make unnecessary redrawing more apparent @@ -105,10 +105,6 @@ class Timeline : public Fl_Overlay_Window, public RWLock Scalebar *hscroll; Fl_Scrollbar *vscroll; - Tempo_Track *tempo_track; - Time_Track *time_track; - Ruler_Track *ruler_track; - static void cb_scroll ( Fl_Widget *w, void *v ); void cb_scroll ( Fl_Widget *w ); @@ -120,6 +116,11 @@ class Timeline : public Fl_Overlay_Window, public RWLock public: + Tempo_Track *tempo_track; + Time_Track *time_track; + Ruler_Track *ruler_track; + + nframes_t xoffset; int _yposition; diff --git a/Timeline/main.C b/Timeline/main.C index 9c439dc..c278305 100644 --- a/Timeline/main.C +++ b/Timeline/main.C @@ -72,7 +72,6 @@ main ( int argc, char **argv ) Fl::scheme( "plastic" ); // Fl::scheme( "gtk+" ); - Loggable::open( "history" ); /* welcome to C++ */ Loggable::register_create( "Region", &Region::create ); Loggable::register_create( "Tempo_Point", &Tempo_Point::create ); @@ -80,12 +79,16 @@ main ( int argc, char **argv ) Loggable::register_create( "Control_Point", &Control_Point::create ); Loggable::register_create( "Track_Header", &Track_Header::create ); + /* TODO: change to seesion dir */ + /* we don't really need a pointer for this */ engine = new Engine; engine->init(); timeline = new Timeline( 0, 24, main_window->w(), main_window->h() - 24, "Timeline" ); + Loggable::open( "history" ); + Fl_Button *o = new Fl_Button( 0, 0, 50, 24, "undo" ); o->shortcut( FL_CTRL + 'z' ); o->callback( cb_undo, 0 );