Alternative BatteryIcon implementation
authorEnrico Zini <enrico@enricozini.org>
Fri, 18 Dec 2009 00:31:01 +0000 (00:31 +0000)
committerEnrico Zini <enrico@enricozini.org>
Fri, 18 Dec 2009 00:31:01 +0000 (00:31 +0000)
README
src/app_power.vala
src/power.vala

diff --git a/README b/README
index fe260845be7791da19175747cb36b1a8e5c3bae6..23f314bdd7abbe106b89e1c58f7132833a2b83f8 100644 (file)
--- a/README
+++ b/README
@@ -155,6 +155,7 @@ TODO list / wish list
  http://git.freesmartphone.org/?p=specs.git;a=blob_plain;f=html/index.html;hb=HEAD
 
  * Features to add:
+ - devkit-power conditional compilation
  - fisheyelist: compute the number of full focus items so that they fill always at leat 1/2 of the screen
  - fisheye list music player
     - player page with seek/skip controls
index 1eac76fd300baca733df9f3fdc435341e0a57cdb..33be3e93f4a51fb1fd232015e19723fdab91343b 100644 (file)
@@ -32,8 +32,8 @@ static long timediff(Posix.timeval* a, Posix.timeval* b)
 
 public class Power : zavai.Resource, Object
 {
-    public dynamic DBus.Object usage;
-    public dynamic DBus.Object gsm_device;
+    //public dynamic DBus.Object usage;
+    //public dynamic DBus.Object gsm_device;
     public bool screen_locked;
     private int screen_lock_fd;
     // Timestamp of the past power button pressed even (0 if the button has
@@ -60,6 +60,7 @@ public class Power : zavai.Resource, Object
         last_short_press.tv_usec = 0;
         button_press_timeout = 0;
 
+        /*
         usage = zavai.registry.sbus.get_object(
             "org.freesmartphone.ousaged",
             "/org/freesmartphone/Usage",
@@ -68,6 +69,7 @@ public class Power : zavai.Resource, Object
             "org.freesmartphone.ogsmd",
             "/org/freesmartphone/GSM/Device",
             "org.freesmartphone.Resource");
+        */
 
         zavai.input.power_button.power_button += on_power_button;
         zavai.input.power_button.request("zavai.ui.powerbutton.power");
@@ -281,6 +283,7 @@ public class Power : zavai.Resource, Object
     }
 }
 
+#if USE_DKP
 public class BatteryIcon : Gtk.StatusIcon
 {
     public Dkp.Device battery;
@@ -329,7 +332,79 @@ public class BatteryIcon : Gtk.StatusIcon
 
         set_from_file(name);
     }
+    public static List<BatteryIcon> create_icons()
+    {
+        List<BatteryIcon> battery_icons = new List<BatteryIcon>();
+
+        // Enumerate batteries
+        var c = new Dkp.Client();
+        unowned GLib.PtrArray devs = c.enumerate_devices();
+        for (int i = 0; i < devs.len; ++i)
+        {
+            Dkp.Device dev = (Dkp.Device)devs.pdata[i];
+            stderr.printf("Found new device %s\n", dev.native_path);
+            dev.print();
+            stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
+            if (!dev.is_rechargeable) continue;
+            var bi = new BatteryIcon(dev);
+            bi.set_visible(true);
+            battery_icons.append(bi);
+        }
+
+        return battery_icons;
+    }
 }
+#else
+public class BatteryIcon : Gtk.StatusIcon
+{
+    public BatteryIcon()
+    {
+        zavai.power.power.changed += on_changed;
+        zavai.power.power.request("zavai.ui.batteryicon");
+
+//      stderr.printf("New battery icon for %s online %s perc %f isrec %s tte %lld ttf %lld\n", dev.native_path, dev.online ? "yes" : "no", dev.percentage, dev.is_rechargeable ? "yes" : "no", dev.time_to_empty, dev.time_to_full);
+
+        update_icon();
+    }
+
+    private void on_changed()
+    {
+        update_icon();
+    }
+
+    protected void update_icon()
+    {
+        string name = zavai.config.icondir + "/battery/";
+
+//stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
+        int capacity = (int)Math.round(zavai.power.power.percentage/10);
+        switch (zavai.power.power.state)
+        {
+            case zavai.power.Power.State.CHARGING:
+                name += "%02d0_charging_500.png".printf(capacity);
+                break;
+            case zavai.power.Power.State.FULLY_CHARGED:
+                name += "100_charging_500.png";
+                break;
+            default:
+                name += "%02d0.png".printf(capacity);
+                break;
+        }
+
+//stderr.printf("Loading icon from %s\n", name);
+
+        set_from_file(name);
+    }
+    public static List<BatteryIcon> create_icons()
+    {
+        List<BatteryIcon> battery_icons = new List<BatteryIcon>();
+        var bi = new BatteryIcon();
+        battery_icons.append(bi);
+        bi.set_visible(true);
+        return battery_icons;
+    }
+}
+#endif
 
 public class ScreenLockButton : Gtk.Button
 {
@@ -608,30 +683,15 @@ public void init()
     power = new Power();
     backlight = new Backlight();
     brightness = new BrightnessAdjustment();
-    
-    try {
-        battery_icons = new List<BatteryIcon>();
-        // Enumerate batteries
-        var c = new Dkp.Client();
-        unowned GLib.PtrArray devs = c.enumerate_devices();
-        for (int i = 0; i < devs.len; ++i)
-        {
-            Dkp.Device dev = (Dkp.Device)devs.pdata[i];
-            stderr.printf("Found new device %s\n", dev.native_path);
-            dev.print();
-            stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
-            if (!dev.is_rechargeable) continue;
-            var bi = new BatteryIcon(dev);
-            bi.set_visible(true);
-            battery_icons.append(bi);
-        }
 
-        power_menu = new PowerMenu();
+    try {
+        battery_icons = BatteryIcon.create_icons();
     } catch (Error e) {
         stderr.printf("Creating power menu: %s\n", e.message);
         power_menu = null;
     }
-    
+    power_menu = new PowerMenu();
+
     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
     //tpm = new TogglePowerMenu();
     //zavai.registry.getmenu("menu.main").add_widget(tpm);
index 2e6edae55f8b899464026d705b9aa57e77ef9b4e..c70fd9b4f2d5fe6c3071dfa439f3dfc9e8523e71 100644 (file)
@@ -25,13 +25,26 @@ namespace power {
 
 class Power : Service
 {
+    public enum State
+    {
+        UNKNOWN,
+        CHARGING,
+        DISCHARGING,
+        FULLY_CHARGED,
+    }
+
     protected string battery_device;
     protected uint timeout;
+    public signal void changed();
+    public State state;
+    public int percentage;
 
     public Power()
     {
         battery_device = "";
         timeout = 0;
+        state = State.UNKNOWN;
+        percentage = 0;
     }
 
     /// Activate the service
@@ -78,12 +91,49 @@ class Power : Service
 
     bool on_timeout()
     {
+        changed();
         return true;
     }
 
     void on_uevent()
     {
-stderr.printf("EVENT %s\n", uevent.uevent.event_data.buffer);
+        /* if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/ac"))
+        {
+            changed();
+        }
+        else */
+        if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/battery"))
+        {
+            State new_state = State.UNKNOWN;
+            int new_percentage = 0;
+
+            Omhacks.UEvent.parse(ref uevent.uevent.event_data);
+            for (int i = 0; uevent.uevent.event_data.envp[i] != null; ++i)
+            {
+                if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_STATUS="))
+                {
+                    var val = uevent.uevent.event_data.envp[i].offset(20);
+                    if (val == "Charging")
+                        new_state = State.CHARGING;
+                    else if (val == "Discharging")
+                        new_state = State.DISCHARGING;
+                    else if (val == "Not charging")
+                        new_state = State.FULLY_CHARGED;
+                    else
+                        zavai.log.warning("Unknown state: " + uevent.uevent.event_data.envp[i]);
+                }
+                else if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_CAPACITY="))
+                {
+                    new_percentage = uevent.uevent.event_data.envp[i].offset(22).to_int();
+                }
+            }
+            if (new_state != state || new_percentage != percentage)
+            {
+                state = new_state;
+                percentage = new_percentage;
+                changed();
+            }
+        }
     }
 }