~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-proc-acpi.c

  • Committer: Matthieu Baerts
  • Date: 2011-08-09 19:51:45 UTC
  • mfrom: (1.1.12 upstream)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: matttbe@gmail.com-20110809195145-6t55jtykipujuyn5
* New upstream release. (LP: #823514)
* Upstream (short) ChangeLog:
 - The Power-Manager applet has been rewritten to work on any plateform.
 - Compiz-icon applet has been replaced by Composite-Manager.
 - Improved integration in the XFCE, LXDE and GNOME3 desktop.
 - Logout is now working even if there is no/an unknow session manager.
 - Several new DBus methods lets you interact on the dock more easily.
 - Added the possibility to shutdown/logout with GMenu.
 - Improved Status-Notifier and added its own watcher.
 - Added a new applet: Impulse (still need testing).
 - Fixed a few bugs as usual.
* debian/patches:
 - Removed all patches (they was already unapplied).
* debian/copyright:
 - Updated with new files/directory.
* debian/cairo-dock-plug-ins.install:
 - Added status-notifier-watcher used by Status-Notifier applet.
* debian/control:
 - Updated the version of cairo-dock-[core;data;dev].
 - Bump Standard-Version to 3.9.2.
 - Added fftw3, pulse (Impulse) and upower-glib (Logout)
    as new dependences for the build process.
 - Added indicator-[me;messages] as new recommendations
    for cairo-dock-plug-ins (needed for [Me;Messaging]Menu applets).
 - Removed gawk from the build dependences list (no longer needed).
* cairo-dock-plug-ins package needs to be recompiled (LP: #811579)

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-proc-acpi.h"
 
29
 
 
30
#define CD_BATTERY_DIR "/proc/acpi/battery"
 
31
//#define CD_BATTERY_DIR "/home/fab/proc/acpi/battery"
 
32
 
 
33
/*present: yes
 
34
capacity state: ok
 
35
charging state: discharging
 
36
present rate: 15000 mW
 
37
remaining capacity: 47040 mWh
 
38
present voltage: 15000 mV*/
 
39
 
 
40
static gboolean _find_battery_in_dir (const gchar *cBatteryPath)
 
41
{
 
42
        // open the folder containing battery data.
 
43
        GDir *dir = g_dir_open (cBatteryPath, 0, NULL);
 
44
        if (dir == NULL)
 
45
        {
 
46
                cd_debug ("powermanager: no battery in %s",cBatteryPath );
 
47
                return FALSE;
 
48
        }
 
49
        
 
50
        // parse the folder and search the battery files.
 
51
        GString *sBatteryInfoFilePath = g_string_new ("");
 
52
        gchar *cContent = NULL, *cPresentLine;
 
53
        gsize length=0;
 
54
        const gchar *cBatteryName;
 
55
        gboolean bBatteryFound = FALSE;
 
56
        do
 
57
        {
 
58
                cBatteryName = g_dir_read_name (dir);  // usually "BAT0".
 
59
                if (cBatteryName == NULL)
 
60
                        break ;
 
61
                
 
62
                // check the battery info.
 
63
                g_string_printf (sBatteryInfoFilePath, "%s/%s/info", cBatteryPath, cBatteryName);
 
64
                length=0;
 
65
                cd_debug ("  examen de la batterie '%s' ...", sBatteryInfoFilePath->str);
 
66
                g_file_get_contents (sBatteryInfoFilePath->str, &cContent, &length, NULL);
 
67
                if (cContent != NULL)
 
68
                {
 
69
                        gchar *str = strchr (cContent, '\n');  // first line: "present:    yes"
 
70
                        if (str != NULL)
 
71
                        {
 
72
                                gchar *str2 = strchr (str+1, ':');
 
73
                                if (str2 != NULL)
 
74
                                {
 
75
                                        str2 ++;
 
76
                                        myData.iCapacity = atoi (str2);
 
77
                                        
 
78
                                        gchar *str3 = strchr (str2, ':');
 
79
                                        if (str3 != NULL)  // prefer the last full capacity if available.
 
80
                                        {
 
81
                                                str3 ++;
 
82
                                                myData.iCapacity = atoi (str3);
 
83
                                        }
 
84
                                        
 
85
                                        cd_debug ("Capacity : %d mWsh\n", myData.iCapacity);
 
86
                                        myData.cBatteryStateFilePath = g_strdup_printf ("%s/%s/state", cBatteryPath, cBatteryName);
 
87
                                        bBatteryFound = TRUE;
 
88
                                }
 
89
                        }
 
90
                        g_free (cContent);
 
91
                }
 
92
        }
 
93
        while (! bBatteryFound);
 
94
        g_dir_close (dir);
 
95
        return bBatteryFound;
 
96
}
 
97
gboolean cd_find_battery_proc_acpi (void)
 
98
{
 
99
        gboolean bBatteryFound = _find_battery_in_dir (CD_BATTERY_DIR);
 
100
        return bBatteryFound;
 
101
}
 
102
 
 
103
 
 
104
#define go_to_next_line \
 
105
        cCurLine = strchr (cCurVal, '\n'); \
 
106
        g_return_val_if_fail (cCurLine != NULL, FALSE); \
 
107
        cCurLine ++; \
 
108
        cCurVal = cCurLine;
 
109
 
 
110
#define jump_to_value \
 
111
        cCurVal = strchr (cCurLine, ':'); \
 
112
        g_return_val_if_fail (cCurVal != NULL, FALSE); \
 
113
        cCurVal ++; \
 
114
        while (*cCurVal == ' ') \
 
115
                cCurVal ++;
 
116
 
 
117
gboolean cd_get_stats_from_proc_acpi (void)
 
118
{
 
119
        //\_______________ get the content of the stats file.
 
120
        gchar *cContent = NULL;
 
121
        gsize length=0;
 
122
        GError *erreur = NULL;
 
123
        g_file_get_contents (myData.cBatteryStateFilePath, &cContent, &length, &erreur);
 
124
        if (erreur != NULL)
 
125
        {
 
126
                cd_warning ("powermanager : %s", erreur->message);
 
127
                g_error_free(erreur);
 
128
                erreur = NULL;
 
129
                return FALSE;
 
130
        }
 
131
        g_return_val_if_fail (cContent != NULL, FALSE);
 
132
        
 
133
        int k;
 
134
        gchar *cCurLine = cContent, *cCurVal = cContent;
 
135
        
 
136
        //\_______________ check the battery presence.
 
137
        jump_to_value  // "present: yes"
 
138
        gboolean bBatteryPresent = (*cCurVal == 'y');
 
139
        if (bBatteryPresent != myData.bBatteryPresent)  // the battery has just been inserted/removed. 
 
140
        {
 
141
                myData.bBatteryPresent = bBatteryPresent;
 
142
                if (! bBatteryPresent)  // if the battery has been removed, we are obviously on the sector.
 
143
                {
 
144
                        cd_debug ("la batterie a ete enlevee\n");
 
145
                        myData.bOnBattery = FALSE;
 
146
                        update_icon();
 
147
                        g_free (cContent);
 
148
                        return TRUE;
 
149
                }
 
150
                
 
151
                // reset the history.
 
152
                cd_debug ("la batterie a ete connectee\n");
 
153
                myData.iPrevTime = 0;
 
154
                myData.iPrevPercentage = 0;
 
155
                /**for (k = 0; k < PM_NB_VALUES; k ++)
 
156
                        myData.fRateHistory[k] = 0;
 
157
                myData.iCurrentIndex = 0;
 
158
                myData.iIndexMax = 0;*/
 
159
                myData.iStatPercentageBegin = 0;
 
160
                myData.iStatPercentage = 0;
 
161
        }
 
162
        
 
163
        go_to_next_line  // -> "capacity state: ok"
 
164
        
 
165
        go_to_next_line  // -> "charging state: discharging"
 
166
        
 
167
        //\_______________ check 'on battery' state.
 
168
        jump_to_value
 
169
        gboolean bOnBattery = (*cCurVal == 'd');  // "discharging"
 
170
        if (bOnBattery != myData.bOnBattery)  // state changed
 
171
        {
 
172
                /**for (k = 0; k < PM_NB_VALUES; k ++)  // reset the history.
 
173
                        myData.fRateHistory[k] = 0;
 
174
                myData.iCurrentIndex = 0;
 
175
                myData.iIndexMax = 0;*/
 
176
                myData.iStatPercentageBegin = 0;
 
177
                myData.iStatPercentage = 0;
 
178
                myData.bOnBattery = bOnBattery;
 
179
        }
 
180
        
 
181
        go_to_next_line  // -> present rate: 15000 mW
 
182
        
 
183
        //\_______________ get the current charge and rate (this one can be 0 if not available).
 
184
        jump_to_value
 
185
        double fPresentRate = atoi (cCurVal);  // 15000 mW OU 1400 mA
 
186
        
 
187
        /*cCurVal ++;
 
188
        while (*cCurVal != ' ')
 
189
                cCurVal ++;
 
190
        while (*cCurVal == ' ')
 
191
                cCurVal ++;
 
192
        if (*cCurVal != 'm')
 
193
                cd_warning ("PowerManager : expecting mA or mW as the present rate unit");
 
194
        cCurVal ++;
 
195
        if (*cCurVal == 'W')
 
196
                bWatt = TRUE;
 
197
        else if (*cCurVal == 'A')
 
198
                bWatt = FALSE;
 
199
        else
 
200
                cd_warning ("PowerManager : expecting A or W as the present rate unit");*/
 
201
        
 
202
        go_to_next_line  // -> "remaining capacity: 47040 mWh"
 
203
        
 
204
        jump_to_value
 
205
        int iRemainingCapacity = atoi (cCurVal);  // 47040 mWh
 
206
        
 
207
        /**go_to_next_line  // -> "present voltage: 15000 mV"
 
208
        
 
209
        jump_to_value
 
210
        int iPresentVoltage = atoi (cCurVal);  // 15000 mV
 
211
        */
 
212
        myData.iPercentage = 100. * iRemainingCapacity / myData.iCapacity;
 
213
        cd_debug ("myData.iPercentage : %.2f%% (%d / %d)", (double)myData.iPercentage, iRemainingCapacity, myData.iCapacity);
 
214
        if (myData.iPercentage > 100)
 
215
                myData.iPercentage = 100;
 
216
        if (myData.iPercentage < 0)
 
217
                myData.iPercentage = 0.;
 
218
        
 
219
        //\_______________ now compute the time.
 
220
        myData.iTime = cd_estimate_time ();
 
221
        
 
222
        //cd_message ("PowerManager : On Battery:%d ; iCapacity:%dmWh ; iRemainingCapacity:%dmWh ; fPresentRate:%.2fmW ; iPresentVoltage:%dmV", myData.bOnBattery, myData.iCapacity, iRemainingCapacity, fPresentRate, iPresentVoltage); 
 
223
        g_free (cContent);
 
224
        return (TRUE);
 
225
}