2 * power - zavai power event handling
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 protected string battery_device;
37 protected uint timeout;
38 public signal void changed();
40 public int percentage;
44 battery_device = "/sys/class/power_supply/battery/";
45 if (Posix.access(battery_device, Posix.R_OK) != 0)
46 battery_device = null;
48 state = State.UNKNOWN;
52 /// Activate the service
53 protected override void start()
58 if (uevent.uevent != null)
60 zavai.log.info("We have uevent: poll rarely");
61 uevent.uevent.event += on_uevent;
62 uevent.uevent.request("zavai.power");
63 // If we can get plug/unplug notifications, it's ok to just poll
65 poll_time = 300 * 1000;
67 if (battery_device == null)
69 zavai.log.warning("No battery device found that I know how to read: try building with devkit-power");
72 zavai.log.info("Polling battery device only");
73 // Else poll every 30 seconds to be somehow reactive to plug/unplug
75 poll_time = 30 * 1000;
81 // Poll battery every minute
83 timeout = Timeout.add(poll_time, on_timeout);
88 /// Deactivate the service
89 protected override void stop()
95 Source.remove(timeout);
97 if (uevent.uevent != null)
99 uevent.uevent.release("zavai.power");
100 uevent.uevent.event -= on_uevent;
106 protected void poll()
108 if (battery_device == null)
112 State new_state = State.UNKNOWN;
113 int new_percentage = 0;
115 FileStream state_fd = FileStream.open(battery_device + "/status", "r");
116 string val = state_fd.gets(buf);
117 if (val == "Charging")
118 new_state = State.CHARGING;
119 else if (val == "Discharging")
120 new_state = State.DISCHARGING;
121 else if (val == "Not charging")
122 new_state = State.FULLY_CHARGED;
124 FileStream cap_fd = FileStream.open(battery_device + "/capacity", "r");
125 val = cap_fd.gets(buf);
126 new_percentage = val.to_int();
128 if (new_state != state || new_percentage != percentage)
131 percentage = new_percentage;
136 protected bool on_timeout()
142 protected void on_uevent()
144 /* if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/ac"))
149 if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/battery"))
151 State new_state = State.UNKNOWN;
152 int new_percentage = 0;
154 Omhacks.UEvent.parse(ref uevent.uevent.event_data);
155 for (int i = 0; uevent.uevent.event_data.envp[i] != null; ++i)
157 if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_STATUS="))
159 var val = uevent.uevent.event_data.envp[i].offset(20);
160 if (val == "Charging")
161 new_state = State.CHARGING;
162 else if (val == "Discharging")
163 new_state = State.DISCHARGING;
164 else if (val == "Not charging")
165 new_state = State.FULLY_CHARGED;
167 zavai.log.warning("Unknown state: " + uevent.uevent.event_data.envp[i]);
169 else if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_CAPACITY="))
171 new_percentage = uevent.uevent.event_data.envp[i].offset(22).to_int();
174 if (new_state != state || new_percentage != percentage)
177 percentage = new_percentage;