1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: x11_timer.cpp,v 1.1 2004/01/03 23:31:34 asmax Exp $
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "x11_timer.hpp"
31 #include "x11_factory.hpp"
34 X11Timer::X11Timer( intf_thread_t *pIntf, const Callback &rCallback ):
35 OSTimer( pIntf ), m_callback( rCallback )
37 // Get the instance of timer loop
38 X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
39 m_pTimerLoop = m_pOsFactory->getTimerLoop();
49 void X11Timer::start( int delay, bool oneShot )
51 m_interval = 1000 * delay;
53 m_nextDate = mdate() + m_interval;
54 m_pTimerLoop->addTimer( *this );
60 m_pTimerLoop->removeTimer( *this );
64 mtime_t X11Timer::getNextDate() const
70 bool X11Timer::execute()
72 m_nextDate += m_interval;
73 // Execute the callback
74 (*(m_callback.getFunc()))( m_callback.getObj() );
80 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
81 SkinObject( pIntf ), m_connectionNumber( connectionNumber )
86 X11TimerLoop::~X11TimerLoop()
91 void X11TimerLoop::addTimer( X11Timer &rTimer )
93 m_timers.push_back( &rTimer );
97 void X11TimerLoop::removeTimer( X11Timer &rTimer )
99 m_timers.remove( &rTimer );
103 void X11TimerLoop::waitNextTimer()
105 mtime_t curDate = mdate();
106 mtime_t nextDate = LAST_MDATE;
108 X11Timer *nextTimer = NULL;
110 // Find the next timer to execute
111 list<X11Timer*>::const_iterator timer;
112 for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
114 mtime_t timerDate = (*timer)->getNextDate();
115 if( timerDate < nextDate )
118 nextDate = timerDate;
122 if( nextTimer == NULL )
128 if( nextDate > curDate )
130 if( this->sleep( (nextDate - curDate ) / 1000 ) )
132 // The sleep has been interrupted: stop here
136 // Execute the timer callback
137 if( ! nextTimer->execute() )
139 // Remove the timer if execute() returned false
140 m_timers.remove( nextTimer );
146 bool X11TimerLoop::sleep( int delay )
150 tv.tv_sec = delay / 1000;
151 tv.tv_usec = 1000 * (delay % 1000);
153 // FD set for select()
156 FD_SET( m_connectionNumber, &rfds );
158 // Wait for an X11 event, or timeout
159 int num = select( m_connectionNumber + 1, &rfds, NULL, NULL, &tv );