2 * clock - clock resource for zavai
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 public enum SourceType
32 public class Clock: zavai.Service
34 protected time_t last_gps_time;
35 protected time_t last_gps_time_system_time;
36 protected time_t last_system_time;
37 protected dynamic DBus.Object gps_time;
38 protected uint system_time_timeout;
39 protected time_t last_minute;
41 // Ticks once a minute
42 public signal void minute_changed(long time, SourceType source);
50 last_gps_time_system_time = 0;
51 last_system_time = time_t();
53 gps_time = zavai.registry.sbus.get_object(
54 "org.freesmartphone.ogpsd",
55 "/org/freedesktop/Gypsy",
56 "org.freedesktop.Gypsy.Time");
59 private void on_gps_time(dynamic DBus.Object pos, int t)
63 last_gps_time_system_time = 0;
66 last_gps_time = (time_t)t;
67 last_gps_time_system_time = time_t();
72 private bool on_system_time()
74 last_system_time = time_t();
79 private void update_time()
83 if (last_gps_time_system_time + 10 > last_system_time)
85 chosen = last_gps_time;
86 type = SourceType.GPS;
90 chosen = last_system_time;
91 type = SourceType.SYSTEM;
93 if (chosen / 60 != last_minute)
95 last_minute = chosen / 60;
96 minute_changed(chosen, type);
100 /// Request GPS resource
101 public override void start()
105 system_time_timeout = Timeout.add(5000, on_system_time);
106 gps_time.TimeChanged += on_gps_time;
107 last_system_time = time_t();
113 public override void stop()
115 if (!started) return;
117 Source.remove(system_time_timeout);
118 gps_time.TimeChanged -= on_gps_time;
124 public Clock clock = null;
130 zavai.registry.register_service(clock);