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