Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

x11_timer.cpp

00001 /*****************************************************************************
00002  * x11_timer.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: x11_timer.cpp 12207 2005-08-15 15:54:32Z asmax $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #ifdef X11_SKINS
00026 
00027 #include <unistd.h>
00028 #include <fcntl.h>
00029 
00030 #include "x11_timer.hpp"
00031 #include "x11_factory.hpp"
00032 #include "../commands/cmd_generic.hpp"
00033 
00034 
00035 X11Timer::X11Timer( intf_thread_t *pIntf, CmdGeneric &rCmd ):
00036     OSTimer( pIntf ), m_rCommand( rCmd )
00037 {
00038     // Get the instance of timer loop
00039     X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
00040     m_pTimerLoop = m_pOsFactory->getTimerLoop();
00041 }
00042 
00043 
00044 X11Timer::~X11Timer()
00045 {
00046     stop();
00047 }
00048 
00049 
00050 void X11Timer::start( int delay, bool oneShot )
00051 {
00052     m_interval = 1000 * delay;
00053     m_oneShot = oneShot;
00054     m_nextDate = mdate() + m_interval;
00055     m_pTimerLoop->addTimer( *this );
00056 }
00057 
00058 
00059 void X11Timer::stop()
00060 {
00061     m_pTimerLoop->removeTimer( *this );
00062 }
00063 
00064 
00065 mtime_t X11Timer::getNextDate() const
00066 {
00067     return m_nextDate;
00068 }
00069 
00070 
00071 bool X11Timer::execute()
00072 {
00073     m_nextDate += m_interval;
00074     // Execute the callback
00075     m_rCommand.execute();
00076 
00077     return !m_oneShot;
00078 }
00079 
00080 
00081 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
00082     SkinObject( pIntf ), m_connectionNumber( connectionNumber )
00083 {
00084 }
00085 
00086 
00087 X11TimerLoop::~X11TimerLoop()
00088 {
00089 }
00090 
00091 
00092 void X11TimerLoop::addTimer( X11Timer &rTimer )
00093 {
00094     m_timers.push_back( &rTimer );
00095 }
00096 
00097 
00098 void X11TimerLoop::removeTimer( X11Timer &rTimer )
00099 {
00100     m_timers.remove( &rTimer );
00101 }
00102 
00103 
00104 void X11TimerLoop::waitNextTimer()
00105 {
00106     mtime_t curDate = mdate();
00107     mtime_t nextDate = LAST_MDATE;
00108 
00109     X11Timer *nextTimer = NULL;
00110 
00111     // Find the next timer to execute
00112     list<X11Timer*>::const_iterator timer;
00113     for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
00114     {
00115         mtime_t timerDate = (*timer)->getNextDate();
00116         if( timerDate < nextDate )
00117         {
00118             nextTimer = *timer;
00119             nextDate = timerDate;
00120         }
00121     }
00122 
00123     if( nextTimer == NULL )
00124     {
00125         this->sleep( 1000 );
00126     }
00127     else
00128     {
00129         if( nextDate > curDate )
00130         {
00131             if( this->sleep( (nextDate - curDate ) / 1000 ) )
00132             {
00133                 // The sleep has been interrupted: stop here
00134                 return;
00135             }
00136         }
00137         // Execute the timer callback
00138         if( ! nextTimer->execute() )
00139         {
00140             // Remove the timer if execute() returned false
00141             m_timers.remove( nextTimer );
00142         }
00143     }
00144 }
00145 
00146 
00147 bool X11TimerLoop::sleep( int delay )
00148 {
00149     // Timeout delay
00150     struct timeval tv;
00151     tv.tv_sec = delay / 1000;
00152     tv.tv_usec = 1000 * (delay % 1000);
00153 
00154     // FD set for select()
00155     fd_set rfds;
00156     FD_ZERO( &rfds );
00157     FD_SET( m_connectionNumber, &rfds );
00158 
00159     // Wait for an X11 event, or timeout
00160     int num = select( m_connectionNumber + 1, &rfds, NULL, NULL, &tv );
00161 
00162     return ( num > 0 );
00163 }
00164 
00165 
00166 #endif

Generated on Tue Dec 20 10:14:44 2005 for vlc-0.8.4a by  doxygen 1.4.2