diff options
author | Richard Hughes <richard@hughsie.com> | 2015-05-26 21:01:43 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2015-05-26 21:01:43 +0100 |
commit | 26020c531bb76e404986851736ee3ece5e35e8ec (patch) | |
tree | 755bc969d5e9eb1a0c1a2e53ab02e8fbde401e10 /gusb | |
parent | 87b42b0d70b3e92742b9213037864e5cb3320c77 (diff) | |
download | gusb-26020c531bb76e404986851736ee3ece5e35e8ec.tar.gz |
Allow setting the GMainContext when used for sync methods
Diffstat (limited to 'gusb')
-rw-r--r-- | gusb/gusb-context.c | 41 | ||||
-rw-r--r-- | gusb/gusb-context.h | 3 | ||||
-rw-r--r-- | gusb/gusb-device.c | 6 |
3 files changed, 47 insertions, 3 deletions
diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c index 7768e18..ab28508 100644 --- a/gusb/gusb-context.c +++ b/gusb/gusb-context.c @@ -55,6 +55,7 @@ enum { **/ struct _GUsbContextPrivate { + GMainContext *main_ctx; GPtrArray *devices; GHashTable *dict_usb_ids; GThread *thread_event; @@ -102,6 +103,7 @@ g_usb_context_dispose (GObject *object) priv->hotplug_poll_id = 0; } + g_clear_pointer (&priv->main_ctx, g_main_context_unref); g_clear_pointer (&priv->devices, g_ptr_array_unref); g_clear_pointer (&priv->dict_usb_ids, g_hash_table_unref); g_clear_pointer (&priv->ctx, libusb_exit); @@ -399,6 +401,44 @@ g_usb_context_rescan_cb (gpointer user_data) return TRUE; } + +/** + * g_usb_context_get_main_context: + * @context: a #GUsbContext + * + * Gets the internal GMainContext to use for syncronous methods. + * By default the value is set to the value of g_main_context_default() + * + * Return value: (transfer none): the #GMainContext + * + * Since: 0.2.5 + **/ +GMainContext * +g_usb_context_get_main_context (GUsbContext *context) +{ + GUsbContextPrivate *priv = context->priv; + return priv->main_ctx; +} + + +/** + * g_usb_context_set_main_context: + * @context: a #GUsbContext + * + * Sets the internal GMainContext to use for syncronous methods. + * + * Since: 0.2.5 + **/ +void +g_usb_context_set_main_context (GUsbContext *context, GMainContext *main_ctx) +{ + GUsbContextPrivate *priv = context->priv; + if (main_ctx != priv->main_ctx){ + g_main_context_unref (priv->main_ctx); + priv->main_ctx = g_main_context_ref (main_ctx); + } +} + /** * g_usb_context_enumerate: * @context: a #GUsbContext @@ -473,6 +513,7 @@ g_usb_context_initable_init (GInitable *initable, return FALSE; } + priv->main_ctx = g_main_context_ref (g_main_context_default ()); priv->ctx = ctx; priv->thread_event_run = 1; priv->thread_event = g_thread_new ("GUsbEventThread", diff --git a/gusb/gusb-context.h b/gusb/gusb-context.h index 24b64b1..1738cd3 100644 --- a/gusb/gusb-context.h +++ b/gusb/gusb-context.h @@ -71,6 +71,9 @@ GUsbContext *g_usb_context_new (GError **error); G_DEPRECATED GUsbSource *g_usb_context_get_source (GUsbContext *context, GMainContext *main_ctx); +GMainContext *g_usb_context_get_main_context (GUsbContext *context); +void g_usb_context_set_main_context (GUsbContext *context, + GMainContext *main_ctx); void g_usb_context_enumerate (GUsbContext *context); diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index 047820f..30f2742 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -800,7 +800,7 @@ g_usb_device_control_transfer (GUsbDevice *device, GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_main_context_new (); + helper.context = g_usb_context_get_main_context (device->priv->context); helper.loop = g_main_loop_new (helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_control_transfer_finish; @@ -863,7 +863,7 @@ g_usb_device_bulk_transfer (GUsbDevice *device, GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_main_context_new (); + helper.context = g_usb_context_get_main_context (device->priv->context); helper.loop = g_main_loop_new (helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_bulk_transfer_finish; @@ -921,7 +921,7 @@ g_usb_device_interrupt_transfer (GUsbDevice *device, GUsbSyncHelper helper; helper.ret = -1; - helper.context = g_main_context_new (); + helper.context = g_usb_context_get_main_context (device->priv->context); helper.loop = g_main_loop_new (helper.context, FALSE); helper.error = error; helper.finish_func = g_usb_device_interrupt_transfer_finish; |