From 0a2b06f2985147a597e9785286317190004fa9a1 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 25 Jan 2010 15:20:09 +0000 Subject: Add libupower so applications can switch away from devkit-power-gobject --- libupower-glib/.gitignore | 14 + libupower-glib/up-client.c | 813 ++++++++++++++++++++++++++++++ libupower-glib/up-client.h | 99 ++++ libupower-glib/up-device.c | 1068 ++++++++++++++++++++++++++++++++++++++++ libupower-glib/up-device.h | 94 ++++ libupower-glib/up-types.c | 279 +++++++++++ libupower-glib/up-types.h | 87 ++++ libupower-glib/up-version.h.in | 39 ++ libupower-glib/upower.h | 35 ++ 9 files changed, 2528 insertions(+) create mode 100644 libupower-glib/.gitignore create mode 100644 libupower-glib/up-client.c create mode 100644 libupower-glib/up-client.h create mode 100644 libupower-glib/up-device.c create mode 100644 libupower-glib/up-device.h create mode 100644 libupower-glib/up-types.c create mode 100644 libupower-glib/up-types.h create mode 100644 libupower-glib/up-version.h.in create mode 100644 libupower-glib/upower.h (limited to 'libupower-glib') diff --git a/libupower-glib/.gitignore b/libupower-glib/.gitignore new file mode 100644 index 0000000..734a709 --- /dev/null +++ b/libupower-glib/.gitignore @@ -0,0 +1,14 @@ +.deps +.libs +*.o +*.la +*.lo +*-marshal.c +*-marshal.h +*.gcov +*.gcda +*.gcno +up-version.h +*.gir +*.typelib + diff --git a/libupower-glib/up-client.c b/libupower-glib/up-client.c new file mode 100644 index 0000000..7e211ca --- /dev/null +++ b/libupower-glib/up-client.c @@ -0,0 +1,813 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008-2010 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "up-client.h" +#include "up-device.h" + +static void up_client_class_init (UpClientClass *klass); +static void up_client_init (UpClient *client); +static void up_client_finalize (GObject *object); + +#define UP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_CLIENT, UpClientPrivate)) + +struct UpClientPrivate +{ + DBusGConnection *bus; + DBusGProxy *proxy; + DBusGProxy *prop_proxy; + GPtrArray *array; + gboolean have_properties; + gchar *daemon_version; + gboolean can_suspend; + gboolean can_hibernate; + gboolean lid_is_closed; + gboolean on_battery; + gboolean on_low_battery; + gboolean lid_is_present; +}; + +enum { + UP_CLIENT_DEVICE_ADDED, + UP_CLIENT_DEVICE_CHANGED, + UP_CLIENT_DEVICE_REMOVED, + UP_CLIENT_CHANGED, + UP_CLIENT_LAST_SIGNAL +}; + +enum { + PROP_0, + PROP_DAEMON_VERSION, + PROP_CAN_SUSPEND, + PROP_CAN_HIBERNATE, + PROP_ON_BATTERY, + PROP_ON_LOW_BATTERY, + PROP_LID_IS_CLOSED, + PROP_LID_IS_PRESENT, + PROP_LAST +}; + +static guint signals [UP_CLIENT_LAST_SIGNAL] = { 0 }; +static gpointer up_client_object = NULL; + +G_DEFINE_TYPE (UpClient, up_client, G_TYPE_OBJECT) + +/** + * up_client_get_device: + **/ +static UpDevice * +up_client_get_device (UpClient *client, const gchar *object_path) +{ + guint i; + const gchar *object_path_tmp; + UpDevice *device; + UpClientPrivate *priv = client->priv; + + for (i=0; iarray->len; i++) { + device = g_ptr_array_index (priv->array, i); + object_path_tmp = up_device_get_object_path (device); + if (g_strcmp0 (object_path_tmp, object_path) == 0) + return device; + } + return NULL; +} + +/** + * up_client_get_devices: + * + * Get a copy of the device objects. + * + * Return value: an array of #UpDevice objects, free with g_ptr_array_unref() + * + * Since: 0.9.0 + **/ +GPtrArray * +up_client_get_devices (UpClient *client) +{ + return g_ptr_array_ref (client->priv->array); +} + +/** + * up_client_get_devices_private: + **/ +static GPtrArray * +up_client_get_devices_private (UpClient *client, GError **error) +{ + gboolean ret; + GError *error_local = NULL; + GPtrArray *devices = NULL; + GType g_type_array; + + if (!client->priv->proxy) + return NULL; + g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH); + ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error_local, + G_TYPE_INVALID, + g_type_array, &devices, + G_TYPE_INVALID); + if (!ret) { + g_warning ("Couldn't enumerate devices: %s", error_local->message); + g_set_error (error, 1, 0, "%s", error_local->message); + g_error_free (error_local); + } + return devices; +} + +/** + * up_client_suspend_sync: + * @client : a #UpClient instance. + * @error : a #GError. + * + * Puts the computer into a low power state, but state is not preserved if the + * power is lost. + * + * NOTE: The system is still consuming a small amount of power + * + * Return value: TRUE if system suspended okay, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_suspend_sync (UpClient *client, GError **error) +{ + gboolean ret; + GError *error_local = NULL; + + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + g_return_val_if_fail (client->priv->proxy != NULL, FALSE); + + ret = dbus_g_proxy_call (client->priv->proxy, "Suspend", &error_local, + G_TYPE_INVALID, G_TYPE_INVALID); + if (!ret) { + /* DBus might time out, which is okay */ + if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) { + g_debug ("DBUS timed out, but recovering"); + ret = TRUE; + goto out; + } + + /* an actual error */ + g_warning ("Couldn't suspend: %s", error_local->message); + g_set_error (error, 1, 0, "%s", error_local->message); + } +out: + if (error_local != NULL) + g_error_free (error_local); + return ret; +} + +/** + * up_client_hibernate_sync: + * @client : a #UpClient instance. + * @error : a #GError. + * + * Puts the computer into a low power state, where state is preserved if the + * power is lost. + * + * Return value: TRUE if system suspended okay, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_hibernate_sync (UpClient *client, GError **error) +{ + gboolean ret; + GError *error_local = NULL; + + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + g_return_val_if_fail (client->priv->proxy != NULL, FALSE); + + ret = dbus_g_proxy_call (client->priv->proxy, "Hibernate", &error_local, + G_TYPE_INVALID, G_TYPE_INVALID); + if (!ret) { + /* DBus might time out, which is okay */ + if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) { + g_debug ("DBUS timed out, but recovering"); + ret = TRUE; + goto out; + } + + /* an actual error */ + g_warning ("Couldn't hibernate: %s", error_local->message); + g_set_error (error, 1, 0, "%s", error_local->message); + } +out: + if (error_local != NULL) + g_error_free (error_local); + return ret; +} + +/** + * up_client_get_properties_sync: + **/ +gboolean +up_client_get_properties_sync (UpClient *client, GError **error) +{ + gboolean ret = TRUE; + GHashTable *props; + GValue *value; + + props = NULL; + + if (client->priv->have_properties) + goto out; + if (!client->priv->prop_proxy) + goto out; + + error = NULL; + ret = dbus_g_proxy_call (client->priv->prop_proxy, "GetAll", error, + G_TYPE_STRING, "org.freedesktop.UPower", + G_TYPE_INVALID, + dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props, + G_TYPE_INVALID); + if (!ret) + goto out; + + value = g_hash_table_lookup (props, "DaemonVersion"); + if (value == NULL) { + g_warning ("No 'DaemonVersion' property"); + goto out; + } + client->priv->daemon_version = g_strdup (g_value_get_string (value)); + + value = g_hash_table_lookup (props, "CanSuspend"); + if (value == NULL) { + g_warning ("No 'CanSuspend' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->can_suspend) { + client->priv->can_suspend = ret; + g_object_notify (G_OBJECT(client), "can-suspend"); + } + + value = g_hash_table_lookup (props, "CanHibernate"); + if (value == NULL) { + g_warning ("No 'CanHibernate' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->can_hibernate) { + client->priv->can_hibernate = ret; + g_object_notify (G_OBJECT(client), "can-hibernate"); + } + + value = g_hash_table_lookup (props, "LidIsClosed"); + if (value == NULL) { + g_warning ("No 'LidIsClosed' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->lid_is_closed) { + client->priv->lid_is_closed = ret; + g_object_notify (G_OBJECT(client), "lid-is-closed"); + } + + value = g_hash_table_lookup (props, "OnBattery"); + if (value == NULL) { + g_warning ("No 'OnBattery' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->on_battery) { + client->priv->on_battery = ret; + g_object_notify (G_OBJECT(client), "on-battery"); + } + + value = g_hash_table_lookup (props, "OnLowBattery"); + if (value == NULL) { + g_warning ("No 'OnLowBattery' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->on_low_battery) { + client->priv->on_low_battery = ret; + g_object_notify (G_OBJECT(client), "on-low-battery"); + } + + value = g_hash_table_lookup (props, "LidIsPresent"); + if (value == NULL) { + g_warning ("No 'LidIsPresent' property"); + goto out; + } + ret = g_value_get_boolean (value); + if (ret != client->priv->lid_is_present) { + client->priv->lid_is_present = ret; + g_object_notify (G_OBJECT(client), "lid-is-present"); + } + + /* cached */ + client->priv->have_properties = TRUE; + +out: + if (props != NULL) + g_hash_table_unref (props); + return ret; +} + +/** + * up_client_get_daemon_version: + * @client : a #UpClient instance. + * + * Get DeviceKit-power daemon version. + * + * Return value: string containing the daemon version, e.g. 008 + * + * Since: 0.9.0 + **/ +const gchar * +up_client_get_daemon_version (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), NULL); + up_client_get_properties_sync (client, NULL); + return client->priv->daemon_version; +} + +/** + * up_client_get_can_hibernate: + * @client : a #UpClient instance. + * + * Get whether the system is able to hibernate. + * + * Return value: TRUE if system can hibernate, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_get_can_hibernate (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + up_client_get_properties_sync (client, NULL); + return client->priv->can_hibernate; +} + +/** + * up_client_get_lid_is_closed: + * @client : a #UpClient instance. + * + * Get whether the laptop lid is closed. + * + * Return value: %TRUE if lid is closed or %FALSE otherwise. + */ +gboolean +up_client_get_lid_is_closed (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + up_client_get_properties_sync (client, NULL); + return client->priv->lid_is_closed; +} + +/** + * up_client_get_can_suspend: + * @client : a #UpClient instance. + * + * Get whether the system is able to suspend. + * + * Return value: TRUE if system can suspend, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_get_can_suspend (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + up_client_get_properties_sync (client, NULL); + return client->priv->can_suspend; +} + +/** + * up_client_get_on_battery: + * @client : a #UpClient instance. + * + * Get whether the system is running on battery power. + * + * Return value: TRUE if the system is currently running on battery, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_get_on_battery (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + up_client_get_properties_sync (client, NULL); + return client->priv->on_battery; +} + +/** + * up_client_get_on_low_battery: + * @client : a #UpClient instance. + * + * Get whether the system is running on low battery power. + * + * Return value: TRUE if the system is currently on low battery power, FALSE other wise. + * + * Since: 0.9.0 + **/ +gboolean +up_client_get_on_low_battery (UpClient *client) +{ + g_return_val_if_fail (UP_IS_CLIENT (client), FALSE); + up_client_get_properties_sync (client, NULL); + return client->priv->on_low_battery; +} + +/** + * up_client_add: + **/ +static void +up_client_add (UpClient *client, const gchar *object_path) +{ + UpDevice *device; + gboolean ret; + + /* create new device */ + device = up_device_new (); + ret = up_device_set_object_path_sync (device, object_path, NULL); + if (!ret) + goto out; + + /* add to array */ + g_ptr_array_add (client->priv->array, g_object_ref (device)); + g_signal_emit (client, signals [UP_CLIENT_DEVICE_ADDED], 0, device); +out: + g_object_unref (device); +} + +/** + * up_client_added_cb: + **/ +static void +up_device_added_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client) +{ + up_client_add (client, object_path); +} + +/** + * up_client_changed_cb: + **/ +static void +up_device_changed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client) +{ + UpDevice *device; + device = up_client_get_device (client, object_path); + if (device != NULL) + g_signal_emit (client, signals [UP_CLIENT_DEVICE_CHANGED], 0, device); +} + +/** + * up_client_removed_cb: + **/ +static void +up_device_removed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client) +{ + UpDevice *device; + device = up_client_get_device (client, object_path); + if (device != NULL) { + g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, device); + g_ptr_array_remove (client->priv->array, device); + } +} + +/** + * up_client_changed_cb: + **/ +static void +up_client_changed_cb (DBusGProxy *proxy, UpClient *client) +{ + client->priv->have_properties = FALSE; + g_signal_emit (client, signals [UP_CLIENT_CHANGED], 0); +} + +static void +up_client_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + UpClient *client; + client = UP_CLIENT (object); + + up_client_get_properties_sync (client, NULL); + + switch (prop_id) { + case PROP_DAEMON_VERSION: + g_value_set_string (value, client->priv->daemon_version); + break; + case PROP_CAN_SUSPEND: + g_value_set_boolean (value, client->priv->can_suspend); + break; + case PROP_CAN_HIBERNATE: + g_value_set_boolean (value, client->priv->can_hibernate); + break; + case PROP_ON_BATTERY: + g_value_set_boolean (value, client->priv->on_battery); + break; + case PROP_ON_LOW_BATTERY: + g_value_set_boolean (value, client->priv->on_low_battery); + break; + case PROP_LID_IS_CLOSED: + g_value_set_boolean (value, client->priv->lid_is_closed); + break; + case PROP_LID_IS_PRESENT: + g_value_set_boolean (value, client->priv->lid_is_present); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/** + * up_client_class_init: + * @klass: The UpClientClass + **/ +static void +up_client_class_init (UpClientClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->get_property = up_client_get_property; + object_class->finalize = up_client_finalize; + + /** + * UpClient:daemon-version: + * + * The daemon version. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_DAEMON_VERSION, + g_param_spec_string ("daemon-version", + "Daemon version", + NULL, + NULL, + G_PARAM_READABLE)); + /** + * UpClient:can-suspend: + * + * If the computer can suspend. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_CAN_SUSPEND, + g_param_spec_boolean ("can-suspend", + "If the computer can suspend", + NULL, + FALSE, + G_PARAM_READABLE)); + /** + * UpClient:can-hibernate: + * + * If the computer can hibernate. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_CAN_HIBERNATE, + g_param_spec_boolean ("can-hibernate", + "If the computer can hibernate", + NULL, + FALSE, + G_PARAM_READABLE)); + /** + * UpClient:on-battery: + * + * If the computer is on battery power. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_ON_BATTERY, + g_param_spec_boolean ("on-battery", + "If the computer is on battery power", + NULL, + FALSE, + G_PARAM_READABLE)); + /** + * UpClient:on-low-battery: + * + * If the computer is on low battery power. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_ON_LOW_BATTERY, + g_param_spec_boolean ("on-low-battery", + "If the computer is on low battery power", + NULL, + FALSE, + G_PARAM_READABLE)); + /** + * UpClient:lid-is-closed: + * + * If the laptop lid is closed. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_LID_IS_CLOSED, + g_param_spec_boolean ("lid-is-closed", + "If the laptop lid is closed", + NULL, + FALSE, + G_PARAM_READABLE)); + /** + * UpClient:lid-is-present: + * + * If a laptop lid is present. + * + * Since: 0.9.0 + */ + g_object_class_install_property (object_class, + PROP_LID_IS_PRESENT, + g_param_spec_boolean ("lid-is-present", + "If a laptop lid is present", + NULL, + FALSE, + G_PARAM_READABLE)); + + signals [UP_CLIENT_DEVICE_ADDED] = + g_signal_new ("device-added", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (UpClientClass, device_added), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + signals [UP_CLIENT_DEVICE_REMOVED] = + g_signal_new ("device-removed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (UpClientClass, device_removed), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + signals [UP_CLIENT_DEVICE_CHANGED] = + g_signal_new ("device-changed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (UpClientClass, device_changed), + NULL, NULL, g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + signals [UP_CLIENT_CHANGED] = + g_signal_new ("changed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (UpClientClass, changed), + NULL, NULL, g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + g_type_class_add_private (klass, sizeof (UpClientPrivate)); +} + +/** + * up_client_enumerate_devices_sync: + **/ +gboolean +up_client_enumerate_devices_sync (UpClient *client, GError **error) +{ + const gchar *object_path; + GPtrArray *devices; + guint i; + gboolean ret = TRUE; + + /* coldplug */ + devices = up_client_get_devices_private (client, error); + if (devices == NULL) { + ret = FALSE; + goto out; + } + for (i=0; ilen; i++) { + object_path = (const gchar *) g_ptr_array_index (devices, i); + up_client_add (client, object_path); + } +out: + return ret; +} + +/** + * up_client_init: + * @client: This class instance + **/ +static void +up_client_init (UpClient *client) +{ + GError *error = NULL; + + client->priv = UP_CLIENT_GET_PRIVATE (client); + client->priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + client->priv->have_properties = FALSE; + + /* get on the bus */ + client->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); + if (client->priv->bus == NULL) { + g_warning ("Couldn't connect to system bus: %s", error->message); + g_error_free (error); + goto out; + } + + /* connect to main interface */ + client->priv->proxy = dbus_g_proxy_new_for_name (client->priv->bus, + "org.freedesktop.UPower", + "/org/freedesktop/UPower", + "org.freedesktop.UPower"); + if (client->priv->proxy == NULL) { + g_warning ("Couldn't connect to proxy"); + goto out; + } + + /* connect to properties interface */ + client->priv->prop_proxy = dbus_g_proxy_new_for_name (client->priv->bus, + "org.freedesktop.UPower", + "/org/freedesktop/UPower", + "org.freedesktop.DBus.Properties"); + if (client->priv->prop_proxy == NULL) { + g_warning ("Couldn't connect to proxy"); + goto out; + } + + 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, "Changed", G_TYPE_INVALID); + + /* all callbacks */ + dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceAdded", + G_CALLBACK (up_device_added_cb), client, NULL); + dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceRemoved", + G_CALLBACK (up_device_removed_cb), client, NULL); + dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceChanged", + G_CALLBACK (up_device_changed_cb), client, NULL); + dbus_g_proxy_connect_signal (client->priv->proxy, "Changed", + G_CALLBACK (up_client_changed_cb), client, NULL); +out: + return; +} + +/** + * up_client_finalize: + **/ +static void +up_client_finalize (GObject *object) +{ + UpClient *client; + + g_return_if_fail (UP_IS_CLIENT (object)); + + client = UP_CLIENT (object); + + g_ptr_array_unref (client->priv->array); + + if (client->priv->bus) + dbus_g_connection_unref (client->priv->bus); + + if (client->priv->proxy != NULL) + g_object_unref (client->priv->proxy); + + if (client->priv->prop_proxy != NULL) + g_object_unref (client->priv->prop_proxy); + + g_free (client->priv->daemon_version); + + G_OBJECT_CLASS (up_client_parent_class)->finalize (object); +} + +/** + * up_client_new: + * + * Return value: a new UpClient object. + * + * Since: 0.9.0 + **/ +UpClient * +up_client_new (void) +{ + if (up_client_object != NULL) { + g_object_ref (up_client_object); + } else { + up_client_object = g_object_new (UP_TYPE_CLIENT, NULL); + g_object_add_weak_pointer (up_client_object, &up_client_object); + } + return UP_CLIENT (up_client_object); +} + diff --git a/libupower-glib/up-client.h b/libupower-glib/up-client.h new file mode 100644 index 0000000..743222f --- /dev/null +++ b/libupower-glib/up-client.h @@ -0,0 +1,99 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008-2010 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#if !defined (__UPOWER_H_INSIDE__) && !defined (UP_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __UP_CLIENT_H +#define __UP_CLIENT_H + +#include +#include + +G_BEGIN_DECLS + +#define UP_TYPE_CLIENT (up_client_get_type ()) +#define UP_CLIENT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UP_TYPE_CLIENT, UpClient)) +#define UP_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), UP_TYPE_CLIENT, UpClientClass)) +#define UP_IS_CLIENT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UP_TYPE_CLIENT)) +#define UP_IS_CLIENT_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), UP_TYPE_CLIENT)) +#define UP_CLIENT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UP_TYPE_CLIENT, UpClientClass)) +#define UP_CLIENT_ERROR (up_client_error_quark ()) +#define UP_CLIENT_TYPE_ERROR (up_client_error_get_type ()) + +typedef struct UpClientPrivate UpClientPrivate; + +typedef struct +{ + GObject parent; + UpClientPrivate *priv; +} UpClient; + +typedef struct +{ + GObjectClass parent_class; + void (*device_added) (UpClient *client, + UpDevice *device); + void (*device_changed) (UpClient *client, + UpDevice *device); + void (*device_removed) (UpClient *client, + UpDevice *device); + void (*changed) (UpClient *client); + /*< private >*/ + /* Padding for future expansion */ + void (*_up_client_reserved1) (void); + void (*_up_client_reserved2) (void); + void (*_up_client_reserved3) (void); + void (*_up_client_reserved4) (void); + void (*_up_client_reserved5) (void); + void (*_up_client_reserved6) (void); + void (*_up_client_reserved7) (void); + void (*_up_client_reserved8) (void); +} UpClientClass; + +/* general */ +GType up_client_get_type (void); +UpClient *up_client_new (void); + +/* sync versions */ +gboolean up_client_get_properties_sync (UpClient *client, + GError **error); +gboolean up_client_enumerate_devices_sync (UpClient *client, + GError **error); +gboolean up_client_suspend_sync (UpClient *client, + GError **error); +gboolean up_client_hibernate_sync (UpClient *client, + GError **error); + +/* accessors */ +GPtrArray *up_client_get_devices (UpClient *client); +const gchar *up_client_get_daemon_version (UpClient *client); +gboolean up_client_get_can_hibernate (UpClient *client); +gboolean up_client_get_lid_is_closed (UpClient *client); +gboolean up_client_get_can_suspend (UpClient *client); +gboolean up_client_get_on_battery (UpClient *client); +gboolean up_client_get_on_low_battery (UpClient *client); + +G_END_DECLS + +#endif /* __UP_CLIENT_H */ + diff --git a/libupower-glib/up-device.c b/libupower-glib/up-device.c new file mode 100644 index 0000000..299f3a9 --- /dev/null +++ b/libupower-glib/up-device.c @@ -0,0 +1,1068 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "up-device.h" + +static void up_device_class_init (UpDeviceClass *klass); +static void up_device_init (UpDevice *device); +static void up_device_finalize (GObject *object); + +#define UP_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_DEVICE, UpDevicePrivate)) + +struct UpDevicePrivate +{ + gchar *object_path; + DBusGConnection *bus; + DBusGProxy *proxy_device; + DBusGProxy *proxy_props; + + /* properties */ + guint64 update_time; + gchar *vendor; + gchar *model; + gchar *serial; + gchar *native_path; + gboolean power_supply; + gboolean online; + gboolean is_present; + gboolean is_rechargeable; + gboolean has_history; + gboolean has_statistics; + UpDeviceType type; + UpDeviceState state; + UpDeviceTechnology technology; + gdouble capacity; /* percent */ + gdouble energy; /* Watt Hours */ + gdouble energy_empty; /* Watt Hours */ + gdouble energy_full; /* Watt Hours */ + gdouble energy_full_design; /* Watt Hours */ + gdouble energy_rate; /* Watts */ + gdouble voltage; /* Volts */ + gint64 time_to_empty; /* seconds */ + gint64 time_to_full; /* seconds */ + gdouble percentage; /* percent */ + gboolean recall_notice; + gchar *recall_vendor; + gchar *recall_url; +}; + +enum { + PROP_0, + PROP_UPDATE_TIME, + PROP_VENDOR, + PROP_MODEL, + PROP_SERIAL, + PROP_NATIVE_PATH, + PROP_POWER_SUPPLY, + PROP_ONLINE, + PROP_IS_PRESENT, + PROP_IS_RECHARGEABLE, + PROP_HAS_HISTORY, + PROP_HAS_STATISTICS, + PROP_TYPE, + PROP_STATE, + PROP_TECHNOLOGY, + PROP_CAPACITY, + PROP_ENERGY, + PROP_ENERGY_EMPTY, + PROP_ENERGY_FULL, + PROP_ENERGY_FULL_DESIGN, + PROP_ENERGY_RATE, + PROP_VOLTAGE, + PROP_TIME_TO_EMPTY, + PROP_TIME_TO_FULL, + PROP_PERCENTAGE, + PROP_RECALL_NOTICE, + PROP_RECALL_VENDOR, + PROP_RECALL_URL, + PROP_LAST +}; + +enum { + SIGNAL_CHANGED, + SIGNAL_LAST +}; + +static guint signals [SIGNAL_LAST] = { 0 }; + +G_DEFINE_TYPE (UpDevice, up_device, G_TYPE_OBJECT) + +/** + * up_device_get_device_properties: + **/ +static GHashTable * +up_device_get_device_properties (UpDevice *device, GError **error) +{ + gboolean ret; + GError *error_local = NULL; + GHashTable *hash_table = NULL; + + ret = dbus_g_proxy_call (device->priv->proxy_props, "GetAll", &error_local, + G_TYPE_STRING, "org.freedesktop.UPower.Device", + G_TYPE_INVALID, + dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), + &hash_table, + G_TYPE_INVALID); + if (!ret) { + g_set_error (error, 1, 0, "Couldn't call GetAll() to get properties for %s: %s", device->priv->object_path, error_local->message); + g_error_free (error_local); + goto out; + } +out: + return hash_table; +} + +/** + * up_device_collect_props_cb: + **/ +static void +up_device_collect_props_cb (const char *key, const GValue *value, UpDevice *device) +{ + if (g_strcmp0 (key, "NativePath") == 0) { + g_free (device->priv->native_path); + device->priv->native_path = g_strdup (g_value_get_string (value)); + } else if (g_strcmp0 (key, "Vendor") == 0) { + g_free (device->priv->vendor); + device->priv->vendor = g_strdup (g_value_get_string (value)); + } else if (g_strcmp0 (key, "Model") == 0) { + g_free (device->priv->model); + device->priv->model = g_strdup (g_value_get_string (value)); + } else if (g_strcmp0 (key, "Serial") == 0) { + g_free (device->priv->serial); + device->priv->serial = g_strdup (g_value_get_string (value)); + } else if (g_strcmp0 (key, "UpdateTime") == 0) { + device->priv->update_time = g_value_get_uint64 (value); + } else if (g_strcmp0 (key, "Type") == 0) { + device->priv->type = g_value_get_uint (value); + } else if (g_strcmp0 (key, "Online") == 0) { + device->priv->online = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "HasHistory") == 0) { + device->priv->has_history = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "HasStatistics") == 0) { + device->priv->has_statistics = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "Energy") == 0) { + device->priv->energy = g_value_get_double (value); + } else if (g_strcmp0 (key, "EnergyEmpty") == 0) { + device->priv->energy_empty = g_value_get_double (value); + } else if (g_strcmp0 (key, "EnergyFull") == 0) { + device->priv->energy_full = g_value_get_double (value); + } else if (g_strcmp0 (key, "EnergyFullDesign") == 0) { + device->priv->energy_full_design = g_value_get_double (value); + } else if (g_strcmp0 (key, "EnergyRate") == 0) { + device->priv->energy_rate = g_value_get_double (value); + } else if (g_strcmp0 (key, "Voltage") == 0) { + device->priv->voltage = g_value_get_double (value); + } else if (g_strcmp0 (key, "TimeToFull") == 0) { + device->priv->time_to_full = g_value_get_int64 (value); + } else if (g_strcmp0 (key, "TimeToEmpty") == 0) { + device->priv->time_to_empty = g_value_get_int64 (value); + } else if (g_strcmp0 (key, "Percentage") == 0) { + device->priv->percentage = g_value_get_double (value); + } else if (g_strcmp0 (key, "Technology") == 0) { + device->priv->technology = g_value_get_uint (value); + } else if (g_strcmp0 (key, "IsPresent") == 0) { + device->priv->is_present = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "IsRechargeable") == 0) { + device->priv->is_rechargeable = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "PowerSupply") == 0) { + device->priv->power_supply = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "Capacity") == 0) { + device->priv->capacity = g_value_get_double (value); + } else if (g_strcmp0 (key, "State") == 0) { + device->priv->state = g_value_get_uint (value); + } else if (g_strcmp0 (key, "RecallNotice") == 0) { + device->priv->recall_notice = g_value_get_boolean (value); + } else if (g_strcmp0 (key, "RecallVendor") == 0) { + g_free (device->priv->recall_vendor); + device->priv->recall_vendor = g_strdup (g_value_get_string (value)); + } else if (g_strcmp0 (key, "RecallUrl") == 0) { + g_free (device->priv->recall_url); + device->priv->recall_url = g_strdup (g_value_get_string (value)); + } else { + g_warning ("unhandled property '%s'", key); + } +} + +/** + * up_device_refresh_internal: + **/ +static gboolean +up_device_refresh_internal (UpDevice *device, GError **error) +{ + GHashTable *hash; + GError *error_local = NULL; + + /* get all the properties */ + hash = up_device_get_device_properties (device, &error_local); + if (hash == NULL) { + g_set_error (error, 1, 0, "Cannot get device properties for %s: %s", device->priv->object_path, error_local->message); + g_error_free (error_local); + return FALSE; + } + g_hash_table_foreach (hash, (GHFunc) up_device_collect_props_cb, device); + g_hash_table_unref (hash); + return TRUE; +} + +/** + * up_device_changed_cb: + **/ +static void +up_device_changed_cb (DBusGProxy *proxy, UpDevice *device) +{ + g_return_if_fail (UP_IS_DEVICE (device)); + up_device_refresh_internal (device, NULL); + g_signal_emit (device, signals [SIGNAL_CHANGED], 0); +} + +/** + * up_device_set_object_path_sync: + * + * Sets the object path of the object and fills up initial properties. + * + * Return value: #TRUE for success, else #FALSE and @error is used + * + * Since: 0.9.0 + **/ +gboolean +up_device_set_object_path_sync (UpDevice *device, const gchar *object_path, GError **error) +{ + GError *error_local = NULL; + gboolean ret = FALSE; + DBusGProxy *proxy_device; + DBusGProxy *proxy_props; + + g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); + + if (device->priv->object_path != NULL) + return FALSE; + if (object_path == NULL) + return FALSE; + + /* connect to the bus */ + device->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error_local); + if (device->priv->bus == NULL) { + g_set_error (error, 1, 0, "Couldn't connect to system bus: %s", error_local->message); + g_error_free (error_local); + goto out; + } + + /* connect to the correct path for properties */ + proxy_props = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.UPower", + object_path, "org.freedesktop.DBus.Properties"); + if (proxy_props == NULL) { + g_set_error_literal (error, 1, 0, "Couldn't connect to proxy"); + goto out; + } + + /* connect to the correct path for all the other methods */ + proxy_device = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.UPower", + object_path, "org.freedesktop.UPower.Device"); + if (proxy_device == NULL) { + g_set_error_literal (error, 1, 0, "Couldn't connect to proxy"); + goto out; + } + + /* listen to Changed */ + dbus_g_proxy_add_signal (proxy_device, "Changed", G_TYPE_INVALID); + dbus_g_proxy_connect_signal (proxy_device, "Changed", + G_CALLBACK (up_device_changed_cb), device, NULL); + + /* yay */ + device->priv->proxy_device = proxy_device; + device->priv->proxy_props = proxy_props; + device->priv->object_path = g_strdup (object_path); + + /* coldplug */ + ret = up_device_refresh_internal (device, &error_local); + if (!ret) { + g_set_error (error, 1, 0, "cannot refresh: %s", error_local->message); + g_error_free (error_local); + } +out: + return ret; +} + +/** + * up_device_get_object_path: + * + * Gets the object path for the device. + * + * Return value: the object path, or %NULL + * + * Since: 0.9.0 + **/ +const gchar * +up_device_get_object_path (UpDevice *device) +{ + g_return_val_if_fail (UP_IS_DEVICE (device), NULL); + return device->priv->object_path; +} + +/** + * up_device_bool_to_string: + **/ +static const gchar * +up_device_bool_to_string (gboolean ret) +{ + return ret ? "yes" : "no"; +} + +/** + * up_device_to_text_time_to_string: + **/ +static gchar * +up_device_to_text_time_to_string (gint seconds) +{ + gfloat value = seconds; + + if (value < 0) + return g_strdup ("unknown"); + if (value < 60) + return g_strdup_printf ("%.0f seconds", value); + value /= 60.0; + if (value < 60) + return g_strdup_printf ("%.1f minutes", value); + value /= 60.0; + if (value < 60) + return g_strdup_printf ("%.1f hours", value); + value /= 24.0; + return g_strdup_printf ("%.1f days", value); +} + +/** + * up_device_to_text: + * + * Return value: text representation of #UpDevice + * + * Since: 0.9.0 + **/ +gchar * +up_device_to_text (UpDevice *device) +{ + struct tm *time_tm; + time_t t; + gchar time_buf[256]; + gchar *time_str; + GString *string; + + g_return_val_if_fail (UP_IS_DEVICE (device), NULL); + + /* get a human readable time */ + t = (time_t) device->priv->update_time; + time_tm = localtime (&t); + strftime (time_buf, sizeof time_buf, "%c", time_tm); + + string = g_string_new (""); + g_string_printf (string, " native-path: %s\n", device->priv->native_path); + if (device->priv->vendor != NULL && device->priv->vendor[0] != '\0') + g_string_printf (string, " vendor: %s\n", device->priv->vendor); + if (device->priv->model != NULL && device->priv->model[0] != '\0') + g_string_printf (string, " model: %s\n", device->priv->model); + if (device->priv->serial != NULL && device->priv->serial[0] != '\0') + g_string_printf (string, " serial: %s\n", device->priv->serial); + g_string_printf (string, " power supply: %s\n", up_device_bool_to_string (device->priv->power_supply)); + g_string_printf (string, " updated: %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - device->priv->update_time)); + g_string_printf (string, " has history: %s\n", up_device_bool_to_string (device->priv->has_history)); + g_string_printf (string, " has statistics: %s\n", up_device_bool_to_string (device->priv->has_statistics)); + g_string_printf (string, " %s\n", up_device_type_to_string (device->priv->type)); + + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MOUSE || + device->priv->type == UP_DEVICE_TYPE_KEYBOARD || + device->priv->type == UP_DEVICE_TYPE_UPS) + g_string_printf (string, " present: %s\n", up_device_bool_to_string (device->priv->is_present)); + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MOUSE || + device->priv->type == UP_DEVICE_TYPE_KEYBOARD) + g_string_printf (string, " rechargeable: %s\n", up_device_bool_to_string (device->priv->is_rechargeable)); + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MOUSE || + device->priv->type == UP_DEVICE_TYPE_KEYBOARD || + device->priv->type == UP_DEVICE_TYPE_UPS) + g_string_printf (string, " state: %s\n", up_device_state_to_string (device->priv->state)); + if (device->priv->type == UP_DEVICE_TYPE_BATTERY) { + g_string_printf (string, " energy: %g Wh\n", device->priv->energy); + g_string_printf (string, " energy-empty: %g Wh\n", device->priv->energy_empty); + g_string_printf (string, " energy-full: %g Wh\n", device->priv->energy_full); + g_string_printf (string, " energy-full-design: %g Wh\n", device->priv->energy_full_design); + } + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MONITOR) + g_string_printf (string, " energy-rate: %g W\n", device->priv->energy_rate); + if (device->priv->type == UP_DEVICE_TYPE_UPS || + device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MONITOR) { + if (device->priv->voltage > 0) + g_string_printf (string, " voltage: %g V\n", device->priv->voltage); + } + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_UPS) { + if (device->priv->time_to_full > 0) { + time_str = up_device_to_text_time_to_string (device->priv->time_to_full); + g_string_printf (string, " time to full: %s\n", time_str); + g_free (time_str); + } + if (device->priv->time_to_empty > 0) { + time_str = up_device_to_text_time_to_string (device->priv->time_to_empty); + g_string_printf (string, " time to empty: %s\n", time_str); + g_free (time_str); + } + } + if (device->priv->type == UP_DEVICE_TYPE_BATTERY || + device->priv->type == UP_DEVICE_TYPE_MOUSE || + device->priv->type == UP_DEVICE_TYPE_KEYBOARD || + device->priv->type == UP_DEVICE_TYPE_UPS) + g_string_printf (string, " percentage: %g%%\n", device->priv->percentage); + if (device->priv->type == UP_DEVICE_TYPE_BATTERY) { + if (device->priv->capacity > 0) + g_string_printf (string, " capacity: %g%%\n", device->priv->capacity); + } + if (device->priv->type == UP_DEVICE_TYPE_BATTERY) { + if (device->priv->technology != UP_DEVICE_TECHNOLOGY_UNKNOWN) + g_string_printf (string, " technology: %s\n", up_device_technology_to_string (device->priv->technology)); + } + if (device->priv->type == UP_DEVICE_TYPE_LINE_POWER) + g_string_printf (string, " online: %s\n", up_device_bool_to_string (device->priv->online)); + if (device->priv->type == UP_DEVICE_TYPE_BATTERY) { + if (device->priv->recall_notice) { + g_string_printf (string, " recall vendor: %s\n", device->priv->recall_vendor); + g_string_printf (string, " recall url: %s\n", device->priv->recall_url); + } + } + + return g_string_free (string, FALSE); +} + +/** + * up_device_refresh_sync: + * + * Refreshes properties on the device. + * This function is normally not required. + * + * Return value: #TRUE for success, else #FALSE and @error is used + * + * Since: 0.9.0 + **/ +gboolean +up_device_refresh_sync (UpDevice *device, GError **error) +{ + GError *error_local = NULL; + gboolean ret; + + g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); + g_return_val_if_fail (device->priv->proxy_device != NULL, FALSE); + + /* just refresh the device */ + ret = dbus_g_proxy_call (device->priv->proxy_device, "Refresh", &error_local, + G_TYPE_INVALID, G_TYPE_INVALID); + if (!ret) { + g_set_error (error, 1, 0, "Refresh() on %s failed: %s", device->priv->object_path, error_local->message); + g_error_free (error_local); + goto out; + } +out: + return ret; +} + +/** + * up_device_set_property: + **/ +static void +up_device_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + UpDevice *device = UP_DEVICE (object); + + switch (prop_id) { + case PROP_NATIVE_PATH: + g_free (device->priv->native_path); + device->priv->native_path = g_strdup (g_value_get_string (value)); + break; + case PROP_VENDOR: + g_free (device->priv->vendor); + device->priv->vendor = g_strdup (g_value_get_string (value)); + break; + case PROP_MODEL: + g_free (device->priv->model); + device->priv->model = g_strdup (g_value_get_string (value)); + break; + case PROP_SERIAL: + g_free (device->priv->serial); + device->priv->serial = g_strdup (g_value_get_string (value)); + break; + case PROP_UPDATE_TIME: + device->priv->update_time = g_value_get_uint64 (value); + break; + case PROP_TYPE: + device->priv->type = g_value_get_uint (value); + break; + case PROP_POWER_SUPPLY: + device->priv->power_supply = g_value_get_boolean (value); + break; + case PROP_ONLINE: + device->priv->online = g_value_get_boolean (value); + break; + case PROP_IS_PRESENT: + device->priv->is_present = g_value_get_boolean (value); + break; + case PROP_IS_RECHARGEABLE: + device->priv->is_rechargeable = g_value_get_boolean (value); + break; + case PROP_HAS_HISTORY: + device->priv->has_history = g_value_get_boolean (value); + break; + case PROP_HAS_STATISTICS: + device->priv->has_statistics = g_value_get_boolean (value); + break; + case PROP_STATE: + device->priv->state = g_value_get_uint (value); + break; + case PROP_CAPACITY: + device->priv->capacity = g_value_get_double (value); + break; + case PROP_ENERGY: + device->priv->energy = g_value_get_double (value); + break; + case PROP_ENERGY_EMPTY: + device->priv->energy_empty = g_value_get_double (value); + break; + case PROP_ENERGY_FULL: + device->priv->energy_full = g_value_get_double (value); + break; + case PROP_ENERGY_FULL_DESIGN: + device->priv->energy_full_design = g_value_get_double (value); + break; + case PROP_ENERGY_RATE: + device->priv->energy_rate = g_value_get_double (value); + break; + case PROP_VOLTAGE: + device->priv->voltage = g_value_get_double (value); + break; + case PROP_TIME_TO_EMPTY: + device->priv->time_to_empty = g_value_get_int64 (value); + break; + case PROP_TIME_TO_FULL: + device->priv->time_to_full = g_value_get_int64 (value); + break; + case PROP_PERCENTAGE: + device->priv->percentage = g_value_get_double (value); + break; + case PROP_TECHNOLOGY: + device->priv->technology = g_value_get_uint (value); + break; + case PROP_RECALL_NOTICE: + device->priv->recall_notice = g_value_get_boolean (value); + break; + case PROP_RECALL_VENDOR: + g_free (device->priv->recall_vendor); + device->priv->recall_vendor = g_strdup (g_value_get_string (value)); + break; + case PROP_RECALL_URL: + g_free (device->priv->recall_url); + device->priv->recall_url = g_strdup (g_value_get_string (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/** + * up_device_get_property: + **/ +static void +up_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + UpDevice *device = UP_DEVICE (object); + + switch (prop_id) { + case PROP_UPDATE_TIME: + g_value_set_uint64 (value, device->priv->update_time); + break; + case PROP_VENDOR: + g_value_set_string (value, device->priv->vendor); + break; + case PROP_MODEL: + g_value_set_string (value, device->priv->model); + break; + case PROP_SERIAL: + g_value_set_string (value, device->priv->serial); + break; + case PROP_NATIVE_PATH: + g_value_set_string (value, device->priv->native_path); + break; + case PROP_POWER_SUPPLY: + g_value_set_boolean (value, device->priv->power_supply); + break; + case PROP_ONLINE: + g_value_set_boolean (value, device->priv->online); + break; + case PROP_IS_PRESENT: + g_value_set_boolean (value, device->priv->is_present); + break; + case PROP_IS_RECHARGEABLE: + g_value_set_boolean (value, device->priv->is_rechargeable); + break; + case PROP_HAS_HISTORY: + g_value_set_boolean (value, device->priv->has_history); + break; + case PROP_HAS_STATISTICS: + g_value_set_boolean (value, device->priv->has_statistics); + break; + case PROP_TYPE: + g_value_set_uint (value, device->priv->type); + break; + case PROP_STATE: + g_value_set_uint (value, device->priv->state); + break; + case PROP_TECHNOLOGY: + g_value_set_uint (value, device->priv->technology); + break; + case PROP_CAPACITY: + g_value_set_double (value, device->priv->capacity); + break; + case PROP_ENERGY: + g_value_set_double (value, device->priv->energy); + break; + case PROP_ENERGY_EMPTY: + g_value_set_double (value, device->priv->energy_empty); + break; + case PROP_ENERGY_FULL: + g_value_set_double (value, device->priv->energy_full); + break; + case PROP_ENERGY_FULL_DESIGN: + g_value_set_double (value, device->priv->energy_full_design); + break; + case PROP_ENERGY_RATE: + g_value_set_double (value, device->priv->energy_rate); + break; + case PROP_VOLTAGE: + g_value_set_double (value, device->priv->voltage); + break; + case PROP_TIME_TO_EMPTY: + g_value_set_int64 (value, device->priv->time_to_empty); + break; + case PROP_TIME_TO_FULL: + g_value_set_int64 (value, device->priv->time_to_full); + break; + case PROP_PERCENTAGE: + g_value_set_double (value, device->priv->percentage); + break; + case PROP_RECALL_NOTICE: + g_value_set_boolean (value, device->priv->recall_notice); + break; + case PROP_RECALL_VENDOR: + g_value_set_string (value, device->priv->recall_vendor); + break; + case PROP_RECALL_URL: + g_value_set_string (value, device->priv->recall_url); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +/** + * up_device_class_init: + **/ +static void +up_device_class_init (UpDeviceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = up_device_finalize; + object_class->set_property = up_device_set_property; + object_class->get_property = up_device_get_property; + + /** + * PkClient::changed: + * @device: the #UpDevice instance that emitted the signal + * + * The ::changed signal is emitted when the device data has changed. + * + * Since: 0.9.0 + **/ + signals [SIGNAL_CHANGED] = + g_signal_new ("changed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (UpDeviceClass, changed), + NULL, NULL, g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + /** + * UpDevice:update-time: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_UPDATE_TIME, + g_param_spec_uint64 ("update-time", + NULL, NULL, + 0, G_MAXUINT64, 0, + G_PARAM_READWRITE)); + /** + * UpDevice:vendor: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_VENDOR, + g_param_spec_string ("vendor", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + /** + * UpDevice:model: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_MODEL, + g_param_spec_string ("model", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + /** + * UpDevice:serial: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_SERIAL, + g_param_spec_string ("serial", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + /** + * UpDevice:native-path: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_NATIVE_PATH, + g_param_spec_string ("native-path", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + /** + * UpDevice:power-supply: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_POWER_SUPPLY, + g_param_spec_boolean ("power-supply", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:online: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ONLINE, + g_param_spec_boolean ("online", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:is-present: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_IS_PRESENT, + g_param_spec_boolean ("is-present", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:is-rechargeable: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_IS_RECHARGEABLE, + g_param_spec_boolean ("is-rechargeable", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:has-history: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_HAS_HISTORY, + g_param_spec_boolean ("has-history", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:has-statistics: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_HAS_STATISTICS, + g_param_spec_boolean ("has-statistics", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:type: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_TYPE, + g_param_spec_uint ("type", + NULL, NULL, + UP_DEVICE_TYPE_UNKNOWN, + UP_DEVICE_TYPE_LAST, + UP_DEVICE_TYPE_UNKNOWN, + G_PARAM_READWRITE)); + /** + * UpDevice:state: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_STATE, + g_param_spec_uint ("state", + NULL, NULL, + UP_DEVICE_STATE_UNKNOWN, + UP_DEVICE_STATE_LAST, + UP_DEVICE_STATE_UNKNOWN, + G_PARAM_READWRITE)); + /** + * UpDevice:technology: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_TECHNOLOGY, + g_param_spec_uint ("technology", + NULL, NULL, + UP_DEVICE_TECHNOLOGY_UNKNOWN, + UP_DEVICE_TECHNOLOGY_LAST, + UP_DEVICE_TECHNOLOGY_UNKNOWN, + G_PARAM_READWRITE)); + /** + * UpDevice:capacity: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_CAPACITY, + g_param_spec_double ("capacity", NULL, NULL, + 0.0, 100.f, 100.0, + G_PARAM_READWRITE)); + /** + * UpDevice:energy: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ENERGY, + g_param_spec_double ("energy", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:energy-empty: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ENERGY_EMPTY, + g_param_spec_double ("energy-empty", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:energy-full: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ENERGY_FULL, + g_param_spec_double ("energy-full", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:energy-full-design: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ENERGY_FULL_DESIGN, + g_param_spec_double ("energy-full-design", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:energy-rate: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_ENERGY_RATE, + g_param_spec_double ("energy-rate", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:voltage: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_VOLTAGE, + g_param_spec_double ("voltage", NULL, NULL, + 0.0, G_MAXDOUBLE, 0.0, + G_PARAM_READWRITE)); + /** + * UpDevice:time-to-empty: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_TIME_TO_EMPTY, + g_param_spec_int64 ("time-to-empty", NULL, NULL, + 0, G_MAXINT64, 0, + G_PARAM_READWRITE)); + /** + * UpDevice:time-to-full: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_TIME_TO_FULL, + g_param_spec_int64 ("time-to-full", NULL, NULL, + 0, G_MAXINT64, 0, + G_PARAM_READWRITE)); + /** + * UpDevice:percentage: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_PERCENTAGE, + g_param_spec_double ("percentage", NULL, NULL, + 0.0, 100.f, 100.0, + G_PARAM_READWRITE)); + /** + * UpDevice:recall-notice: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_RECALL_NOTICE, + g_param_spec_boolean ("recall-notice", + NULL, NULL, + FALSE, + G_PARAM_READWRITE)); + /** + * UpDevice:recall-vendor: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_RECALL_VENDOR, + g_param_spec_string ("recall-vendor", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + /** + * UpDevice:recall-url: + * + * Since: 0.9.0 + **/ + g_object_class_install_property (object_class, + PROP_RECALL_URL, + g_param_spec_string ("recall-url", + NULL, NULL, + NULL, + G_PARAM_READWRITE)); + + g_type_class_add_private (klass, sizeof (UpDevicePrivate)); +} + +/** + * up_device_init: + **/ +static void +up_device_init (UpDevice *device) +{ + device->priv = UP_DEVICE_GET_PRIVATE (device); + device->priv->object_path = NULL; + device->priv->proxy_device = NULL; + device->priv->proxy_props = NULL; +} + +/** + * up_device_finalize: + **/ +static void +up_device_finalize (GObject *object) +{ + UpDevice *device; + + g_return_if_fail (UP_IS_DEVICE (object)); + + device = UP_DEVICE (object); + + g_free (device->priv->object_path); + g_free (device->priv->vendor); + g_free (device->priv->model); + g_free (device->priv->serial); + g_free (device->priv->native_path); + g_free (device->priv->recall_vendor); + g_free (device->priv->recall_url); + if (device->priv->proxy_device != NULL) + g_object_unref (device->priv->proxy_device); + if (device->priv->proxy_props != NULL) + g_object_unref (device->priv->proxy_props); + + G_OBJECT_CLASS (up_device_parent_class)->finalize (object); +} + +/** + * up_device_new: + * + * Return value: a new UpDevice object. + * + * Since: 0.9.0 + **/ +UpDevice * +up_device_new (void) +{ + UpDevice *device; + device = g_object_new (UP_TYPE_DEVICE, NULL); + return UP_DEVICE (device); +} + diff --git a/libupower-glib/up-device.h b/libupower-glib/up-device.h new file mode 100644 index 0000000..f9b2db4 --- /dev/null +++ b/libupower-glib/up-device.h @@ -0,0 +1,94 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#if !defined (__UPOWER_H_INSIDE__) && !defined (UP_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __UP_DEVICE_H +#define __UP_DEVICE_H + +#include +#include + +G_BEGIN_DECLS + +#define UP_TYPE_DEVICE (up_device_get_type ()) +#define UP_DEVICE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UP_TYPE_DEVICE, UpDevice)) +#define UP_DEVICE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), UP_TYPE_DEVICE, UpDeviceClass)) +#define UP_IS_DEVICE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UP_TYPE_DEVICE)) +#define UP_IS_DEVICE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), UP_TYPE_DEVICE)) +#define UP_DEVICE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UP_TYPE_DEVICE, UpDeviceClass)) +#define UP_DEVICE_ERROR (up_device_error_quark ()) +#define UP_DEVICE_TYPE_ERROR (up_device_error_get_type ()) + +typedef struct UpDevicePrivate UpDevicePrivate; + +typedef struct +{ + GObject parent; + UpDevicePrivate *priv; +} UpDevice; + +typedef struct +{ + GObjectClass parent_class; + void (*changed) (UpDevice *device, + gpointer *obj); + /*< private >*/ + /* Padding for future expansion */ + void (*_up_device_reserved1) (void); + void (*_up_device_reserved2) (void); + void (*_up_device_reserved3) (void); + void (*_up_device_reserved4) (void); + void (*_up_device_reserved5) (void); + void (*_up_device_reserved6) (void); + void (*_up_device_reserved7) (void); + void (*_up_device_reserved8) (void); +} UpDeviceClass; + +/* general */ +GType up_device_get_type (void); +UpDevice *up_device_new (void); +gchar *up_device_to_text (UpDevice *device); + +/* sync versions */ +gboolean up_device_refresh_sync (UpDevice *device, + GError **error); +gboolean up_device_set_object_path_sync (UpDevice *device, + const gchar *object_path, + GError **error); +GPtrArray *up_device_get_history_sync (UpDevice *device, + const gchar *type, + guint timespec, + guint resolution, + GError **error); +GPtrArray *up_device_get_statistics_sync (UpDevice *device, + const gchar *type, + GError **error); + +/* accessors */ +const gchar *up_device_get_object_path (UpDevice *device); + +G_END_DECLS + +#endif /* __UP_DEVICE_H */ + diff --git a/libupower-glib/up-types.c b/libupower-glib/up-types.c new file mode 100644 index 0000000..406b346 --- /dev/null +++ b/libupower-glib/up-types.c @@ -0,0 +1,279 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "up-types.h" + +/** + * up_device_type_to_string: + * + * Converts a #UpDeviceType to a string. + * + * Return value: identifier string + * + * Since: 0.9.0 + **/ +const gchar * +up_device_type_to_string (UpDeviceType type_enum) +{ + const gchar *type = NULL; + switch (type_enum) { + case UP_DEVICE_TYPE_LINE_POWER: + type = "line-power"; + break; + case UP_DEVICE_TYPE_BATTERY: + type = "battery"; + break; + case UP_DEVICE_TYPE_UPS: + type = "ups"; + break; + case UP_DEVICE_TYPE_MONITOR: + type = "monitor"; + break; + case UP_DEVICE_TYPE_MOUSE: + type = "mouse"; + break; + case UP_DEVICE_TYPE_KEYBOARD: + type = "keyboard"; + break; + case UP_DEVICE_TYPE_PDA: + type = "pda"; + break; + case UP_DEVICE_TYPE_PHONE: + type = "phone"; + break; + default: + type = "unknown"; + break; + } + return type; +} + +/** + * up_device_type_from_string: + * + * Converts a string to a #UpDeviceType. + * + * Return value: enumerated value + * + * Since: 0.9.0 + **/ +UpDeviceType +up_device_type_from_string (const gchar *type) +{ + if (type == NULL) + return UP_DEVICE_TYPE_UNKNOWN; + if (g_strcmp0 (type, "line-power") == 0) + return UP_DEVICE_TYPE_LINE_POWER; + if (g_strcmp0 (type, "battery") == 0) + return UP_DEVICE_TYPE_BATTERY; + if (g_strcmp0 (type, "ups") == 0) + return UP_DEVICE_TYPE_UPS; + if (g_strcmp0 (type, "monitor") == 0) + return UP_DEVICE_TYPE_MONITOR; + if (g_strcmp0 (type, "mouse") == 0) + return UP_DEVICE_TYPE_MOUSE; + if (g_strcmp0 (type, "keyboard") == 0) + return UP_DEVICE_TYPE_KEYBOARD; + if (g_strcmp0 (type, "pda") == 0) + return UP_DEVICE_TYPE_PDA; + if (g_strcmp0 (type, "phone") == 0) + return UP_DEVICE_TYPE_PHONE; + return UP_DEVICE_TYPE_UNKNOWN; +} + +/** + * up_device_state_to_string: + * + * Converts a #UpDeviceState to a string. + * + * Return value: identifier string + * + * Since: 0.9.0 + **/ +const gchar * +up_device_state_to_string (UpDeviceState state_enum) +{ + const gchar *state = NULL; + switch (state_enum) { + case UP_DEVICE_STATE_CHARGING: + state = "charging"; + break; + case UP_DEVICE_STATE_DISCHARGING: + state = "discharging"; + break; + case UP_DEVICE_STATE_EMPTY: + state = "empty"; + break; + case UP_DEVICE_STATE_FULLY_CHARGED: + state = "fully-charged"; + break; + case UP_DEVICE_STATE_PENDING_CHARGE: + state = "pending-charge"; + break; + case UP_DEVICE_STATE_PENDING_DISCHARGE: + state = "pending-discharge"; + break; + default: + state = "unknown"; + break; + } + return state; +} + +/** + * up_device_state_from_string: + * + * Converts a string to a #UpDeviceState. + * + * Return value: enumerated value + * + * Since: 0.9.0 + **/ +UpDeviceState +up_device_state_from_string (const gchar *state) +{ + if (state == NULL) + return UP_DEVICE_STATE_UNKNOWN; + if (g_strcmp0 (state, "charging") == 0) + return UP_DEVICE_STATE_CHARGING; + if (g_strcmp0 (state, "discharging") == 0) + return UP_DEVICE_STATE_DISCHARGING; + if (g_strcmp0 (state, "empty") == 0) + return UP_DEVICE_STATE_EMPTY; + if (g_strcmp0 (state, "fully-charged") == 0) + return UP_DEVICE_STATE_FULLY_CHARGED; + if (g_strcmp0 (state, "pending-charge") == 0) + return UP_DEVICE_STATE_PENDING_CHARGE; + if (g_strcmp0 (state, "pending-discharge") == 0) + return UP_DEVICE_STATE_PENDING_DISCHARGE; + return UP_DEVICE_STATE_UNKNOWN; +} + +/** + * up_device_technology_to_string: + * + * Converts a #UpDeviceTechnology to a string. + * + * Return value: identifier string + * + * Since: 0.9.0 + **/ +const gchar * +up_device_technology_to_string (UpDeviceTechnology technology_enum) +{ + const gchar *technology = NULL; + switch (technology_enum) { + case UP_DEVICE_TECHNOLOGY_LITHIUM_ION: + technology = "lithium-ion"; + break; + case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER: + technology = "lithium-polymer"; + break; + case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE: + technology = "lithium-iron-phosphate"; + break; + case UP_DEVICE_TECHNOLOGY_LEAD_ACID: + technology = "lead-acid"; + break; + case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM: + technology = "nickel-cadmium"; + break; + case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE: + technology = "nickel-metal-hydride"; + break; + default: + technology = "unknown"; + break; + } + return technology; +} + +/** + * up_device_technology_from_string: + * + * Converts a string to a #UpDeviceTechnology. + * + * Return value: enumerated value + * + * Since: 0.9.0 + **/ +UpDeviceTechnology +up_device_technology_from_string (const gchar *technology) +{ + if (technology == NULL) + return UP_DEVICE_TECHNOLOGY_UNKNOWN; + if (g_strcmp0 (technology, "lithium-ion") == 0) + return UP_DEVICE_TECHNOLOGY_LITHIUM_ION; + if (g_strcmp0 (technology, "lithium-polymer") == 0) + return UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER; + if (g_strcmp0 (technology, "lithium-iron-phosphate") == 0) + return UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE; + if (g_strcmp0 (technology, "lead-acid") == 0) + return UP_DEVICE_TECHNOLOGY_LEAD_ACID; + if (g_strcmp0 (technology, "nickel-cadmium") == 0) + return UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM; + if (g_strcmp0 (technology, "nickel-metal-hydride") == 0) + return UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE; + return UP_DEVICE_TECHNOLOGY_UNKNOWN; +} + +/** + * up_qos_type_to_string: + * + * Converts a #UpQosType to a string. + * + * Return value: identifier string + * + * Since: 0.9.0 + **/ +const gchar * +up_qos_type_to_string (UpQosType type) +{ + if (type == UP_QOS_TYPE_NETWORK) + return "network"; + if (type == UP_QOS_TYPE_CPU_DMA) + return "cpu_dma"; + return NULL; +} + +/** + * up_qos_type_from_string: + * + * Converts a string to a #UpQosType. + * + * Return value: enumerated value + * + * Since: 0.9.0 + **/ +UpQosType +up_qos_type_from_string (const gchar *type) +{ + if (g_strcmp0 (type, "network") == 0) + return UP_QOS_TYPE_NETWORK; + if (g_strcmp0 (type, "cpu_dma") == 0) + return UP_QOS_TYPE_CPU_DMA; + return UP_QOS_TYPE_UNKNOWN; +} + diff --git a/libupower-glib/up-types.h b/libupower-glib/up-types.h new file mode 100644 index 0000000..05f25b2 --- /dev/null +++ b/libupower-glib/up-types.h @@ -0,0 +1,87 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#if !defined (__UPOWER_H_INSIDE__) && !defined (UP_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __UP_TYPES_H +#define __UP_TYPES_H + +#include + +G_BEGIN_DECLS + +typedef enum { + UP_DEVICE_TYPE_UNKNOWN, + UP_DEVICE_TYPE_LINE_POWER, + UP_DEVICE_TYPE_BATTERY, + UP_DEVICE_TYPE_UPS, + UP_DEVICE_TYPE_MONITOR, + UP_DEVICE_TYPE_MOUSE, + UP_DEVICE_TYPE_KEYBOARD, + UP_DEVICE_TYPE_PDA, + UP_DEVICE_TYPE_PHONE, + UP_DEVICE_TYPE_LAST +} UpDeviceType; + +typedef enum { + UP_DEVICE_STATE_UNKNOWN, + UP_DEVICE_STATE_CHARGING, + UP_DEVICE_STATE_DISCHARGING, + UP_DEVICE_STATE_EMPTY, + UP_DEVICE_STATE_FULLY_CHARGED, + UP_DEVICE_STATE_PENDING_CHARGE, + UP_DEVICE_STATE_PENDING_DISCHARGE, + UP_DEVICE_STATE_LAST +} UpDeviceState; + +typedef enum { + UP_DEVICE_TECHNOLOGY_UNKNOWN, + UP_DEVICE_TECHNOLOGY_LITHIUM_ION, + UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER, + UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE, + UP_DEVICE_TECHNOLOGY_LEAD_ACID, + UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM, + UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE, + UP_DEVICE_TECHNOLOGY_LAST +} UpDeviceTechnology; + +typedef enum { + UP_QOS_TYPE_UNKNOWN, + UP_QOS_TYPE_NETWORK, + UP_QOS_TYPE_CPU_DMA, + UP_QOS_TYPE_LAST +} UpQosType; + +const gchar *up_device_type_to_string (UpDeviceType type_enum); +const gchar *up_device_state_to_string (UpDeviceState state_enum); +const gchar *up_device_technology_to_string (UpDeviceTechnology technology_enum); +UpDeviceType up_device_type_from_string (const gchar *type); +UpDeviceState up_device_state_from_string (const gchar *state); +UpDeviceTechnology up_device_technology_from_string (const gchar *technology); +const gchar *up_qos_type_to_string (UpQosType type); +UpQosType up_qos_type_from_string (const gchar *type); + +G_END_DECLS + +#endif /* __UP_TYPES_H */ + diff --git a/libupower-glib/up-version.h.in b/libupower-glib/up-version.h.in new file mode 100644 index 0000000..2a89c50 --- /dev/null +++ b/libupower-glib/up-version.h.in @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2009-2010 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#if !defined (__UPOWER_H_INSIDE__) && !defined (UP_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __UP_VERSION_H +#define __UP_VERSION_H + +/* compile time version + */ +#define UP_COMPILE_VERSION (0x@VERSION@) + +/* check whether a the version is above the compile time version. + */ +#define UP_CHECK_VERSION(ver) \ + (UP_COMPILE_VERSION > (ver) || \ + (UP_COMPILE_VERSION == (ver))) + +#endif /* __UP_VERSION_H */ diff --git a/libupower-glib/upower.h b/libupower-glib/upower.h new file mode 100644 index 0000000..f9a1e7c --- /dev/null +++ b/libupower-glib/upower.h @@ -0,0 +1,35 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008-2010 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __UPOWER_H__ +#define __UPOWER_H__ + +#define __UPOWER_H_INSIDE__ + +#include +#include +#include +#include + +#undef __UPOWER_H_INSIDE__ + +#endif /* __UPOWER_H__ */ + -- cgit v1.2.1