From: Enrico Zini Date: Mon, 17 Aug 2009 12:53:06 +0000 (+0100) Subject: Implemented a sorted alarm list X-Git-Url: https://git.toastfreeware.priv.at/gregoa/zavai.git/commitdiff_plain/b1e9c9fd6b0089f8114a751198fdd2cd989fccdc?ds=sidebyside Implemented a sorted alarm list --- diff --git a/src/clock.vala b/src/clock.vala index 166d537..90c8785 100644 --- a/src/clock.vala +++ b/src/clock.vala @@ -29,6 +29,25 @@ public enum SourceType GPS } +public class Alarm : Object +{ + public signal void trigger(); + + public time_t deadline; + public string label; + + public Alarm(time_t deadline, string label) + { + this.deadline = deadline; + this.label = label; + } +} + +private int alarm_compare(void* a, void* b) +{ + return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline); +} + public class Clock: zavai.Service { protected time_t last_gps_time; @@ -37,6 +56,10 @@ public class Clock: zavai.Service protected dynamic DBus.Object gps_time; protected uint system_time_timeout; protected time_t last_minute; + protected time_t chosen_time; + protected SourceType chosen_type; + + protected SList alarms; // Ticks once a minute public signal void minute_changed(long time, SourceType source); @@ -44,11 +67,12 @@ public class Clock: zavai.Service public Clock() { name = "clock"; - + alarms = null; last_minute = 0; last_gps_time = 0; last_gps_time_system_time = 0; last_system_time = time_t(); + chosen_time = last_system_time; gps_time = zavai.registry.sbus.get_object( "org.freesmartphone.ogpsd", @@ -56,6 +80,25 @@ public class Clock: zavai.Service "org.freedesktop.Gypsy.Time"); } + public void schedule(Alarm a) + { + alarms.insert_sorted(a, alarm_compare); + zavai.log.info("Next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string()); + } + + public void check_alarms() + { + last_system_time = time_t(); + update_time(); + while (alarms != null && alarms.data.deadline <= chosen_time) + { + Alarm a = alarms.data; + alarms.remove(a); + zavai.log.info("Triggering " + a.label); + a.trigger(); + } + } + private void on_gps_time(dynamic DBus.Object pos, int t) { if (t == 0) @@ -78,22 +121,20 @@ public class Clock: zavai.Service private void update_time() { - time_t chosen; - SourceType type; if (last_gps_time_system_time + 10 > last_system_time) { - chosen = last_gps_time; - type = SourceType.GPS; + chosen_time = last_gps_time; + chosen_type = SourceType.GPS; } else { - chosen = last_system_time; - type = SourceType.SYSTEM; + chosen_time = last_system_time; + chosen_type = SourceType.SYSTEM; } - if (chosen / 60 != last_minute) + if (chosen_time / 60 != last_minute) { - last_minute = chosen / 60; - minute_changed(chosen, type); + last_minute = chosen_time / 60; + minute_changed(chosen_time, chosen_type); } } diff --git a/src/zavai.vala b/src/zavai.vala index 9672ec9..7a79953 100644 --- a/src/zavai.vala +++ b/src/zavai.vala @@ -58,6 +58,11 @@ static int main (string[] args) { zavai.gps.init(); zavai.clock.init(); + zavai.clock.clock.schedule(new zavai.clock.Alarm(123456, "Second")); + zavai.clock.clock.schedule(new zavai.clock.Alarm(1234567, "Third")); + zavai.clock.clock.schedule(new zavai.clock.Alarm(12345, "First")); + zavai.clock.clock.check_alarms(); + zavai.registry.register_menu("menu.main", new zavai.Menu("Main menu")); // User interface