1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
31 #include "x11_timer.hpp"
32 #include "x11_factory.hpp"
33 #include "../commands/cmd_generic.hpp"
36 X11Timer::X11Timer( intf_thread_t *pIntf, CmdGeneric &rCmd ):
37 OSTimer( pIntf ), m_rCommand( rCmd )
39 // Get the instance of timer loop
40 X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
41 m_pTimerLoop = m_pOsFactory->getTimerLoop();
51 void X11Timer::start( int delay, bool oneShot )
53 m_interval = 1000 * delay;
55 m_nextDate = mdate() + m_interval;
56 m_pTimerLoop->addTimer( *this );
62 m_pTimerLoop->removeTimer( *this );
66 mtime_t X11Timer::getNextDate() const
72 bool X11Timer::execute()
74 m_nextDate += m_interval;
75 // Execute the callback
82 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
83 SkinObject( pIntf ), m_connectionNumber( connectionNumber )
88 X11TimerLoop::~X11TimerLoop()
93 void X11TimerLoop::addTimer( X11Timer &rTimer )
95 m_timers.push_back( &rTimer );
99 void X11TimerLoop::removeTimer( X11Timer &rTimer )
101 m_timers.remove( &rTimer );
105 void X11TimerLoop::waitNextTimer()
107 mtime_t curDate = mdate();
108 mtime_t nextDate = LAST_MDATE;
110 X11Timer *nextTimer = NULL;
112 // Find the next timer to execute
113 list<X11Timer*>::const_iterator timer;
114 for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
116 mtime_t timerDate = (*timer)->getNextDate();
117 if( timerDate < nextDate )
120 nextDate = timerDate;
124 if( nextTimer == NULL )
130 if( nextDate > curDate )
132 if( this->sleep( (nextDate - curDate ) / 1000 ) )
134 // The sleep has been interrupted: stop here
138 // Execute the timer callback
139 if( ! nextTimer->execute() )
141 // Remove the timer if execute() returned false
142 m_timers.remove( nextTimer );
148 bool X11TimerLoop::sleep( int delay )
151 memset( &ufd, 0, sizeof (ufd) );
152 ufd.fd = m_connectionNumber;
155 // Wait for an X11 event, or timeout
156 return poll( &ufd, 1, delay ) > 0;