Skip to content

Commit

Permalink
Add a non-blocking freenect_process_events_timeout().
Browse files Browse the repository at this point in the history
This should make it easier to integrate libfreenect into some other event loop.

Fixes #43.

Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com>
  • Loading branch information
zarvox committed Nov 10, 2011
1 parent 619a1e5 commit 371ee04
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/libfreenect.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ FREENECTAPI void freenect_set_log_callback(freenect_context *ctx, freenect_log_c
*/
FREENECTAPI int freenect_process_events(freenect_context *ctx);

/**
* Calls the platform specific usb event processor until either an event occurs
* or the timeout parameter time has passed. If a zero timeval is passed, this
* function will handle any already-pending events, then return immediately.
*
* @param ctx Context to process events for
* @param timeout Pointer to a timeval containing the maximum amount of time to block waiting for events, or zero for nonblocking mode
*
* @return 0 on success, other values on error, platform/library dependant
*/
FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval* timeout);

/**
* Return the number of kinect devices currently connected to the
* system
Expand Down
1 change: 1 addition & 0 deletions platform/windows/libusb10emu/libusb-1.0/libusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ int libusb_submit_transfer(struct libusb_transfer* transfer);
int libusb_cancel_transfer(struct libusb_transfer* transfer);

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

// the signature of libusb_device_descriptor is identical to usb_device_descriptor
// which means that the below struct could be replaced by a typedef; however, that
Expand Down
19 changes: 19 additions & 0 deletions platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,25 @@ int libusb_handle_events(libusb_context* ctx)
return(0);
}

int libusb_handle_events_timeout(libusb_context* ctx, struct timeval* timeout)
{
if (ctx == NULL)
ctx = default_context;

if (failguard::Abort())
return(LIBUSB_ERROR_INTERRUPTED);

RAIIMutex lock (ctx->mutex);

if (timeout == NULL)
libusbemu_handle_isochronous(ctx, 0);
else
libusbemu_handle_isochronous(ctx, (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000));

// 0 on success, or a LIBUSB_ERROR code on failure
return(0);
}

void PreprocessTransferNaive(libusb_transfer* transfer, const int read);
void PreprocessTransferFreenect(libusb_transfer* transfer, const int read);
static void(*PreprocessTransfer)(libusb_transfer*, const int) (PreprocessTransferFreenect);
Expand Down
5 changes: 5 additions & 0 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ FREENECTAPI int freenect_process_events(freenect_context *ctx)
return fnusb_process_events(&ctx->usb);
}

FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct timeval *timeout)
{
return fnusb_process_events_timeout(&ctx->usb, timeout);
}

FREENECTAPI int freenect_num_devices(freenect_context *ctx)
{
return fnusb_num_devices(&ctx->usb);
Expand Down
5 changes: 5 additions & 0 deletions src/usb_libusb10.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ int fnusb_process_events(fnusb_ctx *ctx)
return libusb_handle_events(ctx->ctx);
}

int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout)
{
return libusb_handle_events_timeout(ctx->ctx, timeout);
}

int fnusb_open_subdevices(freenect_device *dev, int index)
{
freenect_context *ctx = dev->parent;
Expand Down
1 change: 1 addition & 0 deletions src/usb_libusb10.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attribut
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);
int fnusb_shutdown(fnusb_ctx *ctx);
int fnusb_process_events(fnusb_ctx *ctx);
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout);

int fnusb_open_subdevices(freenect_device *dev, int index);
int fnusb_close_subdevices(freenect_device *dev);
Expand Down

0 comments on commit 371ee04

Please sign in to comment.