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.

843 lines
18KB

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