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.

802 lines
17KB

  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. /* This class handles all journaling. All journaled objects must
  19. inherit from Loggable as well as define a few special methods (via
  20. macros), get and set methods, and have contructors and destructors
  21. that call log_create() and log_destroy() in the appropriate
  22. order. Any action that might affect multiple loggable objects
  23. *must* be braced by calls to Loggable::block_start() and
  24. Loggable::block_end() in order for Undo to work properly. */
  25. #include "Loggable.H"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "file.h"
  30. // #include "const.h"
  31. #include "debug.h"
  32. #include <algorithm>
  33. using std::min;
  34. using std::max;
  35. FILE *Loggable::_fp;
  36. unsigned int Loggable::_log_id = 0;
  37. int Loggable::_level = 0;
  38. int Loggable::_dirty = 0;
  39. off_t Loggable::_undo_offset = 0;
  40. std::map <unsigned int, Loggable::log_pair > Loggable::_loggables;
  41. std::map <std::string, create_func*> Loggable::_class_map;
  42. std::queue <char *> Loggable::_transaction;
  43. progress_func *Loggable::_progress_callback = NULL;
  44. void *Loggable::_progress_callback_arg = NULL;
  45. snapshot_func *Loggable::_snapshot_callback = NULL;
  46. void *Loggable::_snapshot_callback_arg = NULL;
  47. Loggable::~Loggable ( )
  48. {
  49. _loggables[ _id ].loggable = NULL;
  50. }
  51. void
  52. Loggable::block_start ( void )
  53. {
  54. ++Loggable::_level;
  55. }
  56. void
  57. Loggable::block_end ( void )
  58. {
  59. --Loggable::_level;
  60. ASSERT( Loggable::_level >= 0, "Programming error" );
  61. if ( Loggable::_level == 0 )
  62. flush();
  63. }
  64. Loggable *
  65. Loggable::find ( unsigned int id )
  66. {
  67. if ( _relative_id )
  68. id += _relative_id;
  69. return _loggables[ id ].loggable;
  70. }
  71. /** Open the journal /filename/ and replay it, bringing the end state back into RAM */
  72. bool
  73. Loggable::open ( const char *filename )
  74. {
  75. FILE *fp;
  76. Loggable::_fp = NULL;
  77. if ( ! ( fp = fopen( filename, "a+" ) ) )
  78. {
  79. WARNING( "Could not open log file for writing!" );
  80. return false;
  81. }
  82. load_unjournaled_state();
  83. if ( newer( "snapshot", filename ) )
  84. {
  85. MESSAGE( "Loading snapshot" );
  86. FILE *fp = fopen( "snapshot", "r" );
  87. replay( fp );
  88. fclose( fp );
  89. }
  90. else
  91. {
  92. MESSAGE( "Replaying journal" );
  93. replay( fp );
  94. }
  95. fseek( fp, 0, SEEK_END );
  96. _undo_offset = ftell( fp );
  97. Loggable::_fp = fp;
  98. return true;
  99. }
  100. bool
  101. Loggable::load_unjournaled_state ( void )
  102. {
  103. FILE *fp;
  104. fp = fopen( "unjournaled", "r" );
  105. if ( ! fp )
  106. {
  107. DWARNING( "Could not open unjournaled state file for reading" );
  108. return false;
  109. }
  110. unsigned int id;
  111. char buf[BUFSIZ];
  112. while ( fscanf( fp, "%X set %[^\n]\n", &id, buf ) == 2 )
  113. _loggables[ id ].unjournaled_state = new Log_Entry( buf );
  114. fclose( fp );
  115. return true;
  116. }
  117. #include <sys/stat.h>
  118. #include <unistd.h>
  119. /** replay journal or snapshot */
  120. bool
  121. Loggable::replay ( const char *file )
  122. {
  123. if ( FILE *fp = fopen( file, "r" ) )
  124. {
  125. bool r = replay( fp );
  126. fclose( fp );
  127. return r;
  128. }
  129. else
  130. return false;
  131. }
  132. /** replay journal or snapshot */
  133. bool
  134. Loggable::replay ( FILE *fp )
  135. {
  136. /* FIXME: bogus */
  137. char buf[BUFSIZ];
  138. struct stat st;
  139. fstat( fileno( fp ), &st );
  140. off_t total = st.st_size;
  141. off_t current = 0;
  142. if ( _progress_callback )
  143. _progress_callback( 0, _progress_callback_arg );
  144. while ( fscanf( fp, "%[^\n]\n", buf ) == 1 )
  145. {
  146. if ( ! ( ! strcmp( buf, "{" ) || ! strcmp( buf, "}" ) ) )
  147. {
  148. if ( *buf == '\t' )
  149. do_this( buf + 1, false );
  150. else
  151. do_this( buf, false );
  152. }
  153. current = ftell( fp );
  154. if ( _progress_callback )
  155. _progress_callback( current * 100 / total, _progress_callback_arg );
  156. }
  157. if ( _progress_callback )
  158. _progress_callback( 0, _progress_callback_arg );
  159. return true;
  160. }
  161. /** close journal and delete all loggable objects, returing the systemt to a blank slate */
  162. bool
  163. Loggable::close ( void )
  164. {
  165. DMESSAGE( "closing journal and destroying all journaled objects" );
  166. if ( _fp )
  167. {
  168. fclose( _fp );
  169. _fp = NULL;
  170. }
  171. if ( ! snapshot( "snapshot" ) )
  172. WARNING( "Failed to create snapshot" );
  173. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  174. i != _loggables.end(); ++i )
  175. if ( i->second.loggable )
  176. delete i->second.loggable;
  177. save_unjournaled_state();
  178. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  179. i != _loggables.end(); ++i )
  180. if ( i->second.unjournaled_state )
  181. delete i->second.unjournaled_state;
  182. _loggables.clear();
  183. return true;
  184. }
  185. /** save out unjournaled state for all loggables */
  186. bool
  187. Loggable::save_unjournaled_state ( void )
  188. {
  189. FILE *fp;
  190. fp = fopen( "unjournaled", "w" );
  191. if ( ! fp )
  192. {
  193. DWARNING( "Could not open unjournaled state file for writing!" );
  194. return false;
  195. }
  196. for ( std::map <unsigned int, Loggable::log_pair >::iterator i = _loggables.begin();
  197. i != _loggables.end(); ++i )
  198. {
  199. if ( i->second.unjournaled_state )
  200. {
  201. char *s = i->second.unjournaled_state->print();
  202. fprintf( fp, "0x%X set %s\n", i->first, s );
  203. free( s );
  204. }
  205. }
  206. fclose( fp );
  207. return true;
  208. }
  209. /** must be called after construction in create() methods */
  210. void
  211. Loggable::update_id ( unsigned int id )
  212. {
  213. /* make sure we're the last one */
  214. ASSERT( _id == _log_id, "%u != %u", _id, _log_id );
  215. assert( _loggables[ _id ].loggable == this );
  216. _loggables[ _id ].loggable = NULL;
  217. _log_id = max( _log_id, id );
  218. /* return this id number to the system */
  219. // --_log_id;
  220. _id = id;
  221. if ( _loggables[ _id ].loggable )
  222. FATAL( "Attempt to create object with an ID (0x%X) that already exists. The existing object is of type \"%s\", the new one is \"%s\". Corrupt journal?", _id, _loggables[ _id ].loggable->class_name(), class_name() );
  223. _loggables[ _id ].loggable = this;
  224. }
  225. /** return a pointer to a static copy of /s/ with all special characters escaped */
  226. const char *
  227. Loggable::escape ( const char *s )
  228. {
  229. static char r[512];
  230. size_t i = 0;
  231. for ( ; *s && i < sizeof( r ); ++i, ++s )
  232. {
  233. if ( '\n' == *s )
  234. {
  235. r[ i++ ] = '\\';
  236. r[ i ] = 'n';
  237. }
  238. else if ( '"' == *s )
  239. {
  240. r[ i++ ] = '\\';
  241. r[ i ] = '"';
  242. }
  243. else
  244. r[ i ] = *s;
  245. }
  246. r[ i ] = '\0';
  247. return r;
  248. }
  249. unsigned int Loggable::_relative_id = 0;
  250. /* calls to do_this() between invocation of this method and
  251. * end_relative_id_mode() will have all their IDs made relative to the
  252. * highest available ID at this time of this call. Non-Mixer uses
  253. * this to allow importing of module chains */
  254. void
  255. Loggable::begin_relative_id_mode ( void )
  256. {
  257. _relative_id = ++_log_id;
  258. }
  259. void
  260. Loggable::end_relative_id_mode ( void )
  261. {
  262. _relative_id = 0;
  263. }
  264. /** 'do' a message like "Audio_Region 0xF1 set :r 123" */
  265. bool
  266. Loggable::do_this ( const char *s, bool reverse )
  267. {
  268. unsigned int id = 0;
  269. char classname[40];
  270. char command[40];
  271. char *arguments = NULL;
  272. int found = sscanf( s, "%s %X %s ", classname, &id, command );
  273. ASSERT( 3 == found, "Invalid journal entry format \"%s\"", s );
  274. const char *create, *destroy;
  275. if ( reverse )
  276. {
  277. // sscanf( s, "%s %*X %s %*[^\n<]<< %a[^\n]", classname, command, &arguments );
  278. sscanf( s, "%s %*X %s%*[^\n<]<< %a[^\n]", classname, command, &arguments );
  279. create = "destroy";
  280. destroy = "create";
  281. DMESSAGE( "undoing \"%s\"", s );
  282. }
  283. else
  284. {
  285. sscanf( s, "%s %*X %s %a[^\n<]", classname, command, &arguments );
  286. create = "create";
  287. destroy = "destroy";
  288. }
  289. if ( ! strcmp( command, destroy ) )
  290. {
  291. Loggable *l = find( id );
  292. /* deleting eg. a track, which contains a list of other
  293. widgets, causes destroy messages to be emitted for all those
  294. widgets, but when replaying the journal the destroy message
  295. causes the children to be deleted also... This is a temporary
  296. hack. Would it be better to queue up objects for deletion
  297. (when?) */
  298. if ( l )
  299. delete l;
  300. }
  301. else if ( ! strcmp( command, "set" ) )
  302. {
  303. // printf( "got set command (%s).\n", arguments );
  304. Loggable *l = find( id );
  305. ASSERT( l, "Unable to find object 0x%X referenced by command \"%s\"", id, s );
  306. Log_Entry e( arguments );
  307. l->log_start();
  308. l->set( e );
  309. l->log_end();
  310. }
  311. else if ( ! strcmp( command, create ) )
  312. {
  313. Log_Entry e( arguments );
  314. ASSERT( _class_map[ std::string( classname ) ], "Journal contains an object of class \"%s\", but I don't know how to create such objects.", classname );
  315. {
  316. if ( _relative_id )
  317. id += _relative_id;
  318. /* create */
  319. Loggable *l = _class_map[ std::string( classname ) ]( e, id );
  320. l->log_create();
  321. /* we're now creating a loggable. Apply any unjournaled
  322. * state it may have had in the past under this log ID */
  323. Log_Entry *e = _loggables[ id ].unjournaled_state;
  324. if ( e )
  325. l->set( *e );
  326. }
  327. }
  328. if ( arguments )
  329. free( arguments );
  330. return true;
  331. }
  332. /** Reverse the last journal transaction */
  333. void
  334. Loggable::undo ( void )
  335. {
  336. const int bufsiz = 1024;
  337. char buf[bufsiz];
  338. block_start();
  339. long here = ftell( _fp );
  340. fseek( _fp, _undo_offset, SEEK_SET );
  341. backwards_fgets( buf, bufsiz, _fp );
  342. if ( ! strcmp( buf, "}\n" ) )
  343. {
  344. DMESSAGE( "undoing block" );
  345. for ( ;; )
  346. {
  347. backwards_fgets( buf, bufsiz, _fp );
  348. char *s = buf;
  349. if ( *s != '\t' )
  350. break;
  351. else
  352. ++s;
  353. do_this( s, true );
  354. }
  355. }
  356. else
  357. do_this( buf, true );
  358. off_t uo = ftell( _fp );
  359. ASSERT( _undo_offset <= here, "WTF?" );
  360. block_end();
  361. _undo_offset = uo;
  362. }
  363. /** write a snapshot of the current state of all loggable objects to
  364. * file handle /fp/ */
  365. bool
  366. Loggable::snapshot ( FILE *fp )
  367. {
  368. FILE *ofp = _fp;
  369. if ( ! Loggable::_snapshot_callback )
  370. {
  371. DWARNING( "No snapshot callback defined" );
  372. return false;
  373. }
  374. if ( ! ( _fp = fp ) )
  375. {
  376. _fp = ofp;
  377. return false;
  378. }
  379. block_start();
  380. Loggable::_snapshot_callback( _snapshot_callback_arg );
  381. block_end();
  382. _fp = ofp;
  383. return true;
  384. }
  385. /** write a snapshot of the current state of all loggable objects to
  386. * file /name/ */
  387. bool
  388. Loggable::snapshot ( const char *name )
  389. {
  390. FILE *fp;
  391. if ( ! ( fp = fopen( name, "w" ) ))
  392. return false;
  393. bool r = snapshot( fp );
  394. fclose( fp );
  395. return r;
  396. }
  397. /** Replace the journal with a snapshot of the current state */
  398. void
  399. Loggable::compact ( void )
  400. {
  401. fseek( _fp, 0, SEEK_SET );
  402. ftruncate( fileno( _fp ), 0 );
  403. if ( ! snapshot( _fp ) )
  404. FATAL( "Could not write snapshot!" );
  405. fseek( _fp, 0, SEEK_END );
  406. }
  407. #include <stdarg.h>
  408. /** Writes (part of) a line to the journal. Each separate line will be
  409. * stored separately in _transaction until transaction is closed.
  410. */
  411. void
  412. Loggable::log ( const char *fmt, ... )
  413. {
  414. static char * buf = NULL;
  415. static size_t i = 0;
  416. static size_t buf_size = 0;
  417. if ( ! _fp )
  418. return;
  419. if ( NULL == buf )
  420. {
  421. buf_size = 1024;
  422. buf = (char*)malloc( buf_size );
  423. }
  424. va_list args;
  425. if ( fmt )
  426. {
  427. va_start( args, fmt );
  428. for ( ;; )
  429. {
  430. size_t l = vsnprintf( buf + i, buf_size - i, fmt, args );
  431. if ( l >= buf_size - i )
  432. {
  433. buf = (char*)realloc( buf, buf_size += (l + 1) + buf_size );
  434. }
  435. else
  436. {
  437. i += l;
  438. break;
  439. }
  440. }
  441. va_end( args );
  442. }
  443. if ( '\n' == buf[i-1] )
  444. {
  445. _transaction.push( strdup( buf ) );
  446. i = 0;
  447. }
  448. }
  449. /** End the current transaction and commit it to the journal */
  450. void
  451. Loggable::flush ( void )
  452. {
  453. if ( ! _fp )
  454. {
  455. // printf( "error: no log file open!\n" );
  456. while ( ! _transaction.empty() )
  457. {
  458. free( _transaction.front() );
  459. _transaction.pop();
  460. }
  461. return;
  462. }
  463. int n = _transaction.size();
  464. if ( n > 1 )
  465. fprintf( _fp, "{\n" );
  466. while ( ! _transaction.empty() )
  467. {
  468. char *s = _transaction.front();
  469. _transaction.pop();
  470. if ( n > 1 )
  471. fprintf( _fp, "\t" );
  472. fprintf( _fp, "%s", s );
  473. free( s );
  474. }
  475. if ( n > 1 )
  476. fprintf( _fp, "}\n" );
  477. if ( n )
  478. /* something done, reset undo index */
  479. _undo_offset = ftell( _fp );
  480. fflush( _fp );
  481. }
  482. /** Print bidirectional journal entry */
  483. void
  484. Loggable::log_print( const Log_Entry *o, const Log_Entry *n ) const
  485. {
  486. if ( ! _fp )
  487. return;
  488. if ( n )
  489. for ( int i = 0; i < n->size(); ++i )
  490. {
  491. const char *s, *v;
  492. n->get( i, &s, &v );
  493. log( "%s %s%s", s, v, n->size() == i + 1 ? "" : " " );
  494. }
  495. if ( o && o->size() )
  496. {
  497. if ( n ) log( " << " );
  498. for ( int i = 0; i < o->size(); ++i )
  499. {
  500. const char *s, *v;
  501. o->get( i, &s, &v );
  502. log( "%s %s%s", s, v, o->size() == i + 1 ? "" : " " );
  503. }
  504. }
  505. log( "\n" );
  506. }
  507. /** Remember current object state for later comparison. *Must* be
  508. * called before any user action that might change one of the object's
  509. * journaled properties. */
  510. void
  511. Loggable::log_start ( void )
  512. {
  513. if ( ! _old_state )
  514. {
  515. _old_state = new Log_Entry;
  516. get( *_old_state );
  517. }
  518. ++_nest;
  519. }
  520. /** Log any change to the object's state since log_start(). */
  521. void
  522. Loggable::log_end ( void )
  523. {
  524. ASSERT( _old_state, "Programming error: log_end() called before log_start()" );
  525. if ( --_nest > 0 )
  526. return;
  527. Log_Entry *new_state;
  528. new_state = new Log_Entry;
  529. get( *new_state );
  530. if ( Log_Entry::diff( _old_state, new_state ) )
  531. {
  532. log( "%s 0x%X set ", class_name(), _id );
  533. log_print( _old_state, new_state );
  534. ++_dirty;
  535. }
  536. delete new_state;
  537. delete _old_state;
  538. _old_state = NULL;
  539. if ( Loggable::_level == 0 )
  540. Loggable::flush();
  541. }
  542. /** Log object creation. *Must* be called at the end of all public
  543. * constructors for leaf classes */
  544. void
  545. Loggable::log_create ( void ) const
  546. {
  547. ++_dirty;
  548. if ( ! _fp )
  549. /* replaying, don't bother */
  550. return;
  551. log( "%s 0x%X create ", class_name(), _id );
  552. Log_Entry e;
  553. get( e );
  554. if ( e.size() )
  555. log_print( NULL, &e );
  556. else
  557. log( "\n" );
  558. if ( Loggable::_level == 0 )
  559. Loggable::flush();
  560. }
  561. /** record this loggable's unjournaled state in memory */
  562. void
  563. Loggable::record_unjournaled ( void ) const
  564. {
  565. Log_Entry *e = new Log_Entry();
  566. get_unjournaled( *e );
  567. Log_Entry *le = _loggables[ _id ].unjournaled_state;
  568. if ( le )
  569. delete le;
  570. if ( e->size() )
  571. _loggables[ _id ].unjournaled_state = e;
  572. else
  573. {
  574. /* don't waste space on loggables with no unjournaled properties */
  575. _loggables[ _id ].unjournaled_state = NULL;
  576. delete e;
  577. }
  578. }
  579. /** Log object destruction. *Must* be called at the beginning of the
  580. * destructors of leaf classes */
  581. void
  582. Loggable::log_destroy ( void ) const
  583. {
  584. /* the unjournaled state may have changed: make a note of it. */
  585. record_unjournaled();
  586. ++_dirty;
  587. if ( ! _fp )
  588. /* tearing down... don't bother */
  589. return;
  590. log( "%s 0x%X destroy << ", class_name(), _id );
  591. Log_Entry e;
  592. get( e );
  593. log_print( NULL, &e );
  594. if ( Loggable::_level == 0 )
  595. Loggable::flush();
  596. }