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 Alarm : Object
34 public signal void trigger();
36 public time_t deadline;
39 public Alarm(time_t deadline, string label)
41 this.deadline = deadline;
46 private int alarm_compare(void* a, void* b)
48 return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
51 public class Clock: zavai.Service
53 protected time_t last_gps_time;
54 protected time_t last_gps_time_system_time;
55 protected time_t last_system_time;
56 protected dynamic DBus.Object gps_time;
57 protected uint system_time_timeout;
58 protected time_t last_minute;
59 protected time_t chosen_time;
60 protected SourceType chosen_type;
62 protected SList<Alarm> alarms;
64 // Ticks once a minute
65 public signal void minute_changed(long time, SourceType source);
73 last_gps_time_system_time = 0;
74 last_system_time = time_t();
75 chosen_time = last_system_time;
77 gps_time = zavai.registry.sbus.get_object(
78 "org.freesmartphone.ogpsd",
79 "/org/freedesktop/Gypsy",
80 "org.freedesktop.Gypsy.Time");
83 public void schedule(Alarm a)
85 alarms.insert_sorted(a, alarm_compare);
86 zavai.log.info("Next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
89 public void check_alarms()
91 last_system_time = time_t();
93 while (alarms != null && alarms.data.deadline <= chosen_time)
95 Alarm a = alarms.data;
97 zavai.log.info("Triggering " + a.label);
102 private void on_gps_time(dynamic DBus.Object pos, int t)
106 last_gps_time_system_time = 0;
109 last_gps_time = (time_t)t;
110 last_gps_time_system_time = time_t();
115 private bool on_system_time()
117 last_system_time = time_t();
122 private void update_time()
124 if (last_gps_time_system_time + 10 > last_system_time)
126 chosen_time = last_gps_time;
127 chosen_type = SourceType.GPS;
131 chosen_time = last_system_time;
132 chosen_type = SourceType.SYSTEM;
134 if (chosen_time / 60 != last_minute)
136 last_minute = chosen_time / 60;
137 minute_changed(chosen_time, chosen_type);
141 /// Request GPS resource
142 public override void start()
146 system_time_timeout = Timeout.add(5000, on_system_time);
147 gps_time.TimeChanged += on_gps_time;
148 last_system_time = time_t();
154 public override void stop()
156 if (!started) return;
158 Source.remove(system_time_timeout);
159 gps_time.TimeChanged -= on_gps_time;
165 public Clock clock = null;
171 zavai.registry.register_service(clock);