| 1 |
/** |
| 2 |
* This file is a part of the Cairo-Dock project |
| 3 |
* |
| 4 |
* Copyright : (C) see the 'copyright' file. |
| 5 |
3* 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-struct.h" |
| 26 |
#include "powermanager-draw.h" |
| 27 |
#include "powermanager-common.h" |
| 28 |
#include "powermanager-upower.h" |
| 29 |
|
| 30 |
|
| 31 |
#ifdef CD_UPOWER_AVAILABLE // code with libupower |
| 32 |
|
| 33 |
static GList * _cd_upower_add_and_ref_device_if_battery (UpDevice *pDevice, GList *pBatteryDeviceList) |
| 34 |
{ |
| 35 |
UpDeviceKind kind; |
| 36 |
g_object_get (G_OBJECT (pDevice), "kind", &kind, NULL); |
| 37 |
if (kind == UP_DEVICE_KIND_BATTERY) |
| 38 |
{ |
| 39 |
pBatteryDeviceList = g_list_append (pBatteryDeviceList, pDevice); |
| 40 |
g_object_ref (pDevice); // since the object does not belong to us, we ref it here, and we'll keep our ref until the end of the applet. |
| 41 |
} |
| 42 |
return pBatteryDeviceList; |
| 43 |
} |
| 44 |
|
| 45 |
static void _cd_upower_connect_async (CDSharedMemory *pSharedMemory) |
| 46 |
{ |
| 47 |
// connect to UPower on Dbus. |
| 48 |
UpClient *pUPowerClient = up_client_new (); |
| 49 |
|
| 50 |
// get the list of devices. |
| 51 |
if (pUPowerClient == NULL |
| 52 |
#ifndef CD_UPOWER_0_99 // no longer available with UPower 0.99+ |
| 53 |
|| ! up_client_enumerate_devices_sync (pUPowerClient, NULL, NULL) |
| 54 |
#endif |
| 55 |
) |
| 56 |
{ |
| 57 |
cd_warning ("couldn't get devices from UPower daemon"); |
| 58 |
if (pUPowerClient) |
| 59 |
g_object_unref (pUPowerClient); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// find the battery device. |
| 64 |
GPtrArray *pDevices = up_client_get_devices (pUPowerClient); |
| 65 |
g_return_if_fail (pDevices != NULL); // just to be sure. |
| 66 |
UpDevice *pDevice; |
| 67 |
GList *pBatteryDeviceList = NULL; |
| 68 |
guint i; |
| 69 |
for (i = 0; i < pDevices->len; i ++) |
| 70 |
{ |
| 71 |
pDevice = g_ptr_array_index (pDevices, i); |
| 72 |
pBatteryDeviceList = _cd_upower_add_and_ref_device_if_battery (pDevice, pBatteryDeviceList); |
| 73 |
} |
| 74 |
if (pBatteryDeviceList == NULL) |
| 75 |
{ |
| 76 |
cd_debug ("no battery found amongst %d devices", pDevices->len); |
| 77 |
/* g_object_unref (pUPowerClient); // => if we launch the dock without any battery and then connect the battery, we need to be notified |
| 78 |
return;*/ |
| 79 |
} |
| 80 |
|
| 81 |
pSharedMemory->pUPowerClient = pUPowerClient; |
| 82 |
pSharedMemory->pBatteryDeviceList = pBatteryDeviceList; |
| 83 |
} |
| 84 |
|
| 85 |
static void _fetch_current_values (GList *pBatteryDeviceList) |
| 86 |
{ |
| 87 |
myData.bOnBattery = FALSE; |
| 88 |
myData.bBatteryPresent = FALSE; |
| 89 |
myData.iTime = 0; |
| 90 |
myData.iPercentage = 0; // since we are not on battery, this will not trigger any alert. |
| 91 |
if (! pBatteryDeviceList) |
| 92 |
return; |
| 93 |
|
| 94 |
UpDevice *pDevice; |
| 95 |
gboolean is_present; |
| 96 |
gdouble fPercentageGlobal = 0.; |
| 97 |
GList *pItem; |
| 98 |
gint iNbBatteries = 0; |
| 99 |
for (pItem = pBatteryDeviceList; pItem != NULL; pItem = g_list_next (pItem)) |
| 100 |
{ |
| 101 |
pDevice = pItem->data; |
| 102 |
g_object_get (G_OBJECT (pDevice), "is-present", &is_present, NULL); |
| 103 |
myData.bBatteryPresent |= is_present; |
| 104 |
|
| 105 |
if (is_present) |
| 106 |
{ |
| 107 |
UpDeviceState iState; |
| 108 |
g_object_get (G_OBJECT (pDevice), "state", &iState, NULL); |
| 109 |
myData.bOnBattery |= (iState == UP_DEVICE_STATE_DISCHARGING || iState == UP_DEVICE_STATE_PENDING_DISCHARGE); // we assume that all batteries are either charging or discharging, not a mix of both, so that we won't add a 'time-to-empty' with a 'time-to-full'; this avoid us making a first pass on the list to determine 'myData.bOnBattery' and then a 2nd pass to compute the time. |
| 110 |
|
| 111 |
gdouble percentage; |
| 112 |
g_object_get (G_OBJECT (pDevice), "percentage", &percentage, NULL); |
| 113 |
fPercentageGlobal += percentage; |
| 114 |
|
| 115 |
guint64 time; |
| 116 |
g_object_get (G_OBJECT (pDevice), myData.bOnBattery ? "time-to-empty" : "time-to-full", &time, NULL); |
| 117 |
myData.iTime += time; |
| 118 |
|
| 119 |
cd_debug ("New data (%d: %p): OnBattery %d ; percentage %f ; time %lu", iNbBatteries, pDevice, myData.bOnBattery, percentage, time); |
| 120 |
iNbBatteries ++; |
| 121 |
} |
| 122 |
} |
| 123 |
if (iNbBatteries > 0) |
| 124 |
myData.iPercentage = round (fPercentageGlobal / iNbBatteries); |
| 125 |
if (myData.iTime == 0 && myData.iPercentage < 100) // the UPower daemon doesn't give us a time, let's compute it ourselves. |
| 126 |
myData.iTime = cd_estimate_time (); |
| 127 |
} |
| 128 |
|
| 129 |
static void _on_device_list_changed_free_data (void) |
| 130 |
{ |
| 131 |
cd_debug ("Device list changed"); |
| 132 |
g_free (myData.cTechnology); |
| 133 |
myData.cTechnology = NULL; |
| 134 |
g_free (myData.cVendor); |
| 135 |
myData.cVendor = NULL; |
| 136 |
g_free (myData.cModel); |
| 137 |
myData.cModel = NULL; |
| 138 |
} |
| 139 |
|
| 140 |
static gboolean _cd_upower_update_state (CDSharedMemory *pSharedMemory); |
| 141 |
|
| 142 |
static void _on_device_added (UpClient *pClient, UpDevice *pDevice, gpointer data) |
| 143 |
{ |
| 144 |
CD_APPLET_ENTER; |
| 145 |
if (pClient != myData.pUPowerClient) // should not happen... |
| 146 |
{ |
| 147 |
g_object_unref (myData.pUPowerClient); |
| 148 |
myData.pUPowerClient = NULL; |
| 149 |
} |
| 150 |
|
| 151 |
// if it's really a new device (yes, be secured...) |
| 152 |
if (g_list_find (myData.pBatteryDeviceList, pDevice) == NULL) |
| 153 |
{ |
| 154 |
_on_device_list_changed_free_data (); // free data |
| 155 |
|
| 156 |
// update state: get all devices and all info |
| 157 |
CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1); |
| 158 |
pSharedMemory->pBatteryDeviceList = _cd_upower_add_and_ref_device_if_battery (pDevice, myData.pBatteryDeviceList); |
| 159 |
pSharedMemory->pUPowerClient = pClient; |
| 160 |
_cd_upower_update_state (pSharedMemory); |
| 161 |
} |
| 162 |
CD_APPLET_LEAVE (); |
| 163 |
} |
| 164 |
|
| 165 |
static void _on_device_removed (UpClient *pClient, UpDevice *pDevice, gpointer data) |
| 166 |
{ |
| 167 |
CD_APPLET_ENTER; |
| 168 |
if (pClient != myData.pUPowerClient) // should not happen... |
| 169 |
{ |
| 170 |
g_object_unref (myData.pUPowerClient); |
| 171 |
myData.pUPowerClient = NULL; |
| 172 |
} |
| 173 |
|
| 174 |
GList *pOldDevice = g_list_find (myData.pBatteryDeviceList, pDevice); |
| 175 |
if (pOldDevice != NULL) // if we've already added this device (yes, be secured) |
| 176 |
{ |
| 177 |
_on_device_list_changed_free_data (); // free data |
| 178 |
g_object_unref (pDevice); // unref, we no longer need it... |
| 179 |
|
| 180 |
// update state: get all devices and all info |
| 181 |
CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1); |
| 182 |
pSharedMemory->pBatteryDeviceList = g_list_delete_link (myData.pBatteryDeviceList, pOldDevice); |
| 183 |
pSharedMemory->pUPowerClient = pClient; |
| 184 |
_cd_upower_update_state (pSharedMemory); |
| 185 |
} |
| 186 |
CD_APPLET_LEAVE (); |
| 187 |
} |
| 188 |
|
| 189 |
#ifdef CD_UPOWER_0_99 // one more param |
| 190 |
static void _on_device_changed (G_GNUC_UNUSED UpDevice *pDevice, G_GNUC_UNUSED GParamSpec *pSpec, G_GNUC_UNUSED gpointer data) |
| 191 |
#else |
| 192 |
static void _on_device_changed (G_GNUC_UNUSED UpDevice *pDevice, G_GNUC_UNUSED gpointer data) |
| 193 |
#endif |
| 194 |
{ |
| 195 |
// The applet is removed just before an update... |
| 196 |
if (myApplet == NULL) |
| 197 |
return; |
| 198 |
|
| 199 |
CD_APPLET_ENTER; |
| 200 |
cd_debug ("battery properties changed"); |
| 201 |
|
| 202 |
// the client object is already up-to-date, just fetch its values. |
| 203 |
_fetch_current_values (myData.pBatteryDeviceList); |
| 204 |
|
| 205 |
// and update the icon. |
| 206 |
update_icon (); |
| 207 |
CD_APPLET_LEAVE (); |
| 208 |
} |
| 209 |
|
| 210 |
// Can be launched the first time (with the Task) or when a device is added/removed after. |
| 211 |
static gboolean _cd_upower_update_state (CDSharedMemory *pSharedMemory) |
| 212 |
{ |
| 213 |
CD_APPLET_ENTER; |
| 214 |
if (pSharedMemory->pUPowerClient == NULL) // no UPower available, try to find the battery by ourselves. |
| 215 |
{ |
| 216 |
cd_debug ("no UPower available"); |
| 217 |
cd_check_power_files (); |
| 218 |
} |
| 219 |
else // UPower is available, fetch the values we got from it. |
| 220 |
{ |
| 221 |
// fetch the current values we got on the devices |
| 222 |
_fetch_current_values (pSharedMemory->pBatteryDeviceList); |
| 223 |
|
| 224 |
// fetch static values of the devices, and watch for any change |
| 225 |
UpDevice *pDevice; |
| 226 |
GList *pItem; |
| 227 |
gint iSignalID; |
| 228 |
GString *sTechnology = NULL, *sVendor = NULL, *sModel = NULL; |
| 229 |
const gchar *cTechnology; |
| 230 |
gchar *cVendor, *cModel; |
| 231 |
gdouble fMaxAvailableCapacity = 0., fTmp; |
| 232 |
UpDeviceTechnology iTechnology; |
| 233 |
gboolean bFirst = TRUE; |
| 234 |
for (pItem = pSharedMemory->pBatteryDeviceList ; pItem != NULL ; pItem = g_list_next (pItem)) |
| 235 |
{ |
| 236 |
pDevice = pItem->data; |
| 237 |
|
| 238 |
g_object_get (pDevice, "technology", &iTechnology, NULL); |
| 239 |
g_object_get (pDevice, "vendor", &cVendor, NULL); |
| 240 |
g_object_get (pDevice, "model", &cModel, NULL); |
| 241 |
g_object_get (pDevice, "capacity", &fTmp, NULL); |
| 242 |
fMaxAvailableCapacity += fTmp; |
| 243 |
|
| 244 |
cTechnology = up_device_technology_to_string (iTechnology); |
| 245 |
|
| 246 |
cd_debug ("New Battery: %s, %s, %s, %f", cTechnology, cVendor, cModel, fTmp); |
| 247 |
|
| 248 |
if (bFirst) |
| 249 |
{ |
| 250 |
sTechnology = g_string_new (cTechnology); |
| 251 |
sVendor = g_string_new (cVendor); |
| 252 |
sModel = g_string_new (cModel); |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
g_string_append_printf (sTechnology, " & %s", cTechnology); |
| 257 |
g_string_append_printf (sVendor, " & %s", cVendor); |
| 258 |
g_string_append_printf (sModel, " & %s", cModel); |
| 259 |
} |
| 260 |
g_free (cVendor); |
| 261 |
g_free (cModel); |
| 262 |
|
| 263 |
if (myData.pTask != NULL // only the first time |
| 264 |
|| myData.pBatteryDeviceList == NULL // or if it's a new device |
| 265 |
|| g_list_find (myData.pBatteryDeviceList, pDevice) == NULL) |
| 266 |
// or compare the up_device_get_object_path (pDevice) ? |
| 267 |
{ |
| 268 |
/* watch for any change. A priori, no need to watch the |
| 269 |
* "onBattery" signal on the client, since we can deduce this |
| 270 |
* property from the device state. |
| 271 |
* A battery not present is still a valid device. So if we could |
| 272 |
* find a battery device, it will stay here forever, so we don't |
| 273 |
* need to watch for the destruction/creation of a battery device. |
| 274 |
*/ |
| 275 |
#ifdef CD_UPOWER_0_99 // Now called notify |
| 276 |
iSignalID = g_signal_connect (pDevice, "notify", G_CALLBACK (_on_device_changed), NULL); |
| 277 |
#else |
| 278 |
iSignalID = g_signal_connect (pDevice, "changed", G_CALLBACK (_on_device_changed), NULL); |
| 279 |
#endif |
| 280 |
myData.pSignalIDList = g_list_append (myData.pSignalIDList, GINT_TO_POINTER (iSignalID)); |
| 281 |
} |
| 282 |
|
| 283 |
bFirst = FALSE; |
| 284 |
} |
| 285 |
|
| 286 |
myData.fMaxAvailableCapacity = fMaxAvailableCapacity; // Add all capacities |
| 287 |
|
| 288 |
if (! bFirst) // at least one battery, strings have been defined |
| 289 |
{ |
| 290 |
myData.cTechnology = g_string_free (sTechnology, FALSE); |
| 291 |
myData.cVendor = g_string_free (sVendor, FALSE); |
| 292 |
myData.cModel = g_string_free (sModel, FALSE); |
| 293 |
} |
| 294 |
|
| 295 |
if (myData.pTask != NULL // only the first time |
| 296 |
|| pSharedMemory->pUPowerClient != myData.pUPowerClient) // or a new client (should not happen...) |
| 297 |
{ |
| 298 |
myData.iSignalIDAdded = g_signal_connect (pSharedMemory->pUPowerClient, |
| 299 |
"device-added", G_CALLBACK (_on_device_added), NULL); |
| 300 |
myData.iSignalIDRemoved = g_signal_connect (pSharedMemory->pUPowerClient, |
| 301 |
"device-removed", G_CALLBACK (_on_device_removed), NULL); |
| 302 |
// Note: these signals (removed and added) are also send when resuming from suspend... |
| 303 |
} |
| 304 |
|
| 305 |
// keep our client and devices. |
| 306 |
myData.pUPowerClient = pSharedMemory->pUPowerClient; |
| 307 |
pSharedMemory->pUPowerClient = NULL; |
| 308 |
myData.pBatteryDeviceList = pSharedMemory->pBatteryDeviceList; |
| 309 |
pSharedMemory->pBatteryDeviceList = NULL; // so we won't unref it in the destroy callback of the task. |
| 310 |
} |
| 311 |
|
| 312 |
// in any case, update the icon to show the current state we are in. |
| 313 |
update_icon (); |
| 314 |
|
| 315 |
if (myData.pTask != NULL) |
| 316 |
{ |
| 317 |
gldi_task_discard (myData.pTask); |
| 318 |
myData.pTask = NULL; |
| 319 |
} |
| 320 |
|
| 321 |
CD_APPLET_LEAVE (FALSE); |
| 322 |
} |
| 323 |
|
| 324 |
static void _free_shared_memory (CDSharedMemory *pSharedMemory) |
| 325 |
{ |
| 326 |
if (pSharedMemory->pUPowerClient) |
| 327 |
g_object_unref (pSharedMemory->pUPowerClient); |
| 328 |
if (pSharedMemory->pBatteryDeviceList) |
| 329 |
g_list_foreach (pSharedMemory->pBatteryDeviceList, (GFunc) g_object_unref, NULL); // remove the ref we took on it. |
| 330 |
g_free (pSharedMemory); |
| 331 |
} |
| 332 |
|
| 333 |
void cd_powermanager_start (void) |
| 334 |
{ |
| 335 |
if (myData.pTask != NULL) |
| 336 |
{ |
| 337 |
gldi_task_discard (myData.pTask); |
| 338 |
myData.pTask = NULL; |
| 339 |
} |
| 340 |
|
| 341 |
CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1); |
| 342 |
myData.pTask = gldi_task_new_full (0, |
| 343 |
(GldiGetDataAsyncFunc) _cd_upower_connect_async, |
| 344 |
(GldiUpdateSyncFunc) _cd_upower_update_state, |
| 345 |
(GFreeFunc) _free_shared_memory, |
| 346 |
pSharedMemory); |
| 347 |
gldi_task_launch (myData.pTask); |
| 348 |
} |
| 349 |
|
| 350 |
void cd_upower_stop (void) |
| 351 |
{ |
| 352 |
if (myData.pUPowerClient != NULL) |
| 353 |
{ |
| 354 |
g_object_unref (myData.pUPowerClient); |
| 355 |
myData.pUPowerClient = NULL; |
| 356 |
} |
| 357 |
|
| 358 |
if (myData.pSignalIDList) |
| 359 |
{ |
| 360 |
g_list_foreach (myData.pSignalIDList, (GFunc) g_source_remove, NULL); |
| 361 |
g_list_free (myData.pSignalIDList); |
| 362 |
myData.pSignalIDList = NULL; |
| 363 |
} |
| 364 |
|
| 365 |
if (myData.pBatteryDeviceList) |
| 366 |
{ |
| 367 |
g_list_foreach (myData.pBatteryDeviceList, (GFunc) g_object_unref, NULL); // remove the ref we took on the device. it may or not destroy the object, that's why we disconnected manually the signal above. |
| 368 |
g_list_free (myData.pBatteryDeviceList); |
| 369 |
myData.pBatteryDeviceList = NULL; |
| 370 |
} |
| 371 |
|
| 372 |
if (myData.iSignalIDAdded != 0) |
| 373 |
{ |
| 374 |
g_source_remove (myData.iSignalIDAdded); |
| 375 |
myData.iSignalIDAdded = 0; |
| 376 |
} |
| 377 |
if (myData.iSignalIDRemoved != 0) |
| 378 |
{ |
| 379 |
g_source_remove (myData.iSignalIDRemoved); |
| 380 |
myData.iSignalIDRemoved = 0; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
#else // code without libupower |
| 385 |
|
| 386 |
void cd_powermanager_start (void) |
| 387 |
{ |
| 388 |
cd_check_power_files (); |
| 389 |
} |
| 390 |
|
| 391 |
void cd_upower_stop (void) |
| 392 |
{ |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
#endif |