From fec23e569e46f626c72446dbab687c1d5fb3c495 Mon Sep 17 00:00:00 2001 From: iain Date: Mon, 17 Oct 2011 17:48:19 +0100 Subject: Add device type to the info returned from GypsyDiscovery GypsyDiscovery interface has changed to also return a type string: "usb", "internal" or "bluetooth" indicating the device type. --- examples/list-known-gps-devices.c | 8 +++-- gypsy/gypsy-discovery.c | 70 +++++++++++++++++++++++++++++++++------ gypsy/gypsy-discovery.h | 13 ++++++-- gypsy/gypsy-marshal.list | 1 + interfaces/gypsy-discovery.xml | 3 ++ src/gypsy-discovery.c | 54 ++++++++++++++++++++++++++---- src/gypsy-marshal.list | 1 + 7 files changed, 128 insertions(+), 22 deletions(-) diff --git a/examples/list-known-gps-devices.c b/examples/list-known-gps-devices.c index a1e1ec0..c7c4059 100644 --- a/examples/list-known-gps-devices.c +++ b/examples/list-known-gps-devices.c @@ -18,7 +18,7 @@ main (int argc, { GypsyDiscovery *discovery; GError *error = NULL; - char **known_devices; + GPtrArray *known_devices; int i; g_type_init (); @@ -36,9 +36,11 @@ main (int argc, return 0; } - for (i = 0; known_devices[i]; i++) { - g_print ("[%d] %s\n", i + 1, known_devices[i]); + for (i = 0; i < known_devices->len; i++) { + GypsyDiscoveryDeviceInfo *di = known_devices->pdata[i]; + g_print ("[%d] %s (%s)\n", i + 1, di->device_path, di->type); } + g_ptr_array_free (known_devices, TRUE); return 0; } diff --git a/gypsy/gypsy-discovery.c b/gypsy/gypsy-discovery.c index dcfc370..2fdb102 100644 --- a/gypsy/gypsy-discovery.c +++ b/gypsy/gypsy-discovery.c @@ -25,6 +25,7 @@ #include "gypsy-discovery.h" #include "gypsy-discovery-bindings.h" +#include "gypsy-marshal.h" enum { PROP_0, @@ -134,17 +135,19 @@ gypsy_discovery_class_init (GypsyDiscoveryClass *klass) static void device_added_cb (DBusGProxy *proxy, const char *added, + const char *type, GypsyDiscovery *discovery) { - g_signal_emit (discovery, signals[DEVICE_ADDED], 0, added); + g_signal_emit (discovery, signals[DEVICE_ADDED], 0, added, type); } static void device_removed_cb (DBusGProxy *proxy, const char *removed, + const char *type, GypsyDiscovery *discovery) { - g_signal_emit (discovery, signals[DEVICE_REMOVED], 0, removed); + g_signal_emit (discovery, signals[DEVICE_REMOVED], 0, removed, type); } static void @@ -162,14 +165,19 @@ gypsy_discovery_init (GypsyDiscovery *self) return; } + /* We register all the other marshallers in gypsy_control_init, + but GypsyDiscovery can be created before GypsyControl */ + dbus_g_object_register_marshaller (gypsy_marshal_VOID__STRING_STRING, + G_TYPE_NONE, G_TYPE_STRING, + G_TYPE_STRING, G_TYPE_INVALID); priv->proxy = dbus_g_proxy_new_for_name (connection, GYPSY_DISCOVERY_DBUS_SERVICE, GYPSY_DISCOVERY_DBUS_PATH, GYPSY_DISCOVERY_DBUS_INTERFACE); dbus_g_proxy_add_signal (priv->proxy, "DeviceAdded", - G_TYPE_STRING, G_TYPE_INVALID); + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); dbus_g_proxy_add_signal (priv->proxy, "DeviceRemoved", - G_TYPE_STRING, G_TYPE_INVALID); + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); dbus_g_proxy_connect_signal (priv->proxy, "DeviceAdded", G_CALLBACK (device_added_cb), self, NULL); dbus_g_proxy_connect_signal (priv->proxy, "DeviceRemoved", @@ -190,28 +198,50 @@ gypsy_discovery_new (void) * * Obtains the GPS devices that Gypsy knows about. * - * Return value: An array of strings that give the device path or the - * device address (for Bluetooth devices). The array is owned by the caller - * and should be freed with g_strv_free when it is finished with. + * Return value: A #GPtrArray containing #GypsyDiscoveryDeviceInfo. + * The array is owned by the caller and should be freed with + * g_ptr_array_free when it is finished with. */ -char ** +GPtrArray * gypsy_discovery_list_devices (GypsyDiscovery *discovery, GError **error) { GypsyDiscoveryPrivate *priv; gboolean result; - char **known_devices; + char **devices; + char **types; + GPtrArray *known_devices; + int i; g_return_val_if_fail (GYPSY_IS_DISCOVERY (discovery), NULL); priv = discovery->priv; result = org_freedesktop_Gypsy_Discovery_list_devices (priv->proxy, - &known_devices, + &devices, + &types, error); if (!result) { return NULL; } + known_devices = g_ptr_array_new_with_free_func + ((GDestroyNotify) gypsy_discovery_device_info_free); + for (i = 0; devices[i]; i++) { + GypsyDiscoveryDeviceInfo *di; + + di = g_slice_new (GypsyDiscoveryDeviceInfo); + + /* We don't copy the data here, + so we don't need to free it */ + di->device_path = devices[i]; + di->type = types[i]; + g_ptr_array_add (known_devices, di); + } + + /* We only need to free the array here, not the data */ + g_free (devices); + g_free (types); + return known_devices; } @@ -244,3 +274,23 @@ gypsy_discovery_stop_scanning (GypsyDiscovery *discovery, error); return result; } + +GypsyDiscoveryDeviceInfo * +gypsy_discovery_device_info_copy (GypsyDiscoveryDeviceInfo *di) +{ + GypsyDiscoveryDeviceInfo *dicopy; + + dicopy = g_slice_new (GypsyDiscoveryDeviceInfo); + dicopy->device_path = g_strdup (di->device_path); + dicopy->type = g_strdup (di->type); + + return dicopy; +} + +void +gypsy_discovery_device_info_free (GypsyDiscoveryDeviceInfo *device_info) +{ + g_free (device_info->device_path); + g_free (device_info->type); + g_slice_free (GypsyDiscoveryDeviceInfo, device_info); +} diff --git a/gypsy/gypsy-discovery.h b/gypsy/gypsy-discovery.h index 60bdff6..e3fbec2 100644 --- a/gypsy/gypsy-discovery.h +++ b/gypsy/gypsy-discovery.h @@ -59,6 +59,7 @@ G_BEGIN_DECLS typedef struct _GypsyDiscoveryPrivate GypsyDiscoveryPrivate; typedef struct _GypsyDiscovery GypsyDiscovery; typedef struct _GypsyDiscoveryClass GypsyDiscoveryClass; +typedef struct _GypsyDiscoveryDeviceInfo GypsyDiscoveryDeviceInfo; struct _GypsyDiscovery { @@ -72,14 +73,22 @@ struct _GypsyDiscoveryClass GObjectClass parent_class; }; +struct _GypsyDiscoveryDeviceInfo +{ + char *device_path; + char *type; +}; + GType gypsy_discovery_get_type (void) G_GNUC_CONST; GypsyDiscovery *gypsy_discovery_new (void); -char **gypsy_discovery_list_devices (GypsyDiscovery *discovery, - GError **error); +GPtrArray *gypsy_discovery_list_devices (GypsyDiscovery *discovery, + GError **error); gboolean gypsy_discovery_start_scanning (GypsyDiscovery *discovery, GError **error); gboolean gypsy_discovery_stop_scanning (GypsyDiscovery *discovery, GError **error); +GypsyDiscoveryDeviceInfo *gypsy_discovery_device_info_copy (GypsyDiscoveryDeviceInfo *di); +void gypsy_discovery_device_info_free (GypsyDiscoveryDeviceInfo *device_info); G_END_DECLS diff --git a/gypsy/gypsy-marshal.list b/gypsy/gypsy-marshal.list index a5079fb..3f84a91 100644 --- a/gypsy/gypsy-marshal.list +++ b/gypsy/gypsy-marshal.list @@ -1,2 +1,3 @@ VOID:INT,INT,DOUBLE,DOUBLE,DOUBLE VOID:INT,DOUBLE,DOUBLE,DOUBLE +VOID:STRING,STRING diff --git a/interfaces/gypsy-discovery.xml b/interfaces/gypsy-discovery.xml index a34f833..e4c760b 100644 --- a/interfaces/gypsy-discovery.xml +++ b/interfaces/gypsy-discovery.xml @@ -4,14 +4,17 @@ + + + diff --git a/src/gypsy-discovery.c b/src/gypsy-discovery.c index 6735468..20de6a9 100644 --- a/src/gypsy-discovery.c +++ b/src/gypsy-discovery.c @@ -43,6 +43,7 @@ #include "gypsy-debug.h" #include "gypsy-discovery.h" +#include "gypsy-marshal-internal.h" enum { PROP_0, @@ -54,8 +55,15 @@ enum { LAST_SIGNAL, }; +typedef struct _DeviceInfo { + char *device_path; + char *type; +} DeviceInfo; + struct _GypsyDiscoveryPrivate { GUdevClient *client; + + /* Contains DeviceInfo */ GPtrArray *known_devices; #ifdef HAVE_BLUEZ @@ -87,6 +95,7 @@ static guint32 signals[LAST_SIGNAL] = {0,}; static gboolean gypsy_discovery_list_devices (GypsyDiscovery *discovery, char ***devices, + char ***types, GError **error); static gboolean gypsy_discovery_start_scanning (GypsyDiscovery *discovery, GError **error); @@ -95,6 +104,30 @@ static gboolean gypsy_discovery_stop_scanning (GypsyDiscovery *discovery, #include "gypsy-discovery-glue.h" +const char *internal_type = "internal"; +const char *bluetooth_type = "bluetooth"; +const char *usb_type = "usb"; + +static void +device_info_free (gpointer data) +{ + DeviceInfo *di = (DeviceInfo *) data; + + g_free (di->device_path); + g_slice_free (DeviceInfo, di); +} + +static DeviceInfo * +device_info_new (const char *device_path, + const char *type) +{ + DeviceInfo *di = g_slice_new (DeviceInfo); + di->device_path = g_strdup (device_path); + di->type = (char *) type; + + return di; +} + static void gypsy_discovery_finalize (GObject *object) { @@ -152,7 +185,7 @@ gypsy_discovery_class_init (GypsyDiscoveryClass *klass) G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, 0, NULL, NULL, - g_cclosure_marshal_VOID__STRING, + gypsy_marshal_VOID__STRING_STRING, G_TYPE_NONE, 1, G_TYPE_STRING); signals[DEVICE_REMOVED] = g_signal_new ("device-removed", @@ -160,7 +193,7 @@ gypsy_discovery_class_init (GypsyDiscoveryClass *klass) G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, 0, NULL, NULL, - g_cclosure_marshal_VOID__STRING, + gypsy_marshal_VOID__STRING_STRING, G_TYPE_NONE, 1, G_TYPE_STRING); } @@ -227,7 +260,8 @@ get_positioning_devices (GypsyDiscovery *discovery, } g_ptr_array_add (priv->known_devices, - g_value_dup_string (path)); + device_info_new (g_value_get_string (path), + bluetooth_type)); g_object_unref (proxy); g_hash_table_destroy (properties); @@ -359,7 +393,7 @@ maybe_add_device (GypsyDiscovery *discovery, known_ids[i].product_name, name); g_ptr_array_add (priv->known_devices, - g_strdup (name)); + device_info_new (name, usb_type)); return name; } @@ -500,7 +534,7 @@ gypsy_discovery_init (GypsyDiscovery *self) self->priv = priv; - priv->known_devices = g_ptr_array_new_with_free_func (g_free); + priv->known_devices = g_ptr_array_new_with_free_func (device_info_free); priv->client = g_udev_client_new (subsystems); g_signal_connect (priv->client, "uevent", @@ -514,18 +548,24 @@ gypsy_discovery_init (GypsyDiscovery *self) static gboolean gypsy_discovery_list_devices (GypsyDiscovery *discovery, char ***devices, + char ***types, GError **error) { GypsyDiscoveryPrivate *priv = discovery->priv; int i; *devices = g_new (char *, priv->known_devices->len + 1); + *types = g_new (char *, priv->known_devices->len + 1); for (i = 0; i < priv->known_devices->len; i++) { - (*devices)[i] = g_strdup (priv->known_devices->pdata[i]); + DeviceInfo *di = priv->known_devices->pdata[i]; + + (*devices)[i] = g_strdup (di->device_path); + (*types)[i] = g_strdup (di->type); } - /* NULL terminate the array */ + /* NULL terminate the arrays */ (*devices)[i] = NULL; + (*types)[i] = NULL; return TRUE; } diff --git a/src/gypsy-marshal.list b/src/gypsy-marshal.list index a5079fb..3f84a91 100644 --- a/src/gypsy-marshal.list +++ b/src/gypsy-marshal.list @@ -1,2 +1,3 @@ VOID:INT,INT,DOUBLE,DOUBLE,DOUBLE VOID:INT,DOUBLE,DOUBLE,DOUBLE +VOID:STRING,STRING -- cgit v1.2.3