fixed the build
[moon.git] / src / timesource.cpp
blobee72780c7f5a6c2cc7d74755fb09b0a9d9a5467a
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 <glib.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #ifdef HAVE_SYS_TIME_H
19 #include <sys/time.h>
20 #endif
21 #include <time.h>
23 #include "timesource.h"
25 #ifdef _MSC_VER
26 #include "Winsock2.h"
28 static int
29 gettimeofday (struct timeval *tv, void *tz)
31 long int l = GetTickCount ();
33 tv->tv_sec = l / 1000;
34 tv->tv_usec = (l % 1000) * 1000;
35 return 0;
37 #endif // _MSC_VER
40 TimeSpan
41 get_now (void)
43 struct timeval tv;
44 TimeSpan res;
45 #ifdef CLOCK_MONOTONIC
46 struct timespec tspec;
47 if (clock_gettime (CLOCK_MONOTONIC, &tspec) == 0) {
48 res = (TimeSpan)((gint64)tspec.tv_sec * 10000000 + tspec.tv_nsec / 100);
49 return res;
51 #endif
53 if (gettimeofday (&tv, NULL) == 0) {
54 res = (TimeSpan)(tv.tv_sec * 1000000 + tv.tv_usec) * 10;
55 return res;
58 // XXX error
59 return 0;
63 TimeSource::TimeSource (Deployment *deployment) : EventObject (deployment)
65 SetObjectType (Type::TIMESOURCE);
68 TimeSource::TimeSource ()
70 SetObjectType (Type::TIMESOURCE);
73 TimeSource::~TimeSource ()
77 void
78 TimeSource::Start ()
82 void
83 TimeSource::Stop ()
87 void
88 TimeSource::SetTimerFrequency (int frequency)
92 TimeSpan
93 TimeSource::GetNow ()
95 return 0;
98 SystemTimeSource::SystemTimeSource (Deployment *deployment) : TimeSource (deployment)
100 SetObjectType (Type::SYSTEMTIMESOURCE);
101 timeout_id = 0;
102 frequency = -1;
105 SystemTimeSource::SystemTimeSource ()
107 SetObjectType (Type::SYSTEMTIMESOURCE);
108 timeout_id = 0;
109 frequency = -1;
112 SystemTimeSource::~SystemTimeSource ()
114 Stop();
117 void
118 SystemTimeSource::SetTimerFrequency (int timeout)
120 bool running = timeout_id != 0;
122 if (running)
123 Stop ();
125 frequency = timeout;
127 if (running)
128 Start ();
131 void
132 SystemTimeSource::Start ()
134 if (timeout_id != 0)
135 return;
137 if (frequency == -1)
138 g_warning ("SystemTimeSource::frequency uninitialized in ::Start()");
140 timeout_id = g_timeout_add_full (MOON_PRIORITY_DEFAULT, frequency, SystemTimeSource::tick_timeout, this, NULL);
143 void
144 SystemTimeSource::Stop ()
146 if (timeout_id != 0) {
147 g_source_remove (timeout_id);
148 timeout_id = 0;
152 TimeSpan
153 SystemTimeSource::GetNow ()
155 return get_now ();
158 gboolean
159 SystemTimeSource::tick_timeout (gpointer data)
161 SystemTimeSource *source = (SystemTimeSource *)data;
163 source->SetCurrentDeployment ();
164 source->Emit (TimeSource::TickEvent);
165 return TRUE;
168 ManualTimeSource::ManualTimeSource ()
170 SetObjectType (Type::MANUALTIMESOURCE);
171 current_time = 0;
174 ManualTimeSource::~ManualTimeSource ()
178 void
179 ManualTimeSource::SetCurrentTime (TimeSpan current_time)
181 this->current_time = current_time;
182 g_main_context_iteration (g_main_context_default (), false);
183 Emit (TimeSource::TickEvent);
184 Emit (TimeSource::TickEvent);
185 Emit (TimeSource::TickEvent);
188 TimeSpan
189 ManualTimeSource::GetNow ()
191 return current_time;