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
15KB

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