]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_timer.cpp
1719748c03c7d5b8eb44a7ca974d07e2389a76f9
[vlc] / modules / gui / skins2 / x11 / x11_timer.cpp
1 /*****************************************************************************
2  * x11_timer.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <poll.h>
30
31 #include "x11_timer.hpp"
32 #include "x11_factory.hpp"
33 #include "../commands/cmd_generic.hpp"
34
35
36 X11Timer::X11Timer( intf_thread_t *pIntf, CmdGeneric &rCmd ):
37     OSTimer( pIntf ), m_rCommand( rCmd )
38 {
39     // Get the instance of timer loop
40     X11Factory *m_pOsFactory = (X11Factory*)(OSFactory::instance( pIntf ) );
41     m_pTimerLoop = m_pOsFactory->getTimerLoop();
42 }
43
44
45 X11Timer::~X11Timer()
46 {
47     stop();
48 }
49
50
51 void X11Timer::start( int delay, bool oneShot )
52 {
53     m_interval = 1000 * delay;
54     m_oneShot = oneShot;
55     m_nextDate = mdate() + m_interval;
56     m_pTimerLoop->addTimer( *this );
57 }
58
59
60 void X11Timer::stop()
61 {
62     m_pTimerLoop->removeTimer( *this );
63 }
64
65
66 mtime_t X11Timer::getNextDate() const
67 {
68     return m_nextDate;
69 }
70
71
72 bool X11Timer::execute()
73 {
74     m_nextDate += m_interval;
75     // Execute the callback
76     m_rCommand.execute();
77
78     return !m_oneShot;
79 }
80
81
82 X11TimerLoop::X11TimerLoop( intf_thread_t *pIntf, int connectionNumber ):
83     SkinObject( pIntf ), m_connectionNumber( connectionNumber )
84 {
85 }
86
87
88 X11TimerLoop::~X11TimerLoop()
89 {
90 }
91
92
93 void X11TimerLoop::addTimer( X11Timer &rTimer )
94 {
95     m_timers.push_back( &rTimer );
96 }
97
98
99 void X11TimerLoop::removeTimer( X11Timer &rTimer )
100 {
101     m_timers.remove( &rTimer );
102 }
103
104
105 void X11TimerLoop::waitNextTimer()
106 {
107     mtime_t curDate = mdate();
108     mtime_t nextDate = LAST_MDATE;
109
110     X11Timer *nextTimer = NULL;
111
112     // Find the next timer to execute
113     list<X11Timer*>::const_iterator timer;
114     for( timer = m_timers.begin(); timer != m_timers.end(); timer++ )
115     {
116         mtime_t timerDate = (*timer)->getNextDate();
117         if( timerDate < nextDate )
118         {
119             nextTimer = *timer;
120             nextDate = timerDate;
121         }
122     }
123
124     if( nextTimer == NULL )
125     {
126         this->sleep( 1000 );
127     }
128     else
129     {
130         if( nextDate > curDate )
131         {
132             if( this->sleep( (nextDate - curDate ) / 1000 ) )
133             {
134                 // The sleep has been interrupted: stop here
135                 return;
136             }
137         }
138         // Execute the timer callback
139         if( ! nextTimer->execute() )
140         {
141             // Remove the timer if execute() returned false
142             m_timers.remove( nextTimer );
143         }
144     }
145 }
146
147
148 bool X11TimerLoop::sleep( int delay )
149 {
150     struct pollfd ufd;
151     memset( &ufd, 0, sizeof (ufd) );
152     ufd.fd = m_connectionNumber;
153     ufd.events = POLLIN;
154
155     // Wait for an X11 event, or timeout
156     return poll( &ufd, 1, delay ) > 0;
157 }
158
159
160 #endif