2009-06-17 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / timemanager.cpp
blob8b1caa29cc92f0b495ef8272157ea5f036dfe776
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Contact:
4 * Moonlight List (moonlight-list@lists.ximian.com)
6 * Copyright 2007 Novell, Inc. (http://www.novell.com)
8 * See the LICENSE file included with the distribution for details.
9 *
12 #include <config.h>
14 #include "clock.h"
15 #include "timeline.h"
16 #include "timemanager.h"
17 #include "timesource.h"
18 #include "runtime.h"
20 #define TIMERS 0
21 #define PUT_TIME_MANAGER_TO_SLEEP 0
23 #if TIMERS
24 #define STARTTICKTIMER(id,str) STARTTIMER(id,str)
25 #define ENDTICKTIMER(id,str) ENDTIMER(it,str)
26 #else
27 #define STARTTICKTIMER(id,str)
28 #define ENDTICKTIMER(id,str)
29 #endif
32 #define MINIMUM_FPS 5
33 #define DEFAULT_FPS 50
34 #define MAXIMUM_FPS 50
36 #define FPS_TO_DELAY(fps) (int)(((double)1/(fps)) * 1000)
37 #define DELAY_TO_FPS(delay) (1000.0 / delay)
39 class TickCall : public List::Node {
40 public:
41 TickCallHandler func;
42 EventObject *data;
43 TickCall (TickCallHandler func, EventObject *data)
45 this->func = func;
46 this->data = data;
47 if (this->data)
48 this->data->ref ();
50 virtual ~TickCall ()
52 if (data)
53 data->unref ();
58 class RootClockGroup : public ClockGroup
60 public:
61 RootClockGroup (TimelineGroup *timeline) : ClockGroup (timeline, true) { }
63 virtual bool UpdateFromParentTime (TimeSpan parentTime)
65 bool rv = ClockGroup::UpdateFromParentTime (parentTime);
67 bool children_rv = false;
68 for (GList *l = child_clocks; l; l = l->next) {
69 Clock *clock = (Clock*)l->data;
70 children_rv = clock->UpdateFromParentTime (current_time) || children_rv;
73 return rv && children_rv;
76 protected:
77 virtual ~RootClockGroup () { }
80 TimeManager::TimeManager ()
82 SetObjectType (Type::TIMEMANAGER);
84 if (moonlight_flags & RUNTIME_INIT_MANUAL_TIMESOURCE)
85 source = new ManualTimeSource();
86 else
87 source = new SystemTimeSource(Deployment::GetCurrent ());
89 current_timeout = FPS_TO_DELAY (DEFAULT_FPS); /* something suitably small */
90 max_fps = MAXIMUM_FPS;
91 flags = (TimeManagerOp) (TIME_MANAGER_UPDATE_CLOCKS | TIME_MANAGER_RENDER | TIME_MANAGER_TICK_CALL /*| TIME_MANAGER_UPDATE_INPUT*/);
93 start_time = source->GetNow ();
94 start_time_usec = start_time / 10;
95 source->AddHandler (TimeSource::TickEvent, source_tick_callback, this);
97 registered_timeouts = NULL;
98 source_tick_pending = false;
99 first_tick = true;
101 applier = new Applier ();
103 timeline = new ParallelTimeline();
104 timeline->SetDuration (Duration::Forever);
105 root_clock = new RootClockGroup (timeline);
106 char *name = g_strdup_printf ("Surface clock group for time manager (%p)", this);
107 root_clock->SetValue(DependencyObject::NameProperty, name);
108 g_free (name);
109 root_clock->SetTimeManager (this);
112 TimeManager::~TimeManager ()
114 source->RemoveHandler (TimeSource::TickEvent, source_tick_callback, this);
115 source->unref ();
116 source = NULL;
118 timeline->unref ();
119 timeline = NULL;
121 root_clock->unref ();
122 root_clock = NULL;
124 delete applier;
126 RemoveAllRegisteredTimeouts ();
129 void
130 TimeManager::SetMaximumRefreshRate (int hz)
132 if (hz == 0)
133 hz = 1;
135 max_fps = hz;
136 current_timeout = FPS_TO_DELAY (hz);
137 source->SetTimerFrequency (current_timeout);
138 first_tick = true;
141 void
142 TimeManager::Start()
144 last_global_time = current_global_time = source->GetNow();
145 current_global_time_usec = current_global_time / 10;
146 source->SetTimerFrequency (current_timeout);
147 source->Start ();
148 source_tick_pending = true;
151 void
152 TimeManager::Stop ()
154 source->Stop ();
157 void
158 TimeManager::Shutdown ()
160 RemoveAllRegisteredTimeouts ();
161 source->Stop ();
164 void
165 TimeManager::source_tick_callback (EventObject *sender, EventArgs *calldata, gpointer closure)
167 ((TimeManager *) closure)->SourceTick ();
170 bool
171 TimeManager::InvokeTickCall ()
173 TickCall *call = (TickCall *) tick_calls.Pop ();
175 if (call == NULL)
176 return false;
178 call->func (call->data);
179 delete call;
181 return true;
184 guint
185 TimeManager::AddTimeout (gint priority, guint ms_interval, GSourceFunc func, gpointer tick_data)
187 guint rv = g_timeout_add_full (priority, ms_interval, func, tick_data, NULL);
188 registered_timeouts = g_list_prepend (registered_timeouts, GUINT_TO_POINTER (rv));
189 return rv;
192 void
193 TimeManager::RemoveTimeout (guint timeout_id)
195 g_source_remove (timeout_id);
196 registered_timeouts = g_list_remove_all (registered_timeouts, GUINT_TO_POINTER (timeout_id));
199 void
200 TimeManager::RemoveAllRegisteredTimeouts ()
202 GList *t;
203 for (t = registered_timeouts; t; t = t->next)
204 g_source_remove (GPOINTER_TO_UINT (t->data));
206 g_list_free (registered_timeouts);
207 registered_timeouts = NULL;
210 void
211 TimeManager::AddTickCall (TickCallHandler func, EventObject *tick_data)
213 tick_calls.Push (new TickCall (func, tick_data));
215 #if PUT_TIME_MANAGER_TO_SLEEP
216 flags = (TimeManagerOp)(flags | TIME_MANAGER_TICK_CALL);
217 if (!source_tick_pending) {
218 source_tick_pending = true;
219 source->SetTimerFrequency (current_timeout);
220 source->Start();
222 #endif
225 void
226 TimeManager::RemoveTickCall (TickCallHandler func)
228 tick_calls.Lock ();
229 List::Node * call = tick_calls.LinkedList ()->Find (find_tick_call, (void*)func);
230 if (call)
231 tick_calls.LinkedList ()->Remove (call);
232 #if PUT_TIME_MANAGER_TO_SLEEP
233 if (tick_calls.IsEmpty()) {
234 flags = (TimeManagerOp)(flags & ~TIME_MANAGER_TICK_CALL);
235 if (flags == 0 && source_tick_pending)
236 source->Stop();
238 #endif
239 tick_calls.Unlock ();
242 bool
243 find_tick_call (List::Node *node, void *data)
245 if (((TickCall*)node)->func == data)
246 return true;
247 return false;
250 void
251 TimeManager::NeedRedraw ()
253 #if PUT_TIME_MANAGER_TO_SLEEP
254 flags = (TimeManagerOp)(flags | TIME_MANAGER_RENDER);
255 if (!source_tick_pending) {
256 source_tick_pending = true;
257 source->SetTimerFrequency (current_timeout);
258 source->Start();
260 #endif
263 void
264 TimeManager::NeedClockTick ()
266 #if PUT_TIME_MANAGER_TO_SLEEP
267 flags = (TimeManagerOp)(flags | TIME_MANAGER_UPDATE_CLOCKS);
268 if (!source_tick_pending) {
269 source_tick_pending = true;
270 source->SetTimerFrequency (current_timeout);
271 source->Start();
273 #endif
277 static void
278 spaces (int n)
280 while (n--) putchar (' ');
284 static void
285 output_clock (Clock *clock, int level)
287 spaces (level);
288 printf (clock->Is(Type::CLOCKGROUP) ? "ClockGroup " : "Clock ");
289 printf ("(%p) ", clock);
290 if (clock->GetName ()) {
291 printf ("'%s' ", clock->GetName());
294 // getting the natural duration here upsets the clock, so let's not
295 // printf ("%lld / %lld (%.2f) ", clock->GetCurrentTime(), clock->GetNaturalDuration().GetTimeSpan(), clock->GetCurrentProgress());
296 printf ("%lld (%.2f) ", clock->GetCurrentTime(), clock->GetCurrentProgress());
298 printf ("%lld ", clock->GetTimeline()->GetBeginTime());
300 switch (clock->GetClockState()) {
301 case Clock::Active:
302 printf ("A");
303 break;
304 case Clock::Filling:
305 printf ("F");
306 break;
307 case Clock::Stopped:
308 printf ("S");
309 break;
312 if (clock->GetIsPaused())
313 printf (" (paused)");
315 printf ("\n");
317 if (clock->Is(Type::CLOCKGROUP)) {
318 ClockGroup *cg = (ClockGroup*)clock;
319 level += 2;
320 for (GList *l = cg->child_clocks; l; l = l->next) {
321 // if (((Clock*)l->data)->GetClockState () != Clock::Stopped)
322 output_clock ((Clock*)l->data, level);
327 void
328 TimeManager::ListClocks()
330 printf ("Currently registered clocks:\n");
331 printf ("============================\n");
333 output_clock (root_clock, 2);
335 printf ("============================\n");
338 void
339 TimeManager::AddClock (Clock *clock)
341 root_clock->AddChild (clock);
343 // we delay starting the surface's ClockGroup until the first
344 // child has been added. otherwise we run into timing issues
345 // between timelines that explicitly set a BeginTime and those
346 // that don't (and so default to 00:00:00).
347 if (root_clock->GetClockState() != Clock::Active)
348 root_clock->Begin (GetCurrentTime());
350 NeedClockTick ();
353 void
354 TimeManager::SourceTick ()
356 TimeManagerOp current_flags = flags;
358 #if PUT_TIME_MANAGER_TO_SLEEP
359 flags = (TimeManagerOp)0;
360 #endif
362 if (current_flags & TIME_MANAGER_TICK_CALL) {
363 bool remaining_tick_calls = false;
364 do {
365 remaining_tick_calls = InvokeTickCall ();
366 } while (remaining_tick_calls);
369 if (current_flags & TIME_MANAGER_UPDATE_CLOCKS) {
370 STARTTICKTIMER (tick_update_clocks, "TimeManager::Tick - UpdateClocks");
371 current_global_time = source->GetNow();
372 current_global_time_usec = current_global_time / 10;
374 bool need_another_tick = root_clock->UpdateFromParentTime (GetCurrentTime());
376 if (need_another_tick)
377 NeedClockTick ();
379 // ... then cause all clocks to raise the events they've queued up
380 root_clock->RaiseAccumulatedEvents ();
382 applier->Apply ();
383 applier->Flush ();
385 root_clock->RaiseAccumulatedCompleted ();
387 #if CLOCK_DEBUG
388 ListClocks ();
389 #endif
391 ENDTICKTIMER (tick_update_clocks, "TimeManager::Tick - UpdateClocks");
394 if (current_flags & TIME_MANAGER_UPDATE_INPUT) {
395 STARTTICKTIMER (tick_input, "TimeManager::Tick - Input");
396 Emit (UpdateInputEvent);
397 ENDTICKTIMER (tick_input, "TimeManager::Tick - Input");
400 if (current_flags & TIME_MANAGER_RENDER) {
401 // fprintf (stderr, "rendering\n"); fflush (stderr);
402 STARTTICKTIMER (tick_render, "TimeManager::Tick - Render");
403 Emit (RenderEvent, new RenderingEventArgs (get_now()));
404 ENDTICKTIMER (tick_render, "TimeManager::Tick - Render");
407 last_global_time = current_global_time;