Remove LiveTV menu.
[vuplus_xbmc] / xbmc / threads / Timer.cpp
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <algorithm>
22
23 #include "Timer.h"
24 #include "SystemClock.h"
25
26 CTimer::CTimer(ITimerCallback *callback)
27   : CThread("Timer"),
28     m_callback(callback),
29     m_timeout(0),
30     m_interval(false),
31     m_endTime(0)
32 { }
33
34 CTimer::~CTimer()
35 {
36   Stop(true);
37 }
38
39 bool CTimer::Start(uint32_t timeout, bool interval /* = false */)
40 {
41   if (m_callback == NULL || timeout == 0 || IsRunning())
42     return false;
43
44   m_timeout = timeout;
45   m_interval = interval;
46
47   Create();
48   return true;
49 }
50
51 bool CTimer::Stop(bool wait /* = false */)
52 {
53   if (!IsRunning())
54     return false;
55
56   m_bStop = true;
57   m_eventTimeout.Set();
58   StopThread(wait);
59
60   return true;
61 }
62
63 bool CTimer::Restart()
64 {
65   if (!IsRunning())
66     return false;
67
68   Stop(true);
69   return Start(m_timeout, m_interval);
70 }
71
72 float CTimer::GetElapsedSeconds() const
73 {
74   return GetElapsedMilliseconds() / 1000.0f;
75 }
76
77 float CTimer::GetElapsedMilliseconds() const
78 {
79   if (!IsRunning())
80     return 0.0f;
81
82   return (float)(XbmcThreads::SystemClockMillis() - (m_endTime - m_timeout));
83 }
84
85 void CTimer::Process()
86 {
87   uint32_t currentTime = XbmcThreads::SystemClockMillis();
88   m_endTime = currentTime + m_timeout;
89
90   while (!m_bStop)
91   {
92     // wait the necessary time
93     if (!m_eventTimeout.WaitMSec(m_endTime - currentTime))
94     {
95       currentTime = XbmcThreads::SystemClockMillis();
96       if (m_endTime <= currentTime)
97       {
98         // execute OnTimeout() callback
99         m_callback->OnTimeout();
100
101         // stop if this is not an interval timer
102         if (!m_interval)
103           break;
104
105         m_endTime = currentTime + m_timeout;
106       }
107     }
108   }
109 }