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.

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