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;
46 state = State.UNKNOWN;
50 /// Activate the service
51 protected override void start()
56 if (uevent.uevent != null)
58 uevent.uevent.event += on_uevent;
59 uevent.uevent.request("zavai.power");
60 // If we can get plug/unplug notifications, it's ok to just poll
62 poll_time = 300 * 1000;
64 // Else poll every 30 seconds to be somehow reactive to plug/unplug
66 poll_time = 30 * 1000;
69 // Poll battery every minute
70 timeout = Timeout.add(poll_time, on_timeout);
75 /// Deactivate the service
76 protected override void stop()
81 Source.remove(timeout);
83 if (uevent.uevent != null)
85 uevent.uevent.release("zavai.power");
86 uevent.uevent.event -= on_uevent;
100 /* if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/ac"))
105 if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/battery"))
107 State new_state = State.UNKNOWN;
108 int new_percentage = 0;
110 Omhacks.UEvent.parse(ref uevent.uevent.event_data);
111 for (int i = 0; uevent.uevent.event_data.envp[i] != null; ++i)
113 if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_STATUS="))
115 var val = uevent.uevent.event_data.envp[i].offset(20);
116 if (val == "Charging")
117 new_state = State.CHARGING;
118 else if (val == "Discharging")
119 new_state = State.DISCHARGING;
120 else if (val == "Not charging")
121 new_state = State.FULLY_CHARGED;
123 zavai.log.warning("Unknown state: " + uevent.uevent.event_data.envp[i]);
125 else if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_CAPACITY="))
127 new_percentage = uevent.uevent.event_data.envp[i].offset(22).to_int();
130 if (new_state != state || new_percentage != percentage)
133 percentage = new_percentage;