1 /* 2 * this file is part of the oxygen gtk engine 3 * Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or(at your option ) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free 17 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 18 * MA 02110-1301, USA. 19 */ 20 21 #include "oxygentimelineserver.h" 22 #include "oxygentimeline.h" 23 #include "../config.h" 24 25 #include <gdk/gdk.h> 26 #include <iostream> 27 28 namespace Oxygen 29 { 30 31 //! time interval between two updates (use 20 msec for now) 32 static const int updateInterval = 20; 33 34 //____________________________________________________________________ 35 TimeLineServer* TimeLineServer::_instance = 0L; instance(void)36 TimeLineServer& TimeLineServer::instance( void ) 37 { 38 39 if( !_instance ) 40 { _instance = new TimeLineServer(); } 41 42 return *_instance; 43 } 44 45 //____________________________________________________________________ TimeLineServer(void)46 TimeLineServer::TimeLineServer( void ): 47 _timerId( 0 ) 48 { 49 #if OXYGEN_DEBUG 50 std::cerr << "TimeLineServer::TimeLineServer." << std::endl; 51 #endif 52 } 53 54 //____________________________________________________________________ ~TimeLineServer(void)55 TimeLineServer::~TimeLineServer( void ) 56 { 57 58 #if OXYGEN_DEBUG 59 std::cerr << "TimeLineServer::~TimeLineServer." << std::endl; 60 #endif 61 62 // clear timer Id and singleton 63 if( _timerId ) g_source_remove( _timerId ); 64 _instance = 0L; 65 66 } 67 68 //____________________________________________________________________ start(void)69 void TimeLineServer::start( void ) 70 { 71 72 if( !_timerId ) 73 { _timerId = gdk_threads_add_timeout( updateInterval, (GSourceFunc)update, this ); } 74 75 return; 76 77 } 78 79 //____________________________________________________________________ stop(void)80 void TimeLineServer::stop( void ) 81 { 82 83 if( _timerId ) 84 { 85 g_source_remove( _timerId ); 86 _timerId = 0; 87 } 88 89 } 90 91 //____________________________________________________________________ update(gpointer data)92 gboolean TimeLineServer::update( gpointer data ) 93 { 94 95 bool running( false ); 96 TimeLineServer& server( *static_cast<TimeLineServer*>( data ) ); 97 98 // loop over timelines 99 for( TimeLineSet::const_iterator iter = server._timeLines.begin(); iter != server._timeLines.end(); ++iter ) 100 { if( (*iter)->update() ) running = true; } 101 102 // stop timeout 103 if( !running ) server.stop(); 104 105 // return true if at least one timeline is still running 106 return gboolean( running ); 107 108 } 109 110 } 111