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.

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