File indexing completed on 2024-05-12 05:34:36

0001 /*
0002     this file is part of the oxygen gtk engine
0003     SPDX-FileCopyrightText: 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "oxygentimelineserver.h"
0009 #include "oxygentimeline.h"
0010 #include "../config.h"
0011 
0012 #include <gdk/gdk.h>
0013 #include <iostream>
0014 
0015 namespace Oxygen
0016 {
0017 
0018     //! time interval between two updates (use 20 msec for now)
0019     static const int updateInterval = 20;
0020 
0021     //____________________________________________________________________
0022     TimeLineServer* TimeLineServer::_instance = 0L;
0023     TimeLineServer& TimeLineServer::instance( void )
0024     {
0025 
0026         if( !_instance )
0027         { _instance = new TimeLineServer(); }
0028 
0029         return *_instance;
0030     }
0031 
0032     //____________________________________________________________________
0033     TimeLineServer::TimeLineServer( void ):
0034         _timerId( 0 )
0035     {
0036         #if OXYGEN_DEBUG
0037         std::cerr << "TimeLineServer::TimeLineServer." << std::endl;
0038         #endif
0039     }
0040 
0041     //____________________________________________________________________
0042     TimeLineServer::~TimeLineServer( void )
0043     {
0044 
0045         #if OXYGEN_DEBUG
0046         std::cerr << "TimeLineServer::~TimeLineServer." << std::endl;
0047         #endif
0048 
0049         // clear timer Id and singleton
0050         if( _timerId ) g_source_remove( _timerId );
0051         _instance = 0L;
0052 
0053     }
0054 
0055     //____________________________________________________________________
0056     void TimeLineServer::start( void )
0057     {
0058 
0059         if( !_timerId )
0060         { _timerId =  gdk_threads_add_timeout( updateInterval, (GSourceFunc)update, this ); }
0061 
0062         return;
0063 
0064     }
0065 
0066     //____________________________________________________________________
0067     void TimeLineServer::stop( void )
0068     {
0069 
0070         if( _timerId )
0071         {
0072             g_source_remove( _timerId );
0073             _timerId = 0;
0074         }
0075 
0076     }
0077 
0078     //____________________________________________________________________
0079     gboolean TimeLineServer::update( gpointer data )
0080     {
0081 
0082         bool running( false );
0083         TimeLineServer& server( *static_cast<TimeLineServer*>( data ) );
0084 
0085         // loop over timelines
0086         for( TimeLineSet::const_iterator iter = server._timeLines.begin(); iter != server._timeLines.end(); ++iter )
0087         { if( (*iter)->update() ) running = true; }
0088 
0089         // stop timeout
0090         if( !running ) server.stop();
0091 
0092         // return true if at least one timeline is still running
0093         return gboolean( running );
0094 
0095     }
0096 
0097 }