gnome-power-manager r3073 - in trunk: . libdevkit-power src



Author: rhughes
Date: Tue Nov  4 11:40:42 2008
New Revision: 3073
URL: http://svn.gnome.org/viewvc/gnome-power-manager?rev=3073&view=rev

Log:
2008-11-04  Richard Hughes  <richard hughsie com>

* libdevkit-power/dkp-client.c: (dkp_client_enumerate_devices),
(dkp_client_get_on_battery), (dkp_client_get_low_battery),
(dkp_client_can_suspend), (dkp_client_can_hibernate),
(dkp_client_on_battery_changed_cb),
(dkp_client_low_battery_changed_cb), (dkp_client_class_init),
(dkp_client_init):
* libdevkit-power/dkp-client.h:
* src/gpm-ac-adapter.c: (gpm_ac_adapter_is_present),
(gpm_ac_adapter_on_battery_changed_cb), (gpm_ac_adapter_init):
* src/gpm-statistics.c: (main):
Switch GpmAcAdapter to using DeviceKit-power if configured to do so.


Modified:
   trunk/ChangeLog
   trunk/libdevkit-power/dkp-client.c
   trunk/libdevkit-power/dkp-client.h
   trunk/src/gpm-ac-adapter.c
   trunk/src/gpm-statistics.c

Modified: trunk/libdevkit-power/dkp-client.c
==============================================================================
--- trunk/libdevkit-power/dkp-client.c	(original)
+++ trunk/libdevkit-power/dkp-client.c	Tue Nov  4 11:40:42 2008
@@ -48,6 +48,8 @@
 	DKP_CLIENT_ADDED,
 	DKP_CLIENT_CHANGED,
 	DKP_CLIENT_REMOVED,
+	DKP_CLIENT_ON_BATTERY_CHANGED,
+	DKP_CLIENT_LOW_BATTERY_CHANGED,
 	DKP_CLIENT_LAST_SIGNAL
 };
 
@@ -70,26 +72,116 @@
  * dkp_client_enumerate_devices:
  **/
 GPtrArray *
-dkp_client_enumerate_devices (const DkpClient *client)
+dkp_client_enumerate_devices (DkpClient *client, GError **error)
 {
 	gboolean ret;
-	GError *error = NULL;
+	GError *error_local = NULL;
 	GPtrArray *devices = NULL;
 	GType g_type_array;
 
 	g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
-	ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error,
+	ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error_local,
 				 G_TYPE_INVALID,
 				 g_type_array, &devices,
 				 G_TYPE_INVALID);
 	if (!ret) {
-		egg_warning ("Couldn't enumerate devices: %s", error->message);
-		g_error_free (error);
+		egg_warning ("Couldn't enumerate devices: %s", error_local->message);
+		if (error != NULL)
+			*error = g_error_new (1, 0, "%s", error_local->message);
+		g_error_free (error_local);
 	}
 	return devices;
 }
 
 /**
+ * dkp_client_get_on_battery
+ **/
+gboolean
+dkp_client_get_on_battery (DkpClient *client, gboolean *on_battery, GError **error)
+{
+	gboolean ret;
+	GError *error_local = NULL;
+
+	ret = dbus_g_proxy_call (client->priv->proxy, "GetOnBattery", &error_local,
+				 G_TYPE_INVALID,
+				 G_TYPE_BOOLEAN, &on_battery,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_warning ("Couldn't get battery status: %s", error_local->message);
+		if (error != NULL)
+			*error = g_error_new (1, 0, "%s", error_local->message);
+		g_error_free (error_local);
+	}
+	return ret;
+}
+
+/**
+ * dkp_client_get_low_battery
+ **/
+gboolean
+dkp_client_get_low_battery (DkpClient *client, gboolean *low_battery, GError **error)
+{
+	gboolean ret;
+	GError *error_local = NULL;
+
+	ret = dbus_g_proxy_call (client->priv->proxy, "GetLowBattery", &error_local,
+				 G_TYPE_INVALID,
+				 G_TYPE_BOOLEAN, &low_battery,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_warning ("Couldn't get low battery status: %s", error_local->message);
+		if (error != NULL)
+			*error = g_error_new (1, 0, "%s", error_local->message);
+		g_error_free (error_local);
+	}
+	return ret;
+}
+
+/**
+ * dkp_client_can_suspend
+ **/
+gboolean
+dkp_client_can_suspend (DkpClient *client, gboolean interactive, gboolean *can_suspend, GError **error)
+{
+	gboolean ret;
+	GError *error_local = NULL;
+
+	ret = dbus_g_proxy_call (client->priv->proxy, "CanSuspend", &error_local,
+				 G_TYPE_INVALID,
+				 G_TYPE_BOOLEAN, &can_suspend,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_warning ("Couldn't get suspend status: %s", error_local->message);
+		if (error != NULL)
+			*error = g_error_new (1, 0, "%s", error_local->message);
+		g_error_free (error_local);
+	}
+	return ret;
+}
+
+/**
+ * dkp_client_can_hibernate
+ **/
+gboolean
+dkp_client_can_hibernate (DkpClient *client, gboolean interactive, gboolean *can_hibernate, GError **error)
+{
+	gboolean ret;
+	GError *error_local = NULL;
+
+	ret = dbus_g_proxy_call (client->priv->proxy, "CanHibernate", &error_local,
+				 G_TYPE_INVALID,
+				 G_TYPE_BOOLEAN, &can_hibernate,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_warning ("Couldn't get hibernate status: %s", error_local->message);
+		if (error != NULL)
+			*error = g_error_new (1, 0, "%s", error_local->message);
+		g_error_free (error_local);
+	}
+	return ret;
+}
+
+/**
  * dkp_client_add:
  **/
 static DkpClientDevice *
@@ -159,6 +251,24 @@
 }
 
 /**
+ * dkp_client_on_battery_changed_cb:
+ **/
+static void
+dkp_client_on_battery_changed_cb (DBusGProxy *proxy, gboolean on_battery, DkpClient *client)
+{
+	g_signal_emit (client, signals [DKP_CLIENT_ON_BATTERY_CHANGED], 0, on_battery);
+}
+
+/**
+ * dkp_client_low_battery_changed_cb:
+ **/
+static void
+dkp_client_low_battery_changed_cb (DBusGProxy *proxy, gboolean low_battery, DkpClient *client)
+{
+	g_signal_emit (client, signals [DKP_CLIENT_LOW_BATTERY_CHANGED], 0, low_battery);
+}
+
+/**
  * dkp_client_class_init:
  * @klass: The DkpClientClass
  **/
@@ -186,6 +296,18 @@
 			      G_STRUCT_OFFSET (DkpClientClass, changed),
 			      NULL, NULL, g_cclosure_marshal_VOID__POINTER,
 			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+	signals [DKP_CLIENT_ON_BATTERY_CHANGED] =
+		g_signal_new ("on-battery-changed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientClass, changed),
+			      NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
+			      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
+	signals [DKP_CLIENT_LOW_BATTERY_CHANGED] =
+		g_signal_new ("low-battery-changed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientClass, changed),
+			      NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
+			      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
 
 	g_type_class_add_private (klass, sizeof (DkpClientPrivate));
 }
@@ -225,6 +347,8 @@
 	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
 	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
 	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);
+	dbus_g_proxy_add_signal (client->priv->proxy, "OnBatteryChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
+	dbus_g_proxy_add_signal (client->priv->proxy, "LowBatteryChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
 
 	/* all callbacks */
 	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceAdded",
@@ -233,9 +357,13 @@
 				     G_CALLBACK (dkp_client_removed_cb), client, NULL);
 	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceChanged",
 				     G_CALLBACK (dkp_client_changed_cb), client, NULL);
+	dbus_g_proxy_connect_signal (client->priv->proxy, "OnBatteryChanged",
+				     G_CALLBACK (dkp_client_on_battery_changed_cb), client, NULL);
+	dbus_g_proxy_connect_signal (client->priv->proxy, "LowBatteryChanged",
+				     G_CALLBACK (dkp_client_low_battery_changed_cb), client, NULL);
 
 	/* coldplug */
-	devices = dkp_client_enumerate_devices (client);
+	devices = dkp_client_enumerate_devices (client, NULL);
 	if (devices == NULL)
 		goto out;
 	for (i=0; i<devices->len; i++) {

Modified: trunk/libdevkit-power/dkp-client.h
==============================================================================
--- trunk/libdevkit-power/dkp-client.h	(original)
+++ trunk/libdevkit-power/dkp-client.h	Tue Nov  4 11:40:42 2008
@@ -54,11 +54,30 @@
 							 const DkpClientDevice	*device);
 	void			(*removed)		(DkpClient		*client,
 							 const DkpClientDevice	*device);
+	void			(*on_battery_changed)	(DkpClient		*client,
+							 gboolean		 on_battery);
+	void			(*low_battery_changed)	(DkpClient		*client,
+							 gboolean		 low_battery);
 } DkpClientClass;
 
 GType		 dkp_client_get_type			(void) G_GNUC_CONST;
 DkpClient	*dkp_client_new				(void);
-GPtrArray	*dkp_client_enumerate_devices		(const DkpClient	*client);
+GPtrArray	*dkp_client_enumerate_devices		(DkpClient		*client,
+							 GError			**error);
+gboolean	 dkp_client_get_on_battery		(DkpClient		*client,
+							 gboolean		*on_battery,
+							 GError			**error);
+gboolean	 dkp_client_get_low_battery		(DkpClient		*client,
+							 gboolean		*low_battery,
+							 GError			**error);
+gboolean	 dkp_client_can_suspend			(DkpClient		*client,
+							 gboolean		 interactive,
+							 gboolean		*can_suspend,
+							 GError			**error);
+gboolean	 dkp_client_can_hibernate		(DkpClient		*client,
+							 gboolean		 interactive,
+							 gboolean		*can_hibernate,
+							 GError			**error);
 
 G_END_DECLS
 

Modified: trunk/src/gpm-ac-adapter.c
==============================================================================
--- trunk/src/gpm-ac-adapter.c	(original)
+++ trunk/src/gpm-ac-adapter.c	Tue Nov  4 11:40:42 2008
@@ -56,7 +56,7 @@
 {
 	gboolean		 has_hardware;
 #ifdef HAVE_DK_POWER
-	DkpClientDevice		*device;
+	DkpClient		*device;
 #else
 	HalDevice		*device;
 #endif
@@ -81,31 +81,29 @@
 gboolean
 gpm_ac_adapter_is_present (GpmAcAdapter *ac_adapter)
 {
-#ifndef HAVE_DK_POWER
 	gboolean is_on_ac;
+#ifndef HAVE_DK_POWER
 	GError *error;
 
 	g_return_val_if_fail (ac_adapter != NULL, FALSE);
 	g_return_val_if_fail (GPM_IS_AC_ADAPTER (ac_adapter), FALSE);
 
 	/* bodge for now, PC's are considered on AC power */
-	if (ac_adapter->priv->has_hardware == FALSE) {
+	if (!ac_adapter->priv->has_hardware)
 		return TRUE;
-	}
 
 	error = NULL;
-	hal_device_get_bool (ac_adapter->priv->device,
-			      "ac_adapter.present", &is_on_ac, &error);
+	hal_device_get_bool (ac_adapter->priv->device, "ac_adapter.present", &is_on_ac, &error);
 	if (error != NULL) {
 		egg_warning ("could not read ac_adapter.present");
 		g_error_free (error);
 	}
-
-	if (is_on_ac) {
-		return TRUE;
-	}
+#else
+	dkp_client_get_on_battery (ac_adapter->priv->device, &is_on_ac, NULL);
+	/* battery -> not AC */
+	is_on_ac = !is_on_ac;
 #endif
-	return FALSE;
+	return is_on_ac;
 }
 
 #ifndef HAVE_DK_POWER
@@ -135,6 +133,15 @@
 		return;
 	}
 }
+#else
+/**
+ * gpm_ac_adapter_on_battery_changed_cb:
+ */
+static void
+gpm_ac_adapter_on_battery_changed_cb (DkpClientDevice *device, gboolean on_battery, GpmAcAdapter *ac_adapter)
+{
+	g_signal_emit (ac_adapter, signals [AC_ADAPTER_CHANGED], 0, !on_battery);
+}
 #endif
 
 /**
@@ -188,7 +195,9 @@
 {
 	ac_adapter->priv = GPM_AC_ADAPTER_GET_PRIVATE (ac_adapter);
 #ifdef HAVE_DK_POWER
-	egg_debug ("TODO");
+	ac_adapter->priv->device = dkp_client_new ();
+	g_signal_connect (ac_adapter->priv->device, "on-battery-changed",
+			  G_CALLBACK (gpm_ac_adapter_on_battery_changed_cb), ac_adapter);
 #else
 	gchar **device_names;
 	gboolean ret;

Modified: trunk/src/gpm-statistics.c
==============================================================================
--- trunk/src/gpm-statistics.c	(original)
+++ trunk/src/gpm-statistics.c	Tue Nov  4 11:40:42 2008
@@ -1024,7 +1024,7 @@
 	g_signal_connect (client, "changed", G_CALLBACK (gpm_tool_device_changed_cb), NULL);
 
 	/* coldplug */
-	devices = dkp_client_enumerate_devices (client);
+	devices = dkp_client_enumerate_devices (client, NULL);
 	if (devices == NULL)
 		goto out;
 	for (i=0; i < devices->len; i++) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]