]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_timer.cpp
* skins2/src/window_manager.cpp: Added the vlc.isOnTop boolean variable
[vlc] / modules / gui / skins2 / x11 / x11_timer.cpp
1 /*****************************************************************************
2  * x11_timer.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: x11_timer.cpp,v 1.1 2004/01/03 23:31:34 asmax Exp $
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "x11_timer.hpp"
31 #include "x11_factory.hpp"
32
33
34 X11Timer::X11Timer( intf_thread_t *pIntf, const Callback &rCallback ):
35     OSTimer( pIntf ), m_callback( rCallback )
36 {
37     // Get the instance of timer loop
38     X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
39     m_pTimerLoop = m_pOsFactory->getTimerLoop();
40 }
41
42
43 X11Timer::~X11Timer()
44 {
45     stop();
46 }
47
48
49 void X11Timer::start( int delay, bool oneShot )
50 {
51     m_interval = 1000 * delay;
52     m_oneShot = oneShot;
53     m_nextDate = mdate() + m_interval;
54     m_pTimerLoop->addTimer( *this );
55 }
56
57
58 void X11Timer::stop()
59 {
60     m_pTimerLoop->removeTimer( *this );
61 }
62
63
64 mtime_t X11Timer::getNextDate() const
65 {
66     return m_nextDate;
67 }
68
69
70 bool X11Timer::execute()
71 {
72     m_nextDate += m_interval;
73     // Execute the callback
74     (*(m_callback.getFunc()))( m_callback.getObj() );
75
76     return !m_oneShot;
77 }
78
79
80 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
81     SkinObject( pIntf ), m_connectionNumber( connectionNumber )
82 {
83 }
84
85
86 X11TimerLoop::~X11TimerLoop()
87 {
88 }
89
90
91 void X11TimerLoop::addTimer( X11Timer &rTimer )
92 {
93     m_timers.push_back( &rTimer );
94 }
95
96
97 void X11TimerLoop::removeTimer( X11Timer &rTimer )
98 {
99     m_timers.remove( &rTimer );
100 }
101
102
103 void X11TimerLoop::waitNextTimer()
104 {
105     mtime_t curDate = mdate();
106     mtime_t nextDate = LAST_MDATE;
107
108     X11Timer *nextTimer = NULL;
109
110     // Find the next timer to execute
111     list<X11Timer*>::const_iterator timer;
112     for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
113     {
114         mtime_t timerDate = (*timer)->getNextDate();
115         if( timerDate < nextDate )
116         {
117             nextTimer = *timer;
118             nextDate = timerDate;
119         }
120     }
121
122     if( nextTimer == NULL )
123     {
124         this->sleep( 1000 );
125     }
126     else
127     {
128         if( nextDate > curDate )
129         {
130             if( this->sleep( (nextDate - curDate ) / 1000 ) )
131             {
132                 // The sleep has been interrupted: stop here
133                 return;
134             }
135         }
136         // Execute the timer callback
137         if( ! nextTimer->execute() )
138         {
139             // Remove the timer if execute() returned false
140             m_timers.remove( nextTimer );
141         }
142     }
143 }
144
145
146 bool X11TimerLoop::sleep( int delay )
147 {
148     // Timeout delay
149     struct timeval tv;
150     tv.tv_sec = delay / 1000;
151     tv.tv_usec = 1000 * (delay % 1000);
152
153     // FD set for select()
154     fd_set rfds;
155     FD_ZERO( &rfds );
156     FD_SET( m_connectionNumber, &rfds );
157
158     // Wait for an X11 event, or timeout
159     int num = select( m_connectionNumber + 1, &rfds, NULL, NULL, &tv );
160
161     return ( num > 0 );
162 }
163
164
165 #endif