| 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; |
| 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 |
//\_______________ check 'on battery' state. |
| 118 |
gchar *str = strstr (cContent, "STATUS"); |
| 119 |
g_return_val_if_fail (str != NULL, FALSE); |
| 120 |
str += 7; |
| 121 |
gboolean bOnBattery = (*str == 'D'); // "Discharging" |
| 122 |
if (bOnBattery != myData.bOnBattery) // state changed |
| 123 |
{ |
| 124 |
/**for (k = 0; k < PM_NB_VALUES; k ++) // reset the history. |
| 125 |
myData.fRateHistory[k] = 0; |
| 126 |
myData.iCurrentIndex = 0; |
| 127 |
myData.iIndexMax = 0;*/ |
| 128 |
myData.iStatPercentageBegin = 0; |
| 129 |
myData.iStatPercentage = 0; |
| 130 |
myData.bOnBattery = bOnBattery; |
| 131 |
} |
| 132 |
|
| 133 |
//\_______________ check the battery presence. |
| 134 |
str = strstr (cContent, "PRESENT"); |
| 135 |
g_return_val_if_fail (str != NULL, FALSE); |
| 136 |
str += 8; |
| 137 |
gboolean bBatteryPresent = (*str == '1'); |
| 138 |
if (bBatteryPresent != myData.bBatteryPresent) // the battery has just been inserted/removed. |
| 139 |
{ |
| 140 |
myData.bBatteryPresent = bBatteryPresent; |
| 141 |
if (! bBatteryPresent) // if the battery has been removed, we are obviously on the sector. |
| 142 |
{ |
| 143 |
cd_debug ("la batterie a ete enlevee\n"); |
| 144 |
myData.bOnBattery = FALSE; |
| 145 |
update_icon(); |
| 146 |
g_free (cContent); |
| 147 |
return TRUE; |
| 148 |
} |
| 149 |
|
| 150 |
// reset the history. |
| 151 |
cd_debug ("la batterie a ete connectee"); |
| 152 |
myData.iPrevTime = 0; |
| 153 |
myData.iPrevPercentage = 0; |
| 154 |
/**for (k = 0; k < PM_NB_VALUES; k ++) |
| 155 |
myData.fRateHistory[k] = 0; |
| 156 |
myData.iCurrentIndex = 0; |
| 157 |
myData.iIndexMax = 0;*/ |
| 158 |
myData.iStatPercentageBegin = 0; |
| 159 |
myData.iStatPercentage = 0; |
| 160 |
} |
| 161 |
|
| 162 |
//\_______________ get the current charge. |
| 163 |
if (myData.iCapacity == 0) // not yet got |
| 164 |
{ |
| 165 |
str = strstr (cContent, "CHARGE_FULL="); |
| 166 |
g_return_val_if_fail (str != NULL, FALSE); |
| 167 |
str += 12; |
| 168 |
myData.iCapacity = atoi (str); |
| 169 |
g_return_val_if_fail (myData.iCapacity != 0, FALSE); |
| 170 |
// also get few other static params. |
| 171 |
gchar *cr; |
| 172 |
_get_static_value ("TECHNOLOGY", myData.cTechnology) |
| 173 |
_get_static_value ("MANUFACTURER", myData.cVendor) |
| 174 |
_get_static_value ("MODEL_NAME", myData.cModel) |
| 175 |
str = strstr (cContent, "FULL_DESIGN"); |
| 176 |
if (str) |
| 177 |
{ |
| 178 |
str += 12; |
| 179 |
int iMaxCapacity = atoi (str); |
| 180 |
if (iMaxCapacity != 0) |
| 181 |
myData.fMaxAvailableCapacity = 100. * myData.iCapacity / iMaxCapacity; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
str = strstr (cContent, "CHARGE_NOW"); |
| 186 |
g_return_val_if_fail (str != NULL, FALSE); |
| 187 |
str += 11; |
| 188 |
int iRemainingCapacity = atoi (str); |
| 189 |
|
| 190 |
myData.iPercentage = 100. * iRemainingCapacity / myData.iCapacity; |
| 191 |
cd_debug ("myData.iPercentage : %.2f%% (%d / %d)", (double)myData.iPercentage, iRemainingCapacity, myData.iCapacity); |
| 192 |
if (myData.iPercentage > 100) |
| 193 |
myData.iPercentage = 100; |
| 194 |
if (myData.iPercentage < 0) |
| 195 |
myData.iPercentage = 0.; |
| 196 |
|
| 197 |
//\_______________ now compute the time. |
| 198 |
myData.iTime = cd_estimate_time (); |
| 199 |
|
| 200 |
g_free (cContent); |
| 201 |
return (TRUE); |
| 202 |
} |