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.

730 lines
16KB

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