~jezra/vattery/trunk

« back to all changes in this revision

Viewing changes to src/battery.vala

  • Committer: jezra
  • Date: 2011-06-03 04:00:05 UTC
  • Revision ID: jezra@jezra.net-20110603040005-h0hjsi4fw2t3mevi
separation of battery

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
this is a part of vattery
 
4
copyright 2011 jezra lickter http://www.jezra.net
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
*/
 
20
public enum BatteryDataType {
 
21
        PROC,
 
22
        SYS
 
23
}
 
24
 
 
25
public struct BatteryData {
 
26
    public int percent;
 
27
    public string state;
 
28
}
 
29
 
 
30
 
 
31
public class Battery {
 
32
        Regex bat_curr_regex;
 
33
        Regex bat_tot_regex;
 
34
        Regex ac_regex;
 
35
        int bat_percent;
 
36
        string ac_state;
 
37
        public BatteryDataType data_type;
 
38
        
 
39
        public Battery() {
 
40
                //create a regex to retrieve the current battery power
 
41
                try {
 
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);
 
45
                }
 
46
                //create a regex to retrieve the last full capacity of the battery
 
47
                try {
 
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);
 
51
                }
 
52
                //create a regex to detect the "charging" status
 
53
                try {
 
54
                        this.ac_regex = new Regex(".*charging.*:[ \t\n\r\f]*([a-z]*).*");
 
55
                } catch(GLib.RegexError error) {
 
56
                        stdout.printf(error.message);
 
57
                }
 
58
        }
 
59
        public BatteryData get_status( ) {
 
60
                BatteryData ret = BatteryData();
 
61
                //determine if we need to get sys or proc
 
62
                get_proc_status();
 
63
                ret.percent = this.bat_percent;
 
64
                ret.state = this.ac_state;
 
65
                return ret;
 
66
                
 
67
        }
 
68
        public void check_data_type() {
 
69
                
 
70
        }
 
71
        private bool get_proc_status() {
 
72
                string contents;
 
73
                MatchInfo match_info;
 
74
                string bat_curr="";
 
75
                string bat_total="";
 
76
                string ac_state="";
 
77
                int bats_current=0;
 
78
                int bats_total=0;
 
79
                int bats_percent;
 
80
                string state_file;
 
81
                string info_file;
 
82
                bool battery_data_found=false;
 
83
                string battery_root="/proc/acpi/battery/";
 
84
                //open the battery root for reading
 
85
                bool tloop = true;
 
86
                try {
 
87
                        //stdout.printf("reading %s\n", battery_root);
 
88
                        GLib.Dir dir = GLib.Dir.open(battery_root,0);
 
89
                        dir.rewind();
 
90
                        while (tloop) {
 
91
                                var name = dir.read_name();
 
92
                                if(name == null){
 
93
                                        tloop=false;
 
94
                                }else{
 
95
                                        //something was found
 
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) ){        
 
104
                                                        try{
 
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
 
110
                                                                        if (ac_state==""){
 
111
                                                                                this.ac_regex.match(contents,GLib.RegexMatchFlags.NEWLINE_ANY,out match_info);
 
112
                                                                                ac_state = match_info.fetch(1);
 
113
                                                                        }
 
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);
 
125
                                                                        }
 
126
                                                                }
 
127
                                                        }catch( GLib.FileError error){
 
128
                                                                //there is a chance that no files exist
 
129
                                                        }
 
130
                                                }
 
131
                                        }
 
132
                                }
 
133
                        }        
 
134
                }catch(Error e){
 
135
                        stdout.printf( e.message );
 
136
                }
 
137
 
 
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;
 
143
                
 
144
                }else{
 
145
                        this.bat_percent = 0;
 
146
                        this.ac_state = "NONE";
 
147
                }
 
148
                return true;
 
149
        }
 
150
}