Skip to content

Commit 371ee04

Browse files
committed
Add a non-blocking freenect_process_events_timeout().
This should make it easier to integrate libfreenect into some other event loop. Fixes OpenKinect#43. Signed-off-by: Drew Fisher <[email protected]>
1 parent 619a1e5 commit 371ee04

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

include/libfreenect.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ FREENECTAPI void freenect_set_log_callback(freenect_context *ctx, freenect_log_c
231231
*/
232232
FREENECTAPI int freenect_process_events(freenect_context *ctx);
233233

234+
/**
235+
* Calls the platform specific usb event processor until either an event occurs
236+
* or the timeout parameter time has passed. If a zero timeval is passed, this
237+
* function will handle any already-pending events, then return immediately.
238+
*
239+
* @param ctx Context to process events for
240+
* @param timeout Pointer to a timeval containing the maximum amount of time to block waiting for events, or zero for nonblocking mode
241+
*
242+
* @return 0 on success, other values on error, platform/library dependant
243+
*/
244+
FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval* timeout);
245+
234246
/**
235247
* Return the number of kinect devices currently connected to the
236248
* system

platform/windows/libusb10emu/libusb-1.0/libusb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ int libusb_submit_transfer(struct libusb_transfer* transfer);
9696
int libusb_cancel_transfer(struct libusb_transfer* transfer);
9797

9898
int libusb_handle_events(libusb_context* ctx); // WORK IN PROGRESS...
99+
int libusb_handle_events_timeout(libusb_context* ctx, struct timeval* timeout);
99100

100101
// the signature of libusb_device_descriptor is identical to usb_device_descriptor
101102
// which means that the below struct could be replaced by a typedef; however, that

platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,25 @@ int libusb_handle_events(libusb_context* ctx)
624624
return(0);
625625
}
626626

627+
int libusb_handle_events_timeout(libusb_context* ctx, struct timeval* timeout)
628+
{
629+
if (ctx == NULL)
630+
ctx = default_context;
631+
632+
if (failguard::Abort())
633+
return(LIBUSB_ERROR_INTERRUPTED);
634+
635+
RAIIMutex lock (ctx->mutex);
636+
637+
if (timeout == NULL)
638+
libusbemu_handle_isochronous(ctx, 0);
639+
else
640+
libusbemu_handle_isochronous(ctx, (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000));
641+
642+
// 0 on success, or a LIBUSB_ERROR code on failure
643+
return(0);
644+
}
645+
627646
void PreprocessTransferNaive(libusb_transfer* transfer, const int read);
628647
void PreprocessTransferFreenect(libusb_transfer* transfer, const int read);
629648
static void(*PreprocessTransfer)(libusb_transfer*, const int) (PreprocessTransferFreenect);

src/core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ FREENECTAPI int freenect_process_events(freenect_context *ctx)
7272
return fnusb_process_events(&ctx->usb);
7373
}
7474

75+
FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval *timeout)
76+
{
77+
return fnusb_process_events_timeout(&ctx->usb, timeout);
78+
}
79+
7580
FREENECTAPI int freenect_num_devices(freenect_context *ctx)
7681
{
7782
return fnusb_num_devices(&ctx->usb);

src/usb_libusb10.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ int fnusb_process_events(fnusb_ctx *ctx)
152152
return libusb_handle_events(ctx->ctx);
153153
}
154154

155+
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout)
156+
{
157+
return libusb_handle_events_timeout(ctx->ctx, timeout);
158+
}
159+
155160
int fnusb_open_subdevices(freenect_device *dev, int index)
156161
{
157162
freenect_context *ctx = dev->parent;

src/usb_libusb10.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attribut
8989
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);
9090
int fnusb_shutdown(fnusb_ctx *ctx);
9191
int fnusb_process_events(fnusb_ctx *ctx);
92+
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout);
9293

9394
int fnusb_open_subdevices(freenect_device *dev, int index);
9495
int fnusb_close_subdevices(freenect_device *dev);

0 commit comments

Comments
 (0)