Alternative BatteryIcon implementation
[gregoa/zavai.git] / src / power.vala
1 /*
2  * power - zavai power event handling
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace power {
25
26 class Power : Service
27 {
28     public enum State
29     {
30         UNKNOWN,
31         CHARGING,
32         DISCHARGING,
33         FULLY_CHARGED,
34     }
35
36     protected string battery_device;
37     protected uint timeout;
38     public signal void changed();
39     public State state;
40     public int percentage;
41
42     public Power()
43     {
44         battery_device = "";
45         timeout = 0;
46         state = State.UNKNOWN;
47         percentage = 0;
48     }
49
50     /// Activate the service
51     protected override void start()
52     {
53         if (started) return;
54
55         uint poll_time;
56         if (uevent.uevent != null)
57         {
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
61             // every 5 minutes
62             poll_time = 300 * 1000;
63         } else {
64             // Else poll every 30 seconds to be somehow reactive to plug/unplug
65             // events
66             poll_time = 30 * 1000;
67         }
68
69         // Poll battery every minute
70         timeout = Timeout.add(poll_time, on_timeout);
71
72         base.start();
73     }
74
75     /// Deactivate the service
76     protected override void stop()
77     {
78         if (!started) return;
79
80         // Stop polling
81         Source.remove(timeout);
82
83         if (uevent.uevent != null)
84         {
85             uevent.uevent.release("zavai.power");
86             uevent.uevent.event -= on_uevent;
87         }
88
89         base.stop();
90     }
91
92     bool on_timeout()
93     {
94         changed();
95         return true;
96     }
97
98     void on_uevent()
99     {
100         /* if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/ac"))
101         {
102             changed();
103         }
104         else */
105         if (uevent.uevent.event_data.buffer.has_prefix("change@/class/power_supply/battery"))
106         {
107             State new_state = State.UNKNOWN;
108             int new_percentage = 0;
109
110             Omhacks.UEvent.parse(ref uevent.uevent.event_data);
111             for (int i = 0; uevent.uevent.event_data.envp[i] != null; ++i)
112             {
113                 if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_STATUS="))
114                 {
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;
122                     else
123                         zavai.log.warning("Unknown state: " + uevent.uevent.event_data.envp[i]);
124                 }
125                 else if (uevent.uevent.event_data.envp[i].has_prefix("POWER_SUPPLY_CAPACITY="))
126                 {
127                     new_percentage = uevent.uevent.event_data.envp[i].offset(22).to_int();
128                 }
129             }
130             if (new_state != state || new_percentage != percentage)
131             {
132                 state = new_state;
133                 percentage = new_percentage;
134                 changed();
135             }
136         }
137     }
138 }
139
140 Power power;
141
142 public void init()
143 {
144     power = new Power();
145 }
146
147 }
148 }