Alarms via at work
authorEnrico Zini <enrico@enricozini.org>
Sun, 29 Nov 2009 17:34:16 +0000 (18:34 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 29 Nov 2009 17:34:16 +0000 (18:34 +0100)
src/app_main.vala
src/at.vala
src/clock.vala
src/config.vala
src/zavai.vala

index 6f42629625674e053715257ce5d85def19b7655d..eb8a72c3abb6782c728b5e91939745a36f794d3e 100644 (file)
@@ -68,6 +68,7 @@ public class Clock : Gtk.VBox
 
         zavai.clock.clock.minute_changed += on_minute_changed;
         zavai.clock.clock.schedule_changed += on_schedule_changed;
+       on_schedule_changed(zavai.clock.clock.next_alarm());
 
         zavai.clock.clock.request("ui.main.clock");
     }
@@ -77,20 +78,17 @@ public class Clock : Gtk.VBox
            zavai.app.push_applet("ui.calendar");
     }
 
-    private void on_schedule_changed()
+    private void on_schedule_changed(zavai.clock.Alarm? next)
     {
-        zavai.clock.Alarm a = zavai.clock.clock.next_alarm();
-        if (a == null)
-        {
-            last_deadline = 0;
-            last_deadline_label = "";
-        }
-        else
-        {
-            last_deadline = a.deadline;
-            last_deadline_label = a.label;
-        }
-        on_minute_changed((long)last_time, last_time_type);
+           if (next == null)
+           {
+                   last_deadline = 0;
+                   last_deadline_label = "";
+           } else {
+                   last_deadline = next.ev.deadline;
+                   last_deadline_label = next.label;
+           }
+           on_minute_changed((long)last_time, last_time_type);
     }
 
     private void on_minute_changed(long ts, zavai.clock.SourceType type)
@@ -237,10 +235,11 @@ public abstract class AddDeadline : Applet
     protected void on_submit(Gtk.Button b)
     {
         string label = ((Gtk.Entry)dl_label.get_child()).get_text();
-        zavai.log.info("Scheduling deadline " + label);
-        zavai.clock.Alarm a = new zavai.clock.Alarm(get_deadline(), label);
-        a.trigger += zavai.audio.audio.notify_alarm;
-        zavai.clock.clock.schedule(a);
+       time_t deadlinets = get_deadline();
+       Time deadline = Time.local(deadlinets);
+       string timespec = deadline.format("%H:%M %m/%d/%Y");
+        zavai.log.info("Scheduling deadline " + label + " at " + timespec);
+       zavai.clock.clock.schedule(timespec, label);
         back();
     }
 }
index 0eb17bb78e3e03e8d4dbcd20bbb873cadc8cb87e..33cdf2746a6d05cbf29a1a934f5f884aa87b6b63 100644 (file)
@@ -22,10 +22,18 @@ using GLib;
 
 namespace at {
 
+public struct Event
+{
+       int id;
+       time_t deadline;
+}
+
 // Return the earliest ID in the queue, or -1 if none are found
 // Queue is null for all queues, otherwise a queue name
-public static int earliestID(string? queue = null)
+public static Event earliestID(string? queue = null)
 {
+       Event res = { -1, 0 };
+
        string argv[4];
        argv[0] = "/usr/bin/atq";
        if (queue == null)
@@ -43,18 +51,16 @@ public static int earliestID(string? queue = null)
        try
        {
                if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
-                       return -1;
+                       return res;
        } catch (SpawnError e) {
                stderr.printf("Cannot run 'at -q': %s\n", e.message);
-               return -1;
+               return res;
        }
 
        FileStream fs = FileStream.fdopen(stdout, "r");
        if (fs == null)
-               return -1;
+               return res;
 
-       int first_id = -1;
-       time_t first_ts = 0;
        char buf[200];
        string? line;
        while ((line = fs.gets(buf)) != null)
@@ -65,15 +71,20 @@ public static int earliestID(string? queue = null)
                Time t = Time();
                rest = t.strptime(rest.offset(1), "%a %b %d %H:%M:%S %Y");
                if (rest == null) continue;
+               if (rest.size() < 2) continue;
+               //stderr.printf("PARSE QUEUE rest %s\n", rest);
+               // Skip the queue of tasks currently being executed
+               //if (rest[1] == '=') continue;
+               // Skip entries not in the wanted queue
+               if (queue != null && rest[1] != queue[0]) continue;
                time_t tt = t.mktime();
-               if (first_ts == 0 || tt < first_ts)
-               {
-                       first_ts = tt;
-                       first_id = (int)id;
+               if (res.deadline == 0 || tt < res.deadline) {
+                       res.id = (int)id;
+                       res.deadline = tt;
                }
        }
        Process.close_pid(pid);
-       return first_id;
+       return res;
 }
 
 public delegate bool jobParser(int fd);
index 5a22847a7ba13f247ac3217ddf650e462670bbc2..6bc889c5816cbf7673619a3d3ef77d5451a497b5 100644 (file)
@@ -71,29 +71,18 @@ at alarm triggered through zavai: refresh_alarms()
 
 public class Alarm : Object
 {
-       // TODO old stuff
-       // Notify of an alarm being triggered
-       public signal void trigger(Alarm a);
-
-       public time_t deadline;
+       public at.Event ev;
        public string label;
 
-       public Alarm(time_t deadline, string label)
-       {
-               this.deadline = deadline;
-               this.label = label;
-       }
-
-
-       // TODO new stuff
-
        // Schedule with at
        public static void schedule(string timespec, string label) throws Error
        {
-               string argv[4];
+               string argv[5];
                argv[0] = "/usr/bin/at";
-               argv[1] = timespec;
-               argv[2] = null;
+               argv[1] = "-q";
+               argv[2] = "z";
+               argv[3] = timespec;
+               argv[4] = null;
 
                Pid pid;
                int stdinfd;
@@ -103,10 +92,13 @@ public class Alarm : Object
 
                {
                        FileStream fs = FileStream.fdopen(stdinfd, "w");
+                       string display = GLib.Environment.get_variable("DISPLAY");
+                       if (display != null)
+                               fs.printf("DISPLAY=\"%s\"; export DISPLAY\n", display);
                        fs.printf("# Zavai variables start here\n");
                        fs.printf("ZAVAI_LABEL=\"%s\"\n", label.escape(""));
                        fs.printf("# Zavai commands starts here\n");
-                       fs.printf("echo \"$ZAVAI_LABEL\" | zavai --notify");
+                       fs.printf("%s notify \"$ZAVAI_LABEL\"", zavai.config.argv0);
                }
                
                Process.close_pid(pid);
@@ -137,22 +129,10 @@ public class Alarm : Object
        }
 }
 
-private int alarm_compare(void* a, void* b)
-{
-       return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
-}
-
-[DBus (name = "org.freesmartphone.Notification")]
-public class AlarmNotification : Object {
-       public void Alarm () {
-               clock.check_alarms();
-       }
-}
-
 [DBus (name = "org.enricozini.zavai.Alarm")]
 public class ZavaiClock : Object {
        public void Notify (string label) {
-               clock.notify(label);
+               clock.notify_alarm(label);
        }
 }
 
@@ -166,7 +146,6 @@ public class Clock: zavai.Service
        protected time_t last_minute;
        protected time_t chosen_time;
        protected SourceType chosen_type;
-       protected AlarmNotification listener;
        protected ZavaiClock dbusClock;
 
        protected dynamic DBus.Object otimed_alarm;
@@ -175,13 +154,13 @@ public class Clock: zavai.Service
 
        // Ticks once a minute
        public signal void minute_changed(long time, SourceType source);
-       public signal void schedule_changed();
+       public signal void schedule_changed(Alarm? next);
+       public signal void alarm_triggered(string label);
 
        public Clock()
        {
                Object(name: "clock");
                alarms = null;
-               listener = new AlarmNotification();
                dbusClock = new ZavaiClock();
                last_minute = 0;
                last_gps_time = 0;
@@ -205,68 +184,33 @@ public class Clock: zavai.Service
                                "/org/freesmartphone/Device/RTC/0",
                                "org.freesmartphone.Device.RealtimeClock");
 
-               zavai.registry.sbus.register_object("/", listener);
                zavai.registry.sbus.register_object("/org/enricozini/Zavai/Clock", dbusClock);
        }
 
-       public void notify(string label)
+       public void notify_alarm(string label)
        {
-               stderr.printf("HAHA %s\n", label);
+               stderr.printf("Notifying %s\n", label);
+               alarm_triggered(label);
+               schedule_changed(next_alarm());
        }
 
        public Alarm? next_alarm()
        {
-               if (alarms == null)
+               at.Event ev;
+               ev = at.earliestID("z");
+               if (ev.deadline == 0)
                        return null;
-               return alarms.data;
-       }
-
-       public void schedule(Alarm a)
-       {
-               alarms.insert_sorted(a, alarm_compare);
-               otimed_reschedule();
-       }
-
-       private void otimed_reschedule()
-       {
-               if (alarms != null)
-               {
-                       zavai.log.info("Scheduling next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
-                       zavai.log.info("Scheduling at abs " + "%d".printf((int)alarms.data.deadline));
-
-                       try {
-                               otimed_alarm.ClearAlarm(zavai.registry.bus_name);
-                       } catch (Error e) {
-                               zavai.log.error("Cannot clear alarms: " + e.message);
-                       }
-                       try {
-                               otimed_alarm.SetAlarm(zavai.registry.bus_name, (int)alarms.data.deadline);
-                       } catch (Error e) {
-                               zavai.log.error("Cannot reschedule alarms: " + e.message);
-                       }
-
-                       int t = rtc.GetCurrentTime();
-                       stderr.printf("Current time: %d, RTC time: %d\n", (int)time_t(), t);
-                       t = rtc.GetWakeupTime();
-                       stderr.printf("Scheduled alarm: %d, RTC wakeup time: %d\n", (int)alarms.data.deadline, t);
-               } else
-                       zavai.log.info("No alarms left to reschedule");
-               schedule_changed();
+               string label = Alarm.getLabel(ev.id);
+               Alarm res = new Alarm();
+               res.ev = ev;
+               res.label = label;
+               return res;
        }
 
-       public void check_alarms()
+       public void schedule(string timespec, string label) throws Error
        {
-               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(a);
-               }
-
-               otimed_reschedule();
+               Alarm.schedule(timespec, label);
+               schedule_changed(next_alarm());
        }
 
        private void on_gps_time(dynamic DBus.Object pos, int t)
index f2413adcc59926fcbf92906f0aa6443a8d289569..dec3b202c22844eb124256c8a7086d3e1569a123 100644 (file)
@@ -29,6 +29,7 @@ public class Config
     public string gprs_apn { get; set; }
     public string gprs_user { get; set; }
     public string gprs_pass { get; set; }
+    public string argv0 { get; set; }
 
     public Config()
     {
index 467aab667ecd0f612473e2aa6375e85de3ed4801..c8e9305821be7c22e47b7cf163ccdd498eb516f7 100644 (file)
@@ -60,6 +60,7 @@ static int main (string[] args) {
 
        if (args.length > 1 && args[1] == "notify")
        {
+               stdout.printf("NOTIFY\n");
                DBus.Connection sbus;
                dynamic DBus.Object zav;
                try {
@@ -74,7 +75,10 @@ static int main (string[] args) {
                        "/org/enricozini/Zavai/Clock",
                        "org.enricozini.zavai.Alarm");
                if (args.length > 2)
+               {
+                       stdout.printf("NOTIFY %s\n", args[2]);
                        zav.Notify(args[2]);
+               }
                else
                        zav.Notify("Alarm");
                
@@ -83,6 +87,7 @@ static int main (string[] args) {
 
     // Core infrastructure
        zavai.config = new zavai.Config();
+       zavai.config.argv0 = args[0];
        zavai.registry = new zavai.Registry();
 
        zavai.app = new zavai.Zavai();