4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef XCSOAR_EVENT_TIMER_HPP
25 #define XCSOAR_EVENT_TIMER_HPP
27 #if defined(ANDROID) || defined(USE_EGL)
29 #elif defined(ENABLE_SDL)
30 #include <SDL_timer.h>
33 #include "Screen/Window.hpp"
34 #include "Screen/Timer.hpp"
41 * A timer that, once initialized, periodically calls OnTimer() after
42 * a specified amount of time, until Cancel() gets called.
44 * Initially, this class does not schedule a timer.
46 * This class is not thread safe; all of the methods must be called
47 * from the main thread.
49 * The class #WindowTimer is cheaper on WIN32; use it instead of this
50 * class if you are implementing a #Window.
54 : private Window
, private WindowTimer
57 #if defined(ANDROID) || defined(USE_EGL)
58 std::atomic
<bool> enabled
, queued
;
60 #elif defined(ENABLE_SDL)
64 * True when the timer event has been pushed to the event queue.
65 * This is used to prevent duplicate items stacking on the event
68 std::atomic
<bool> queued
;
73 * Construct a Timer object that is not set initially.
75 #if defined(ANDROID) || defined(USE_EGL)
76 Timer():enabled(false), queued(false) {}
77 #elif defined(ENABLE_SDL)
78 Timer():id(NULL
), queued(false) {}
80 Timer():WindowTimer(*(Window
*)this) {
81 Window::CreateMessageWindow();
85 Timer(const Timer
&other
) = delete;
89 * The move constructor may only be used on inactive timers. This
90 * shall only be used by derived classes to pass inactive instances
95 :WindowTimer(*(Window
*)this)
99 assert(!other
.IsActive());
104 /* timer must be cleaned up explicitly */
108 assert(!queued
.load(std::memory_order_relaxed
));
109 assert(!enabled
.load(std::memory_order_relaxed
));
114 /* inherit WindowTimer's methods */
115 using WindowTimer::IsActive
;
116 using WindowTimer::Schedule
;
117 using WindowTimer::Cancel
;
121 * Is the timer active, i.e. is it waiting for the current period to
124 bool IsActive() const {
125 #if defined(ANDROID) || defined(USE_EGL)
126 return enabled
.load(std::memory_order_relaxed
);
127 #elif defined(ENABLE_SDL)
133 * Schedule the timer. Cancels the previous setting if there was
136 void Schedule(unsigned ms
);
139 * Cancels the scheduled timer, if any. This is safe to be called
140 * while the timer is running.
148 * This method gets called after the configured time has elapsed.
151 virtual void OnTimer() = 0;
153 #if defined(ANDROID) || defined(USE_EGL)
156 #elif defined(ENABLE_SDL)
159 static void Invoke(void *ctx
);
161 Uint32
Callback(Uint32 interval
);
162 static Uint32
Callback(Uint32 interval
, void *param
);
165 /* virtual methods from class Window */
166 virtual bool OnTimer(WindowTimer
&timer
) override
;