~cairo-dock-team/ubuntu/precise/cairo-dock-plug-ins/3.0.0.0rc1

« back to all changes in this revision

Viewing changes to powermanager/src/powermanager-sys-class.c

  • Committer: Kees Cook
  • Date: 2011-08-11 23:17:39 UTC
  • mfrom: (20.1.1 cairo-dock-plug-ins)
  • Revision ID: kees@outflux.net-20110811231739-cteedan51tmdg77v
Tags: 2.4.0~0beta2-0ubuntu1
releasing version 2.4.0~0beta2-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* 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, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include <math.h>
 
21
#include <string.h>
 
22
#include <dirent.h>
 
23
#include <dbus/dbus-glib.h>
 
24
 
 
25
#include "powermanager-draw.h"
 
26
#include "powermanager-struct.h"
 
27
#include "powermanager-common.h"
 
28
#include "powermanager-sys-class.h"
 
29
 
 
30
#define CD_BATTERY_DIR "/sys/class/power_supply"
 
31
 
 
32
/*POWER_SUPPLY_NAME=BAT1
 
33
POWER_SUPPLY_STATUS=Discharging
 
34
POWER_SUPPLY_PRESENT=1
 
35
POWER_SUPPLY_TECHNOLOGY=Li-ion
 
36
POWER_SUPPLY_CYCLE_COUNT=0
 
37
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
 
38
POWER_SUPPLY_VOLTAGE_NOW=11100000
 
39
POWER_SUPPLY_CURRENT_NOW=0
 
40
POWER_SUPPLY_CHARGE_FULL_DESIGN=2200000
 
41
POWER_SUPPLY_CHARGE_FULL=2200000
 
42
POWER_SUPPLY_CHARGE_NOW=1760000
 
43
POWER_SUPPLY_MODEL_NAME=
 
44
POWER_SUPPLY_MANUFACTURER=DELL
 
45
POWER_SUPPLY_SERIAL_NUMBER=11
 
46
*/
 
47
 
 
48
static gboolean _find_battery_in_dir (const gchar *cBatteryPath)
 
49
{
 
50
        // open the folder containing battery data.
 
51
        GDir *dir = g_dir_open (cBatteryPath, 0, NULL);
 
52
        if (dir == NULL)
 
53
        {
 
54
                cd_debug ("powermanager: no battery in %s",cBatteryPath );
 
55
                return FALSE;
 
56
        }
 
57
        
 
58
        // parse the folder and search the battery files.
 
59
        GString *sBatteryInfoFilePath = g_string_new ("");
 
60
        gchar *cContent = NULL, *cPresentLine;
 
61
        gsize length=0;
 
62
        const gchar *cBatteryName;
 
63
        gboolean bBatteryFound = FALSE;
 
64
        do
 
65
        {
 
66
                cBatteryName = g_dir_read_name (dir);  // usually "BAT0" or "BAT1".
 
67
                if (cBatteryName == NULL)
 
68
                        break ;
 
69
                
 
70
                // check the battery type.
 
71
                g_string_printf (sBatteryInfoFilePath, "%s/%s/type", cBatteryPath, cBatteryName);
 
72
                length=0;
 
73
                cd_debug ("  examen de la batterie '%s' ...", sBatteryInfoFilePath->str);
 
74
                g_file_get_contents (sBatteryInfoFilePath->str, &cContent, &length, NULL);
 
75
                if (cContent != NULL && strncmp (cContent, "Battery", 7) == 0)  // there may be a \n at the end, so let's ignore it.
 
76
                {
 
77
                        myData.cBatteryStateFilePath = g_strdup_printf ("%s/%s/uevent", cBatteryPath, cBatteryName);
 
78
                        bBatteryFound = TRUE;  // get the capacity when we read the uevent for the first time.
 
79
                        cd_debug ("  myData.cBatteryStateFilePath: %s", myData.cBatteryStateFilePath);
 
80
                }
 
81
                g_free (cContent);
 
82
        }
 
83
        while (! bBatteryFound);
 
84
        g_dir_close (dir);
 
85
        return bBatteryFound;
 
86
}
 
87
gboolean cd_find_battery_sys_class (void)
 
88
{
 
89
        gboolean bBatteryFound = _find_battery_in_dir (CD_BATTERY_DIR);
 
90
        return bBatteryFound;
 
91
}
 
92
 
 
93
#define _get_static_value(s, x) \
 
94
str = strstr (cContent, s);\
 
95
if (str) { \
 
96
        str += strlen(s)+1;\
 
97
        cr = strchr (str, '\n');\
 
98
        if (cr) x = g_strndup (str, cr - str);\
 
99
        else x = g_strdup (str); }
 
100
 
 
101
gboolean cd_get_stats_from_sys_class (void)
 
102
{
 
103
        //\_______________ get the content of the stats file.
 
104
        gchar *cContent = NULL;
 
105
        gsize length=0;
 
106
        GError *erreur = NULL;
 
107
        g_file_get_contents (myData.cBatteryStateFilePath, &cContent, &length, &erreur);
 
108
        if (erreur != NULL)
 
109
        {
 
110
                cd_warning ("powermanager : %s", erreur->message);
 
111
                g_error_free(erreur);
 
112
                erreur = NULL;
 
113
                return FALSE;
 
114
        }
 
115
        g_return_val_if_fail (cContent != NULL, FALSE);
 
116
        
 
117
        int k;
 
118
        
 
119
        //\_______________ check 'on battery' state.
 
120
        gchar *str = strstr (cContent, "STATUS");
 
121
        g_return_val_if_fail (str != NULL, FALSE);
 
122
        str += 7;
 
123
        gboolean bOnBattery = (*str == 'D');  // "Discharging"
 
124
        if (bOnBattery != myData.bOnBattery)  // state changed
 
125
        {
 
126
                /**for (k = 0; k < PM_NB_VALUES; k ++)  // reset the history.
 
127
                        myData.fRateHistory[k] = 0;
 
128
                myData.iCurrentIndex = 0;
 
129
                myData.iIndexMax = 0;*/
 
130
                myData.iStatPercentageBegin = 0;
 
131
                myData.iStatPercentage = 0;
 
132
                myData.bOnBattery = bOnBattery;
 
133
        }
 
134
        
 
135
        //\_______________ check the battery presence.
 
136
        str = strstr (cContent, "PRESENT");
 
137
        g_return_val_if_fail (str != NULL, FALSE);
 
138
        str += 8;
 
139
        gboolean bBatteryPresent = (*str == '1');
 
140
        if (bBatteryPresent != myData.bBatteryPresent)  // the battery has just been inserted/removed. 
 
141
        {
 
142
                myData.bBatteryPresent = bBatteryPresent;
 
143
                if (! bBatteryPresent)  // if the battery has been removed, we are obviously on the sector.
 
144
                {
 
145
                        cd_debug ("la batterie a ete enlevee\n");
 
146
                        myData.bOnBattery = FALSE;
 
147
                        update_icon();
 
148
                        g_free (cContent);
 
149
                        return TRUE;
 
150
                }
 
151
                
 
152
                // reset the history.
 
153
                cd_debug ("la batterie a ete connectee");
 
154
                myData.iPrevTime = 0;
 
155
                myData.iPrevPercentage = 0;
 
156
                /**for (k = 0; k < PM_NB_VALUES; k ++)
 
157
                        myData.fRateHistory[k] = 0;
 
158
                myData.iCurrentIndex = 0;
 
159
                myData.iIndexMax = 0;*/
 
160
                myData.iStatPercentageBegin = 0;
 
161
                myData.iStatPercentage = 0;
 
162
        }
 
163
        
 
164
        //\_______________ get the current charge.
 
165
        if (myData.iCapacity == 0)  // not yet got
 
166
        {
 
167
                str = strstr (cContent, "CHARGE_FULL=");
 
168
                g_return_val_if_fail (str != NULL, FALSE);
 
169
                str += 12;
 
170
                myData.iCapacity = atoi (str);
 
171
                g_return_val_if_fail (myData.iCapacity != 0, FALSE);
 
172
                // also get few other static params.
 
173
                gchar *cr;
 
174
                _get_static_value ("TECHNOLOGY", myData.cTechnology)
 
175
                _get_static_value ("MANUFACTURER", myData.cVendor)
 
176
                _get_static_value ("MODEL_NAME", myData.cModel)
 
177
                str = strstr (cContent, "FULL_DESIGN");
 
178
                if (str)
 
179
                {
 
180
                        str += 12;
 
181
                        int iMaxCapacity = atoi (str);
 
182
                        if (iMaxCapacity != 0)
 
183
                                myData.fMaxAvailableCapacity = 100. * myData.iCapacity / iMaxCapacity;
 
184
                }
 
185
        }
 
186
        
 
187
        str = strstr (cContent, "CHARGE_NOW");
 
188
        g_return_val_if_fail (str != NULL, FALSE);
 
189
        str += 11;
 
190
        int iRemainingCapacity = atoi (str);
 
191
        
 
192
        myData.iPercentage = 100. * iRemainingCapacity / myData.iCapacity;
 
193
        cd_debug ("myData.iPercentage : %.2f%% (%d / %d)", (double)myData.iPercentage, iRemainingCapacity, myData.iCapacity);
 
194
        if (myData.iPercentage > 100)
 
195
                myData.iPercentage = 100;
 
196
        if (myData.iPercentage < 0)
 
197
                myData.iPercentage = 0.;
 
198
        
 
199
        //\_______________ now compute the time.
 
200
        myData.iTime = cd_estimate_time ();
 
201
        
 
202
        g_free (cContent);
 
203
        return (TRUE);
 
204
}