make the LowBatteryChanged and OnBatteryChanged signals work

This commit is contained in:
Richard Hughes 2008-08-05 12:30:33 +01:00
parent 16f2c597aa
commit 12c592ddee
3 changed files with 118 additions and 32 deletions

View file

@ -34,6 +34,7 @@
#include "dkp-debug.h"
#include "dkp-daemon.h"
#include "dkp-device.h"
#include "dkp-source.h"
#include "dkp-device-list.h"
#include "dkp-daemon-glue.h"
@ -68,6 +69,8 @@ struct DkpDaemonPrivate
static void dkp_daemon_class_init (DkpDaemonClass *klass);
static void dkp_daemon_init (DkpDaemon *seat);
static void dkp_daemon_finalize (GObject *object);
static gboolean dkp_daemon_get_on_battery_local (DkpDaemon *daemon);
static gboolean dkp_daemon_get_low_battery_local (DkpDaemon *daemon);
G_DEFINE_TYPE (DkpDaemon, dkp_daemon, G_TYPE_OBJECT)
@ -197,11 +200,6 @@ static void
dkp_daemon_init (DkpDaemon *daemon)
{
daemon->priv = DKP_DAEMON_GET_PRIVATE (daemon);
/* as soon as _any_ battery goes discharging, this is true */
daemon->priv->on_battery = FALSE;
/* as soon as _all_ batteries are low, this is true */
daemon->priv->low_battery = FALSE;
daemon->priv->list = dkp_device_list_new ();
}
/**
@ -311,6 +309,62 @@ gpk_daemon_dbus_filter (DBusConnection *connection, DBusMessage *message, void *
static void gpk_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event);
static void gpk_daemon_device_remove (DkpDaemon *daemon, DevkitDevice *d);
/**
* dkp_daemon_get_on_battery_local:
*
* As soon as _any_ battery goes discharging, this is true
**/
static gboolean
dkp_daemon_get_on_battery_local (DkpDaemon *daemon)
{
guint i;
gboolean ret;
gboolean result = FALSE;
gboolean on_battery;
DkpDevice *device;
const GPtrArray *array;
/* ask each device */
array = dkp_device_list_get_array (daemon->priv->list);
for (i=0; i<array->len; i++) {
device = (DkpDevice *) g_ptr_array_index (array, i);
ret = dkp_source_get_on_battery (DKP_SOURCE (device), &on_battery);
if (ret && on_battery) {
result = TRUE;
break;
}
}
return result;
}
/**
* dkp_daemon_get_low_battery_local:
*
* As soon as _all_ batteries are low, this is true
**/
static gboolean
dkp_daemon_get_low_battery_local (DkpDaemon *daemon)
{
guint i;
gboolean ret;
gboolean result = TRUE;
gboolean low_battery;
DkpDevice *device;
const GPtrArray *array;
/* ask each device */
array = dkp_device_list_get_array (daemon->priv->list);
for (i=0; i<array->len; i++) {
device = (DkpDevice *) g_ptr_array_index (array, i);
ret = dkp_source_get_low_battery (DKP_SOURCE (device), &low_battery);
if (ret && !low_battery) {
result = FALSE;
break;
}
}
return result;
}
/**
* gpk_daemon_device_changed:
**/
@ -318,6 +372,21 @@ static void
gpk_daemon_device_changed (DkpDaemon *daemon, DevkitDevice *d, gboolean synthesized)
{
DkpDevice *device;
gboolean ret;
/* check if the on_battery and low_battery state has changed */
ret = dkp_daemon_get_on_battery_local (daemon);
if (ret != daemon->priv->on_battery) {
daemon->priv->on_battery = ret;
dkp_debug ("now on_battery = %s", ret ? "yes" : "no");
g_signal_emit (daemon, signals[ON_BATTERY_CHANGED_SIGNAL], 0, ret);
}
ret = dkp_daemon_get_low_battery_local (daemon);
if (ret != daemon->priv->low_battery) {
daemon->priv->low_battery = ret;
dkp_debug ("now low_battery = %s", ret ? "yes" : "no");
g_signal_emit (daemon, signals[LOW_BATTERY_CHANGED_SIGNAL], 0, ret);
}
/* does the device exist in the db? */
device = dkp_device_list_lookup (daemon->priv->list, d);
@ -399,10 +468,6 @@ gpk_daemon_device_remove (DkpDaemon *daemon, DevkitDevice *d)
}
}
//TODO: hook into the devices
//g_signal_emit (daemon, signals[ON_BATTERY_CHANGED_SIGNAL], 0, FALSE);
//g_signal_emit (daemon, signals[LOW_BATTERY_CHANGED_SIGNAL], 0, FALSE);
/**
* gpk_daemon_device_event_signal_handler:
**/
@ -536,6 +601,7 @@ dkp_daemon_new (void)
daemon = DKP_DAEMON (g_object_new (DKP_SOURCE_TYPE_DAEMON, NULL));
daemon->priv->list = dkp_device_list_new ();
if (!gpk_daemon_register_power_daemon (DKP_DAEMON (daemon))) {
g_object_unref (daemon);
return NULL;
@ -550,6 +616,7 @@ dkp_daemon_new (void)
g_object_unref (daemon);
return NULL;
}
for (l = devices; l != NULL; l = l->next) {
DevkitDevice *device = l->data;
gpk_daemon_device_add (daemon, device, FALSE);
@ -557,6 +624,9 @@ dkp_daemon_new (void)
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
g_list_free (devices);
daemon->priv->on_battery = dkp_daemon_get_on_battery_local (daemon);
daemon->priv->low_battery = dkp_daemon_get_low_battery_local (daemon);
return daemon;
}

View file

@ -469,6 +469,7 @@ dkp_source_new (DkpDaemon *daemon, DevkitDevice *d)
source->priv->obj->native_path = g_strdup (native_path);
source->priv->history = dkp_history_new ();
/* detect what kind of device we are */
if (sysfs_file_exists (native_path, "online")) {
source->priv->obj->type = DKP_SOURCE_TYPE_LINE_POWER;
} else {
@ -476,13 +477,17 @@ dkp_source_new (DkpDaemon *daemon, DevkitDevice *d)
source->priv->obj->type = DKP_SOURCE_TYPE_BATTERY;
}
/* coldplug */
if (!dkp_source_update (source)) {
dkp_warning ("coldplug of %s failed", source->priv->obj->native_path);
g_object_unref (source);
source = NULL;
goto out;
}
/* register on the bus */
if (!dkp_source_register_source (DKP_SOURCE (source))) {
dkp_warning ("failed to register on the bus");
g_object_unref (source);
source = NULL;
goto out;
@ -490,11 +495,8 @@ dkp_source_new (DkpDaemon *daemon, DevkitDevice *d)
/* get the id so we can load the old history */
id = dkp_object_get_id (source->priv->obj);
if (id == NULL) {
dkp_debug ("cannot get device ID, not loading history");
goto out;
}
dkp_history_set_id (source->priv->history, id);
if (id != NULL)
dkp_history_set_id (source->priv->history, id);
g_free (id);
out:
@ -603,25 +605,21 @@ dkp_source_reset_values (DkpSource *source)
dkp_object_clear (source->priv->obj);
}
/**
* dkp_source_get_id:
**/
gchar *
dkp_source_get_id (DkpSource *source)
{
return dkp_object_get_id (source->priv->obj);
}
/**
* dkp_source_get_on_battery:
**/
gboolean
dkp_source_get_on_battery (DkpSource *source)
dkp_source_get_on_battery (DkpSource *source, gboolean *on_battery)
{
g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
g_return_val_if_fail (on_battery != NULL, FALSE);
if (source->priv->obj->type != DKP_SOURCE_TYPE_BATTERY)
return FALSE;
if (source->priv->obj->battery_state != DKP_SOURCE_STATE_DISCHARGING)
if (!source->priv->obj->battery_is_present)
return FALSE;
*on_battery = (source->priv->obj->battery_state == DKP_SOURCE_STATE_DISCHARGING);
return TRUE;
}
@ -629,14 +627,26 @@ dkp_source_get_on_battery (DkpSource *source)
* dkp_source_get_low_battery:
**/
gboolean
dkp_source_get_low_battery (DkpSource *source)
dkp_source_get_low_battery (DkpSource *source, gboolean *low_battery)
{
gboolean ret;
ret = dkp_source_get_on_battery (source);
gboolean on_battery;
g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
g_return_val_if_fail (low_battery != NULL, FALSE);
/* reuse the common checks */
ret = dkp_source_get_on_battery (source, &on_battery);
if (!ret)
return FALSE;
if (source->priv->obj->battery_percentage > 10)
return FALSE;
/* shortcut */
if (!on_battery) {
*low_battery = FALSE;
return TRUE;
}
*low_battery = (source->priv->obj->battery_percentage < 10);
return TRUE;
}
@ -921,6 +931,9 @@ dkp_source_get_statistics (DkpSource *source, const gchar *type, guint timespan,
GValue *value;
guint i;
g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
g_return_val_if_fail (type != NULL, FALSE);
/* get the correct data */
if (strcmp (type, "rate") == 0)
array = dkp_history_get_rate_data (source->priv->history, timespan);
@ -963,6 +976,8 @@ out:
gboolean
dkp_source_refresh (DkpSource *source, DBusGMethodInvocation *context)
{
g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
dkp_source_update (source);
dbus_g_method_return (context);
return TRUE;

View file

@ -69,15 +69,16 @@ DkpSource *dkp_source_new (DkpDaemon *daemon,
/* exported methods */
gboolean dkp_source_refresh (DkpSource *source,
DBusGMethodInvocation *context);
gchar *dkp_source_get_id (DkpSource *source);
gboolean dkp_source_get_statistics (DkpSource *source,
const gchar *type,
guint timespan,
DBusGMethodInvocation *context);
/* needed for the on_battery and low_battery logic */
gboolean dkp_source_get_on_battery (DkpSource *source);
gboolean dkp_source_get_low_battery (DkpSource *source);
gboolean dkp_source_get_on_battery (DkpSource *source,
gboolean *on_battery);
gboolean dkp_source_get_low_battery (DkpSource *source,
gboolean *low_battery);
G_END_DECLS