1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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.
16 #include "timemanager.h"
17 #include "timesource.h"
21 #define PUT_TIME_MANAGER_TO_SLEEP 0
24 #define STARTTICKTIMER(id,str) STARTTIMER(id,str)
25 #define ENDTICKTIMER(id,str) ENDTIMER(it,str)
27 #define STARTTICKTIMER(id,str)
28 #define ENDTICKTIMER(id,str)
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
{
43 TickCall (TickCallHandler func
, EventObject
*data
)
58 class RootClockGroup
: public ClockGroup
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
;
77 virtual ~RootClockGroup () { }
80 TimeManager::TimeManager ()
82 SetObjectType (Type::TIMEMANAGER
);
84 if (moonlight_flags
& RUNTIME_INIT_MANUAL_TIMESOURCE
)
85 source
= new ManualTimeSource();
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;
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
);
109 root_clock
->SetTimeManager (this);
112 TimeManager::~TimeManager ()
114 source
->RemoveHandler (TimeSource::TickEvent
, source_tick_callback
, this);
121 root_clock
->unref ();
126 RemoveAllRegisteredTimeouts ();
130 TimeManager::SetMaximumRefreshRate (int hz
)
136 current_timeout
= FPS_TO_DELAY (hz
);
137 source
->SetTimerFrequency (current_timeout
);
144 last_global_time
= current_global_time
= source
->GetNow();
145 current_global_time_usec
= current_global_time
/ 10;
146 source
->SetTimerFrequency (current_timeout
);
148 source_tick_pending
= true;
158 TimeManager::Shutdown ()
160 RemoveAllRegisteredTimeouts ();
165 TimeManager::source_tick_callback (EventObject
*sender
, EventArgs
*calldata
, gpointer closure
)
167 ((TimeManager
*) closure
)->SourceTick ();
171 TimeManager::InvokeTickCall ()
173 TickCall
*call
= (TickCall
*) tick_calls
.Pop ();
178 call
->func (call
->data
);
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
));
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
));
200 TimeManager::RemoveAllRegisteredTimeouts ()
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
;
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
);
226 TimeManager::RemoveTickCall (TickCallHandler func
)
229 List::Node
* call
= tick_calls
.LinkedList ()->Find (find_tick_call
, (void*)func
);
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
)
239 tick_calls
.Unlock ();
243 find_tick_call (List::Node
*node
, void *data
)
245 if (((TickCall
*)node
)->func
== data
)
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
);
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
);
280 while (n
--) putchar (' ');
285 output_clock (Clock
*clock
, int 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()) {
312 if (clock
->GetIsPaused())
313 printf (" (paused)");
317 if (clock
->Is(Type::CLOCKGROUP
)) {
318 ClockGroup
*cg
= (ClockGroup
*)clock
;
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
);
328 TimeManager::ListClocks()
330 printf ("Currently registered clocks:\n");
331 printf ("============================\n");
333 output_clock (root_clock
, 2);
335 printf ("============================\n");
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());
354 TimeManager::SourceTick ()
356 TimeManagerOp current_flags
= flags
;
358 #if PUT_TIME_MANAGER_TO_SLEEP
359 flags
= (TimeManagerOp
)0;
362 if (current_flags
& TIME_MANAGER_TICK_CALL
) {
363 bool remaining_tick_calls
= false;
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
)
379 // ... then cause all clocks to raise the events they've queued up
380 root_clock
->RaiseAccumulatedEvents ();
385 root_clock
->RaiseAccumulatedCompleted ();
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
;