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 *****************************************************************************/
30 #include "x11_timer.hpp"
31 #include "x11_factory.hpp"
32 #include "../commands/cmd_generic.hpp"
35 X11Timer::X11Timer( intf_thread_t *pIntf, CmdGeneric &rCmd ):
36 OSTimer( pIntf ), m_rCommand( rCmd )
38 // Get the instance of timer loop
39 X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
40 m_pTimerLoop = m_pOsFactory->getTimerLoop();
50 void X11Timer::start( int delay, bool oneShot )
52 m_interval = 1000 * delay;
54 m_nextDate = mdate() + m_interval;
55 m_pTimerLoop->addTimer( *this );
61 m_pTimerLoop->removeTimer( *this );
65 mtime_t X11Timer::getNextDate() const
71 bool X11Timer::execute()
73 m_nextDate += m_interval;
74 // Execute the callback
81 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
82 SkinObject( pIntf ), m_connectionNumber( connectionNumber )
87 X11TimerLoop::~X11TimerLoop()
92 void X11TimerLoop::addTimer( X11Timer &rTimer )
94 m_timers.push_back( &rTimer );
98 void X11TimerLoop::removeTimer( X11Timer &rTimer )
100 m_timers.remove( &rTimer );
104 void X11TimerLoop::waitNextTimer()
106 mtime_t curDate = mdate();
107 mtime_t nextDate = LAST_MDATE;
109 X11Timer *nextTimer = NULL;
111 // Find the next timer to execute
112 list<X11Timer*>::const_iterator timer;
113 for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
115 mtime_t timerDate = (*timer)->getNextDate();
116 if( timerDate < nextDate )
119 nextDate = timerDate;
123 if( nextTimer == NULL )
129 if( nextDate > curDate )
131 if( this->sleep( (nextDate - curDate ) / 1000 ) )
133 // The sleep has been interrupted: stop here
137 // Execute the timer callback
138 if( ! nextTimer->execute() )
140 // Remove the timer if execute() returned false
141 m_timers.remove( nextTimer );
147 bool X11TimerLoop::sleep( int delay )
150 memset( &ufd, 0, sizeof (ufd) );
151 ufd.fd = m_connectionNumber;
154 // Wait for an X11 event, or timeout
155 return poll( &ufd, 1, delay ) > 0;