Wire auxbutton and vibrator properly
[gregoa/zavai.git] / src / app_alarm.vala
index 3b787525bb3e82e88acdefafcf313b2bac83301d..2d2699bee642f76869118e1ecfae6942ec61284d 100644 (file)
@@ -24,121 +24,140 @@ namespace zavai {
 namespace ui {
 namespace alarm {
 
+/*
 // Compute a-b in microseconds
 static long timediff(Posix.timeval* a, Posix.timeval* b)
 {
     return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec);
 }
+*/
 
-public class AlarmNotifier : zavai.Resource, Gtk.Window
+protected class AlarmNotifierDialog : ui.notify.Notifier
 {
-    protected Gtk.VBox vbox;
-    protected bool shown;
     protected Gtk.Label message;
-    protected Gtk.Button ack;
-    protected uint cancel_timeout;
+    protected Gtk.Button message_button;
 
-    public AlarmNotifier()
+    public AlarmNotifierDialog(string title, string text)
     {
         Object(
-            type: Gtk.WindowType.TOPLEVEL,
-            title: "Alarm"
+            notifier_name: "alarm",
+            title: title
         );
-        shown = false;
-        destroy_with_parent = true;
-        set_transient_for(zavai.app);
-        set_modal(true);
-        set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
-        set_size_request(300, 500);
 
-        vbox = new Gtk.VBox(false, 0);
-        add(vbox);
+        set_size_request(300, 500);
 
-        cancel_timeout = 0;
+        add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK);
+        add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL);
+        set_default_response(Gtk.ResponseType.OK);
 
-        //destroy += Gtk.main_quit;
-        //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
-        //visibility_notify_event += on_visibility;
-        set_skip_pager_hint(true);
-        set_skip_taskbar_hint(true);
-        set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
+        weak Gtk.VBox vbox = (Gtk.VBox)get_content_area();
 
-        message = new Gtk.Label("no message");
-        vbox.pack_start(message, false, true, 0);
+        message = new Gtk.Label(text);
+        message_button = new Gtk.Button();
+        message_button.set_image(message);
+        message_button.clicked += (b) => { response(Gtk.ResponseType.OK); };
+        vbox.pack_start(message_button, true, true, 0);
+        message_button.show();
+    }
 
-        ack = new Gtk.Button.with_label("Ack");
-        ack.clicked += on_ack;
-        vbox.pack_start(ack, true, true, 0);
+    protected override bool push_aux_state()
+    {
+        var state = new zavai.led.LedState(notifier_name);
+        state.set_blink(255);
+        zavai.led.aux.push_state(state);
+        return true;
+    }
 
-        //vbox.show_all();
-        zavai.registry.register(this);
+    protected override bool push_vibrator_state()
+    {
+        var state = new zavai.led.LedState(notifier_name);
+        state.set_blink(255);
+        zavai.led.vibrator.push_state(state);
+        return true;
     }
 
-    protected void on_ack()
+    protected override bool push_ringtone_state()
     {
-        confirm();
+        var state = new zavai.audio.PlayerState(notifier_name, config.ringtone_alarm, true);
+        zavai.audio.soundplayer.push_state(state);
+        return true;
     }
+}
+
+public class AlarmNotifier : zavai.Resource, ui.notify.Notifier
+{
+    protected weak clock.AlarmTriggerInfo current = null;
+    protected AlarmNotifierDialog dialog = null;
 
-    protected bool on_timeout()
+    public AlarmNotifier()
     {
-        cancel();
-        return false;
+        zavai.registry.register(this);
+
+        clock.alarm_trigger_queue.triggered += on_trigger;
+        clock.alarm_trigger_queue.acked += on_done;
+        clock.alarm_trigger_queue.canceled += on_done;
     }
 
-    public void trigger(string text)
+    public void on_trigger(clock.AlarmTriggerInfo info)
     {
-        message.set_text(text);
+        current = info;
+        dialog = new AlarmNotifierDialog("Alarm", info.label);
 
-        if (!shown)
-        {
-            show_all();
-            show();
-            visible = true;
-            present();
-            shown = true;
-        } else {
-            // TODO: do more in case it is visible but has no visibility (is covered by others)
-            visible = !visible;
-            if (visible)
-                present();
-        }
+        if (zavai.led.auxbutton != null)
+            zavai.led.auxbutton.event += on_auxbutton;
 
-        if (cancel_timeout != 0)
-        {
-            Source.remove(cancel_timeout);
+        uint cancel_timeout = 0;
+        cancel_timeout = Timeout.add(30 * 1000, () => {
+            dialog.response(Gtk.ResponseType.CANCEL);
             cancel_timeout = 0;
+            return false;
+        });
+        int res = dialog.run();
+        if (cancel_timeout != 0) Source.remove(cancel_timeout);
+        switch (res)
+        {
+            case Gtk.ResponseType.OK:
+                clock.alarm_trigger_queue.ack(current);
+                break;
+            case Gtk.ResponseType.CANCEL:
+                clock.alarm_trigger_queue.cancel(current);
+                break;
         }
-        cancel_timeout = Timeout.add(2000, on_timeout);
+
+        if (zavai.led.auxbutton != null)
+            zavai.led.auxbutton.event -= on_auxbutton;
+
+        dialog.destroy();
+        dialog = null;
+        current = null;
     }
 
-    public void confirm()
+    protected bool on_auxbutton(ulong time, bool pressed)
     {
-        visible = false;
-        if (cancel_timeout != 0)
-        {
-            Source.remove(cancel_timeout);
-            cancel_timeout = 0;
-        }
+        if (dialog == null) return false;
+        dialog.response(Gtk.ResponseType.OK);
+        return true;
     }
 
-    public void cancel()
+    public void on_done(clock.AlarmTriggerInfo info)
     {
-        visible = false;
-        if (cancel_timeout != 0)
-        {
-            Source.remove(cancel_timeout);
-            cancel_timeout = 0;
-        }
+        if (current == null || dialog == null || current != info) return;
+        dialog.response(Gtk.ResponseType.NONE);
     }
 
-    public void shutdown() {}
+    public void shutdown()
+    {
+        if (dialog == null) return;
+        dialog.response(Gtk.ResponseType.CANCEL);
+    }
 }
 
-public AlarmNotifier notifier;
+public AlarmNotifier notifier = null;
 
 public void init()
 {
-    notifier = new AlarmNotifier();
+    if (clock.alarm_trigger_queue != null)
+        notifier = new AlarmNotifier();
 }
 
 }