3
this is a part of vattery
4
copyright 2011 jezra lickter http://www.jezra.net
6
vattery 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 3 of the License, or
9
(at your option) any later version.
11
vattery 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 vattery. If not, see <http://www.gnu.org/licenses/>.
20
public enum BatteryDataType {
25
public struct BatteryData {
31
public class Battery {
37
public BatteryDataType data_type;
40
//create a regex to retrieve the current battery power
42
this.bat_curr_regex = new Regex(".*remaining.*:[ \t\n\r\f]*([0-9]*).*",GLib.RegexCompileFlags.CASELESS,RegexMatchFlags.NEWLINE_ANY);
43
}catch(GLib.RegexError error) {
44
stdout.printf(error.message);
46
//create a regex to retrieve the last full capacity of the battery
48
this.bat_tot_regex = new Regex(".*last full capacity:[ \t\n\r\f]*([0-9]*).*",GLib.RegexCompileFlags.CASELESS,RegexMatchFlags.NEWLINE_ANY);
49
} catch(GLib.RegexError error) {
50
stdout.printf(error.message);
52
//create a regex to detect the "charging" status
54
this.ac_regex = new Regex(".*charging.*:[ \t\n\r\f]*([a-z]*).*");
55
} catch(GLib.RegexError error) {
56
stdout.printf(error.message);
59
public BatteryData get_status( ) {
60
BatteryData ret = BatteryData();
61
//determine if we need to get sys or proc
63
ret.percent = this.bat_percent;
64
ret.state = this.ac_state;
68
public void check_data_type() {
71
private bool get_proc_status() {
82
bool battery_data_found=false;
83
string battery_root="/proc/acpi/battery/";
84
//open the battery root for reading
87
//stdout.printf("reading %s\n", battery_root);
88
GLib.Dir dir = GLib.Dir.open(battery_root,0);
91
var name = dir.read_name();
96
var check_path = "%s%s".printf(battery_root,name);
97
//is this a directory?
98
if(FileUtils.test(check_path,GLib.FileTest.IS_DIR) ) {
99
//this is a directory, pretend it is a battery
100
state_file = "%s/state".printf(check_path);
101
info_file = "%s/info".printf(check_path);
102
//does the state_file exist?
103
if(FileUtils.test(state_file,FileTest.EXISTS) ){
105
if ( FileUtils.get_contents(state_file, out contents) ) {
106
//regex to get current capacity
107
this.bat_curr_regex.match(contents,GLib.RegexMatchFlags.NEWLINE_ANY, out match_info);
108
bat_curr = match_info.fetch(1);
109
//regex to get the "charging" only if no ac state has been detected so far
111
this.ac_regex.match(contents,GLib.RegexMatchFlags.NEWLINE_ANY,out match_info);
112
ac_state = match_info.fetch(1);
114
//now check the info file to get the last recorded max capacity
115
if(FileUtils.get_contents(info_file, out contents) ) {
116
//regex to get total capacity
117
this.bat_tot_regex.match(contents,GLib.RegexMatchFlags.NEWLINE_ANY,out match_info);
118
bat_total = match_info.fetch(1);
119
//if we ever get this far, then data was found
120
battery_data_found=true;
121
//add this batteries current charge to the overall charge
122
bats_current+= int.parse(bat_curr);
123
//add this batteries current total to the overall total
124
bats_total+=int.parse(bat_total);
127
}catch( GLib.FileError error){
128
//there is a chance that no files exist
135
stdout.printf( e.message );
138
//did we find any data?
139
if(battery_data_found){
140
bats_percent = bats_current*100/bats_total;
141
this.bat_percent = bats_percent;
142
this.ac_state = ac_state;
145
this.bat_percent = 0;
146
this.ac_state = "NONE";