From: Martin Mares Date: Sat, 11 Apr 2020 21:21:00 +0000 (+0200) Subject: Generalized glue between LibUSB and LibUCW mainloop X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=ed7db1e07ba9ca31a7baab970f42d476b2c03021;p=ursary.git Generalized glue between LibUSB and LibUCW mainloop --- diff --git a/Makefile b/Makefile index a1bdabe..40de57e 100644 --- a/Makefile +++ b/Makefile @@ -15,13 +15,14 @@ LDLIBS=$(LIBUCW_LIBS) $(LIBUSB_LIBS) $(LIBPULSE_LIBS) all: ursaryd -ursaryd: ursaryd.o mpd.o nocturn.o pulse.o pulse-ucw.o +ursaryd: ursaryd.o mpd.o nocturn.o pulse.o pulse-ucw.o usb.o -ursaryd.o: ursaryd.c ursaryd.h +ursaryd.o: ursaryd.c ursaryd.h usb.h mpd.o: mpd.c ursaryd.h -nocturn.o: nocturn.c ursaryd.h +nocturn.o: nocturn.c ursaryd.h usb.h pulse.o: pulse.c ursaryd.h pulse-ucw.o: pulse-ucw.c ursaryd.h +usb.o: usb.c ursaryd.h usb.h clean: rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name .depend -or -name .#*` diff --git a/nocturn.c b/nocturn.c index 7353822..96cda66 100644 --- a/nocturn.c +++ b/nocturn.c @@ -11,74 +11,20 @@ #include #include -#include -#include #include #include -#include #include #include -#include -#include - -#include #include "ursaryd.h" +#include "usb.h" -static libusb_context *usb_ctx; static libusb_device_handle *usb_dev; static bool usb_iface_claimed; static void noct_error(int usb_err, char *text); -static struct main_file **usb_fds; - -static int usb_fd_ready(struct main_file *f UNUSED) -{ - DBG("USB: Handling events (ready on fd %d)", f->fd); - struct timeval tv = { 0, 0 }; - int comp = 0; - int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp); - if (err < 0) - msg(L_ERROR, "libusb_handle_events: error %d", err); - return HOOK_IDLE; -} - -static void usb_added_fd(int fd, short events, void *user_data UNUSED) -{ - if (fd >= (int) GARY_SIZE(usb_fds)) - GARY_RESIZE(usb_fds, fd + 1); - - struct main_file *f = usb_fds[fd]; - if (!f) - { - f = xmalloc_zero(sizeof(*f)); - usb_fds[fd] = f; - } - else if (file_is_active(f)) - { - DBG("USB: Releasing fd %d", fd); - file_del(f); - } - - DBG("USB: Adding fd %d with event mask %u", fd, events); - f->fd = fd; - f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL; - f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL; - file_add(f); -} - -static void usb_removed_fd(int fd, void *user_data UNUSED) -{ - DBG("USB: Releasing fd %d", fd); - ASSERT(fd < (int) GARY_SIZE(usb_fds)); - struct main_file *f = usb_fds[fd]; - ASSERT(f); - ASSERT(file_is_active(f)); - file_del(f); -} - static const char noct_magic[4][9] = { { 3, 0xb0, 0x00, 0x00 }, { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 }, @@ -488,26 +434,6 @@ bool noct_is_ready(void) void noct_init(void) { - int err; - - // Initialize libusb - if ((err = libusb_init(&usb_ctx)) < 0) - die("libusb_init failed: error %d", err); - - // Connect libusb to UCW mainloop - - if (!libusb_pollfds_handle_timeouts(usb_ctx)) - die("Unsupported version of libusb, please fix me"); - - GARY_INIT_ZERO(usb_fds, 0); - libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL); - - const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx); - ASSERT(fds); - for (int i=0; fds[i]; i++) - usb_added_fd(fds[i]->fd, fds[i]->events, NULL); - free(fds); - // Prepare error handling hook noct_error_hook.handler = noct_error_handler; diff --git a/ursaryd.c b/ursaryd.c index 9433488..8df65df 100644 --- a/ursaryd.c +++ b/ursaryd.c @@ -21,6 +21,7 @@ #include #include "ursaryd.h" +#include "usb.h" /* * Map of all controls @@ -729,6 +730,7 @@ static void daemon_body(struct daemon_params *dp) main_init(); update_timer.handler = do_update; + usb_init(); noct_init(); pulse_init(); mpd_init(); diff --git a/usb.c b/usb.c new file mode 100644 index 0000000..706cc00 --- /dev/null +++ b/usb.c @@ -0,0 +1,92 @@ +/* + * LibUSB over LibUCW Mainloop + * + * (c) 2014--2020 Martin Mares + */ + +#undef LOCAL_DEBUG + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "ursaryd.h" +#include "usb.h" + +libusb_context *usb_ctx; + +static struct main_file **usb_fds; + +static int usb_fd_ready(struct main_file *f UNUSED) +{ + DBG("USB: Handling events (ready on fd %d)", f->fd); + struct timeval tv = { 0, 0 }; + int comp = 0; + int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp); + if (err < 0) + msg(L_ERROR, "libusb_handle_events: error %d", err); + return HOOK_IDLE; +} + +static void usb_added_fd(int fd, short events, void *user_data UNUSED) +{ + if (fd >= (int) GARY_SIZE(usb_fds)) + GARY_RESIZE(usb_fds, fd + 1); + + struct main_file *f = usb_fds[fd]; + if (!f) + { + f = xmalloc_zero(sizeof(*f)); + usb_fds[fd] = f; + } + else if (file_is_active(f)) + { + DBG("USB: Releasing fd %d", fd); + file_del(f); + } + + DBG("USB: Adding fd %d with event mask %u", fd, events); + f->fd = fd; + f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL; + f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL; + file_add(f); +} + +static void usb_removed_fd(int fd, void *user_data UNUSED) +{ + DBG("USB: Releasing fd %d", fd); + ASSERT(fd < (int) GARY_SIZE(usb_fds)); + struct main_file *f = usb_fds[fd]; + ASSERT(f); + ASSERT(file_is_active(f)); + file_del(f); +} + +void usb_init(void) +{ + int err; + + // Initialize libusb + if ((err = libusb_init(&usb_ctx)) < 0) + die("libusb_init failed: error %d", err); + + // Connect libusb to UCW mainloop + + if (!libusb_pollfds_handle_timeouts(usb_ctx)) + die("Unsupported version of libusb, please fix me"); + + GARY_INIT_ZERO(usb_fds, 0); + libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL); + + const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx); + ASSERT(fds); + for (int i=0; fds[i]; i++) + usb_added_fd(fds[i]->fd, fds[i]->events, NULL); + free(fds); +} diff --git a/usb.h b/usb.h new file mode 100644 index 0000000..9ca372b --- /dev/null +++ b/usb.h @@ -0,0 +1,11 @@ +/* + * LibUSB over LibUCW Mainloop + * + * (c) 2014--2020 Martin Mares + */ + +#include + +extern libusb_context *usb_ctx; + +void usb_init(void);