2009-10-09 Chris Toshok <toshok@ximian.com>
[moon.git] / src / timemanager.cpp
blobcca64e8ca85b9c9b3f070fbe78b58247099d858a
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 = Clock::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 root_clock->Dispose ();
120 timeline->unref ();
121 timeline = NULL;
123 root_clock->unref ();
124 root_clock = NULL;
126 delete applier;
127 applier = NULL;
129 RemoveAllRegisteredTimeouts ();
132 void
133 TimeManager::SetMaximumRefreshRate (int hz)
135 if (hz == 0)
136 hz = 1;
138 max_fps = hz;
139 current_timeout = FPS_TO_DELAY (hz);
140 source->SetTimerFrequency (current_timeout);
141 first_tick = true;
144 void
145 TimeManager::Start()
147 last_global_time = current_global_time = source->GetNow();
148 current_global_time_usec = current_global_time / 10;
149 source->SetTimerFrequency (current_timeout);
150 source->Start ();
151 source_tick_pending = true;
154 void
155 TimeManager::Stop ()
157 source->Stop ();
160 void
161 TimeManager::Shutdown ()
163 RemoveAllRegisteredTimeouts ();
164 source->Stop ();
167 void
168 TimeManager::source_tick_callback (EventObject *sender, EventArgs *calldata, gpointer closure)
170 ((TimeManager *) closure)->SourceTick ();
173 bool
174 TimeManager::InvokeTickCall ()
176 TickCall *call = (TickCall *) tick_calls.Pop ();
178 if (call == NULL)
179 return false;
181 call->func (call->data);
182 delete call;
184 return true;
187 void
188 TimeManager::InvokeTickCalls ()
190 bool remaining_tick_calls = false;
191 do {
192 remaining_tick_calls = InvokeTickCall ();
193 } while (remaining_tick_calls);
196 guint
197 TimeManager::AddTimeout (gint priority, guint ms_interval, GSourceFunc func, gpointer tick_data)
199 guint rv = g_timeout_add_full (priority, ms_interval, func, tick_data, NULL);
200 registered_timeouts = g_list_prepend (registered_timeouts, GUINT_TO_POINTER (rv));
201 return rv;
204 void
205 TimeManager::RemoveTimeout (guint timeout_id)
207 g_source_remove (timeout_id);
208 registered_timeouts = g_list_remove_all (registered_timeouts, GUINT_TO_POINTER (timeout_id));
211 void
212 TimeManager::RemoveAllRegisteredTimeouts ()
214 GList *t;
215 for (t = registered_timeouts; t; t = t->next)
216 g_source_remove (GPOINTER_TO_UINT (t->data));
218 g_list_free (registered_timeouts);
219 registered_timeouts = NULL;
222 void
223 TimeManager::AddTickCall (TickCallHandler func, EventObject *tick_data)
225 tick_calls.Push (new TickCall (func, tick_data));
227 #if PUT_TIME_MANAGER_TO_SLEEP
228 flags = (TimeManagerOp)(flags | TIME_MANAGER_TICK_CALL);
229 if (!source_tick_pending) {
230 source_tick_pending = true;
231 source->SetTimerFrequency (current_timeout);
232 source->Start();
234 #endif
237 struct TickCallFindData {
238 TickCallHandler func;
239 EventObject *data;
242 void
243 TimeManager::RemoveTickCall (TickCallHandler func, EventObject *tick_data)
245 TickCallFindData fd;
247 fd.func = func;
248 fd.data = tick_data;
250 tick_calls.Lock ();
252 List::Node * call = tick_calls.LinkedList ()->Find (find_tick_call, &fd);
253 if (call)
254 tick_calls.LinkedList ()->Remove (call);
255 #if PUT_TIME_MANAGER_TO_SLEEP
256 if (tick_calls.IsEmpty()) {
257 flags = (TimeManagerOp)(flags & ~TIME_MANAGER_TICK_CALL);
258 if (flags == 0 && source_tick_pending)
259 source->Stop();
261 #endif
262 tick_calls.Unlock ();
265 bool
266 find_tick_call (List::Node *node, void *data)
268 TickCall *tc = (TickCall*)node;
269 TickCallFindData *fd = (TickCallFindData*)data;
271 return (tc->func == fd->func &&
272 tc->data == fd->data);
275 void
276 TimeManager::NeedRedraw ()
278 #if PUT_TIME_MANAGER_TO_SLEEP
279 flags = (TimeManagerOp)(flags | TIME_MANAGER_RENDER);
280 if (!source_tick_pending) {
281 source_tick_pending = true;
282 source->SetTimerFrequency (current_timeout);
283 source->Start();
285 #endif
288 void
289 TimeManager::NeedClockTick ()
291 #if PUT_TIME_MANAGER_TO_SLEEP
292 flags = (TimeManagerOp)(flags | TIME_MANAGER_UPDATE_CLOCKS);
293 if (!source_tick_pending) {
294 source_tick_pending = true;
295 source->SetTimerFrequency (current_timeout);
296 source->Start();
298 #endif
302 static void
303 spaces (int n)
305 while (n--) putchar (' ');
308 static void
309 output_clock (Clock *clock, int level)
311 spaces (level);
312 printf (clock->Is(Type::CLOCKGROUP) ? "ClockGroup " : "Clock ");
313 printf ("(%p) ", clock);
314 if (clock->GetName ()) {
315 printf ("'%s' ", clock->GetName());
318 // getting the natural duration here upsets the clock, so let's not
319 // printf ("%lld / %lld (%.2f) ", clock->GetCurrentTime(), clock->GetNaturalDuration().GetTimeSpan(), clock->GetCurrentProgress());
320 printf ("%lld (%.2f) ", clock->GetCurrentTime(), clock->GetCurrentProgress());
322 printf ("%lld ", clock->GetTimeline()->GetBeginTime());
324 switch (clock->GetClockState()) {
325 case Clock::Active:
326 printf ("A");
327 break;
328 case Clock::Filling:
329 printf ("F");
330 break;
331 case Clock::Stopped:
332 printf ("S");
333 break;
336 if (clock->GetIsPaused())
337 printf (" (paused)");
339 printf ("\n");
341 if (clock->Is(Type::CLOCKGROUP)) {
342 ClockGroup *cg = (ClockGroup*)clock;
343 level += 2;
344 for (GList *l = cg->child_clocks; l; l = l->next) {
345 // if (((Clock*)l->data)->GetClockState () != Clock::Stopped)
346 output_clock ((Clock*)l->data, level);
351 void
352 TimeManager::ListClocks()
354 printf ("Currently registered clocks:\n");
355 printf ("============================\n");
357 output_clock (root_clock, 2);
359 printf ("============================\n");
362 void
363 TimeManager::AddClock (Clock *clock)
365 root_clock->AddChild (clock);
367 // we delay starting the surface's ClockGroup until the first
368 // child has been added. otherwise we run into timing issues
369 // between timelines that explicitly set a BeginTime and those
370 // that don't (and so default to 00:00:00).
371 if (root_clock->GetClockState() != Clock::Active)
372 root_clock->Begin (GetCurrentTime());
374 NeedClockTick ();
377 void
378 TimeManager::SourceTick ()
380 TimeManagerOp current_flags = flags;
382 #if PUT_TIME_MANAGER_TO_SLEEP
383 flags = (TimeManagerOp)0;
384 #endif
386 if (current_flags & TIME_MANAGER_TICK_CALL) {
387 InvokeTickCalls ();
390 if (current_flags & TIME_MANAGER_UPDATE_CLOCKS) {
391 STARTTICKTIMER (tick_update_clocks, "TimeManager::Tick - UpdateClocks");
392 current_global_time = source->GetNow();
393 current_global_time_usec = current_global_time / 10;
395 bool need_another_tick = root_clock->UpdateFromParentTime (GetCurrentTime());
397 if (need_another_tick)
398 NeedClockTick ();
400 // ... then cause all clocks to raise the events they've queued up
401 root_clock->RaiseAccumulatedEvents ();
403 applier->Apply ();
404 applier->Flush ();
406 root_clock->RaiseAccumulatedCompleted ();
408 #if CLOCK_DEBUG
409 ListClocks ();
410 #endif
412 ENDTICKTIMER (tick_update_clocks, "TimeManager::Tick - UpdateClocks");
415 if (current_flags & TIME_MANAGER_UPDATE_INPUT) {
416 STARTTICKTIMER (tick_input, "TimeManager::Tick - Input");
417 Emit (UpdateInputEvent);
418 ENDTICKTIMER (tick_input, "TimeManager::Tick - Input");
421 if (current_flags & TIME_MANAGER_RENDER) {
422 // fprintf (stderr, "rendering\n"); fflush (stderr);
423 STARTTICKTIMER (tick_render, "TimeManager::Tick - Render");
424 Emit (RenderEvent, new RenderingEventArgs (get_now()));
425 ENDTICKTIMER (tick_render, "TimeManager::Tick - Render");
428 last_global_time = current_global_time;