Skip to content

Commit f421ac4

Browse files
Antonio Ospitezarvox
authored andcommitted
libusbemu: make libusb functions callable with a NULL context
Implement a default_context mechanism similar to the one in libusb-1.0 itself, so that we can still pass a NULL context to libusb functions. NOTE: we still assume libusb_init() needs to be called before any other libusb_* function, otherwise the code will crash happily. Signed-off-by: Antonio Ospite <[email protected]>
1 parent a1e42c8 commit f421ac4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ using namespace libusbemu;
8383
#define LIBUSB_DEBUG_CMD(cmd)
8484
#endif//LIBUSBEMU_DEBUG_BUILD
8585

86+
static libusb_context *default_context = NULL;
87+
8688
int libusb_init(libusb_context** context)
8789
{
8890
usb_init();
@@ -97,13 +99,19 @@ int libusb_init(libusb_context** context)
9799
// however, it is wise to emulate such context structure to localize and
98100
// keep track of any resource and/or internal data structures, as well as
99101
// to be able to clean-up itself at libusb_exit()
102+
if (context == NULL)
103+
context = &default_context;
104+
100105
*context = new libusb_context;
101106
// 0 on success; LIBUSB_ERROR on failure
102107
return(0);
103108
}
104109

105110
void libusb_exit(libusb_context* ctx)
106111
{
112+
if (ctx == NULL)
113+
ctx = default_context;
114+
107115
ctx->mutex.Enter();
108116
// before deleting the context, delete all devices/transfers still in there:
109117
while (!ctx->devices.empty())
@@ -132,6 +140,9 @@ void libusb_exit(libusb_context* ctx)
132140

133141
ssize_t libusb_get_device_list(libusb_context* ctx, libusb_device*** list)
134142
{
143+
if (ctx == NULL)
144+
ctx = default_context;
145+
135146
// libusb_device*** list demystified:
136147
// libusb_device*** is the C equivalent to libusb_device**& in C++; such declaration
137148
// allows the scope of this function libusb_get_device_list() to write on a variable
@@ -471,6 +482,9 @@ int libusb_cancel_transfer(struct libusb_transfer* transfer)
471482

472483
int libusbemu_handle_isochronous(libusb_context* ctx, const unsigned int milliseconds)
473484
{
485+
if (ctx == NULL)
486+
ctx = default_context;
487+
474488
//QuickThread::Myself().RaisePriority();
475489
RAIIMutex lock (ctx->mutDeliveryPool);
476490
int index = ctx->hWantToDeliverPool.WaitAnyUntilTimeout(milliseconds);
@@ -491,6 +505,9 @@ int libusbemu_handle_isochronous(libusb_context* ctx, const unsigned int millise
491505

492506
int libusb_handle_events(libusb_context* ctx)
493507
{
508+
if (ctx == NULL)
509+
ctx = default_context;
510+
494511
if (failguard::Abort())
495512
return(LIBUSB_ERROR_INTERRUPTED);
496513

0 commit comments

Comments
 (0)