Notify alarms using the new Notifier infrastructure
authorEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 13:34:27 +0000 (14:34 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 13:34:27 +0000 (14:34 +0100)
src/app_alarm.vala
src/app_notify.vala
src/leds.vala

index c3bfd7cfd4013021b707f8748ec294dc19e9c6b5..48d138bb35cb83acc161e29a8a8fd1062be361f7 100644 (file)
@@ -32,114 +32,107 @@ static long timediff(Posix.timeval* a, Posix.timeval* b)
 }
 */
 
-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 weak clock.AlarmTriggerInfo current = null;
+    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);
 
-        cancel_timeout = 0;
-
-        //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);
-
-        message = new Gtk.Label("no message");
-        vbox.pack_start(message, false, true, 0);
+        set_size_request(300, 500);
 
-        ack = new Gtk.Button.with_label("Ack");
-        ack.clicked += on_iface_ack;
-        vbox.pack_start(ack, true, true, 0);
+        add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK);
+        add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL);
+        set_default_response(Gtk.ResponseType.OK);
 
-        //vbox.show_all();
-        zavai.registry.register(this);
+        weak Gtk.VBox vbox = (Gtk.VBox)get_content_area();
 
-        clock.alarm_trigger_queue.triggered += on_trigger;
-        clock.alarm_trigger_queue.acked += on_done;
-        clock.alarm_trigger_queue.canceled += on_done;
+        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();
     }
 
-    protected void abort_timeout()
+    protected virtual bool push_aux_state()
     {
-        if (cancel_timeout != 0)
-        {
-            Source.remove(cancel_timeout);
-            cancel_timeout = 0;
-        }
+        var state = new zavai.led.LedState(name);
+        state.set_blink(255);
+        zavai.led.aux.push_state(state);
+        return true;
     }
 
-    protected void on_iface_ack()
+    protected virtual bool push_vibrator_state()
     {
-        if (current == null) return;
-        clock.alarm_trigger_queue.ack(current);
+        var state = new zavai.led.LedState(name);
+        state.set_blink(255);
+        zavai.led.aux.push_state(state);
+        return true;
     }
 
-    protected bool on_iface_timeout()
+    protected virtual bool push_ringtone_state()
     {
-        if (current == null) return false;
-        clock.alarm_trigger_queue.cancel(current);
         return false;
     }
+}
+
+public class AlarmNotifier : zavai.Resource, ui.notify.Notifier
+{
+    protected weak clock.AlarmTriggerInfo current = null;
+    protected AlarmNotifierDialog dialog = null;
+
+    public AlarmNotifier()
+    {
+        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 on_trigger(clock.AlarmTriggerInfo info)
     {
         current = info;
-        message.set_text(info.label);
-
-        ui.power.backlight.request("alarmnotifier");
+        dialog = new AlarmNotifierDialog("Alarm", info.label);
 
-        if (!shown)
+        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)
         {
-            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();
+            case Gtk.ResponseType.OK:
+                clock.alarm_trigger_queue.ack(current);
+                break;
+            case Gtk.ResponseType.CANCEL:
+                clock.alarm_trigger_queue.cancel(current);
+                break;
         }
-
-        abort_timeout();
-        cancel_timeout = Timeout.add(30 * 1000, on_iface_timeout);
+        dialog.destroy();
+        dialog = null;
+        current = null;
     }
 
     public void on_done(clock.AlarmTriggerInfo info)
     {
-        if (current == null || current != info) return;
-        visible = false;
-        abort_timeout();
-        current = null;
-        ui.power.backlight.release("alarmnotifier");
+        if (current == null || dialog == null || current != info) return;
+        dialog.response(Gtk.ResponseType.NONE);
     }
 
     public void shutdown()
     {
-        abort_timeout();
+        if (dialog == null) return;
+        dialog.response(Gtk.ResponseType.CANCEL);
     }
 }
 
index d6c086afda37f8adad6d938803f1feab8ee72dad..13b5ae64e37b91bb4d39f710b4eeaa5427c75d36 100644 (file)
@@ -26,7 +26,13 @@ namespace notify {
 
 public abstract class Notifier : Gtk.Dialog
 {
-    public string name { get; construct; }
+    public string notifier_name { get; construct; }
+
+    construct {
+        set_transient_for(zavai.app);
+        set_modal(true);
+        set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
+    }
 
     protected virtual bool push_aux_state()
     {
@@ -45,20 +51,20 @@ public abstract class Notifier : Gtk.Dialog
 
     public int run()
     {
-        ui.power.backlight.request(name);
+        ui.power.backlight.request(notifier_name);
 
         // Setup our attention seeking strategy
-        bool has_aux = push_aux_state();
+        bool has_aux = zavai.led.aux != null && push_aux_state();
         bool has_vibrator = zavai.led.vibrator != null && push_vibrator_state();
-        bool has_ringtone = zavai.audio.soundplayer != null && push_ringtone_state();
+        // TODO bool has_ringtone = zavai.audio.soundplayer != null && push_ringtone_state();
 
         // Run dialog
         int res = base.run();
 
         // TODO: Restore ringtone state
-        if (has_vibrator) zavai.led.vibrator.pop_state(name);
-        // TODO: Restore aux state
-        ui.power.backlight.release(name);
+        if (has_vibrator) zavai.led.vibrator.pop_state(notifier_name);
+        if (has_aux) zavai.led.aux.pop_state(notifier_name);
+        ui.power.backlight.release(notifier_name);
         return res;
     }
 }
index a089ef6ba2f9704a4b4ebd01991b1a9006c1eeea..013e1faf230948805b372ccd150e5f2a06dcbf7b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * leds - Leds/buttons support
+ * leds - Leds support
  *
  * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
  *
@@ -62,32 +62,31 @@ public class LedState
     }
 }
 
-public class Vibrator : zavai.Resource, Object
+public class Led : zavai.Resource, Object
 {
-    protected Omhacks.Led vibrator;
+    protected Omhacks.Led led;
 
     protected List<LedState> states;
 
-    public Vibrator() throws FileError
+    public Led(string name) throws FileError
     {
-        if (vibrator.init("neo1973:vibrator") != 0)
-            throw new FileError.NOENT("vibrator not found");
-
+        if (led.init(name) != 0)
+            throw new FileError.NOENT("led " + name + " not found");
         states = new List<LedState>();
     }
 
     public void turn_off()
     {
-        vibrator.brightness = 0;
-        Memory.copy(vibrator.trigger, "none", 5);
-        vibrator.set();
+        led.brightness = 0;
+        Memory.copy(led.trigger, "none", 5);
+        led.set();
     }
 
     public void push_state(LedState state)
     {
         states.prepend(state);
-        state.to_omhacks(ref vibrator);
-        vibrator.set();
+        state.to_omhacks(ref led);
+        led.set();
     }
 
     public void pop_state(string name)
@@ -112,8 +111,8 @@ public class Vibrator : zavai.Resource, Object
                 turn_off();
             else {
                 // Activate the new top
-                states.data.to_omhacks(ref vibrator);
-                vibrator.set();
+                states.data.to_omhacks(ref led);
+                led.set();
             }
     }
 
@@ -125,29 +124,12 @@ public class Vibrator : zavai.Resource, Object
     }
 }
 
-public class AUX: zavai.Service
+public class AUXButton: zavai.Service
 {
-    protected Omhacks.Led auxled;
-    protected bool has_aux;
-
-    protected weak clock.AlarmTriggerInfo current_alarm = null;
+    public signal bool event(ulong time, bool pressed);
 
-    public AUX()
+    public AUXButton()
     {
-        Object(name: "ui.aux");
-
-        has_aux = (auxled.init("gta02-aux:red") == 0);
-
-        if (has_aux)
-        {
-            zavai.log.warning("aux: can notify alarm triggers");
-            clock.alarm_trigger_queue.triggered += on_alarm_trigger;
-            clock.alarm_trigger_queue.acked += on_alarm_done;
-            clock.alarm_trigger_queue.canceled += on_alarm_done;
-        } else {
-            zavai.log.warning("aux: no way to notify alarm triggers");
-        }
-
         // Listen to the button via X
         input.hotkeys.hotkey += on_auxbutton;
         input.hotkeys.grab(zavai.config.aux_button_keycode, 0, false);
@@ -157,64 +139,29 @@ public class AUX: zavai.Service
     protected bool on_auxbutton(uint keycode, ulong time, bool pressed)
     {
         if (keycode == zavai.config.aux_button_keycode)
-        {
-            if (pressed)
-            {
-                zavai.log.debug("AUX button pressed");
-                if (current_alarm != null)
-                {
-                    zavai.log.debug("HASCA");
-                    clock.alarm_trigger_queue.ack(current_alarm);
-                }
-            }
-            else
-                zavai.log.debug("AUX button released");
-            return true;
-        }
+            return event(time, pressed);
         return false;
     }
-
-    public void on_alarm_trigger(clock.AlarmTriggerInfo info)
-    {
-        zavai.log.debug("Start blinking");
-        auxled.brightness = 256;
-        // FIXME: is there a better way? I hope there is a better way. Please
-        // tell me there is a better way.
-        var trig = "timer";
-        for (int i = 0; ; ++i)
-        {
-            auxled.trigger[i] = (char)trig[i];
-            if (trig[i] == 0) break;
-        }
-        auxled.delay_on = 200;
-        auxled.delay_off = 300;
-        auxled.set();
-        current_alarm = info;
-    }
-
-    public void on_alarm_done(clock.AlarmTriggerInfo info)
-    {
-        zavai.log.debug("Stop blinking");
-        var trig = "none";
-        for (int i = 0; ; ++i)
-        {
-            auxled.trigger[i] = (char)trig[i];
-            if (trig[i] == 0) break;
-        }
-        auxled.brightness = 0;
-        auxled.set();
-        current_alarm = null;
-    }
 }
 
-public AUX aux = null;
-public Vibrator vibrator = null;
+public Led aux = null;
+public Led vibrator = null;
+public AUXButton auxbutton = null;
 
 public void init()
 {
-    aux = new AUX();
+    auxbutton = new AUXButton();
+
+    try {
+        aux = new Led("gta02-aux:red");
+        zavai.registry.register(aux);
+    } catch (Error e) {
+        zavai.log.info("No aux led found");
+        aux = null;
+    }
+
     try {
-        vibrator = new Vibrator();
+        vibrator = new Led("neo1973:vibrator");
         zavai.registry.register(vibrator);
     } catch (Error e) {
         zavai.log.info("No vibrator found");