summaryrefslogtreecommitdiff
path: root/gusb/gusb-source.c
blob: a6ed19ba0a37071aa01663b69118e9e095841de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:gusb-source
 * @short_description: GSource integration for libusb
 *
 * This object can be used to integrate libusb into the GLib main loop.
 */

#include "config.h"

#include <libusb-1.0/libusb.h>
#include <stdlib.h>

#include "gusb-context-private.h"
#include "gusb-context.h"
#include "gusb-source-private.h"
#include "gusb-source.h"
#include "gusb-util.h"

/* the <poll.h> header is not available on all platforms */
#ifndef POLLIN
#define POLLIN	0x0001
#define POLLOUT 0x0004
#endif

/**
 * g_usb_source_error_quark:
 *
 * Return value: Our personal error quark.
 *
 * Since: 0.1.0
 **/
GQuark
g_usb_source_error_quark(void)
{
	static GQuark quark = 0;
	if (!quark)
		quark = g_quark_from_static_string("g_usb_source_error");
	return quark;
}

struct _GUsbSource {
	GSource source;
	GSList *pollfds;
	libusb_context *ctx;
};

static void
g_usb_source_pollfd_add(GUsbSource *self, int fd, short events)
{
	GPollFD *pollfd = g_new0(GPollFD, 1);

	pollfd->fd = fd;
	pollfd->events = 0;
	pollfd->revents = 0;
	if (events & POLLIN)
		pollfd->events |= G_IO_IN;
	if (events & POLLOUT)
		pollfd->events |= G_IO_OUT;

	self->pollfds = g_slist_prepend(self->pollfds, pollfd);
	g_source_add_poll((GSource *)self, pollfd);
}

static void
g_usb_source_pollfd_added_cb(int fd, short events, void *user_data)
{
	GUsbSource *self = user_data;
	g_usb_source_pollfd_add(self, fd, events);
}

static void
g_usb_source_pollfd_removed_cb(int fd, void *user_data)
{
	GUsbSource *self = user_data;

	/* find the pollfd in the list */
	for (GSList *elem = self->pollfds; elem != NULL; elem = elem->next) {
		GPollFD *pollfd = elem->data;
		if (pollfd->fd == fd) {
			g_source_remove_poll((GSource *)self, pollfd);
			g_free(pollfd);
			self->pollfds = g_slist_delete_link(self->pollfds, elem);
			return;
		}
	}
	g_warning("couldn't find fd %d in list", fd);
}

static gboolean
g_usb_source_prepare(GSource *source, gint *timeout)
{
	GUsbSource *self = (GUsbSource *)source;

	/* before the poll */
	libusb_lock_events(self->ctx);

	/* release the lock at least once per minute */
	*timeout = 60000;
	return FALSE;
}

static gboolean
g_usb_source_check(GSource *source)
{
	GUsbSource *self = (GUsbSource *)source;

	for (GSList *elem = self->pollfds; elem != NULL; elem = elem->next) {
		GPollFD *pollfd = elem->data;
		if (pollfd->revents)
			return TRUE;
	}
	libusb_unlock_events(self->ctx);
	return FALSE;
}

static gboolean
g_usb_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
	GUsbSource *self = (GUsbSource *)source;
	struct timeval tv = {0, 0};
	gint rc;

	rc = libusb_handle_events_locked(self->ctx, &tv);
	libusb_unlock_events(self->ctx);
	if (rc < 0)
		g_warning("failed to handle event: %s [%i]", g_usb_strerror(rc), rc);

	if (callback != NULL)
		callback(user_data);

	return G_SOURCE_CONTINUE;
}

static void
g_usb_source_finalize(GSource *source)
{
	GUsbSource *self = (GUsbSource *)source;
	g_slist_free(self->pollfds);
}

static GSourceFuncs usb_source_funcs = {g_usb_source_prepare,
					g_usb_source_check,
					g_usb_source_dispatch,
					g_usb_source_finalize};

GUsbSource *
_g_usb_source_new(GMainContext *main_ctx, GUsbContext *gusb_ctx)
{
	GUsbSource *self;
	const struct libusb_pollfd **pollfds;

	self = (GUsbSource *)g_source_new(&usb_source_funcs, sizeof(GUsbSource));
	self->pollfds = NULL;
	self->ctx = _g_usb_context_get_context(gusb_ctx);

	/* watch the fd's already created */
	pollfds = libusb_get_pollfds(self->ctx);
	for (guint i = 0; pollfds != NULL && pollfds[i] != NULL; i++)
		g_usb_source_pollfd_add(self, pollfds[i]->fd, pollfds[i]->events);
	free(pollfds);

	/* watch for PollFD changes */
	g_source_attach((GSource *)self, main_ctx);
	libusb_set_pollfd_notifiers(self->ctx,
				    g_usb_source_pollfd_added_cb,
				    g_usb_source_pollfd_removed_cb,
				    self);
	return self;
}

static void
g_usb_source_pollfd_remove_cb(gpointer data, gpointer user_data)
{
	GPollFD *pollfd = (GPollFD *)data;
	GSource *source = (GSource *)user_data;
	g_source_remove_poll(source, pollfd);
}

void
_g_usb_source_destroy(GUsbSource *self)
{
	libusb_set_pollfd_notifiers(self->ctx, NULL, NULL, NULL);
	g_slist_foreach(self->pollfds, g_usb_source_pollfd_remove_cb, self);
	g_slist_free_full(g_steal_pointer(&self->pollfds), g_free);
	g_source_destroy((GSource *)self);
}

/**
 * g_usb_source_set_callback:
 * @self: a #GUsbSource
 * @func: a function to call
 * @data: data to pass to @func
 * @notify: a #GDestroyNotify
 *
 * Set a callback to be called when the source is dispatched.
 *
 * Since: 0.1.0
 **/
void
g_usb_source_set_callback(GUsbSource *self, GSourceFunc func, gpointer data, GDestroyNotify notify)
{
	g_source_set_callback((GSource *)self, func, data, notify);
}