Battery status via devicekit-power
authorEnrico Zini <enrico@enricozini.org>
Wed, 30 Sep 2009 13:04:06 +0000 (14:04 +0100)
committerEnrico Zini <enrico@enricozini.org>
Wed, 30 Sep 2009 13:04:06 +0000 (14:04 +0100)
README
configure.ac
src/Makefile.am
src/app_power.vala
src/devkit-power-gobject.vapi

diff --git a/README b/README
index d4fe7487a3526c14d221430b3f8c76de0cad1999..016c69ba08c7847fb3412d21690730f678352990 100644 (file)
--- a/README
+++ b/README
@@ -11,9 +11,14 @@ Getting it to work:
 
  * Install dependencies
 
-   apt-get install valac libglib2.0-dev libdbus-glib-1-dev libgee-dev libgtk2.0-dev libwnck-dev
+   # Build time
+   apt-get install valac libglib2.0-dev libdbus-glib-1-dev libgee-dev libgtk2.0-dev libwnck-dev libdevkit-power-gobject-dev
 
-   Optional: apt-get install polygen polygen-data
+   # Runtime
+   apt-get install devicekit-power
+
+   # Optional runtime
+   apt-get install polygen polygen-data
 
  * Compile
 
index cb545ae0a420487738d1c8d389658da9bd687a81..188dcd78a73e4a5ebc26ced16da2a32ca5468453 100644 (file)
@@ -39,6 +39,10 @@ PKG_CHECK_MODULES(WNCK, libwnck-1.0 >= 2.26.0)
 AC_SUBST(WNCK_CFLAGS)
 AC_SUBST(WNCK_LIBS)
 
+PKG_CHECK_MODULES(DEVKITPOWER, devkit-power-gobject >= 010)
+AC_SUBST(DEVKITPOWER_CFLAGS)
+AC_SUBST(DEVKITPOWER_LIBS)
+
 AC_CONFIG_FILES([Makefile
            src/Makefile])
 
index b6a6c182d22fc244d1ff95ee696f2c4dae107deb..651ad4b2ed68b5e0653f88efc56bce5c5006da1d 100644 (file)
@@ -1,4 +1,4 @@
-VFLAGS=-g --pkg gee-1.0 --pkg dbus-glib-1 --pkg gtk+-2.0 --pkg posix --pkg libwnck-1.0 --vapidir=. --pkg linux-input --pkg dbus-extra
+VFLAGS=-g --pkg gee-1.0 --pkg dbus-glib-1 --pkg gtk+-2.0 --pkg posix --pkg libwnck-1.0 --vapidir=. --pkg linux-input --pkg dbus-extra --pkg devkit-power-gobject
 
 #zavai: $(SOURCES)
 #      valac -o zavai $(VFLAGS) $^
@@ -20,7 +20,9 @@ AM_CPPFLAGS = \
        $(GEE_CFLAGS) \
        $(GTK_CFLAGS) \
        $(WNCK_CFLAGS) \
-       -DWNCK_I_KNOW_THIS_IS_UNSTABLE
+       $(DEVKITPOWER_CFLAGS) \
+       -DWNCK_I_KNOW_THIS_IS_UNSTABLE \
+       -DI_KNOW_THE_DEVICEKIT_POWER_API_IS_SUBJECT_TO_CHANGE
 
 BUILT_SOURCES = zavai.vala.stamp $(zavai_VALASOURCES:.vala=.c)
 
@@ -64,7 +66,8 @@ zavai_LDADD = \
        $(DBUS_LIBS) \
        $(GEE_LIBS) \
        $(GTK_LIBS) \
-       $(WNCK_LIBS)
+       $(WNCK_LIBS) \
+       $(DEVKITPOWER_LIBS)
 
 #vapidir = $(datadir)/vala/vapi
 
index 156320e0e02f212bf70e3bc66ae55c29b4f07663..eb9687fd92f84c4a46345337b0d1a65131bdd761 100644 (file)
@@ -232,28 +232,42 @@ public class Power : zavai.Resource, Object
 
 public class BatteryIcon : Gtk.StatusIcon
 {
-       public dynamic DBus.Object battery;
-       protected string last_status;
-       protected int last_capacity;
+       //public dynamic DBus.Object battery;
+       public Dkp.Device battery;
 
-       public BatteryIcon()
+       public BatteryIcon(Dkp.Device dev)
        {
+               battery = dev;
+               battery.changed += on_changed;
+
+               stderr.printf("DEV %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);
+
+               /*
                battery = zavai.registry.sbus.get_object(
                        "org.freesmartphone.odeviced",
                        "/org/freesmartphone/Device/PowerSupply/battery",
                        "org.freesmartphone.Device.PowerSupply");
+               */
 
                // activate += on_activate;
 
+               /*
                battery.PowerStatus += on_power_status;
                battery.Capacity += on_capacity;
 
                last_status = battery.GetPowerStatus();
                last_capacity = battery.GetCapacity();
+               */
                
                update_icon();
        }
 
+       private void on_changed(void* obj)
+       {
+               update_icon();
+       }
+
+       /*
        private void on_power_status(dynamic DBus.Object bat, string status)
        {
                zavai.log.info("New battery status: " + status);
@@ -267,6 +281,7 @@ stderr.printf("NEW CAPACITY: %d\n", val);
                last_capacity = val;
                update_icon();
        }
+       */
 
        /*
        private void on_activate()
@@ -277,11 +292,28 @@ stderr.printf("NEW CAPACITY: %d\n", val);
        protected void update_icon()
        {
                string name = zavai.config.icondir + "/battery/";
+               bool charging = false;
+               Dkp.DeviceState state = (Dkp.DeviceState)battery.state;
 
-               if (last_status == "charging")
-                       name += "%02d0_charging_500.png".printf(last_capacity/10);
-               else
-                       name += "%02d0.png".printf(last_capacity/10);
+stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
+               int capacity = (int)Math.round(battery.percentage/10);
+               switch (state)
+               {
+                       case Dkp.DeviceState.CHARGING:
+                               name += "%02d0_charging_500.png".printf(capacity);
+                               break;
+                       case Dkp.DeviceState.FULLY_CHARGED:
+                               name += "100_charging_500.png";
+                               break;
+                       case Dkp.DeviceState.UNKNOWN:
+                       case Dkp.DeviceState.DISCHARGING:
+                       case Dkp.DeviceState.EMPTY:
+                       case Dkp.DeviceState.PENDING_CHARGE:
+                       case Dkp.DeviceState.PENDING_DISCHARGE:
+                       case Dkp.DeviceState.LAST:
+                               name += "%02d0.png".printf(capacity);
+                               break;
+               }
 
 stderr.printf("Loading icon from %s\n", name);
 
@@ -545,7 +577,7 @@ public class TogglePowerMenu : Gtk.Button
 
 Power power;
 PowerMenu power_menu;
-BatteryIcon battery_icon;
+Gee.ArrayList<BatteryIcon> battery_icons;
 Backlight backlight;
 //TogglePowerMenu tpm;
 
@@ -554,9 +586,19 @@ public void init()
        power = new Power();
        backlight = new Backlight();
        zavai.registry.register_service(backlight);
-
-       battery_icon = new BatteryIcon();
-       battery_icon.set_visible(true);
+       
+       battery_icons = new Gee.ArrayList<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];
+               if (!dev.is_rechargeable) continue;
+               var bi = new BatteryIcon(dev);
+               bi.set_visible(true);
+               battery_icons.add(bi);
+       }
 
        power_menu = new PowerMenu();
        zavai.registry.register_resource("powermenu", power_menu);
index 80a7a75f84ba43778853db65ea83082ebf83a65a..139abc2ac6b21e33ac43cf59e240d5176c2face4 100644 (file)
@@ -2,7 +2,7 @@
 
 [CCode (cprefix = "Dkp", lower_case_cprefix = "dkp_")]
 namespace Dkp {
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class Client : GLib.Object {
                [CCode (has_construct_function = false)]
                public Client ();
@@ -38,7 +38,7 @@ namespace Dkp {
                public virtual signal void device_changed (void* device);
                public virtual signal void device_removed (void* device);
        }
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class Device : GLib.Object {
                [CCode (has_construct_function = false)]
                public Device ();
@@ -111,7 +111,7 @@ namespace Dkp {
                public virtual signal void changed (void* obj);
        }
        [Compact]
-       [CCode (copy_function = "dkp_history_obj_copy", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (copy_function = "dkp_history_obj_copy", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class HistoryObj {
                public Dkp.DeviceState state;
                public uint time;
@@ -127,7 +127,7 @@ namespace Dkp {
                public unowned string to_string ();
        }
        [Compact]
-       [CCode (copy_function = "dkp_qos_obj_copy", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (copy_function = "dkp_qos_obj_copy", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class QosObj {
                public weak string cmdline;
                public uint cookie;
@@ -145,7 +145,7 @@ namespace Dkp {
                public bool print ();
        }
        [Compact]
-       [CCode (copy_function = "dkp_stats_obj_copy", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (copy_function = "dkp_stats_obj_copy", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class StatsObj {
                public double accuracy;
                public double value;
@@ -156,7 +156,7 @@ namespace Dkp {
                public static unowned Dkp.StatsObj from_string (string text);
                public unowned string to_string ();
        }
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class Wakeups : GLib.Object {
                [CCode (has_construct_function = false)]
                public Wakeups ();
@@ -167,7 +167,7 @@ namespace Dkp {
                public virtual signal void total_changed (uint value);
        }
        [Compact]
-       [CCode (copy_function = "dkp_wakeups_obj_copy", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (copy_function = "dkp_wakeups_obj_copy", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public class WakeupsObj {
                public weak string cmdline;
                public weak string details;
@@ -181,7 +181,7 @@ namespace Dkp {
                public bool equal (Dkp.WakeupsObj obj2);
                public bool print ();
        }
-       [CCode (cprefix = "DKP_DEVICE_STATE_", has_type_id = "0", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cprefix = "DKP_DEVICE_STATE_", has_type_id = "0", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public enum DeviceState {
                UNKNOWN,
                CHARGING,
@@ -192,7 +192,7 @@ namespace Dkp {
                PENDING_DISCHARGE,
                LAST
        }
-       [CCode (cprefix = "DKP_DEVICE_TECHNOLOGY_", has_type_id = "0", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cprefix = "DKP_DEVICE_TECHNOLOGY_", has_type_id = "0", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public enum DeviceTechnology {
                UNKNOWN,
                LITHIUM_ION,
@@ -203,7 +203,7 @@ namespace Dkp {
                NICKEL_METAL_HYDRIDE,
                LAST
        }
-       [CCode (cprefix = "DKP_DEVICE_TYPE_", has_type_id = "0", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cprefix = "DKP_DEVICE_TYPE_", has_type_id = "0", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public enum DeviceType {
                UNKNOWN,
                LINE_POWER,
@@ -216,17 +216,17 @@ namespace Dkp {
                PHONE,
                LAST
        }
-       [CCode (cprefix = "DKP_QOS_TYPE_", has_type_id = "0", cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cprefix = "DKP_QOS_TYPE_", has_type_id = "0", cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public enum QosType {
                UNKNOWN,
                NETWORK,
                CPU_DMA,
                LAST
        }
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public const int COMPILE_VERSION;
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public static Dkp.QosType qos_type_from_text (string type);
-       [CCode (cheader_filename = "devkit-power-gobject.h")]
+       [CCode (cheader_filename = "devkit-power-gobject/devicekit-power.h")]
        public static unowned string qos_type_to_text (Dkp.QosType type);
 }