moko-specific battery handling
authorEnrico Zini <enrico@enricozini.org>
Fri, 18 Dec 2009 00:45:01 +0000 (00:45 +0000)
committerEnrico Zini <enrico@enricozini.org>
Fri, 18 Dec 2009 00:45:01 +0000 (00:45 +0000)
src/power.vala

index c70fd9b4f2d5fe6c3071dfa439f3dfc9e8523e71..a2a19fc04fec68292529ee4dd22f410bc6fbed28 100644 (file)
@@ -41,7 +41,9 @@ class Power : Service
 
     public Power()
     {
-        battery_device = "";
+        battery_device = "/sys/class/power_supply/battery/";
+        if (Posix.access(battery_device, Posix.R_OK) != 0)
+            battery_device = null;
         timeout = 0;
         state = State.UNKNOWN;
         percentage = 0;
@@ -61,13 +63,22 @@ class Power : Service
             // every 5 minutes
             poll_time = 300 * 1000;
         } else {
-            // Else poll every 30 seconds to be somehow reactive to plug/unplug
-            // events
-            poll_time = 30 * 1000;
+            if (battery_device == null)
+            {
+                zavai.log.warning("No battery device found that I know how to read: try building with devkit-power");
+                poll_time = 0;
+            } else {
+                // Else poll every 30 seconds to be somehow reactive to plug/unplug
+                // events
+                poll_time = 30 * 1000;
+            }
         }
 
+        poll();
+
         // Poll battery every minute
-        timeout = Timeout.add(poll_time, on_timeout);
+        if (poll_time != 0)
+            timeout = Timeout.add(poll_time, on_timeout);
 
         base.start();
     }
@@ -78,7 +89,8 @@ class Power : Service
         if (!started) return;
 
         // Stop polling
-        Source.remove(timeout);
+        if (timeout != 0)
+            Source.remove(timeout);
 
         if (uevent.uevent != null)
         {
@@ -89,13 +101,43 @@ class Power : Service
         base.stop();
     }
 
-    bool on_timeout()
+    protected void poll()
+    {
+        if (battery_device == null)
+            return;
+
+        char buf[200];
+        State new_state = State.UNKNOWN;
+        int new_percentage = 0;
+
+        FileStream state_fd = FileStream.open(battery_device + "/status", "r");
+        string val = state_fd.gets(buf);
+        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;
+
+        FileStream cap_fd = FileStream.open(battery_device + "/capacity", "r");
+        val = cap_fd.gets(buf);
+        new_percentage = val.to_int();
+
+        if (new_state != state || new_percentage != percentage)
+        {
+            state = new_state;
+            percentage = new_percentage;
+            changed();
+        }
+    }
+
+    protected bool on_timeout()
     {
-        changed();
+        poll();
         return true;
     }
 
-    void on_uevent()
+    protected void on_uevent()
     {
         /* if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/ac"))
         {