mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-04-19 20:40:40 +02:00
split out the device list abstraction as a proper GObject so we can easily get the device list for the power queries
This commit is contained in:
parent
4e4859c9cb
commit
eed12da295
4 changed files with 277 additions and 53 deletions
|
|
@ -44,6 +44,7 @@ dbusif_DATA = org.freedesktop.DeviceKit.Power.xml org.freedesktop.DeviceKit.Powe
|
|||
devkit_power_daemon_SOURCES = \
|
||||
dkp-daemon.h dkp-daemon.c \
|
||||
dkp-device.h dkp-device.c \
|
||||
dkp-device-list.h dkp-device-list.c \
|
||||
dkp-source.h dkp-source.c \
|
||||
dkp-history.h dkp-history.c \
|
||||
sysfs-utils.h sysfs-utils.c \
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@
|
|||
#include "dkp-debug.h"
|
||||
#include "dkp-daemon.h"
|
||||
#include "dkp-device.h"
|
||||
#include "dkp-device-list.h"
|
||||
|
||||
#include "dkp-daemon-glue.h"
|
||||
#include "dkp-marshal.h"
|
||||
|
|
@ -57,7 +58,7 @@ struct DkpDaemonPrivate
|
|||
PolKitContext *pk_context;
|
||||
PolKitTracker *pk_tracker;
|
||||
|
||||
GHashTable *map_native_path_to_device;
|
||||
DkpDeviceList *list;
|
||||
gboolean on_battery;
|
||||
gboolean low_battery;
|
||||
|
||||
|
|
@ -200,7 +201,7 @@ dkp_daemon_init (DkpDaemon *daemon)
|
|||
daemon->priv->on_battery = FALSE;
|
||||
/* as soon as _all_ batteries are low, this is true */
|
||||
daemon->priv->low_battery = FALSE;
|
||||
daemon->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
daemon->priv->list = dkp_device_list_new ();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -233,8 +234,8 @@ dkp_daemon_finalize (GObject *object)
|
|||
if (daemon->priv->devkit_client != NULL)
|
||||
g_object_unref (daemon->priv->devkit_client);
|
||||
|
||||
if (daemon->priv->map_native_path_to_device != NULL)
|
||||
g_hash_table_unref (daemon->priv->map_native_path_to_device);
|
||||
if (daemon->priv->list != NULL)
|
||||
g_object_unref (daemon->priv->list);
|
||||
|
||||
G_OBJECT_CLASS (dkp_daemon_parent_class)->finalize (object);
|
||||
}
|
||||
|
|
@ -317,47 +318,31 @@ static void
|
|||
gpk_daemon_device_changed (DkpDaemon *daemon, DevkitDevice *d, gboolean synthesized)
|
||||
{
|
||||
DkpDevice *device;
|
||||
const gchar *native_path;
|
||||
|
||||
native_path = devkit_device_get_native_path (d);
|
||||
device = g_hash_table_lookup (daemon->priv->map_native_path_to_device, native_path);
|
||||
/* does the device exist in the db? */
|
||||
device = dkp_device_list_lookup (daemon->priv->list, d);
|
||||
if (device != NULL) {
|
||||
if (!dkp_device_changed (device, d, synthesized)) {
|
||||
dkp_debug ("changed triggered remove on %s", native_path);
|
||||
dkp_debug ("changed triggered remove on %s", dkp_device_get_object_path (device));
|
||||
gpk_daemon_device_remove (daemon, d);
|
||||
} else {
|
||||
dkp_debug ("changed %s", native_path);
|
||||
dkp_debug ("changed %s", dkp_device_get_object_path (device));
|
||||
}
|
||||
} else {
|
||||
dkp_debug ("treating change event as add on %s", native_path);
|
||||
dkp_debug ("treating change event as add on %s", dkp_device_get_object_path (device));
|
||||
gpk_daemon_device_add (daemon, d, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* gpk_daemon_device_went_away_remove_cb:
|
||||
**/
|
||||
static gboolean
|
||||
gpk_daemon_device_went_away_remove_cb (gpointer key, gpointer value, gpointer user_data)
|
||||
{
|
||||
if (value == user_data) {
|
||||
dkp_debug ("removed %s", (char *) key);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* gpk_daemon_device_went_away:
|
||||
**/
|
||||
static void
|
||||
gpk_daemon_device_went_away (gpointer user_data, GObject *where_the_object_was)
|
||||
gpk_daemon_device_went_away (gpointer user_data, GObject *_device)
|
||||
{
|
||||
DkpDaemon *daemon = DKP_DAEMON (user_data);
|
||||
|
||||
g_hash_table_foreach_remove (daemon->priv->map_native_path_to_device,
|
||||
gpk_daemon_device_went_away_remove_cb,
|
||||
where_the_object_was);
|
||||
DkpDevice *device = DKP_DEVICE (_device);
|
||||
dkp_device_list_remove (daemon->priv->list, device);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -367,13 +352,12 @@ static void
|
|||
gpk_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event)
|
||||
{
|
||||
DkpDevice *device;
|
||||
const gchar *native_path;
|
||||
|
||||
native_path = devkit_device_get_native_path (d);
|
||||
device = g_hash_table_lookup (daemon->priv->map_native_path_to_device, native_path);
|
||||
/* does device exist in db? */
|
||||
device = dkp_device_list_lookup (daemon->priv->list, d);
|
||||
if (device != NULL) {
|
||||
/* we already have the device; treat as change event */
|
||||
dkp_debug ("treating add event as change event on %s", native_path);
|
||||
dkp_debug ("treating add event as change event on %s", dkp_device_get_object_path (device));
|
||||
gpk_daemon_device_changed (daemon, d, FALSE);
|
||||
} else {
|
||||
device = dkp_device_new (daemon, d);
|
||||
|
|
@ -384,16 +368,13 @@ gpk_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event)
|
|||
* dbus-glib, no cookie for you.
|
||||
*/
|
||||
g_object_weak_ref (G_OBJECT (device), gpk_daemon_device_went_away, daemon);
|
||||
g_hash_table_insert (daemon->priv->map_native_path_to_device,
|
||||
g_strdup (native_path),
|
||||
device);
|
||||
dkp_debug ("added %s", native_path);
|
||||
dkp_device_list_insert (daemon->priv->list, d, device);
|
||||
if (emit_event) {
|
||||
g_signal_emit (daemon, signals[DEVICE_ADDED_SIGNAL], 0,
|
||||
dkp_device_get_object_path (device));
|
||||
}
|
||||
} else {
|
||||
dkp_debug ("ignoring add event on %s", native_path);
|
||||
dkp_debug ("ignoring add event on %s", dkp_device_get_object_path (device));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -405,12 +386,11 @@ static void
|
|||
gpk_daemon_device_remove (DkpDaemon *daemon, DevkitDevice *d)
|
||||
{
|
||||
DkpDevice *device;
|
||||
const gchar *native_path;
|
||||
|
||||
native_path = devkit_device_get_native_path (d);
|
||||
device = g_hash_table_lookup (daemon->priv->map_native_path_to_device, native_path);
|
||||
/* does device exist in db? */
|
||||
device = dkp_device_list_lookup (daemon->priv->list, d);
|
||||
if (device == NULL) {
|
||||
dkp_debug ("ignoring remove event on %s", native_path);
|
||||
dkp_debug ("ignoring remove event on %s", dkp_device_get_object_path (device));
|
||||
} else {
|
||||
dkp_device_removed (device);
|
||||
g_signal_emit (daemon, signals[DEVICE_REMOVED_SIGNAL], 0,
|
||||
|
|
@ -667,27 +647,29 @@ gpk_daemon_throw_error (DBusGMethodInvocation *context, int error_code, const ch
|
|||
|
||||
/* exported methods */
|
||||
|
||||
/**
|
||||
* gpk_daemon_enumerate_cb:
|
||||
**/
|
||||
static void
|
||||
gpk_daemon_enumerate_cb (gpointer key, gpointer value, gpointer user_data)
|
||||
{
|
||||
DkpDevice *device = DKP_DEVICE (value);
|
||||
GPtrArray *object_paths = user_data;
|
||||
g_ptr_array_add (object_paths, g_strdup (dkp_device_get_object_path (device)));
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_daemon_enumerate_devices:
|
||||
**/
|
||||
gboolean
|
||||
dkp_daemon_enumerate_devices (DkpDaemon *daemon, DBusGMethodInvocation *context)
|
||||
{
|
||||
guint i;
|
||||
const GPtrArray *array;
|
||||
GPtrArray *object_paths;
|
||||
DkpDevice *device;
|
||||
|
||||
/* build a pointer array of the object paths */
|
||||
object_paths = g_ptr_array_new ();
|
||||
g_hash_table_foreach (daemon->priv->map_native_path_to_device, gpk_daemon_enumerate_cb, object_paths);
|
||||
array = dkp_device_list_get_array (daemon->priv->list);
|
||||
for (i=0; i<array->len; i++) {
|
||||
device = (DkpDevice *) g_ptr_array_index (array, i);
|
||||
g_ptr_array_add (object_paths, g_strdup (dkp_device_get_object_path (device)));
|
||||
}
|
||||
|
||||
/* return it on the bus */
|
||||
dbus_g_method_return (context, object_paths);
|
||||
|
||||
/* free */
|
||||
g_ptr_array_foreach (object_paths, (GFunc) g_free, NULL);
|
||||
g_ptr_array_free (object_paths, TRUE);
|
||||
return TRUE;
|
||||
|
|
|
|||
173
src/dkp-device-list.c
Normal file
173
src/dkp-device-list.c
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "dkp-debug.h"
|
||||
#include "dkp-device.h"
|
||||
#include "dkp-device-list.h"
|
||||
|
||||
static void dkp_device_list_class_init (DkpDeviceListClass *klass);
|
||||
static void dkp_device_list_init (DkpDeviceList *list);
|
||||
static void dkp_device_list_finalize (GObject *object);
|
||||
|
||||
#define DKP_DEVICE_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DK_TYPE_DEVICE_LIST, DkpDeviceListPrivate))
|
||||
|
||||
struct DkpDeviceListPrivate
|
||||
{
|
||||
GPtrArray *array;
|
||||
GHashTable *map_native_path_to_device;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (DkpDeviceList, dkp_device_list, G_TYPE_OBJECT)
|
||||
|
||||
/**
|
||||
* dkp_device_list_lookup:
|
||||
*
|
||||
* Convert a %DevkitDevice into a %DkpDevice -- we use the native path
|
||||
* to look these up as it's the only thing they share.
|
||||
**/
|
||||
DkpDevice *
|
||||
dkp_device_list_lookup (DkpDeviceList *list, DevkitDevice *d)
|
||||
{
|
||||
DkpDevice *device;
|
||||
const gchar *native_path;
|
||||
|
||||
/* does device exist in db? */
|
||||
native_path = devkit_device_get_native_path (d);
|
||||
device = g_hash_table_lookup (list->priv->map_native_path_to_device, native_path);
|
||||
return device;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_insert:
|
||||
*
|
||||
* Insert a %DevkitDevice device and it's mapping to a %DkpDevice device
|
||||
* into a list of devices.
|
||||
**/
|
||||
gboolean
|
||||
dkp_device_list_insert (DkpDeviceList *list, DevkitDevice *d, DkpDevice *device)
|
||||
{
|
||||
const gchar *native_path;
|
||||
native_path = devkit_device_get_native_path (d);
|
||||
g_hash_table_insert (list->priv->map_native_path_to_device,
|
||||
g_strdup (native_path), device);
|
||||
g_ptr_array_add (list->priv->array, device);
|
||||
dkp_debug ("added %s", native_path);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_remove_cb:
|
||||
**/
|
||||
static gboolean
|
||||
dkp_device_list_remove_cb (gpointer key, gpointer value, gpointer user_data)
|
||||
{
|
||||
if (value == user_data) {
|
||||
dkp_debug ("removed %s", (char *) key);
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_remove:
|
||||
**/
|
||||
gboolean
|
||||
dkp_device_list_remove (DkpDeviceList *list, DkpDevice *device)
|
||||
{
|
||||
/* remove the device from the db */
|
||||
g_hash_table_foreach_remove (list->priv->map_native_path_to_device,
|
||||
dkp_device_list_remove_cb, device);
|
||||
g_ptr_array_remove (list->priv->array, device);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_get_array:
|
||||
*
|
||||
* This is quick to iterate when we don't have DevkitDevice's to resolve
|
||||
**/
|
||||
const GPtrArray *
|
||||
dkp_device_list_get_array (DkpDeviceList *list)
|
||||
{
|
||||
return list->priv->array;
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_class_init:
|
||||
* @klass: The DkpDeviceListClass
|
||||
**/
|
||||
static void
|
||||
dkp_device_list_class_init (DkpDeviceListClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
object_class->finalize = dkp_device_list_finalize;
|
||||
g_type_class_add_private (klass, sizeof (DkpDeviceListPrivate));
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_init:
|
||||
* @list: This class instance
|
||||
**/
|
||||
static void
|
||||
dkp_device_list_init (DkpDeviceList *list)
|
||||
{
|
||||
list->priv = DKP_DEVICE_LIST_GET_PRIVATE (list);
|
||||
list->priv->array = g_ptr_array_new ();
|
||||
list->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_finalize:
|
||||
* @object: The object to finalize
|
||||
**/
|
||||
static void
|
||||
dkp_device_list_finalize (GObject *object)
|
||||
{
|
||||
DkpDeviceList *list;
|
||||
|
||||
g_return_if_fail (DK_IS_DEVICE_LIST (object));
|
||||
|
||||
list = DKP_DEVICE_LIST (object);
|
||||
g_ptr_array_free (list->priv->array, TRUE);
|
||||
g_hash_table_unref (list->priv->map_native_path_to_device);
|
||||
|
||||
G_OBJECT_CLASS (dkp_device_list_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
/**
|
||||
* dkp_device_list_new:
|
||||
*
|
||||
* Return value: a new DkpDeviceList object.
|
||||
**/
|
||||
DkpDeviceList *
|
||||
dkp_device_list_new (void)
|
||||
{
|
||||
DkpDeviceList *list;
|
||||
list = g_object_new (DK_TYPE_DEVICE_LIST, NULL);
|
||||
return DKP_DEVICE_LIST (list);
|
||||
}
|
||||
|
||||
68
src/dkp-device-list.h
Normal file
68
src/dkp-device-list.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
||||
*
|
||||
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
|
||||
*
|
||||
* 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 __DKP_DEVICE_LIST_H
|
||||
#define __DKP_DEVICE_LIST_H
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <dkp-enum.h>
|
||||
|
||||
#include "dkp-device.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define DK_TYPE_DEVICE_LIST (dkp_device_list_get_type ())
|
||||
#define DKP_DEVICE_LIST(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), DK_TYPE_DEVICE_LIST, DkpDeviceList))
|
||||
#define DKP_DEVICE_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), DK_TYPE_DEVICE_LIST, DkpDeviceListClass))
|
||||
#define DK_IS_DEVICE_LIST(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), DK_TYPE_DEVICE_LIST))
|
||||
#define DK_IS_DEVICE_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), DK_TYPE_DEVICE_LIST))
|
||||
#define DKP_DEVICE_LIST_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), DK_TYPE_DEVICE_LIST, DkpDeviceListClass))
|
||||
#define DKP_DEVICE_LIST_ERROR (dkp_device_list_error_quark ())
|
||||
#define DKP_DEVICE_LIST_TYPE_ERROR (dkp_device_list_error_get_type ())
|
||||
|
||||
typedef struct DkpDeviceListPrivate DkpDeviceListPrivate;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObject parent;
|
||||
DkpDeviceListPrivate *priv;
|
||||
} DkpDeviceList;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
} DkpDeviceListClass;
|
||||
|
||||
GType dkp_device_list_get_type (void) G_GNUC_CONST;
|
||||
DkpDeviceList *dkp_device_list_new (void);
|
||||
DkpDevice *dkp_device_list_lookup (DkpDeviceList *list,
|
||||
DevkitDevice *d);
|
||||
gboolean dkp_device_list_insert (DkpDeviceList *list,
|
||||
DevkitDevice *d,
|
||||
DkpDevice *device);
|
||||
gboolean dkp_device_list_remove (DkpDeviceList *list,
|
||||
DkpDevice *device);
|
||||
const GPtrArray *dkp_device_list_get_array (DkpDeviceList *list);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __DKP_DEVICE_LIST_H */
|
||||
|
||||
Loading…
Add table
Reference in a new issue