From b82f6acf0c6c1d2f4b1afdbd2c240061344e14c7 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Sun, 14 Aug 2022 16:15:20 +0100 Subject: Use G_DECLARE_DERIVABLE_TYPE to reduce boilerplate --- gusb/gusb-autocleanups.h | 20 ----- gusb/gusb-context.c | 98 +++++++++++---------- gusb/gusb-context.h | 13 +-- gusb/gusb-device-list.c | 23 ++--- gusb/gusb-device-list.h | 15 +--- gusb/gusb-device.c | 221 +++++++++++++++++++++++++++-------------------- gusb/gusb-device.h | 13 +-- gusb/gusb-self-test.c | 1 - gusb/gusb.h | 1 - gusb/meson.build | 2 - 10 files changed, 190 insertions(+), 217 deletions(-) delete mode 100644 gusb/gusb-autocleanups.h diff --git a/gusb/gusb-autocleanups.h b/gusb/gusb-autocleanups.h deleted file mode 100644 index c831041..0000000 --- a/gusb/gusb-autocleanups.h +++ /dev/null @@ -1,20 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- - * - * Copyright (C) 2015 Kalev Lember - * - * SPDX-License-Identifier: LGPL-2.1+ - */ - -#pragma once - -#include -#include -#include - -#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC - -G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUsbContext, g_object_unref) -G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUsbDevice, g_object_unref) -G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUsbDeviceList, g_object_unref) - -#endif diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c index 4ccfd17..ee423cb 100644 --- a/gusb/gusb-context.c +++ b/gusb/gusb-context.c @@ -17,7 +17,6 @@ #include -#include "gusb-autocleanups.h" #include "gusb-context-private.h" #include "gusb-device-private.h" #include "gusb-util.h" @@ -28,12 +27,14 @@ enum { DEVICE_ADDED_SIGNAL, DEVICE_REMOVED_SIGNAL, LAST_SIGNAL }; #define G_USB_CONTEXT_HOTPLUG_POLL_INTERVAL_DEFAULT 1000 /* ms */ +#define GET_PRIVATE(o) (g_usb_context_get_instance_private(o)) + /** * GUsbContextPrivate: * * Private #GUsbContext data **/ -struct _GUsbContextPrivate { +typedef struct { GMainContext *main_ctx; GPtrArray *devices; GHashTable *dict_usb_ids; @@ -50,7 +51,7 @@ struct _GUsbContextPrivate { GPtrArray *idle_events; GMutex idle_events_mutex; guint idle_events_id; -}; +} GUsbContextPrivate; /* not defined in FreeBSD */ #ifndef HAVE_LIBUSB_CAP_HAS_HOTPLUG @@ -114,7 +115,7 @@ static void g_usb_context_dispose(GObject *object) { GUsbContext *context = G_USB_CONTEXT(object); - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); /* this is safe to call even when priv->hotplug_id is unset */ if (g_atomic_int_dec_and_test(&priv->thread_event_run)) { @@ -146,7 +147,7 @@ static void g_usb_context_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { GUsbContext *context = G_USB_CONTEXT(object); - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); switch (prop_id) { case PROP_LIBUSB_CONTEXT: @@ -165,7 +166,7 @@ static void g_usb_context_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { GUsbContext *context = G_USB_CONTEXT(object); - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); switch (prop_id) { case PROP_DEBUG_LEVEL: @@ -246,8 +247,10 @@ g_usb_context_class_init(GUsbContextClass *klass) static void g_usb_context_emit_device_add(GUsbContext *context, GUsbDevice *device) { + GUsbContextPrivate *priv = GET_PRIVATE(context); + /* emitted directly by g_usb_context_enumerate */ - if (!context->priv->done_enumerate) + if (!priv->done_enumerate) return; g_signal_emit(context, signals[DEVICE_ADDED_SIGNAL], 0, device); @@ -256,8 +259,10 @@ g_usb_context_emit_device_add(GUsbContext *context, GUsbDevice *device) static void g_usb_context_emit_device_remove(GUsbContext *context, GUsbDevice *device) { + GUsbContextPrivate *priv = GET_PRIVATE(context); + /* should not happen, if it does we would not fire any signal */ - if (!context->priv->done_enumerate) + if (!priv->done_enumerate) return; g_signal_emit(context, signals[DEVICE_REMOVED_SIGNAL], 0, device); @@ -266,7 +271,7 @@ g_usb_context_emit_device_remove(GUsbContext *context, GUsbDevice *device) static void g_usb_context_add_device(GUsbContext *context, struct libusb_device *dev) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); GUsbContextReplugHelper *replug_helper; const gchar *platform_id; guint8 bus; @@ -319,7 +324,7 @@ g_usb_context_add_device(GUsbContext *context, struct libusb_device *dev) static void g_usb_context_remove_device(GUsbContext *context, struct libusb_device *dev) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); GUsbContextReplugHelper *replug_helper; const gchar *platform_id; guint8 bus; @@ -368,7 +373,7 @@ static gboolean g_usb_context_idle_hotplug_cb(gpointer user_data) { GUsbContext *context = G_USB_CONTEXT(user_data); - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->idle_events_mutex); for (guint i = 0; i < priv->idle_events->len; i++) { @@ -400,7 +405,7 @@ g_usb_context_hotplug_cb(struct libusb_context *ctx, { GUsbContext *context = G_USB_CONTEXT(user_data); GUsbContextIdleHelper *helper; - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->idle_events_mutex); helper = g_new0(GUsbContextIdleHelper, 1); @@ -418,7 +423,7 @@ g_usb_context_hotplug_cb(struct libusb_context *ctx, static void g_usb_context_rescan(GUsbContext *context) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); libusb_device **dev_list = NULL; g_autoptr(GList) existing_devices = NULL; @@ -476,7 +481,7 @@ g_usb_context_rescan_cb(gpointer user_data) GMainContext * g_usb_context_get_main_context(GUsbContext *context) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_val_if_fail(G_USB_IS_CONTEXT(context), NULL); return priv->main_ctx; } @@ -492,7 +497,7 @@ g_usb_context_get_main_context(GUsbContext *context) void g_usb_context_set_main_context(GUsbContext *context, GMainContext *main_ctx) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_if_fail(G_USB_IS_CONTEXT(context)); @@ -505,7 +510,7 @@ g_usb_context_set_main_context(GUsbContext *context, GMainContext *main_ctx) static void g_usb_context_ensure_rescan_timeout(GUsbContext *context) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); if (priv->hotplug_poll_id > 0) { g_source_remove(priv->hotplug_poll_id); @@ -530,7 +535,7 @@ g_usb_context_ensure_rescan_timeout(GUsbContext *context) guint g_usb_context_get_hotplug_poll_interval(GUsbContext *context) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_val_if_fail(G_USB_IS_CONTEXT(context), G_MAXUINT); return priv->hotplug_poll_id; } @@ -549,7 +554,7 @@ g_usb_context_get_hotplug_poll_interval(GUsbContext *context) void g_usb_context_set_hotplug_poll_interval(GUsbContext *context, guint hotplug_poll_interval) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_if_fail(G_USB_IS_CONTEXT(context)); @@ -578,7 +583,7 @@ g_usb_context_set_hotplug_poll_interval(GUsbContext *context, guint hotplug_poll void g_usb_context_enumerate(GUsbContext *context) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); /* only ever initially scan once, then rely on hotplug / poll */ if (priv->done_enumerate) @@ -615,7 +620,8 @@ g_usb_context_enumerate(GUsbContext *context) void g_usb_context_set_flags(GUsbContext *context, GUsbContextFlags flags) { - context->priv->flags = flags; + GUsbContextPrivate *priv = GET_PRIVATE(context); + priv->flags = flags; } /** @@ -631,14 +637,15 @@ g_usb_context_set_flags(GUsbContext *context, GUsbContextFlags flags) GUsbContextFlags g_usb_context_get_flags(GUsbContext *context) { - return context->priv->flags; + GUsbContextPrivate *priv = GET_PRIVATE(context); + return priv->flags; } static gpointer g_usb_context_event_thread_cb(gpointer data) { GUsbContext *context = G_USB_CONTEXT(data); - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); struct timeval tv = { .tv_usec = 0, .tv_sec = 2, @@ -653,9 +660,8 @@ g_usb_context_event_thread_cb(gpointer data) static void g_usb_context_init(GUsbContext *context) { - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); - priv = context->priv = g_usb_context_get_instance_private(context); priv->flags = G_USB_CONTEXT_FLAGS_NONE; priv->hotplug_poll_interval = G_USB_CONTEXT_HOTPLUG_POLL_INTERVAL_DEFAULT; priv->devices = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); @@ -672,12 +678,10 @@ static gboolean g_usb_context_initable_init(GInitable *initable, GCancellable *cancellable, GError **error) { GUsbContext *context = G_USB_CONTEXT(initable); - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); gint rc; libusb_context *ctx; - priv = context->priv; - rc = libusb_init(&ctx); if (rc < 0) { g_set_error(error, @@ -706,7 +710,7 @@ g_usb_context_initable_init(GInitable *initable, GCancellable *cancellable, GErr LIBUSB_HOTPLUG_MATCH_ANY, g_usb_context_hotplug_cb, context, - &context->priv->hotplug_id); + &priv->hotplug_id); if (rc != LIBUSB_SUCCESS) { g_warning("Error creating a hotplug callback: %s", g_usb_strerror(rc)); } @@ -734,7 +738,8 @@ g_usb_context_initable_iface_init(GInitableIface *iface) libusb_context * _g_usb_context_get_context(GUsbContext *context) { - return context->priv->ctx; + GUsbContextPrivate *priv = GET_PRIVATE(context); + return priv->ctx; } /** @@ -769,13 +774,11 @@ g_usb_context_get_source(GUsbContext *context, GMainContext *main_ctx) void g_usb_context_set_debug(GUsbContext *context, GLogLevelFlags flags) { - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); int debug_level; g_return_if_fail(G_USB_IS_CONTEXT(context)); - priv = context->priv; - if (flags & (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO)) debug_level = 3; else if (flags & (G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING)) @@ -813,13 +816,11 @@ g_usb_context_set_debug(GUsbContext *context, GLogLevelFlags flags) GUsbDevice * g_usb_context_find_by_bus_address(GUsbContext *context, guint8 bus, guint8 address, GError **error) { - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_val_if_fail(G_USB_IS_CONTEXT(context), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - priv = context->priv; - g_usb_context_enumerate(context); for (guint i = 0; i < priv->devices->len; i++) { GUsbDevice *device = g_ptr_array_index(priv->devices, i); @@ -852,13 +853,11 @@ g_usb_context_find_by_bus_address(GUsbContext *context, guint8 bus, guint8 addre GUsbDevice * g_usb_context_find_by_platform_id(GUsbContext *context, const gchar *platform_id, GError **error) { - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_val_if_fail(G_USB_IS_CONTEXT(context), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - priv = context->priv; - g_usb_context_enumerate(context); for (guint i = 0; i < priv->devices->len; i++) { GUsbDevice *device = g_ptr_array_index(priv->devices, i); @@ -889,13 +888,11 @@ g_usb_context_find_by_platform_id(GUsbContext *context, const gchar *platform_id GUsbDevice * g_usb_context_find_by_vid_pid(GUsbContext *context, guint16 vid, guint16 pid, GError **error) { - GUsbContextPrivate *priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); g_return_val_if_fail(G_USB_IS_CONTEXT(context), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - priv = context->priv; - g_usb_context_enumerate(context); for (guint i = 0; i < priv->devices->len; i++) { GUsbDevice *device = g_ptr_array_index(priv->devices, i); @@ -915,13 +912,14 @@ g_usb_context_find_by_vid_pid(GUsbContext *context, guint16 vid, guint16 pid, GE static gboolean g_usb_context_load_usb_ids(GUsbContext *context, GError **error) { + GUsbContextPrivate *priv = GET_PRIVATE(context); guint16 pid; guint16 vid = 0x0000; g_autofree gchar *data = NULL; g_auto(GStrv) lines = NULL; /* already loaded */ - if (g_hash_table_size(context->priv->dict_usb_ids) > 0) + if (g_hash_table_size(priv->dict_usb_ids) > 0) return TRUE; /* parse */ @@ -943,7 +941,7 @@ g_usb_context_load_usb_ids(GUsbContext *context, GError **error) if (vid == 0) break; - g_hash_table_insert(context->priv->dict_usb_ids, + g_hash_table_insert(priv->dict_usb_ids, g_strdup(lines[i]), g_strdup(lines[i] + 6)); } else { @@ -952,7 +950,7 @@ g_usb_context_load_usb_ids(GUsbContext *context, GError **error) lines[i][5] = '\0'; pid = g_ascii_strtoull(lines[i] + 1, NULL, 16); - g_hash_table_insert(context->priv->dict_usb_ids, + g_hash_table_insert(priv->dict_usb_ids, g_strdup_printf("%04x:%04x", vid, pid), g_strdup(lines[i] + 7)); } @@ -976,6 +974,7 @@ g_usb_context_load_usb_ids(GUsbContext *context, GError **error) const gchar * _g_usb_context_lookup_vendor(GUsbContext *context, guint16 vid, GError **error) { + GUsbContextPrivate *priv = GET_PRIVATE(context); const gchar *tmp; g_autofree gchar *key = NULL; @@ -988,7 +987,7 @@ _g_usb_context_lookup_vendor(GUsbContext *context, guint16 vid, GError **error) /* find */ key = g_strdup_printf("%04x", vid); - tmp = g_hash_table_lookup(context->priv->dict_usb_ids, key); + tmp = g_hash_table_lookup(priv->dict_usb_ids, key); if (tmp == NULL) { g_set_error(error, G_USB_CONTEXT_ERROR, @@ -1017,6 +1016,7 @@ _g_usb_context_lookup_vendor(GUsbContext *context, guint16 vid, GError **error) const gchar * _g_usb_context_lookup_product(GUsbContext *context, guint16 vid, guint16 pid, GError **error) { + GUsbContextPrivate *priv = GET_PRIVATE(context); const gchar *tmp; g_autofree gchar *key = NULL; @@ -1029,7 +1029,7 @@ _g_usb_context_lookup_product(GUsbContext *context, guint16 vid, guint16 pid, GE /* find */ key = g_strdup_printf("%04x:%04x", vid, pid); - tmp = g_hash_table_lookup(context->priv->dict_usb_ids, key); + tmp = g_hash_table_lookup(priv->dict_usb_ids, key); if (tmp == NULL) { g_set_error(error, G_USB_CONTEXT_ERROR, @@ -1053,11 +1053,13 @@ _g_usb_context_lookup_product(GUsbContext *context, guint16 vid, guint16 pid, GE GPtrArray * g_usb_context_get_devices(GUsbContext *context) { + GUsbContextPrivate *priv = GET_PRIVATE(context); + g_return_val_if_fail(G_USB_IS_CONTEXT(context), NULL); g_usb_context_enumerate(context); - return g_ptr_array_ref(context->priv->devices); + return g_ptr_array_ref(priv->devices); } static gboolean @@ -1094,7 +1096,7 @@ g_usb_context_wait_for_replug(GUsbContext *context, guint timeout_ms, GError **error) { - GUsbContextPrivate *priv = context->priv; + GUsbContextPrivate *priv = GET_PRIVATE(context); const gchar *platform_id; g_autoptr(GUsbContextReplugHelper) replug_helper = NULL; diff --git a/gusb/gusb-context.h b/gusb/gusb-context.h index baf7a1f..7165743 100644 --- a/gusb/gusb-context.h +++ b/gusb/gusb-context.h @@ -14,18 +14,9 @@ G_BEGIN_DECLS #define G_USB_TYPE_CONTEXT (g_usb_context_get_type()) -#define G_USB_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_CAST((o), G_USB_TYPE_CONTEXT, GUsbContext)) -#define G_USB_IS_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), G_USB_TYPE_CONTEXT)) #define G_USB_CONTEXT_ERROR (g_usb_context_error_quark()) -typedef struct _GUsbContextPrivate GUsbContextPrivate; -typedef struct _GUsbContext GUsbContext; -typedef struct _GUsbContextClass GUsbContextClass; - -struct _GUsbContext { - GObject parent; - GUsbContextPrivate *priv; -}; +G_DECLARE_DERIVABLE_TYPE(GUsbContext, g_usb_context, G_USB, CONTEXT, GObject) struct _GUsbContextClass { GObjectClass parent_class; @@ -53,8 +44,6 @@ typedef enum { G_USB_CONTEXT_FLAGS_LAST } GUsbContextFlags; -GType -g_usb_context_get_type(void); GQuark g_usb_context_error_quark(void); diff --git a/gusb/gusb-device-list.c b/gusb/gusb-device-list.c index 1ca9a20..2b5b8ec 100644 --- a/gusb/gusb-device-list.c +++ b/gusb/gusb-device-list.c @@ -27,19 +27,21 @@ enum { PROP_0, PROP_CONTEXT }; enum { DEVICE_ADDED_SIGNAL, DEVICE_REMOVED_SIGNAL, LAST_SIGNAL }; -struct _GUsbDeviceListPrivate { +typedef struct { GUsbContext *context; -}; +} GUsbDeviceListPrivate; static guint signals[LAST_SIGNAL] = {0}; G_DEFINE_TYPE_WITH_PRIVATE(GUsbDeviceList, g_usb_device_list, G_TYPE_OBJECT); +#define GET_PRIVATE(o) (g_usb_device_list_get_instance_private(o)) + static void g_usb_device_list_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { GUsbDeviceList *list = G_USB_DEVICE_LIST(object); - GUsbDeviceListPrivate *priv = list->priv; + GUsbDeviceListPrivate *priv = GET_PRIVATE(list); switch (prop_id) { case PROP_CONTEXT: @@ -70,7 +72,7 @@ g_usb_device_list_set_property(GObject *object, GParamSpec *pspec) { GUsbDeviceList *list = G_USB_DEVICE_LIST(object); - GUsbDeviceListPrivate *priv = list->priv; + GUsbDeviceListPrivate *priv = GET_PRIVATE(list); switch (prop_id) { case PROP_CONTEXT: @@ -151,7 +153,6 @@ g_usb_device_list_class_init(GUsbDeviceListClass *klass) static void g_usb_device_list_init(GUsbDeviceList *list) { - list->priv = g_usb_device_list_get_instance_private(list); } /** @@ -165,9 +166,9 @@ g_usb_device_list_init(GUsbDeviceList *list) GPtrArray * g_usb_device_list_get_devices(GUsbDeviceList *list) { + GUsbDeviceListPrivate *priv = GET_PRIVATE(list); g_return_val_if_fail(G_USB_IS_DEVICE_LIST(list), NULL); - - return g_usb_context_get_devices(list->priv->context); + return g_usb_context_get_devices(priv->context); } /** @@ -203,10 +204,10 @@ g_usb_device_list_find_by_bus_address(GUsbDeviceList *list, guint8 address, GError **error) { + GUsbDeviceListPrivate *priv = GET_PRIVATE(list); g_return_val_if_fail(G_USB_IS_DEVICE_LIST(list), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - - return g_usb_context_find_by_bus_address(list->priv->context, bus, address, error); + return g_usb_context_find_by_bus_address(priv->context, bus, address, error); } /** @@ -225,10 +226,10 @@ g_usb_device_list_find_by_bus_address(GUsbDeviceList *list, GUsbDevice * g_usb_device_list_find_by_vid_pid(GUsbDeviceList *list, guint16 vid, guint16 pid, GError **error) { + GUsbDeviceListPrivate *priv = GET_PRIVATE(list); g_return_val_if_fail(G_USB_IS_DEVICE_LIST(list), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - - return g_usb_context_find_by_vid_pid(list->priv->context, vid, pid, error); + return g_usb_context_find_by_vid_pid(priv->context, vid, pid, error); } /** diff --git a/gusb/gusb-device-list.h b/gusb/gusb-device-list.h index d0297e9..95c4423 100644 --- a/gusb/gusb-device-list.h +++ b/gusb/gusb-device-list.h @@ -13,18 +13,8 @@ G_BEGIN_DECLS #define G_USB_TYPE_DEVICE_LIST (g_usb_device_list_get_type()) -#define G_USB_DEVICE_LIST(o) \ - (G_TYPE_CHECK_INSTANCE_CAST((o), G_USB_TYPE_DEVICE_LIST, GUsbDeviceList)) -#define G_USB_IS_DEVICE_LIST(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), G_USB_TYPE_DEVICE_LIST)) -typedef struct _GUsbDeviceListPrivate GUsbDeviceListPrivate; -typedef struct _GUsbDeviceList GUsbDeviceList; -typedef struct _GUsbDeviceListClass GUsbDeviceListClass; - -struct _GUsbDeviceList { - GObject parent; - GUsbDeviceListPrivate *priv; -}; +G_DECLARE_DERIVABLE_TYPE(GUsbDeviceList, g_usb_device_list, G_USB, DEVICE_LIST, GObject) struct _GUsbDeviceListClass { GObjectClass parent_class; @@ -39,9 +29,6 @@ struct _GUsbDeviceListClass { gchar _gusb_reserved[64]; }; -GType -g_usb_device_list_get_type(void); - G_DEPRECATED_FOR(g_usb_context_new) GUsbDeviceList * g_usb_device_list_new(GUsbContext *context); diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index 576b148..add6352 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -30,13 +30,13 @@ * * Private #GUsbDevice data **/ -struct _GUsbDevicePrivate { +typedef struct { gchar *platform_id; GUsbContext *context; libusb_device *device; libusb_device_handle *handle; struct libusb_device_descriptor desc; -}; +} GUsbDevicePrivate; enum { PROP_0, PROP_LIBUSB_DEVICE, PROP_CONTEXT, PROP_PLATFORM_ID, N_PROPERTIES }; @@ -47,12 +47,15 @@ static GParamSpec *pspecs[N_PROPERTIES] = { static void g_usb_device_initable_iface_init(GInitableIface *iface); -G_DEFINE_TYPE_WITH_CODE(GUsbDevice, - g_usb_device, - G_TYPE_OBJECT, - G_ADD_PRIVATE(GUsbDevice) - G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, - g_usb_device_initable_iface_init)) +G_DEFINE_TYPE_EXTENDED(GUsbDevice, + g_usb_device, + G_TYPE_OBJECT, + 0, + G_ADD_PRIVATE(GUsbDevice) + G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, + g_usb_device_initable_iface_init)); + +#define GET_PRIVATE(o) (g_usb_device_get_instance_private(o)) /* clang-format off */ /** @@ -69,7 +72,7 @@ static void g_usb_device_finalize(GObject *object) { GUsbDevice *device = G_USB_DEVICE(object); - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_free(priv->platform_id); @@ -80,7 +83,7 @@ static void g_usb_device_dispose(GObject *object) { GUsbDevice *device = G_USB_DEVICE(object); - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_clear_pointer(&priv->device, libusb_unref_device); g_clear_object(&priv->context); @@ -92,7 +95,7 @@ static void g_usb_device_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { GUsbDevice *device = G_USB_DEVICE(object); - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); switch (prop_id) { case PROP_LIBUSB_DEVICE: @@ -107,7 +110,7 @@ g_usb_device_get_property(GObject *object, guint prop_id, GValue *value, GParamS static void set_libusb_device(GUsbDevice *device, struct libusb_device *dev) { - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_clear_pointer(&priv->device, libusb_unref_device); @@ -119,7 +122,7 @@ static void g_usb_device_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { GUsbDevice *device = G_USB_DEVICE(object); - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); switch (prop_id) { case PROP_LIBUSB_DEVICE: @@ -144,7 +147,7 @@ g_usb_device_constructed(GObject *object) GUsbDevicePrivate *priv; gint rc; - priv = device->priv; + priv = GET_PRIVATE(device); if (!priv->device) g_error("constructed without a libusb_device"); @@ -200,7 +203,6 @@ g_usb_device_class_init(GUsbDeviceClass *klass) static void g_usb_device_init(GUsbDevice *device) { - device->priv = g_usb_device_get_instance_private(device); } /* not defined in FreeBSD */ @@ -250,7 +252,7 @@ g_usb_device_initable_init(GInitable *initable, GCancellable *cancellable, GErro GUsbDevicePrivate *priv; gint rc; - priv = device->priv; + priv = GET_PRIVATE(device); if (priv->device == NULL) { g_set_error_literal(error, @@ -313,7 +315,8 @@ _g_usb_device_new(GUsbContext *context, libusb_device *device, GError **error) libusb_device * _g_usb_device_get_device(GUsbDevice *device) { - return device->priv->device; + GUsbDevicePrivate *priv = GET_PRIVATE(device); + return priv->device; } static gboolean @@ -400,9 +403,10 @@ g_usb_device_async_not_open_error(GUsbDevice *device, gboolean _g_usb_device_open_internal(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; - if (device->priv->handle != NULL) { + if (priv->handle != NULL) { g_set_error(error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_ALREADY_OPEN, @@ -413,7 +417,7 @@ _g_usb_device_open_internal(GUsbDevice *device, GError **error) } /* open device */ - rc = libusb_open(device->priv->device, &device->priv->handle); + rc = libusb_open(priv->device, &priv->handle); return g_usb_device_libusb_error_to_gerror(device, rc, error); } @@ -433,11 +437,13 @@ _g_usb_device_open_internal(GUsbDevice *device, GError **error) gboolean g_usb_device_open(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); + g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); /* ignore */ - if (g_usb_context_get_flags(device->priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES) + if (g_usb_context_get_flags(priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES) return TRUE; /* open */ @@ -465,12 +471,13 @@ g_usb_device_get_custom_index(GUsbDevice *device, guint8 protocol_id, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); const struct libusb_interface_descriptor *ifp; gint rc; guint8 idx = 0x00; struct libusb_config_descriptor *config; - rc = libusb_get_active_config_descriptor(device->priv->device, &config); + rc = libusb_get_active_config_descriptor(priv->device, &config); if (!g_usb_device_libusb_error_to_gerror(device, rc, error)) return 0x00; @@ -527,6 +534,7 @@ g_usb_device_get_interface(GUsbDevice *device, guint8 protocol_id, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); const struct libusb_interface_descriptor *ifp; gint rc; GUsbInterface *interface = NULL; @@ -535,7 +543,7 @@ g_usb_device_get_interface(GUsbDevice *device, g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - rc = libusb_get_active_config_descriptor(device->priv->device, &config); + rc = libusb_get_active_config_descriptor(priv->device, &config); if (!g_usb_device_libusb_error_to_gerror(device, rc, error)) return NULL; @@ -583,6 +591,7 @@ g_usb_device_get_interface(GUsbDevice *device, GPtrArray * g_usb_device_get_interfaces(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); const struct libusb_interface_descriptor *ifp; gint rc; struct libusb_config_descriptor *config; @@ -591,7 +600,7 @@ g_usb_device_get_interfaces(GUsbDevice *device, GError **error) g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - rc = libusb_get_active_config_descriptor(device->priv->device, &config); + rc = libusb_get_active_config_descriptor(priv->device, &config); if (!g_usb_device_libusb_error_to_gerror(device, rc, error)) return NULL; @@ -627,6 +636,7 @@ g_usb_device_get_interfaces(GUsbDevice *device, GError **error) GUsbBosDescriptor * g_usb_device_get_bos_descriptor(GUsbDevice *device, guint8 capability, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; guint8 num_device_caps; GUsbBosDescriptor *bos_descriptor = NULL; @@ -635,7 +645,7 @@ g_usb_device_get_bos_descriptor(GUsbDevice *device, guint8 capability, GError ** g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - rc = libusb_get_bos_descriptor(device->priv->handle, &bos); + rc = libusb_get_bos_descriptor(priv->handle, &bos); if (!g_usb_device_libusb_error_to_gerror(device, rc, error)) return NULL; @@ -680,6 +690,7 @@ g_usb_device_get_bos_descriptor(GUsbDevice *device, guint8 capability, GError ** GPtrArray * g_usb_device_get_bos_descriptors(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; guint8 num_device_caps; struct libusb_bos_descriptor *bos = NULL; @@ -688,7 +699,7 @@ g_usb_device_get_bos_descriptors(GUsbDevice *device, GError **error) g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - rc = libusb_get_bos_descriptor(device->priv->handle, &bos); + rc = libusb_get_bos_descriptor(priv->handle, &bos); if (!g_usb_device_libusb_error_to_gerror(device, rc, error)) return NULL; @@ -724,18 +735,20 @@ g_usb_device_get_bos_descriptors(GUsbDevice *device, GError **error) gboolean g_usb_device_close(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); + g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); /* ignore */ - if (g_usb_context_get_flags(device->priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES) + if (g_usb_context_get_flags(priv->context) & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES) return TRUE; - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); - libusb_close(device->priv->handle); - device->priv->handle = NULL; + libusb_close(priv->handle); + priv->handle = NULL; return TRUE; } @@ -759,14 +772,15 @@ g_usb_device_close(GUsbDevice *device, GError **error) gboolean g_usb_device_reset(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); - rc = libusb_reset_device(device->priv->handle); + rc = libusb_reset_device(priv->handle); if (rc == LIBUSB_ERROR_NOT_FOUND) return TRUE; return g_usb_device_libusb_error_to_gerror(device, rc, error); @@ -788,18 +802,19 @@ g_usb_device_reset(GUsbDevice *device, GError **error) gint g_usb_device_get_configuration(GUsbDevice *device, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; int config; g_return_val_if_fail(G_USB_IS_DEVICE(device), -1); g_return_val_if_fail(error == NULL || *error == NULL, -1); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_not_open_error(device, error); return -1; } - rc = libusb_get_configuration(device->priv->handle, &config); + rc = libusb_get_configuration(priv->handle, &config); if (rc != LIBUSB_SUCCESS) { g_usb_device_libusb_error_to_gerror(device, rc, error); return -1; @@ -825,17 +840,18 @@ g_usb_device_get_configuration(GUsbDevice *device, GError **error) gboolean g_usb_device_set_configuration(GUsbDevice *device, gint configuration, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; gint config_tmp = 0; g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); /* verify we've not already set the same configuration */ - rc = libusb_get_configuration(device->priv->handle, &config_tmp); + rc = libusb_get_configuration(priv->handle, &config_tmp); if (rc != LIBUSB_SUCCESS) { return g_usb_device_libusb_error_to_gerror(device, rc, error); } @@ -843,7 +859,7 @@ g_usb_device_set_configuration(GUsbDevice *device, gint configuration, GError ** return TRUE; /* different, so change */ - rc = libusb_set_configuration(device->priv->handle, configuration); + rc = libusb_set_configuration(priv->handle, configuration); return g_usb_device_libusb_error_to_gerror(device, rc, error); } @@ -866,23 +882,24 @@ g_usb_device_claim_interface(GUsbDevice *device, GUsbDeviceClaimInterfaceFlags flags, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); if (flags & G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER) { - rc = libusb_detach_kernel_driver(device->priv->handle, interface); + rc = libusb_detach_kernel_driver(priv->handle, interface); if (rc != LIBUSB_SUCCESS && rc != LIBUSB_ERROR_NOT_FOUND && /* No driver attached */ rc != LIBUSB_ERROR_NOT_SUPPORTED && /* win32 */ rc != LIBUSB_ERROR_BUSY /* driver rebound already */) return g_usb_device_libusb_error_to_gerror(device, rc, error); } - rc = libusb_claim_interface(device->priv->handle, interface); + rc = libusb_claim_interface(priv->handle, interface); return g_usb_device_libusb_error_to_gerror(device, rc, error); } @@ -905,20 +922,21 @@ g_usb_device_release_interface(GUsbDevice *device, GUsbDeviceClaimInterfaceFlags flags, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); - rc = libusb_release_interface(device->priv->handle, interface); + rc = libusb_release_interface(priv->handle, interface); if (rc != LIBUSB_SUCCESS) return g_usb_device_libusb_error_to_gerror(device, rc, error); if (flags & G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER) { - rc = libusb_attach_kernel_driver(device->priv->handle, interface); + rc = libusb_attach_kernel_driver(priv->handle, interface); if (rc != LIBUSB_SUCCESS && rc != LIBUSB_ERROR_NOT_FOUND && /* No driver attached */ rc != LIBUSB_ERROR_NOT_SUPPORTED && /* win32 */ rc != LIBUSB_ERROR_BUSY /* driver rebound already */) @@ -944,15 +962,16 @@ g_usb_device_release_interface(GUsbDevice *device, gboolean g_usb_device_set_interface_alt(GUsbDevice *device, gint interface, guint8 alt, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; g_return_val_if_fail(G_USB_IS_DEVICE(device), FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - if (device->priv->handle == NULL) + if (priv->handle == NULL) return g_usb_device_not_open_error(device, error); - rc = libusb_set_interface_alt_setting(device->priv->handle, interface, (gint)alt); + rc = libusb_set_interface_alt_setting(priv->handle, interface, (gint)alt); if (rc != LIBUSB_SUCCESS) return g_usb_device_libusb_error_to_gerror(device, rc, error); @@ -974,6 +993,7 @@ g_usb_device_set_interface_alt(GUsbDevice *device, gint interface, guint8 alt, G gchar * g_usb_device_get_string_descriptor(GUsbDevice *device, guint8 desc_index, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; /* libusb_get_string_descriptor_ascii returns max 128 bytes */ unsigned char buf[128]; @@ -981,12 +1001,12 @@ g_usb_device_get_string_descriptor(GUsbDevice *device, guint8 desc_index, GError g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_not_open_error(device, error); return NULL; } - rc = libusb_get_string_descriptor_ascii(device->priv->handle, desc_index, buf, sizeof(buf)); + rc = libusb_get_string_descriptor_ascii(priv->handle, desc_index, buf, sizeof(buf)); if (rc < 0) { g_usb_device_libusb_error_to_gerror(device, rc, error); return NULL; @@ -1016,18 +1036,19 @@ g_usb_device_get_string_descriptor_bytes_full(GUsbDevice *device, gsize length, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); gint rc; g_autofree guint8 *buf = g_malloc0(length); g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); g_return_val_if_fail(error == NULL || *error == NULL, NULL); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_not_open_error(device, error); return NULL; } - rc = libusb_get_string_descriptor(device->priv->handle, desc_index, langid, buf, length); + rc = libusb_get_string_descriptor(priv->handle, desc_index, langid, buf, length); if (rc < 0) { g_usb_device_libusb_error_to_gerror(device, rc, error); return NULL; @@ -1122,10 +1143,11 @@ g_usb_device_control_transfer(GUsbDevice *device, GCancellable *cancellable, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_usb_context_get_main_context(device->priv->context); + helper.context = g_usb_context_get_main_context(priv->context); helper.loop = g_main_loop_new(helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_control_transfer_finish; @@ -1184,10 +1206,11 @@ g_usb_device_bulk_transfer(GUsbDevice *device, GCancellable *cancellable, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_usb_context_get_main_context(device->priv->context); + helper.context = g_usb_context_get_main_context(priv->context); helper.loop = g_main_loop_new(helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_bulk_transfer_finish; @@ -1241,10 +1264,11 @@ g_usb_device_interrupt_transfer(GUsbDevice *device, GCancellable *cancellable, GError **error) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_usb_context_get_main_context(device->priv->context); + helper.context = g_usb_context_get_main_context(priv->context); helper.loop = g_main_loop_new(helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_interrupt_transfer_finish; @@ -1420,6 +1444,7 @@ g_usb_device_control_transfer_async(GUsbDevice *device, GAsyncReadyCallback callback, gpointer user_data) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GTask *task; GcmDeviceReq *req; gint rc; @@ -1428,7 +1453,7 @@ g_usb_device_control_transfer_async(GUsbDevice *device, g_return_if_fail(G_USB_IS_DEVICE(device)); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_async_not_open_error(device, callback, user_data, @@ -1462,7 +1487,7 @@ g_usb_device_control_transfer_async(GUsbDevice *device, /* fill in transfer details */ libusb_fill_control_transfer(req->transfer, - device->priv->handle, + priv->handle, req->data_raw, g_usb_device_control_transfer_cb, task, @@ -1536,6 +1561,7 @@ g_usb_device_bulk_transfer_async(GUsbDevice *device, GAsyncReadyCallback callback, gpointer user_data) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GTask *task; GcmDeviceReq *req; gint rc; @@ -1543,7 +1569,7 @@ g_usb_device_bulk_transfer_async(GUsbDevice *device, g_return_if_fail(G_USB_IS_DEVICE(device)); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_async_not_open_error(device, callback, user_data, @@ -1564,7 +1590,7 @@ g_usb_device_bulk_transfer_async(GUsbDevice *device, /* fill in transfer details */ libusb_fill_bulk_transfer(req->transfer, - device->priv->handle, + priv->handle, endpoint, data, length, @@ -1640,6 +1666,7 @@ g_usb_device_interrupt_transfer_async(GUsbDevice *device, GAsyncReadyCallback callback, gpointer user_data) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GTask *task; GcmDeviceReq *req; GError *error = NULL; @@ -1647,7 +1674,7 @@ g_usb_device_interrupt_transfer_async(GUsbDevice *device, g_return_if_fail(G_USB_IS_DEVICE(device)); - if (device->priv->handle == NULL) { + if (priv->handle == NULL) { g_usb_device_async_not_open_error(device, callback, user_data, @@ -1668,7 +1695,7 @@ g_usb_device_interrupt_transfer_async(GUsbDevice *device, /* fill in transfer details */ libusb_fill_interrupt_transfer(req->transfer, - device->priv->handle, + priv->handle, endpoint, data, length, @@ -1732,9 +1759,9 @@ g_usb_device_interrupt_transfer_finish(GUsbDevice *device, GAsyncResult *res, GE const gchar * g_usb_device_get_platform_id(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); - - return device->priv->platform_id; + return priv->platform_id; } /** @@ -1750,7 +1777,7 @@ g_usb_device_get_platform_id(GUsbDevice *device) GUsbDevice * g_usb_device_get_parent(GUsbDevice *device) { - GUsbDevicePrivate *priv = device->priv; + GUsbDevicePrivate *priv = GET_PRIVATE(device); libusb_device *parent; parent = libusb_get_parent(priv->device); @@ -1776,17 +1803,17 @@ g_usb_device_get_parent(GUsbDevice *device) GPtrArray * g_usb_device_get_children(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); GPtrArray *children; - GUsbDevice *device_tmp; - GUsbDevicePrivate *priv = device->priv; g_autoptr(GPtrArray) devices = NULL; /* find any devices that have @device as a parent */ children = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); devices = g_usb_context_get_devices(priv->context); for (guint i = 0; i < devices->len; i++) { - device_tmp = g_ptr_array_index(devices, i); - if (priv->device == libusb_get_parent(device_tmp->priv->device)) + GUsbDevice *device_tmp = g_ptr_array_index(devices, i); + GUsbDevicePrivate *priv_tmp = GET_PRIVATE(device_tmp); + if (priv->device == libusb_get_parent(priv_tmp->device)) g_ptr_array_add(children, g_object_ref(device_tmp)); } @@ -1806,9 +1833,9 @@ g_usb_device_get_children(GUsbDevice *device) guint8 g_usb_device_get_bus(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return libusb_get_bus_number(device->priv->device); + return libusb_get_bus_number(priv->device); } /** @@ -1824,9 +1851,9 @@ g_usb_device_get_bus(GUsbDevice *device) guint8 g_usb_device_get_address(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return libusb_get_device_address(device->priv->device); + return libusb_get_device_address(priv->device); } /** @@ -1842,8 +1869,9 @@ g_usb_device_get_address(GUsbDevice *device) guint8 g_usb_device_get_port_number(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - return libusb_get_port_number(device->priv->device); + return libusb_get_port_number(priv->device); } /** @@ -1859,9 +1887,9 @@ g_usb_device_get_port_number(GUsbDevice *device) guint16 g_usb_device_get_vid(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.idVendor; + return priv->desc.idVendor; } /** @@ -1877,9 +1905,9 @@ g_usb_device_get_vid(GUsbDevice *device) guint16 g_usb_device_get_pid(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.idProduct; + return priv->desc.idProduct; } /** @@ -1895,9 +1923,9 @@ g_usb_device_get_pid(GUsbDevice *device) guint16 g_usb_device_get_release(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.bcdDevice; + return priv->desc.bcdDevice; } /** @@ -1914,9 +1942,9 @@ g_usb_device_get_release(GUsbDevice *device) guint16 g_usb_device_get_spec(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.bcdUSB; + return priv->desc.bcdUSB; } /** @@ -1932,10 +1960,9 @@ g_usb_device_get_spec(GUsbDevice *device) const gchar * g_usb_device_get_vid_as_str(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); - return _g_usb_context_lookup_vendor(device->priv->context, - device->priv->desc.idVendor, - NULL); + return _g_usb_context_lookup_vendor(priv->context, priv->desc.idVendor, NULL); } /** @@ -1951,10 +1978,11 @@ g_usb_device_get_vid_as_str(GUsbDevice *device) const gchar * g_usb_device_get_pid_as_str(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), NULL); - return _g_usb_context_lookup_product(device->priv->context, - device->priv->desc.idVendor, - device->priv->desc.idProduct, + return _g_usb_context_lookup_product(priv->context, + priv->desc.idVendor, + priv->desc.idProduct, NULL); } @@ -1972,13 +2000,14 @@ g_usb_device_get_pid_as_str(GUsbDevice *device) guint8 g_usb_device_get_configuration_index(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); struct libusb_config_descriptor *config; gint rc; guint8 index; g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - rc = libusb_get_active_config_descriptor(device->priv->device, &config); + rc = libusb_get_active_config_descriptor(priv->device, &config); g_return_val_if_fail(rc == 0, 0); index = config->iConfiguration; @@ -2000,9 +2029,9 @@ g_usb_device_get_configuration_index(GUsbDevice *device) guint8 g_usb_device_get_manufacturer_index(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.iManufacturer; + return priv->desc.iManufacturer; } /** @@ -2018,9 +2047,9 @@ g_usb_device_get_manufacturer_index(GUsbDevice *device) guint8 g_usb_device_get_device_class(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.bDeviceClass; + return priv->desc.bDeviceClass; } /** @@ -2037,9 +2066,9 @@ g_usb_device_get_device_class(GUsbDevice *device) guint8 g_usb_device_get_device_subclass(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.bDeviceSubClass; + return priv->desc.bDeviceSubClass; } /** @@ -2056,9 +2085,9 @@ g_usb_device_get_device_subclass(GUsbDevice *device) guint8 g_usb_device_get_device_protocol(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.bDeviceProtocol; + return priv->desc.bDeviceProtocol; } /** @@ -2074,9 +2103,9 @@ g_usb_device_get_device_protocol(GUsbDevice *device) guint8 g_usb_device_get_product_index(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.iProduct; + return priv->desc.iProduct; } /** @@ -2092,7 +2121,7 @@ g_usb_device_get_product_index(GUsbDevice *device) guint8 g_usb_device_get_serial_number_index(GUsbDevice *device) { + GUsbDevicePrivate *priv = GET_PRIVATE(device); g_return_val_if_fail(G_USB_IS_DEVICE(device), 0); - - return device->priv->desc.iSerialNumber; + return priv->desc.iSerialNumber; } diff --git a/gusb/gusb-device.h b/gusb/gusb-device.h index 633af23..755f046 100644 --- a/gusb/gusb-device.h +++ b/gusb/gusb-device.h @@ -16,13 +16,9 @@ G_BEGIN_DECLS #define G_USB_TYPE_DEVICE (g_usb_device_get_type()) -#define G_USB_DEVICE(o) (G_TYPE_CHECK_INSTANCE_CAST((o), G_USB_TYPE_DEVICE, GUsbDevice)) -#define G_USB_IS_DEVICE(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), G_USB_TYPE_DEVICE)) #define G_USB_DEVICE_ERROR (g_usb_device_error_quark()) -typedef struct _GUsbDevicePrivate GUsbDevicePrivate; -typedef struct _GUsbDevice GUsbDevice; -typedef struct _GUsbDeviceClass GUsbDeviceClass; +G_DECLARE_DERIVABLE_TYPE(GUsbDevice, g_usb_device, G_USB, DEVICE, GObject) /** * GUsbDeviceDirection: @@ -127,11 +123,6 @@ typedef enum { G_USB_DEVICE_LANGID_ENGLISH_UNITED_STATES = 0x0409, } GUsbDeviceLangid; -struct _GUsbDevice { - GObject parent; - GUsbDevicePrivate *priv; -}; - struct _GUsbDeviceClass { GObjectClass parent_class; /*< private >*/ @@ -142,8 +133,6 @@ struct _GUsbDeviceClass { gchar _gusb_reserved[64]; }; -GType -g_usb_device_get_type(void); GQuark g_usb_device_error_quark(void); diff --git a/gusb/gusb-self-test.c b/gusb/gusb-self-test.c index 903770b..e9dd20a 100644 --- a/gusb/gusb-self-test.c +++ b/gusb/gusb-self-test.c @@ -7,7 +7,6 @@ #include "config.h" -#include "gusb-autocleanups.h" #include "gusb-context-private.h" static void diff --git a/gusb/gusb.h b/gusb/gusb.h index 928d50a..0a698e8 100644 --- a/gusb/gusb.h +++ b/gusb/gusb.h @@ -9,7 +9,6 @@ #define __GUSB_INSIDE__ -#include #include #include #include diff --git a/gusb/meson.build b/gusb/meson.build index 150e8b5..285c776 100644 --- a/gusb/meson.build +++ b/gusb/meson.build @@ -26,7 +26,6 @@ lib_incdir = include_directories('.') install_headers([ gusb_version_h, - 'gusb-autocleanups.h', 'gusb-context.h', 'gusb-context-private.h', 'gusb-device.h', @@ -106,7 +105,6 @@ if get_option('introspection') libgusb_girtarget = gnome.generate_gir(gusb, sources : [ gusb_version_h, - 'gusb-autocleanups.h', 'gusb-context.c', 'gusb-context.h', 'gusb-context-private.h', -- cgit v1.2.1