Move vibrator together with aux
authorEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 12:35:18 +0000 (13:35 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 12:35:18 +0000 (13:35 +0100)
src/app_aux.vala [deleted file]
src/app_notify.vala
src/audio.vala
src/leds.vala [new file with mode: 0644]
src/zavai.vala

diff --git a/src/app_aux.vala b/src/app_aux.vala
deleted file mode 100644 (file)
index db05e99..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * app_aux - AUX button interaction
- *
- * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-using GLib;
-
-namespace zavai {
-namespace ui {
-namespace aux {
-
-public class AUX: zavai.Service
-{
-    protected Omhacks.Led auxled;
-    protected bool has_aux;
-
-    protected weak clock.AlarmTriggerInfo current_alarm = null;
-
-    public AUX()
-    {
-        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);
-        input.hotkeys.request("auxbutton");
-    }
-
-    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 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 void init()
-{
-    aux = new AUX();
-}
-
-}
-}
-}
index 8759bb393bbb3318e4337ce19db10f2d8729bfcf..d6c086afda37f8adad6d938803f1feab8ee72dad 100644 (file)
@@ -28,16 +28,35 @@ public abstract class Notifier : Gtk.Dialog
 {
     public string name { get; construct; }
 
+    protected virtual bool push_aux_state()
+    {
+        return false;
+    }
+
+    protected virtual bool push_vibrator_state()
+    {
+        return false;
+    }
+
+    protected virtual bool push_ringtone_state()
+    {
+        return false;
+    }
+
     public int run()
     {
         ui.power.backlight.request(name);
-        // TODO: Save aux state
-        // TODO: Save vibrator state
-        // TODO: Save ringtone state
-        // TODO: set our state
+
+        // Setup our attention seeking strategy
+        bool has_aux = push_aux_state();
+        bool has_vibrator = zavai.led.vibrator != null && push_vibrator_state();
+        bool has_ringtone = zavai.audio.soundplayer != null && push_ringtone_state();
+
+        // Run dialog
         int res = base.run();
+
         // TODO: Restore ringtone state
-        // TODO: Restore vibrator state
+        if (has_vibrator) zavai.led.vibrator.pop_state(name);
         // TODO: Restore aux state
         ui.power.backlight.release(name);
         return res;
index 7639bcb3080584e835054f3b25f4cea33a2b9f28..0a3967c8095e6c21639e1c86a46b3e2104c6f56a 100644 (file)
@@ -23,108 +23,6 @@ using GLib;
 namespace zavai {
 namespace audio {
 
-public class VibratorState
-{
-    public string name;
-    public int brightness;
-    public string trigger;
-    public int delay_on;
-    public int delay_off;
-
-    public VibratorState(string name)
-    {
-        this.name = name;
-        set_constant(0);
-    }
-
-    public void set_constant(int power)
-    {
-        brightness = power;
-        trigger = "none";
-        delay_on = 0;
-        delay_off = 0;
-    }
-
-    public void set_blink(int power, int delay_on=200, int delay_off=300)
-    {
-        brightness = power;
-        trigger = "timer";
-        this.delay_on = delay_on;
-        this.delay_off = delay_off;
-    }
-
-    public void to_omhacks(ref Omhacks.Led led)
-    {
-        led.brightness = brightness;
-        Memory.copy(led.trigger, trigger, trigger.size());
-        led.delay_on = 200;
-        led.delay_off = 300;
-    }
-}
-
-public class Vibrator : zavai.Resource, Object
-{
-    protected Omhacks.Led vibrator;
-
-    protected List<VibratorState> states;
-
-    public Vibrator() throws FileError
-    {
-        if (vibrator.init("neo1973:vibrator") != 0)
-            throw new FileError.NOENT("vibrator not found");
-
-        states = new List<VibratorState>();
-    }
-
-    public void turn_off()
-    {
-        vibrator.brightness = 0;
-        Memory.copy(vibrator.trigger, "none", 5);
-        vibrator.set();
-    }
-
-    public void push_state(VibratorState state)
-    {
-        states.prepend(state);
-        state.to_omhacks(ref vibrator);
-        vibrator.set();
-    }
-
-    public void pop_state(string name)
-    {
-        // Handle empty list
-        if (states == null) return;
-
-        // Track if the list head changed
-        weak List<VibratorState> old_top = states;
-
-        // Remove state "name" from the stack
-        for (weak List<VibratorState> i = states; i != null; i = i.next)
-            if (i.data.name == name)
-            {
-                states.delete_link(i);
-                break;
-            }
-
-        // If the list head changed, put into action the new top state
-        if (states != old_top)
-            if (states == null)
-                turn_off();
-            else {
-                // Activate the new top
-                states.data.to_omhacks(ref vibrator);
-                vibrator.set();
-            }
-    }
-
-    public void shutdown()
-    {
-        while (states != null)
-            states.delete_link(states);
-        turn_off();
-    }
-}
-
 public class Audio: zavai.Service
 {
     /*
@@ -154,11 +52,11 @@ public class Audio: zavai.Service
     public void on_alarm_trigger(clock.AlarmTriggerInfo info)
     {
         zavai.log.debug("Make noise for alarm");
-        if (vibrator != null)
+        if (zavai.led.vibrator != null)
         {
-            var state = new VibratorState("alarm");
+            var state = new zavai.led.LedState("alarm");
             state.set_blink(255);
-            vibrator.push_state(state);
+            zavai.led.vibrator.push_state(state);
         }
         soundplayer.play(config.ringtone_alarm, true);
     }
@@ -166,8 +64,8 @@ public class Audio: zavai.Service
     public void on_alarm_done(clock.AlarmTriggerInfo info)
     {
         zavai.log.debug("Stop noise for alarm");
-        if (vibrator != null)
-            vibrator.pop_state("alarm");
+        if (zavai.led.vibrator != null)
+            zavai.led.vibrator.pop_state("alarm");
         soundplayer.stop();
     }
 }
@@ -271,20 +169,12 @@ stderr.printf("Playing %s\n", uri);
     }
 }
 
-public Vibrator vibrator = null;
 public Audio audio = null;
 public Player musicplayer = null;
 public Player soundplayer = null;
 
 public void init()
 {
-    try {
-        vibrator = new Vibrator();
-        zavai.registry.register(vibrator);
-    } catch (Error e) {
-        zavai.log.info("No vibrator found");
-        vibrator = null;
-    }
     audio = new Audio();
     musicplayer = new Player();
     soundplayer = new Player();
diff --git a/src/leds.vala b/src/leds.vala
new file mode 100644 (file)
index 0000000..a089ef6
--- /dev/null
@@ -0,0 +1,226 @@
+/*
+ * leds - Leds/buttons support
+ *
+ * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+using GLib;
+
+namespace zavai {
+namespace led {
+
+public class LedState
+{
+    public string name;
+    public int brightness;
+    public string trigger;
+    public int delay_on;
+    public int delay_off;
+
+    public LedState(string name)
+    {
+        this.name = name;
+        set_constant(0);
+    }
+
+    public void set_constant(int power)
+    {
+        brightness = power;
+        trigger = "none";
+        delay_on = 0;
+        delay_off = 0;
+    }
+
+    public void set_blink(int power, int delay_on=200, int delay_off=300)
+    {
+        brightness = power;
+        trigger = "timer";
+        this.delay_on = delay_on;
+        this.delay_off = delay_off;
+    }
+
+    public void to_omhacks(ref Omhacks.Led led)
+    {
+        led.brightness = brightness;
+        Memory.copy(led.trigger, trigger, trigger.size());
+        led.delay_on = 200;
+        led.delay_off = 300;
+    }
+}
+
+public class Vibrator : zavai.Resource, Object
+{
+    protected Omhacks.Led vibrator;
+
+    protected List<LedState> states;
+
+    public Vibrator() throws FileError
+    {
+        if (vibrator.init("neo1973:vibrator") != 0)
+            throw new FileError.NOENT("vibrator not found");
+
+        states = new List<LedState>();
+    }
+
+    public void turn_off()
+    {
+        vibrator.brightness = 0;
+        Memory.copy(vibrator.trigger, "none", 5);
+        vibrator.set();
+    }
+
+    public void push_state(LedState state)
+    {
+        states.prepend(state);
+        state.to_omhacks(ref vibrator);
+        vibrator.set();
+    }
+
+    public void pop_state(string name)
+    {
+        // Handle empty list
+        if (states == null) return;
+
+        // Track if the list head changed
+        weak List<LedState> old_top = states;
+
+        // Remove state "name" from the stack
+        for (weak List<LedState> i = states; i != null; i = i.next)
+            if (i.data.name == name)
+            {
+                states.delete_link(i);
+                break;
+            }
+
+        // If the list head changed, put into action the new top state
+        if (states != old_top)
+            if (states == null)
+                turn_off();
+            else {
+                // Activate the new top
+                states.data.to_omhacks(ref vibrator);
+                vibrator.set();
+            }
+    }
+
+    public void shutdown()
+    {
+        while (states != null)
+            states.delete_link(states);
+        turn_off();
+    }
+}
+
+public class AUX: zavai.Service
+{
+    protected Omhacks.Led auxled;
+    protected bool has_aux;
+
+    protected weak clock.AlarmTriggerInfo current_alarm = null;
+
+    public AUX()
+    {
+        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);
+        input.hotkeys.request("auxbutton");
+    }
+
+    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 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 void init()
+{
+    aux = new AUX();
+    try {
+        vibrator = new Vibrator();
+        zavai.registry.register(vibrator);
+    } catch (Error e) {
+        zavai.log.info("No vibrator found");
+        vibrator = null;
+    }
+}
+
+}
+}
index b99d55140f9d05b67b69f8fe116bf44d41789b59..7e6f9fab221abf7f293b0c0bcd94a736e03e0ca8 100644 (file)
@@ -205,6 +205,7 @@ static int main (string[] args) {
        zavai.clock.init();
        zavai.audio.init();
        zavai.log.init();
+       zavai.led.init();
        zavai.wifi.init();
        zavai.bluetooth.init();
 
@@ -223,7 +224,6 @@ static int main (string[] args) {
        zavai.ui.gsm.init();
        zavai.config.find_and_run_script("display", "init");
        zavai.ui.power.init();
-       zavai.ui.aux.init();
        zavai.ui.kbd.init();
        zavai.ui.wm.init();
        zavai.ui.calendar.init();